Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate code dynamically with annotations at build time in Java?

Tags:

I'm looking for a solution for generating code. I have googled, searched on SO and some blogs but I didn't find a good solution.

I'd like to put an annotation on my class and at compilation time, some methods and properties would be automatically added to the class.

Key points of the solution I'm looking for :

  • Generated code customizable (MANDATORY)
  • No external tool like apt have to be called (MANDATORY)
  • JDK only, no third-party framework (MANDATORY OPTIONAL)
  • Annotation name customizable (OPTIONAL)

For example :

@Aliasable public class MyClass { //Some properties  // Contructor ...  // Some methods } 

My class would look like this after compilation :

public class MyClass {    //Some properties    private String alias;     // Contructor ...     // Some methods    public String getAlias() {       return alias;    }     public void setAlias(String alias) {       this.alias=alias;    } } 

EDIT:
Finally, I turned my third requirement from MANDATORY to OPTIONAL and choosed project Lombok (easy integration with Maven and Eclipse, virtually no work to do for using it).

like image 501
Stephan Avatar asked Sep 09 '11 13:09

Stephan


People also ask

How are annotations executed in java?

Annotations don't execute; they're notes or markers that are read by various tools. Some are read by your compiler, like @Override ; others are embedded in the class files and read by tools like Hibernate at runtime.

Can annotations be extended?

In Java SE 6, annotations cannot subclass one another, and an annotation is not allowed to extend/implement any interfaces.

How can we create our own Java annotations?

Geek, now you must be wondering how can we create our own java annotations, for that refer to simple steps sequentially as follows: To create your own Java Annotation you must use @interface Annotation_name, this will create a new Java Annotation for you.

What are the different types of annotations in Java?

Compile-time instructions - Compile-time instructions provided by these annotations help the software build tools to generate code, XML files and many more. Runtime instructions - Some annotations can be defined to give instructions to the program at runtime. These annotations are accessed using Java Reflection.

How do I create a class from an annotated class?

Read the annotated class via reflection and transform it into a String which represents the source code of your new class. Write this string to file, compile this String using the Java compiler API and then load and instantiate the new class, all programatically; see exact steps here.

How do I make an annotation repeatable in Java?

For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java.lang.annotation package. Its value field specifies the container type for the repeatable annotation.


1 Answers

The annotation processing tool has been integrated in javac since version 1.6 and is part of the JDK. So there is no need for external tools when using the Pluggable Annotation API. You can generate any code by analysing custom annotations or method/parameter/field/class declarations using the Mirror API.

The annotation processor API says you shouldn't change existing classes. Instead you should generate subclasses of existing classes and create extension methods on those subclasses.

It seems to be possible to change classes anyway (e.g. by using bytecode manipulation libraries) though that would in contrast to the specification and could lead to issues with other annotation processors and may not work with all compilers in the same way.

like image 119
kapex Avatar answered Oct 23 '22 16:10

kapex