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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


2 + = eleven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close