Redian新闻
>
Is it possible to get Class object for T from a generic class? (下列空档,是否可填)
avatar
Is it possible to get Class object for T from a generic class? (下列空档,是否可填)# Java - 爪哇娇娃
l*w
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
avatar
f*n
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.
avatar
l*w
3
言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class objectClass; //cntr passed in
...
}//end class GenericReflection
这样勉强可保证object是null时,也可知其可能的Class。
谢谢
avatar
N*7
4
根本不需要check object,一行搞定。
(Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
avatar
l*w
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
avatar
f*n
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.
avatar
l*w
7
言之有理。多谢了。
看来实在需要的话,只好多加一个attribute....
public class GenericReflection
{
private T object;
private Class objectClass; //cntr passed in
...
}//end class GenericReflection
这样勉强可保证object是null时,也可知其可能的Class。
谢谢
avatar
N*7
8
根本不需要check object,一行搞定。
(Class) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。