determing the generic type

A

Aryeh M. Friedman

First of all let me say I am aware of type erasure.

Let's say I have something like:

ArrayList<Integer> foo=new ArrayList<Integer>();

is there any way at run time to determine the type parameter is
Integer... more specifically if
I have

public interface MyClass<T>
{
....
}

public class MyClassImpl<T> implements MyClass<T>
{
....
}

public class MockMyClass<T> implements MyClass<T>
{
....
}

public
Is there any way of knowing the value of T at run time.

If the above is not possible maybe there is still a way to accomplish
what I have in mind.... basically if some class does the following:

public class MyApp<T>
{
private ArrayList<T> data;

public MyApp()
{
data=new ArrayList<T>();
}
.....
}

I want to be able to replace T with TImpl (i.e. in the case of MyClass
replace it with an instance of MockMyClass [NOT MyClassImpl]) {this
needs to be done via reflection and not modifing the source code}....
Note I already have something that works for single instances and
arrays but not for collections (thus the need for determing the generic
type)
 
C

Chris Smith

Aryeh M. Friedman said:
Let's say I have something like:

ArrayList<Integer> foo=new ArrayList<Integer>();

is there any way at run time to determine the type parameter is
Integer...

How far are you willing to go? What do you have access to? If you're
trying to do this with only reflection on class files without modifying
any code, then you're out of luck. The following may work, though:

1. If you have control of construction of the class, it's fairly common
to have its constructor take a parameter of type Class<T>, where T is
the type parameter. This can then be used for this kind of thing, and
there's compile-time checking that the right Class object was passed in.

2. If you have access to the source code, you could parse it.

3. If you have access to the bytecode and it's a field or parameter, you
could look at the attributes of the class file, which will contain the
generic type information. Note this doesn't work for local variables,
and may not work for private variables.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aryeh M. Friedman schreef:
First of all let me say I am aware of type erasure.

Let's say I have something like:

ArrayList<Integer> foo=new ArrayList<Integer>();

is there any way at run time to determine the type parameter is
Integer... more specifically if
I have

public interface MyClass<T>
{
...
}

public class MyClassImpl<T> implements MyClass<T>
{
...
}

public class MockMyClass<T> implements MyClass<T>
{
...
}

public
Is there any way of knowing the value of T at run time.

No. Chris Smith old you some hoops you could jump through to get near.
If the above is not possible maybe there is still a way to accomplish
what I have in mind.... basically if some class does the following:

public class MyApp<T>
{
private ArrayList<T> data;

public MyApp()
{
data=new ArrayList<T>();
}
....
}

I want to be able to replace T with TImpl (i.e. in the case of MyClass
replace it with an instance of MockMyClass [NOT MyClassImpl]) {this
needs to be done via reflection and not modifing the source code}....
Note I already have something that works for single instances and
arrays but not for collections (thus the need for determing the generic
type)

So actually your problem is that the interface is not general enough.
Can’t you change the interface?

You can make the constructor generic:

public class MyApp<T>
{
private ArrayList<? extends T> data;

public <S extends T> MyApp()
{
data=new ArrayList<S>();
}
.....
}

But unfortunately data is declared array list of T. So you’d have to
know the type even before compiling. Even if you succeed in getting the
type, all objects you get out of data will only have the T interface.

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEt14Ie+7xMGD3itQRAhDCAJ4y4uLsxJ/f1jMtkNCcciMZplw3ZgCfYn5W
A6kAixUKv71P614ljiPFRnQ=
=Z5gH
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top