@AutoValue - “找不到生成的符号类";错误 [英] @AutoValue - "cannot find symbol class Generated" Error

查看:64
本文介绍了@AutoValue - “找不到生成的符号类";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 @AutoValue 注释时出现找不到符号类生成".

I am getting "cannot find symbol class Generated" while using the @AutoValue annotation.

public abstract  class Office{

public static Office create(String cityName, String companyName, String regionName) {
    return new AutoValue_Office(cityName, companyName, regionName);
}


public abstract String getCompanyName();
public abstract String getCityName();
public abstract String getRegionName();
}

Gradle 依赖编译'com.google.auto.value:auto-value:1.0-rc1'

Gradle dependency compile 'com.google.auto.value:auto-value:1.0-rc1'

另外,如何仅将选定的属性添加到 equals 和 hashcode 函数中.

Also, how can add only selected properties to equals and hashcode function.

推荐答案

问题是您使用的 Android 版本没有注释 javax.annotations.Generated(在 Java 6 中添加).您可以按照 这个问题.

The problem is that you are using a version of Android that doesn't have the annotation javax.annotations.Generated (which was added in Java 6). You could add that manually as described in the answer to this question.

关于从 equals 和 hashCode 中排除某些属性的问题,目前没有完美的方法来做到这一点.通常,这样做的愿望表明需要将应该包含在单独类中的属性分开,并在 equals 和 hashCode 中使用它.或者,您可以向 Office 类添加非最终字段并设置它们.例如,如果不包括 regionName,您可以这样写:

Concerning the question of excluding certain properties from equals and hashCode, there is currently no perfect way to do that. Often the desire to do this indicates a need to separate the properties that should be included into a separate class and use that in equals and hashCode. Alternatively, you could add non-final fields to the Office class and set them. For example, if regionName was not to be included, you could write something like this:

@AutoValue
public abstract class Office {
  private String regionName;

  public abstract String getCompanyName();
  public abstract String getCityName();
  public String getRegionName() {
    return regionName;
  }

  public static Office create(String cityName, String companyName, String regionName) {
    Office office = new AutoValue_Office(cityName, companyName);
    office.regionName = regionName;
    return office;
  }
}

主要缺点是 regionName 不是最终的,因此您无法像对其他属性一样保证从其他线程访问它.

The main disadvantage is that regionName is not final, so you don't get the same guarantees about access to it from other threads as you do for the other properties.

这篇关于@AutoValue - “找不到生成的符号类";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆