Re: Reflecting generics



Timbo sez:
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??

No. I've just been through this exercise ("fun with generics" thread)
and my conclusions are:
1. you can't really nest angle brackets,
2. you can't really use a specific type (incl. wildcard) inside
angle brackets.

Dima
--
.... If you want to make sure you don't put a Pig in a List of airplanes and
have it fail at insertion rather than extraction, use
planelist.add((Airplane)o) instead of planelist.add(o). It's that easy.
-- Mark 'Kamikaze' Hughes
.



Relevant Pages

  • FreeBSD 5.x source update and compilation problem in HP Vectra VE18 -- SOLVED
    ... It was 4GB seagate IDE HDD. ... Somehow it was causing the problem. ... However I'm having problem compiling sources. ... I even tried with FreeBSD 5.2RC2, ...
    (freebsd-hackers)
  • Re: Register corruption --patch
    ... >>posiible to easily diagnose this somehow? ... In a way patch helped to track down the error: while compiling ... A witch!** ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Kernel compilation in Suse 9
    ... > works ok, but when I boot with the new kernel, somehow the / fs is ... compiling an Athlon-Kernel. ... I haven't found the reason why, but solved that problem by copying the ...
    (alt.os.linux.suse)
  • Re: using user controls in class file
    ... I somehow got it fixed by changing the instance declaration to "public". ... I also had the /reference option when compiling. ... >> I am getting compilation error, saying "missing using directive or an ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)