Two Laptop Bag*
The moose likes Beginning Java and the fly likes how to determine the real type of a generic property? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to determine the real type of a generic property?" Watch "how to determine the real type of a generic property?" New topic
Author

how to determine the real type of a generic property?

Geronimo M. Hernandez
Greenhorn

Joined: Mar 16, 2009
Posts: 7
Hello,

I have a concrete class, which is a child of a generic class. That generic class is a bean with a generic propery (I simplified the classes for sample use):


The concrete class looks like


From a beanprocessor I like to determine the type of the "elem" property used at the concrete type "Derived".
How can I figure that out?
Using reflection, the return-type of getElem() is Object.

Any hint is welcome.

regards Gero
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You can't, because of type erasure. In short, generics are a compile time only mechanism, and all traces of it are removed from the generated byte code.

How I solved this once is by giving the abstract class an abstract method:

Not the most ideal solution though, because you have to override this method in all concrete subclasses. Moreso, the following provides a problem:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Geronimo M. Hernandez
Greenhorn

Joined: Mar 16, 2009
Posts: 7
Thank you for your support!

I will implement that getType().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to determine the real type of a generic property?