Re: Reflecting generics




"Timbo" <timbo@xxxxxxxxxxxxxxx> wrote in message news:e0jr7q$ebd$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Domagoj Klepac wrote:
I'm wondering if this is possible - and after much digging, it seems
that it isn't. I have the following piece of code:

ArrayList<String> stringArray = new ArrayList<String>();
Class c = stringArray.getClass();

Is it possible to, somehow, get "java.lang.String" from c?
Or is that information lost after compiling?

Domchi

Hmm... it appears that you can't. I would have thought that you could do the following:

Class<? extends ArrayList<String>> c = stringArray.getClass();

which would at least give you the compile-time type, but my compiler is complaining that the assignment:

Test.java:8: incompatible types
found : java.lang.Class<capture of ? extends java.util.ArrayList>
required: java.lang.Class<? extends java.util.ArrayList<java.lang.String>>
Class<? extends ArrayList<String>> c = stringArray.getClass();
^
Am I missing something here??

The following code:
<code>
import java.util.ArrayList;

public class FillTest {
public static void main(String args[]) {
ArrayList<String> stringArray = new ArrayList<String>();
Class<? extends ArrayList<String>> c = stringArray.getClass();
}
}
</code>

Compiles fine for me on Eclipse 3.2M5.

- Oliver

.



Relevant Pages

  • Re: Reflecting generics
    ... Or is that information lost after compiling? ... In a case of local variable this info could be retrieved as well using one of bytecode manipulation libraries, but you have to add the local variables debugging info into your class file. ...
    (comp.lang.java.programmer)
  • Re: Reflecting generics
    ... Domagoj Klepac wrote: ... Is it possible to, somehow, get "java.lang.String" from c? ... Or is that information lost after compiling? ... which would at least give you the compile-time type, but my compiler is complaining that the assignment: ...
    (comp.lang.java.programmer)