Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type of Generic

 
Mic Popolin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I get the type of the generic using package java.lang.reflect?
Like:

class Boo<E>{
execute(){
//How can I get the Class of E ???
}
}

or

class TesteBoo(Boo<?> boo){
//What the type of '?' ???
}

Thank's
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't. Generics are meaningful only at the source code level. They are not preserved in the class file. So there is no way by reflection to get information about generics from the compiled class.

This is called "erasure." The reason for it is to preserve binary compatibility with older compiled code that does not use generics.

Geoffrey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic