Software Development

Using the AutoValue Code Generator in Eclipse

My colleague Moritz of EclipseSource recently came across Google Auto, a collection of Java code generators. AutoValue is one of them and generates Java code for immutable objects. It provides an annotation and an annotation processor to generate Java code for immutable value objects. The current version is labelled RC1 and a 1.0 release is soon to come.

The idea is to have an abstract class that defines the outside behavior and the annotation processor will do the rest. For each desired field, an abstract accessor method is defined. Finally a factory method is needed to create instances of the generated class.
 

@AutoValue
abstract class Person {
  static Person create( String name, int age ) {
    return new AutoValue_Person( name, age );
  }

  abstract String name();
  abstract int age();
}

After running the processor, you will also have an AutoValue_Person class that lives in the same package and implements the accessor methods to return the field values. It also implements equals, hashCode and toString. This is how the generated code looks like:

final class AutoValue_Person extends Person {
  private final String name;
  private final int age;

  AutoValue_Person(String name, int age) {
    if (name == null)
      throw new NullPointerException("Null name");
    this.name = name;
    this.age = age;
  }

  String name() {
    return name;
  }
  ...

  public String toString() {
    ...
  }

  public boolean equals(Object o) {
    ...
  }

  public int hashCode() {
    ...
  }
}

As you can see, there is no magic involved. The processor generates readable and debug-steppable code without any runtime-dependencies. Aspects like nullability, serialization, custom implementations of equals, hashCode and toString and more can be customized. Follow the link in the project’s readme for all the details.

I quite like to idea of a small tool that does one thing and does it good. A good share of writing EqualsTester was motivated through testing equals and hashCode for – you guess it – immutable value objects. If AutoValue would have been around then, it might have saved some effort. However, this tool would be of limited practical use if it didn’t integrate well into your IDE of choice.

Integrate AutoValue into Eclipse

When using AutoValue in an IDE you will likely want to have the code generated as soon as you save the source file. My IDE of choice is Eclipse and thus I took these steps to integrate the annotation processor in Eclipse.

Rules for annotation processing in Eclipse must be specified per project. Hence, the first thing is to enable annotation processing for the project that uses the processor (Project properties > Java Compiler > Annotation Processing). By default the generated code is written to a separate source folder (Generated source directory setting) so that it can be excluded from version control.

Next I told Eclipse where to find the annotation processor for @AutoValue annotations. The processor is packaged together with the annotation itself in the auto-value-1.0-rc1.jar. Starting with this jar and going along its dependency chain, I ended up with these libraries that are necessary to run the processor (all available from Maven Central):

  • auto-service-1.0-rc1.jar
  • guava-16.0.1.jar
  • jsr-305-2.0.3.jar
  • auto-value-1.0-rc1.jar

apt-factory-path
Well, everything comes at a price. However, adding these jars to the Factory Path is sufficient. The processor (re-)generates code for every @AutoValue annotated class within the project whenever I hit Ctrl+S – very sleek…

What is left, is to make the setup easily consumable for other team members. The problem here is to place the jars in a location that is portable across different developer computers. I didn’t find a solution other than checking the settings and the ~2MB of jars into the source code repository. While having binaries in a repository certainly isn’t desirable, having every team member pull the jars from some place is as bad. Please share your thoughts if you know a way out of this dilemma.
 

Reference: Using the AutoValue Code Generator in Eclipse from our JCG partner Rudiger Herrmann at the Code Affine blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button