l*******w 发帖数: 61 | 1 见 //Q:处。
我想不出什么高招可以完成 getObjectClass() ... 惭愧。。。也许是做不到的?
请教各位高见。谢谢。
------------------------------
public class GenericReflection
{
private T object;
//---- get/set ---
public void setObject( T v ){ object=v; }
public T getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;
if( object!=null ) retval = (Class) object.getClass();
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection |
f*******n 发帖数: 12623 | 2 No, all generics are erased after compiling. There is no T. Anything you can
get you must get from stuff that you have at runtime.
Think about it, after type erasure your class looks like this:
public class GenericReflection
{
private Object object;
//---- get/set ---
public void setObject( Object v ){ object=v; }
public Object getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;
if( object!=null ) retval = object.getClass();
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection
How do you expect to get the answer?
Also, even your code for when it is non-null is not correct. The object's
class might be a subclass of T, so all you can return is Class extends T>,
not Class. |
l*******w 发帖数: 61 | 3 言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class extends T> objectClass; //cntr passed in
...
}//end class GenericReflection
这样勉强可保证object是null时,也可知其可能的Class。
谢谢 |
N******7 发帖数: 1297 | 4 根本不需要check object,一行搞定。
(Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]; |
l*******w 发帖数: 61 | 5 见 //Q:处。
我想不出什么高招可以完成 getObjectClass() ... 惭愧。。。也许是做不到的?
请教各位高见。谢谢。
------------------------------
public class GenericReflection
{
private T object;
//---- get/set ---
public void setObject( T v ){ object=v; }
public T getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;
if( object!=null ) retval = (Class) object.getClass();
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection |
f*******n 发帖数: 12623 | 6 No, all generics are erased after compiling. There is no T. Anything you can
get you must get from stuff that you have at runtime.
Think about it, after type erasure your class looks like this:
public class GenericReflection
{
private Object object;
//---- get/set ---
public void setObject( Object v ){ object=v; }
public Object getObject(){ return object; }
//---- is it possible to get the class for the object? ----
public Class getObjectClass()
{
Class retval = null;
if( object!=null ) retval = object.getClass();
else
{
//Q: can the class be determined when object is null,
// thru some kind of reflection?
}
return retval;
}//end getObjectClass(.)
}//end class GenericReflection
How do you expect to get the answer?
Also, even your code for when it is non-null is not correct. The object's
class might be a subclass of T, so all you can return is Class extends T>,
not Class. |
l*******w 发帖数: 61 | 7 言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class extends T> objectClass; //cntr passed in
...
}//end class GenericReflection
这样勉强可保证object是null时,也可知其可能的Class。
谢谢 |
N******7 发帖数: 1297 | 8 根本不需要check object,一行搞定。
(Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]; |