USER'S GUIDE SOURCE CODE SAMPLES CONTACT DONATE

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.

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.

How it works:

User's Guide:

Androjeta

Advanced

Stable version: 2.3
Available on Java 1.7+
Android Compatible
Fork me on GitHub