views:

179

answers:

1

I have a class like this :

public class SubClass <T> extends SuperClass<Collection<T>>{
   public Class<T> getInner() {
      // What goes here?
   }
}

Where SuperClass is simply a generic class. My question is how to gain access to a Class object in order to return it? I can't seem to be able to do this because of Type erasure.

Any help?

+2  A: 

Exactly, type erasure means you have to do it explicitly.

Basically you need SubClass to take a Class<T> as a parameter in the constructor, and then hang onto it. You can still end up with type erasure issues there though, if T is actually meant to be (say) List<String>. See Neal Gafter's blog for more info on this and a workaround, and the Guice 2.0 TypeLiteral class for an implementation.

Jon Skeet
hmm i was afraid this would be the only option... I need to have this class with no contructor with parameters since it is one of many subclasses of superclass that are created using reflection with a no parameter call to the newInstanse method. So there is absolutely no trick i can use to get around this restriciton? Any ugly hack would help :)
Savvas Dalkitsis
No. The information just isn't present at execution time.
Jon Skeet
update to your answer: I will only pass non generic classes to collection<T> so the added type erasure issue will not pop up.
Savvas Dalkitsis
thanks that pretty much buries my efforts :P
Savvas Dalkitsis