我想使用ASM字节码操作将Annotation添加到外部类中,但似乎我遗漏了一些东西。打印结果仍然没有任何注释。
private void fixAnnotations4Classes() throws Exception {
final ClassReader reader = new ClassReader("some/3rdparty/class/Foo");
final ClassNode classNode = new ClassNode();
reader.accept(classNode, 0);
List<FieldNode> fields = classNode.fields;
List<AnnotationNode> visibleAnnotations = fields.get(0).visibleAnnotations;
visibleAnnotations.add(new AnnotationNode("my/new/Annotation"));
ClassWriter writer = new ClassWriter(0);
classNode.accept(writer);
System.out.println(Arrays.toString(Foo.class.getDeclaredField("profile").getAnnotations()));
System.out.println("FIN");
}
答案 0 :(得分:0)
在使用附加注释创建新类时,您永远不会替换实际加载的原始类文件。
因此,您的Foo类将以其原始版本加载,或者已经加载,以便您的更改是无操作。