actual type of variable type in parameterized type

Hi all,

Is there way to find out what is actual type of the Variable type in Parameterized type, for example:

class A<T> {

//actual type of variable type T

Class<T> tClass;

public void setTClass() {

// I don't know what should I do here

}

public Class<T> getTClass() {

return tClass;

}

public static void main(String[] args) {

assert((new A<Integer>().getTClass()).toString().equals("class java.lang.Integer"));

}

}

Message was edited by:

MjLaali

Message was edited by:

MjLaali

Message was edited by:

MjLaali

[663 byte] By [MjLaalia] at [2007-10-3 4:48:47]
# 1

This is pretty mixed up stuff...

1) No, you can't directly find out the runtime type of a type parameter

2) You can achieve the same result using a technique called "type tokens" which is pretty much what you have already with your tClass member variable

3) You don't want the setTClass() method - people shouldn't be able to change this

4) You want a parameter of type Class<T> in your constructor:

...

public A(Class<T> clazz) {

this.tClass = clazz;

}

...

5) In your assert statement, don't futz about with strings, just do ...getTClass() == Integer.class

dannyyatesa at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...
# 2
> 3) You don't want the setTClass() method - people> shouldn't be able to change thisYou can't have the setTClass() method and people can't change it. The only way to change it is via a reference in the code to A<some other type>.
ejpa at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...
# 3

yes ofcource I can't chenge the actual type of variable type T in run time, but I would like to get actual class of T in run time like this example :

package test;

import java.lang.reflect.ParameterizedType;

abstract class B<T>{

private Class<T> variableClass;

public B() {

this.variableClass = (Class<T>) ((ParameterizedType) getClass()

.getGenericSuperclass()).getActualTypeArguments()[0];

}

public Class<T> getTClass(){

return variableClass;

}

}

class A extends B<Integer> {

public static void main(String[] args) {

assert(new A().getTClass() == Integer.class);

}

}

but I don't want class A and I would like to move main method to class B.

MjLaalia at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...
# 4
you can find the answer here: http://www-128.ibm.com/developerworks/java/library/j-jtp01255.htmlThere is no way :(Thanks for you answer.
MjLaalia at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...
# 5
This is related to the self-type pattern for generics. Take a look at the following for more info about this problem and support (or lack thereof) for it. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6479372
jonbarrila at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...
# 6
Dude, are you going to post this message to any more threads?In this case, you're wrong. This is nothing to do with self-typing. The OP just wants to know that the runtime type of T is.
dannyyatesa at 2007-7-14 22:53:15 > top of Java-index,Core,Core APIs...