如何使用BCEL获取类的通用信息?

时间:2012-06-12 13:16:34

标签: generics bytecode bcel

我想使用BCEL 6.0-SNAPSHOT获取CounterPersistence类的通用信息( Counter 类)。签名是这样的:

public interface CounterPersistence extends BasePersistence<Counter> {
....
}

我正在使用以下代码来读取字节码

JavaClass javaClass = ...;
Attribute[] attributes = javaClass.getAttributes();
for (Attribute attribute : attributes) {
    if (attribute instanceof Signature) {
        Signature signature = (Signature) attribute;
        //put the code here that get the Counter class from the signature
    }
}

但是我没有编写解析签名的代码,并允许我获取类型计数器。   想法?

2 个答案:

答案 0 :(得分:1)

您可以使用FindBugs中的BCEL实用程序,例如GenericUtilities.getTypeParameters(...)方法或更好的方法切换到ASM框架。

答案 1 :(得分:0)

我找到了解决方案:

Class.forName(org.apache.bcel.classfile.Utility.methodSignatureArgumentTypes("(" + signature + ")V")[0]);
相关问题