• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get generic type?

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

This is the class declaration -

I want to get the T class.

In case that this class extended by another class its done by:


Any idea how its done for this?


need to get ItemTO.class

Thank you
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody else asked the same question very recently: here. It is not possible by any simple means, but have a look at the other thread which might give some information.
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure its not possible.
He has another question.

Thank you
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avihai marchiano wrote:I am not sure its not possible.
He has another question.

Thank you


The main difference between the other question and your question, is that the other question actually doesn't know the type of elements in the collection that is passed into the method.

With your question, you actually know that it is a Criteria<ItemTO> instance, so you know it is the ItemTO.class object that you want. And can just specify it iin source. No need to search based on the Criteria object.


Now, if you don't want to define the ItemTO.class in source, but want to search based on the Criteria<ItemTO> instance, then you have the same issue as the other question.

Henry
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i define the template type through using like this:
Criteria<ItemTO> criteria=new Criteria<ItemTO>();

So, T in the following declaration -
Criteria<T>

T will be ItemTO

I dont want to set the type by extend it.

Thank you
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Generic is of a type erasure. this is done for backwards compatibility. this means that you won't be able to code it to know the class of T during runtime in your Generic class. not sure this is related what you are looking for, but hope this will help a bit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic