Generics and getClass

J

JavaEnquirer

Anyone know how I go about getting the class of a generic type e.g. I'd
like to be able to do the following:

Class clazz = T.getClass();

Seems a strange request, but without going into detail, I need to be
able to do this. I don't have access to an instance of type T and do
not want to pass an instance in either. In fact, I need to create an
instance of type T inside of a class paramaterised with type T.

many thanks in advance.
 
O

Oliver Wong

JavaEnquirer said:
Anyone know how I go about getting the class of a generic type e.g. I'd
like to be able to do the following:

Class clazz = T.getClass();

Not possible. The generic type information is erased at runtime.
Seems a strange request, but without going into detail, I need to be
able to do this. I don't have access to an instance of type T and do
not want to pass an instance in either. In fact, I need to create an
instance of type T inside of a class paramaterised with type T.

many thanks in advance.

The solution I've most commonly seen is to pass in an instance of
Class<T>.

E.g.

String foo = makeMeThisObject(String.class);

- Oliver
 
R

Roedy Green

Seems a strange request, but without going into detail, I need to be
able to do this. I don't have access to an instance of type T and do
not want to pass an instance in either. In fact, I need to create an
instance of type T inside of a class paramaterised with type T.

the problem is type erasure. All record of what T was is gone by run
time. T is usually just an Object by then.
 
J

JavaEnquirer

The solution I've most commonly seen is to pass in an instance of
Class<T>.

E.g.

String foo = makeMeThisObject(String.class);

- Oliver

Cheers Oliver, I feared that I'd have to do that, though, fear is too
strong a word!!!
 
Joined
May 4, 2011
Messages
1
Reaction score
0
There is a way to obtain the Class of the generic object.
For example in a method inside your class

public Class getGenericClass() {
Class result = null;
Type type = this.getClass().getGenericSuperclass();

if(type instanceof ParameterizedType){
ParameterizedType pt = (ParameterizedType) type;
Type[] fieldArgTypes = pt.getActualTypeArguments();
result = (Class) fieldArgTypes[0];
}

return result;
}

In bold lines you could see that "this" is refered to the class that is impelementing the generic object

hope you could continue with this clean solution
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
Generics ? 14
eclipse and un-generics 2
Generics 12
Generics 24
Hairy generics question 29
Generics annoyance 18
can this be done with generics? 32

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top