Re: Reflecting generics
- From: Dimitri Maziuk <dima@xxxxxxxxx>
- Date: Fri, 31 Mar 2006 17:04:54 +0000 (UTC)
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
.
- References:
- Reflecting generics
- From: Domagoj Klepac
- Re: Reflecting generics
- From: Timbo
- Reflecting generics
- Prev by Date: Re: Class Loading on Different Opperating Systems
- Next by Date: Re: Java Socket Programming
- Previous by thread: Re: Reflecting generics
- Next by thread: Re: Reflecting generics
- Index(es):
Relevant Pages
|