本文介绍了Field.getGenericType()返回java.lang.Class的实例,而不是类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ProGuard和一些反思的东西的问题。

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

Myclass.java

package not.obfuscated
class MyClass {
    public List<InnerClass> childs;
}

InnerClass.java

package not.obfuscated
class InnerClass {
    //.somestuff
}

里面proguard.cfg我有:

Inside proguard.cfg I have:

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

在另一个班我弄到了MyClass.childs领域的场实例,然后试图让getGenericType以确定哪些类是List括号内(小于将InnerClass>)

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 >)

有关日志记录我做了以下Log.d code(的字段的是现场重新presenting MyClass.childs的实例):

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):

正如你所看到的field.getGenericType.toString()可能是正确的,但是当我问这个类返回java.lang.Class中。事实上,一对夫妇线之后,当我做:

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();

我收到一个ClassCastException:

I receive a ClassCastException:

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

我坚信这是由于ProGuard的,但据我所知的ProGuard的我已经排除了not.obfuscated包内的所有类。作为最后一次尝试我也插线-keep类java.lang.List(明明什么都没有发生)。

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).

推荐答案

默认情况下Proguard的删除某些类型的信息:How你从删除类型参数停止Proguard的?

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

这篇关于Field.getGenericType()返回java.lang.Class的实例,而不是类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:47