AutoValue

1282 ワード

Googleのオープンソース項目は、Javaテンプレートコードを自動的に補完するために使用されます。以下のコードの自動生成を含みます。
  • equals
  • hashCode
  • toString
  • getter/setter
  • getter
  • AutoValueは、カスタマイズされた抽象的なクラス(通常はエンティティクラス)を注釈で読み取ることにより、コンパイル時に同じパッケージディレクトリの下で、クラス名のプレフィックスとしてAutoValue_を作成する。
    典型的な例
    //     
    dependencies {
      apt 'com.google.auto.value:auto-value:1.2'
    }
    
    @AutoValue
    public abstract class Story{
      public abstract int id();
      public abstract String title();
      
      //     ,              
      public static Story create(int id, String title){
        new AutoValue_Story(id,title);
      }
    }
    
    自動的にParcel ableを実現します。
    dependencies {
      provided 'com.google.auto.value:auto-value:1.2'
      apt 'com.google.auto.value:auto-value:1.2'
      apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.1'
    }
    
    //        Parcelable     
    @AutoValue
    public abstract class Story implements Parcelable{
      public abstract int id();
      public abstract String title();
      public static Story create(int id, String title){
        new AutoValue_Story(id,title);
      }
    }
    
    参照
  • AutoValue-GitHub
  • Google AutoValueを使って自動的にコードを生成する