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.
Related Whitepaper:

Transforming a Generic Java IDE to Your Application Specific IDE

Improve software development by passing along shared knowledge and best practices.

One of the biggest challenges in application development today is how to factor in application-specific information. The answer lies in the ability to capture application evolution and developers' knowledge of building and extending the application as metadata so that it's not lost in time or translation. Once that can be done effectively, the combination of the application code along with application-specific metadata can be meaningfully packaged into reusable software asset modules. This white paper will discuss how 'Application Factories' can transform an IDE to play a central role in capturing both the application evolution and the developers' knowledge.

Get it Now!  

Leave a Reply


six − 6 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books