【问题标题】:Using @AutoValue with nested classes gives a "cannot find symbol" error将 @AutoValue 与嵌套类一起使用会出现“找不到符号”错误
【发布时间】:2014-03-26 18:21:05
【问题描述】:

当尝试将@AutoValue 与嵌套类一起使用时:

public class Nested {
  @AutoValue
  public static abstract class Example {
    public static Example create(String name, int integer) {
      return new AutoValue_Example(name, integer);
    }
    public abstract String name();
    public abstract int integer();
  }
}

对于AutoValue_Example,我收到编译器错误cannot find symbol。关于我做错了什么有什么想法吗?

【问题讨论】:

  • 是编译时类路径上的AutoValue 类型。

标签: java auto-value


【解决方案1】:

当您的类像这样嵌套时,生成的 AutoValue 类将命名为 AutoValue_Nested_Example。如docs中所述:

嵌套

对于名为 Foo.Bar.Qux 的嵌套抽象值类型,生成的实现类名为 AutoValue_Foo_Bar_Qux。

【讨论】:

  • 难道Example 类也需要是static 吗? (顺便说一句,create 方法也缺少 static 关键字)
  • 是的,一般来说。
  • 仅供参考:我刚刚编辑了问题以添加缺少的 static 关键字。
【解决方案2】:

内部类(如果它是静态的)在名为“AutoValue_outerClass_innerClass”的单独源文件中生成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2019-11-29
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多