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

3

27

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).

Ansela answered 9/9, 2011 at 13:50 Comment(3)
I know this is old...but do you find a good solution for this problem? Some code will be really appreciated...Equilibrium
@MarcoCastano Please see my edit.Ansela
Oh ok sorry...at least I managed to create code based on annotation at compile timeEquilibrium
T
15

Have a look at Project Lombok. It generates code as you ask when you write:

public class MyClass {
  @Getter @Setter private String alias;
}

It also does a lot more if you need it. I know you asked for no external tools, but you would basically be recreating this.

Treble answered 10/9, 2011 at 11:2 Comment(4)
Lombok is really impressionning. But under it's really a big hack. Maybe this hack will weaken it.Ansela
Thanks for pointing that out. I didn't realize it used undocumented compiler API: notatube.blogspot.com/2010/11/…Treble
For the author it was important to not use any 3rd party library.Banquet
@Banquet indeed, but there will be many more people (such as yourself) reading this answer besides the OP. Perhaps they will find it useful.Treble
F
16

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.

Fraenum answered 9/9, 2011 at 15:35 Comment(0)
T
15

Have a look at Project Lombok. It generates code as you ask when you write:

public class MyClass {
  @Getter @Setter private String alias;
}

It also does a lot more if you need it. I know you asked for no external tools, but you would basically be recreating this.

Treble answered 10/9, 2011 at 11:2 Comment(4)
Lombok is really impressionning. But under it's really a big hack. Maybe this hack will weaken it.Ansela
Thanks for pointing that out. I didn't realize it used undocumented compiler API: notatube.blogspot.com/2010/11/…Treble
For the author it was important to not use any 3rd party library.Banquet
@Banquet indeed, but there will be many more people (such as yourself) reading this answer besides the OP. Perhaps they will find it useful.Treble
C
3

I use XML and XSLT to generate code. It is used for EJB, Logic and the CRUD part of the views. It isnt gerated at runtime but instead on the buildserver. Developers can generate the code manually for well development purposes. This is done with the same command ANT uses on the buildserver.

Because the generation is with XML and XSLT it is highly customizable.

If you google Java code generation with XSLT you will run into alot of examples. Please note that this technique dates from ~2000 and thus probably has been preceded by now by easier solutions.

Centroclinal answered 9/9, 2011 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.