这样子是新创建了
Subscription subscription= (Subscription) field.getType().newInstance();
但是我不想新建
field.get()这个方法又有什么用
这样不行 Subscription subscription=(Subscription) field;不能转类型
这样能转类型,但是直接就报错了field不能转成Subscription
Object obj=(Object)field;
Subscription subscription=(Subscription) obj;
只是想注解,然后在基类的onDestory销毁类中注解的Subscription
不知道我的描述是否正确,如有不准确的地方请指正,谢谢,注解和反射也没有经常用
ringa_lee2017-04-18 10:36:49
If there is no problem with my understanding, what the questioner wants onDestory
时拿到堆内存中的对象实例。如果是这样,Unsafe
should be achievable. But there is no need to do this, because when the object instance saved in the heap does not have any references, it will be recycled in the next GC.
Unsafe
是java直接可以操作内存的后门,并非与平台无关,而且很不安全。在有GC管理内存的情况下,没有必要只用Unsafe
.