Is it possible to use a bytecode manipulation library like ASM at compile time?
是否可以在编译时使用像ASM这样的字节码操作库?
Specifically, I'd like to use Java's annotation processing API to implement boilerplate-heavy methods on annotated classes. Implementing an annotation processor is straightforward enough, but it seems like the .class
files don't yet exist when the Processor
is run. Is there another way?
具体来说,我想使用Java的注释处理API在带注释的类上实现样板重的方法。实现注释处理器很简单,但运行处理器时似乎还没有.class文件。还有另一种方式吗?
3 个解决方案
#1
1
It should be possible since the following project is doing it: Project Lombok
应该可以实现以下项目:Lombok项目
Also:
也:
Java 8 will bring a new mechanism that allows you to write plug-ins for the Java compiler (javac). A compiler plug-in lets you add new phases to javac without making changes to its code base. New behavior can be encapsulated in a plug-in and distributed for other people to use. For example, javac plug-ins could be used to do the following:
Java 8将带来一种新机制,允许您为Java编译器(javac)编写插件。编译器插件允许您向javac添加新阶段,而无需更改其代码库。新行为可以封装在插件中并分发给其他人使用。例如,javac插件可用于执行以下操作:
• Add extra compile-time checks
•添加额外的编译时检查
• Add code transformations
•添加代码转换
• Perform customized analysis of source code
•执行源代码的自定义分析
#2
0
You might be interested in Javassist ( http://www.jboss.org/javassist ) which can enhance and save classes as a post-compilation step.
您可能对Javassist(http://www.jboss.org/javassist)感兴趣,它可以增强和保存类作为后编译步骤。
This article describes how to save enhanced classes : http://java.dzone.com/articles/implementing-build-time
本文介绍如何保存增强类:http://java.dzone.com/articles/implementing-build-time
in particular, once you have altered a class, you can do something like this:
特别是,一旦你改变了一个类,你可以做这样的事情:
compiledClass.writeFile("/tmp/modifiedClassesFolder");
#3
-1
You should use CGLib instead. With CGLib you can add proxies with method interceptors and have the interceptor implement your boilerplate code. Another option is to look at Javassist. With Javassist you literally create a new subclass using actual text (in strings) and have javassist compile it into byte-code.
你应该使用CGLib。使用CGLib,您可以使用方法拦截器添加代理,并让拦截器实现您的样板代码。另一个选择是看看Javassist。使用Javassist,您可以使用实际文本(在字符串中)创建一个新的子类,并让javassist将其编译为字节代码。
#1
1
It should be possible since the following project is doing it: Project Lombok
应该可以实现以下项目:Lombok项目
Also:
也:
Java 8 will bring a new mechanism that allows you to write plug-ins for the Java compiler (javac). A compiler plug-in lets you add new phases to javac without making changes to its code base. New behavior can be encapsulated in a plug-in and distributed for other people to use. For example, javac plug-ins could be used to do the following:
Java 8将带来一种新机制,允许您为Java编译器(javac)编写插件。编译器插件允许您向javac添加新阶段,而无需更改其代码库。新行为可以封装在插件中并分发给其他人使用。例如,javac插件可用于执行以下操作:
• Add extra compile-time checks
•添加额外的编译时检查
• Add code transformations
•添加代码转换
• Perform customized analysis of source code
•执行源代码的自定义分析
#2
0
You might be interested in Javassist ( http://www.jboss.org/javassist ) which can enhance and save classes as a post-compilation step.
您可能对Javassist(http://www.jboss.org/javassist)感兴趣,它可以增强和保存类作为后编译步骤。
This article describes how to save enhanced classes : http://java.dzone.com/articles/implementing-build-time
本文介绍如何保存增强类:http://java.dzone.com/articles/implementing-build-time
in particular, once you have altered a class, you can do something like this:
特别是,一旦你改变了一个类,你可以做这样的事情:
compiledClass.writeFile("/tmp/modifiedClassesFolder");
#3
-1
You should use CGLib instead. With CGLib you can add proxies with method interceptors and have the interceptor implement your boilerplate code. Another option is to look at Javassist. With Javassist you literally create a new subclass using actual text (in strings) and have javassist compile it into byte-code.
你应该使用CGLib。使用CGLib,您可以使用方法拦截器添加代理,并让拦截器实现您的样板代码。另一个选择是看看Javassist。使用Javassist,您可以使用实际文本(在字符串中)创建一个新的子类,并让javassist将其编译为字节代码。