Getting around Type Erasure in Java?

Maybe you could use something like Javassist to do some meta-programming It's used in Tapestry for that purpose, see this article.

Maybe you could use something like Javassist to do some meta-programming. It's used in Tapestry for that purpose, see this article.

Take a look at Super Type Tokens ..seems like that should work for you in this case . Another link.

Interesting to hear that erasing erasure might be part of Java 7. – Dean J Oct 6 '09 at 15:37.

Even after erasure has done its job, it is still possible to retrieve generic information from: Field, Method ( including method parameters ) and Class using reflection. For example, it is possible to retrieve the SystemErrorReportCommand from SystemErrorReportController using ((ParameterizedType) SystemErrorReportController.class. GetGenericSuperType()).

GetActualTypeArguments0; Still, that's only the beginning. You need to start making use of that information using reflection api, so it is likely that your design will suffer if you were not expecting something like this. Here is a little example of what you can extract at runtime: public abstract class Test extends ArrayList implements Comparable, List{ public List field; public abstract M method(List arg); public static void main(String... args) throws Exception { TypeVariable> t = Test.class.

GetTypeParameters()0; Class upperBound = (Class) t.getBounds()0; System.out. Println("Test"); System.out. Println("extends " + Test.class.

GetGenericSuperclass()); System.out. Println("implements " + Arrays. ToString(Test.class.

GetGenericInterfaces())); System.out. Println("{"); System.out. Println(Test.class.getMethods()1.toGenericString()); System.out.

Println(Test.class.getFields()0.toGenericString()); System.out. Println("}"); } } That will output: Test extends java.util. ArrayList implements java.lang.

Comparable, java.util. List { public abstract M Test. Method(java.util.

List) public java.util. List Test. Field } I'm using toGenericString() method.

However this is just a pretty printing method! There is no need to parse that String: reflection api provides all the necessary information, eg: Field.getGenericType(), Method. GetArgumentTypes, ...

Let me check this on Monday. We're already using quite a bit of reflection, I may need to be more specific with my question, but don't want to misspeak without the example in front of me. – Dean J Oct 3 '09 at 15:23 1 Okay, checked.

The problem here is that there's something like: class Foo; -and- class Baz extends Bar; -and- class Zab extends Bar; When something calls Foo with Baz (Bar's subclass), all I can get back is Bar, not Baz; I've lost the subclass and can only get back the explicit type. Did that make any sense? – Dean J Oct 5 '09 at 20:29 Yep, unfortunatly, that's the kind of info that is erased.At runtime, there is only 1 instance of the class Foo (Foo.

Class). So you only get the "declared" generic type rather than the "instance" generic type. This is what "reified generic" discussion (gafter.blogspot.

Com/2006/11/reified-generics-for-java. Html) is about. Unfortunatly that won't be part of java 7 either.

– vdr Oct 7 '09 at 8:33.

Contrary to what is widely accepted and rarely known type erasure can be avoided, which means that the callee do have the ability to know which generic parameters were employed during the call. Please have a look at: Using TypeTokens to retrieve generic parameters Thanks.

Even after erasure has done its job, it is still possible to retrieve generic information from: Field, Method ( including method parameters ) and Class using reflection. Still, that's only the beginning. You need to start making use of that information using reflection api, so it is likely that your design will suffer if you were not expecting something like this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions