2

I'm porting an SDK from Android to plain Java and have run into an AutoParcel annotation that I don't understand.

Here's the original class and a snippet below:

@AutoParcel.Builder
public abstract static class Builder {
    public abstract Builder id(String id);
...
    public abstract SimpleFeature build();
}

public static Builder builder() {
    return new AutoParcel_SimpleFeature.Builder();
}

I am able to pretty much port everything to AutoValue without incident, except that last function, as I don't understand what it is or it's equivalent in AutoValue.

Can someone explain what this is, and what its equivalent is in AutoValue?

Tony Laidig
  • 1,048
  • 2
  • 11
  • 33

2 Answers2

2

The build annotation allows you to construct the immutable POJOs using the builder pattern i.e. something like

SimpleFeature.builder().id("test").build()

The equivalent annotation (not surprisingly since AutoParcel is a port of Autovalue with android specific features i.e. Parcelable)

@AutoValue.Builder

You should be able to find much more comprehensive documentation at https://github.com/google/auto/tree/master/value#builders

JohnWowUs
  • 3,053
  • 1
  • 13
  • 20
  • I tried that originally, but Eclipse suggests that the annotation is disallowed for this location. – Tony Laidig Mar 17 '16 at 15:18
  • Thank you for the response and pointers to the docs. Builder is a new, interesting pattern for me. I still see the error that AutoValue_SimpleFeature cannot be resolved to a type when creating the public static class. Eclipse wants to fill in all of the required functions from the abstract class, but that makes AutoValue worthless. – Tony Laidig Mar 17 '16 at 15:48
  • I think the problem is your eclipse settings. I don't use Eclipse myself but [this link](http://www.codeaffine.com/2014/03/04/using-the-autovalue-code-generator-in-eclipse/) might help to get AutoValue and Eclipse working together. – JohnWowUs Mar 17 '16 at 15:56
2

As JohnWowUs' comment suggests, this was largely an Eclipse issue.

The link he mentioned was only part of the solution, but I didn't need to drop more JARs into the project. With the help of an issue in the AutoValue repo and specifically configuring the maven-compiler-plugin, setting JDK1.7 as a target, with the following section added to the pom.xml:

<annotationProcessors>
    <annotationProcessor>com.google.auto.value.processor.AutoValueProcessor</annotationProcessor>
</annotationProcessors>
Tony Laidig
  • 1,048
  • 2
  • 11
  • 33