Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JVM bytecode be manipulated at compile time?

Is it possible to use a bytecode manipulation library like ASM at compile time?

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?

like image 713
Mike Craig Avatar asked Jan 28 '13 18:01

Mike Craig


Video Answer


1 Answers

You might be interested in Javassist ( http://www.jboss.org/javassist ) which can enhance and save classes as a post-compilation step.

This article describes how to save enhanced classes : https://dzone.com/articles/implementing-build-time

in particular, once you have altered a class, you can do something like this:

 compiledClass.writeFile("/tmp/modifiedClassesFolder");
like image 145
Tom Carchrae Avatar answered Sep 20 '22 10:09

Tom Carchrae