java class.gettype,Field.getGenericType()返回java.lang.Class的实例,而不是类型

I'm having issues with proguard and some reflection stuff.

Myclass.java

package not.obfuscated

class MyClass {

public List childs;

}

InnerClass.java

package not.obfuscated

class InnerClass {

//.somestuff

}

Inside proguard.cfg I have:

-keep class not.obfuscated.** {*;}

Inside another class I manage to get the "Field" instance for the MyClass.childs field and then try to get the getGenericType to determine which class is inside the List brackets (< InnerClass >)

For logging purposes I did the following Log.d code (field is the instance of Field representing MyClass.childs):

Log.d("FIELD", field.getName()+" generic type: "+ field.getGenericType()+ " class: "+ field.getGenericType().getClass().getName());

The output is following (2nd line):

MsQuV.png

As you see the field.getGenericType.toString() may be correct, but when I ask for the class it returns java.lang.Class. Indeed, a couple of line afterwards, when I do:

ParameterizedType listType = (ParameterizedType) field.getGenericType();

I receive a ClassCastException:

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

I strongly believe this is due to proguard, but as far as I know of proguard I already excluded all the classes inside the not.obfuscated package. As last try I also inserted the line -keep class java.lang.List (obviously nothing happened).

解决方案

By default Proguard removes some of the type information: How do you stop Proguard from removing type parameters?

Adding the following line should fix the issue:

-keepattributes Signature

May be the whole magic line would work, i.e.:

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值