4

I'm running Eclipse Kepler SR2, with Maven 3.1.1 attached with the m2e and m2e-apt plugins, and I'm getting an error I don't know how to resolve.

I managed to find all the dependencies needed to get @AutoValue working into my pom.xml, but now I'm in a state where it only works if the methods which need to be defined all have primitive return types. If I provide an abstract getter which returns an Object or more specific, I get this error:

@AutoValue processor threw an exception:
  java.lang.IllegalArgumentException:
    Failed to invoke com.google.auto.value.processor.AutoValueProcessor$Property.nullable() on getObject...

I've tried the basics - cleared the maven cache, restarted Eclipse, rebuilt the project... no dice. So I dug down into the source code and I found a discrepancy which I'm not sure how it's intended to be resolved.

In the Velocity template for the generated AutoValue class, there is some basic logic for rendering primitives differently than objects, for instance on line 37, p.nullable is checked. The p variable is an instance of AutoValueProcessor$Property class, which, as can be seen on line 205 of the preceeding link, has an isNullable() method, but no nullable method or property.

How is the Velocity rendering phase intended to work then? Does Velocity auto-expand p.nullable to p.isNullable some how, but not for me because reasons? Is this a bug? I'm not sure what to do from here.


Example class that doesn't compile:

@AutoValue
public abstract class Point {

  public static Point of(double x, double y) {
    return new AutoValue_Point(x, y);
  }

  public abstract Double x();

  public abstract Double y();

}

Eclipse highlights the described error under Point at the head of the class declaration.

torquestomp
  • 3,304
  • 19
  • 26
  • VTL expands references to either properties or methods, but I wonder if this is even related to your issue. Is the getter method returning a nullable Object (e.g. did you annotate it with @Nullable)? If not, does your test set it to a non-null value? – pmorken Jul 22 '14 at 01:11
  • @pmorken The error is not in test, it's at compile time, during the annotation processing phase (during AutoValueProcessor's runtime), so there is no runtime object to speak of. I've tried setting the getter to `@Nullable`, `@NonNull`, and neither, but all yield the same results. – torquestomp Jul 22 '14 at 04:06
  • I followed the instructions on the [Autovalue Github page](https://github.com/google/auto/tree/master/value) to create a [sample app](https://github.com/manish-in-java/google-autovalue). It worked fine. I have run this sample from the command-line (Maven 3.1.1), Eclipse Kepler (with m2e) and IDEA 13. Can you provide the code for the class and the field where you are running into the error? Does your code work when you run Maven from the command line? – manish Jul 25 '14 at 12:15
  • @manish Unfortunately I'm unable to debug this at present, flew out last Saturday and I won't be back at my PC for a week. I was able to produce the error with consistency with any @AutoValue class with a getter that returned an `Object`, I've posted some dummy code above for clarification. – torquestomp Jul 29 '14 at 03:48
  • I added your sample class to my [example project](https://github.com/manish-in-java/google-autovalue) and it too works fine for me. I simply followed the instructions on the [Github page](https://github.com/google/auto/tree/master/value#how-to-use-autovalue). I also notice that you mention m2e, m2e-apt plugins and "AutoValue dependencies" (plural) in your post. The Github page does not mention the Eclipse plugins and has a single Maven dependency. May be if you can list the exact build steps you are following and a screenshot from your Eclipse workbench, we can sort it out quicker. – manish Jul 29 '14 at 05:45

2 Answers2

5

It appears that the dependency com.google.code.findbugs:jsr305 is missing when Eclipse runs the annotation processor. Try adding it by opening the project properties, browsing to Java Compiler -> Annotation Processing -> Factory Path, clicking on "Add External JARs" and then selecting the jsr305 JAR. If you have built the project with maven from the command line, you should be able to select the JAR from your .m2 directory.

Here's what the proprties look like in my project (the first entry is automatically added by Eclipse and doesn't seem to be relevant):

Screenshot of project properties

In the pom.xml in version 1.0-rc1 of AutoValue, there is a comment "Must have this where procesor runs" at the jsr305 dependency. The dependency was removed after the release of 1.0-rc1, so adding it to the annotation processor factory path will probably not be necessary with version 1.0.

See also this blog post for an introduction to using AutoValue with Eclipse.

Borstel
  • 128
  • 5
3

You might want to install the m2e-apt plugin, which handles automatic annotation processing based on the pom.xml dependencies:

https://marketplace.eclipse.org/content/m2e-apt

Make sure to enable it in you project preferences or globally in section:

Maven -> "Annotation processing" -> select "Automatically configure JDT APT..."

Detailed info here and here.

PJ_Finnegan
  • 1,981
  • 1
  • 20
  • 17