javax.annotation.processing
It’s good to know: Jeta isn’t evaluating the annotations by using Reflection API at runtime. It generates all the necessary code at compile-time. It allows to emit metacode as it’s handwritten. Also, unlike reflection, it’s possible to check the code for errors before it’s launched.
How it works:
There’s a bit of information on the Internet, but javac
allows us to generate source code. Moreover, it’s available since Java 1.5. It can launch an Annotation Processor before your Java sources will be compiled, so you can scan and process all the annotations in your code.
Currenty jeta is tested on Java 1.7. Older version might be supported in future releases.
Thanks to JavaPoet by Square for the great framework that Jeta uses to write Java code.
Hello, World!
Let’s take a look at the example, in which @SayHello
annotation sets “Hello, World!” into the field.
public @interface SayHello {
}
public class HelloWorldSample {
@SayHello
String str;
}
If Jeta had a processor for this example, the metacode would be:
public class HelloWorldSample_Metacode implements HelloWorldMetacode<HelloWorldSample> {
@Override
public void setHelloWorld(HelloWorldSample master) {
master.str = "Hello, World!";
}
}
You can find sample code of such processor on this page. Also, there’s a similar example on GitHub.
How HelloWorldSample_Metacode
is applied to HelloWorldSample
is explained in the next article.