由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Is it possible to get Class object for T from a generic class? (下列空档,是否可填)
相关主题
Re: 初级问题extending generic class , but not mentioning its parameterized type?
请问有没有generic的array问一个generic的问题吧
what's inside an java object?看了zhaoce073大水忍不住说2句
Re: 如何在两个窗口之间通信?how to copy an Object?
[合集] 谁能解释一下这里的protected specifiera simple java programming question
泛型问题please help me to solve this question!
请教一个动态cast的问题Question: reflection and generics
abstract class 的简单例子access java parent class protected variable question
相关话题的讨论汇总
话题: class话题: object话题: retval话题: getobjectclass话题: genericreflection
1 (共1页)
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,
not Class.
l*******w
发帖数: 61
3
言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class 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,
not Class.
l*******w
发帖数: 61
7
言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class objectClass; //cntr passed in
...
}//end class GenericReflection
这样勉强可保证object是null时,也可知其可能的Class。
谢谢
N******7
发帖数: 1297
8
根本不需要check object,一行搞定。
(Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
1 (共1页)
相关主题
access java parent class protected variable question[合集] 谁能解释一下这里的protected specifier
这个应该是java本身的错误吧?泛型问题
如何删除 linked list 的最后一个元素请教一个动态cast的问题
generics这样改对马?abstract class 的简单例子
Re: 初级问题extending generic class , but not mentioning its parameterized type?
请问有没有generic的array问一个generic的问题吧
what's inside an java object?看了zhaoce073大水忍不住说2句
Re: 如何在两个窗口之间通信?how to copy an Object?
相关话题的讨论汇总
话题: class话题: object话题: retval话题: getobjectclass话题: genericreflection