使用 Apache Bcel 库读取注释
Read annotations using Apache Bcel library
我正在尝试使用此代码阅读 class 注释:
JavaClass jclas = new ClassParser("src\test\org\poc\TargetHello.class").parse();
ClassGen cg = new ClassGen(jclas);
Attribute[] attributes = cg.getAttributes();
for (Attribute attribute : attributes) {
if (attribute instanceof Annotations) {
Annotations annotations = (Annotations) attribute;
AnnotationEntry[] entries= annotations.getAnnotationEntries();
}
}
但是对于这段代码 attribute instanceof Annotations
我得到错误:Inconvertible types; cannot cast 'com.sun.org.apache.bcel.internal.classfile.Attribute' to 'org.apache.bcel.classfile.Annotations'
你知道我怎么解决这个问题吗?
这对我有用。您没有提供完整的可编译示例,也没有说明您的命令 运行。这是我所做的。
文件Hello.java
:
@Deprecated
public class Hello {
public static void main(String[] args) {}
}
文件AttributeAnnotations.java
:
import java.io.IOException;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Annotations;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ClassGen;
public class AttributeAnnotations {
public static void main(String[] args) throws IOException {
JavaClass jclas = new ClassParser("Hello.class").parse();
ClassGen cg = new ClassGen(jclas);
Attribute[] attributes = cg.getAttributes();
for (Attribute attribute : attributes) {
System.out.println("attribute: " + attribute);
if (attribute instanceof Annotations) {
Annotations annotations = (Annotations) attribute;
System.out.println("annotations: " + annotations);
AnnotationEntry[] entries = annotations.getAnnotationEntries();
}
}
}
}
对 运行 的命令:
wget https://repo1.maven.org/maven2/org/apache/bcel/bcel/6.4.1/bcel-6.4.1.jar
javac Hello.java
javac -cp bcel-6.4.1.jar AttributeAnnotations.java
java -cp .:bcel-6.4.1.jar AttributeAnnotations
所有命令均已完成且没有错误。
我正在尝试使用此代码阅读 class 注释:
JavaClass jclas = new ClassParser("src\test\org\poc\TargetHello.class").parse();
ClassGen cg = new ClassGen(jclas);
Attribute[] attributes = cg.getAttributes();
for (Attribute attribute : attributes) {
if (attribute instanceof Annotations) {
Annotations annotations = (Annotations) attribute;
AnnotationEntry[] entries= annotations.getAnnotationEntries();
}
}
但是对于这段代码 attribute instanceof Annotations
我得到错误:Inconvertible types; cannot cast 'com.sun.org.apache.bcel.internal.classfile.Attribute' to 'org.apache.bcel.classfile.Annotations'
你知道我怎么解决这个问题吗?
这对我有用。您没有提供完整的可编译示例,也没有说明您的命令 运行。这是我所做的。
文件Hello.java
:
@Deprecated
public class Hello {
public static void main(String[] args) {}
}
文件AttributeAnnotations.java
:
import java.io.IOException;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.Annotations;
import org.apache.bcel.classfile.Attribute;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ClassGen;
public class AttributeAnnotations {
public static void main(String[] args) throws IOException {
JavaClass jclas = new ClassParser("Hello.class").parse();
ClassGen cg = new ClassGen(jclas);
Attribute[] attributes = cg.getAttributes();
for (Attribute attribute : attributes) {
System.out.println("attribute: " + attribute);
if (attribute instanceof Annotations) {
Annotations annotations = (Annotations) attribute;
System.out.println("annotations: " + annotations);
AnnotationEntry[] entries = annotations.getAnnotationEntries();
}
}
}
}
对 运行 的命令:
wget https://repo1.maven.org/maven2/org/apache/bcel/bcel/6.4.1/bcel-6.4.1.jar
javac Hello.java
javac -cp bcel-6.4.1.jar AttributeAnnotations.java
java -cp .:bcel-6.4.1.jar AttributeAnnotations
所有命令均已完成且没有错误。