Eclipse AutoValue class fails to build

2019-05-06 16:55发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-05-06 17:13

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.

查看更多
迷人小祖宗
3楼-- · 2019-05-06 17:30

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.

查看更多
登录 后发表回答