Eclipse AutoValue class fails to build

假装没事ソ 提交于 2019-12-22 08:39:36

问题


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.


回答1:


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):

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.




回答2:


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.



来源:https://stackoverflow.com/questions/24844399/eclipse-autovalue-class-fails-to-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!