Accessing generic type information at runtime
A common misconception about generics in Java 5 is that you can't access them at runtime.
What you can't find out at runtime is which generic type is associated with an instance of an object. However you can use reflection to look at which types have been staticly associated with a member of a class.
public class GenericsTest extends TestCase {
class Thing {
public Map<String,Integer> stuff;
}
public void test() throws Exception {
Field field = Thing.class.getField("stuff");
ParameterizedType type = (ParameterizedType) field.getGenericType();
assertEquals(Map.class, type.getRawType());
assertEquals(String.class, type.getActualTypeArguments()[0]);
assertEquals(Integer.class, type.getActualTypeArguments()[1]);
}
}
Just wanted to clear that up.
(This is something that I'll probably exploit in XStream for J5 users to further simplify the XML.)
Comments
Joe,
How do you plan to support J5 users while still supporting the rest of the community? This is something I want to do with WebWork (support Annotations and Generics, but not require it). Any plans? Drop me an email sometime and let me know.
Patrick
ParameterizedType type = (ParameterizedType) field.getGenericType();
assertEquals(Map.class, type.getRawType());
assertEquals(String.class, type.getActualTypeArguments()[0]);
assertEquals(Integer.class, type.getActualTypeArguments()[1]);
}
http://www.usdebtconsolidation.net
Im interested in support for J5 as well.
Thanks for the welcome
Christmas Day falls on December 25. It is preceded by Christmas Eve on December 24, and in some countries is followed by Boxing Day on December 26. Some Eastern Orthodox Churches celebrate Christmas on January 7, which corresponds to December 25 on the Julian calendar. December 25 as a birthdate for Jesus is merely traditional, and is not thought to be his actual date of birth.Good luck!
Cool design, great info! Would you please also visit my homepage?
[@../doorway/garrik/g3.txt||10||p-1||1||
I just want to say thank you for taking the time & effort for put this web page together! Visit my sites, please:
Hmm... sweet! [*../nice_site2.txt*]
Hi! Your site is very nice! Would you please also visit my homepage?
Hi! Your site is very nice! Would you please also visit my homepage?
Yo! Cool stuff! Thanks for being here. Would you please also visit my homepage?
Yo! Cool stuff! Thanks for being here. Would you please also visit my homepage?
Cool design, great info! Please visit my homepage:
Cool design, great info! Please visit my homepage:
First time here on your site. I am delighted to find your wonderful website online. Would you please also visit my homepage?
Nice webpage, lovely, cool design. Please visit my site too:
Awesome site! Design is great! Please visit my homepage too:
I'm glad I found your site! It's nice! Please visit my homepage too:
Your site looks great! Please visit my homepage too:
Hi people! Great job! Please visit my homepage:
Hi people! Great job! Please visit my homepage:
Hmm... sweet! [*../nice_site2.txt*]