Android                
            Typedef注釈:@IntDef、@StringDef
        
        
            
    サーチ…
備考
アノテーションパッケージには、独自のコードを飾ることができる有用なメタデータアノテーションが多数含まれており、バグを捉えるのに役立ちます。
 build.gradleファイルに依存関係を追加するだけです。 
dependencies {
    compile 'com.android.support:support-annotations:25.3.1'
}
        IntDefアノテーション
このアノテーションは、期待する有効な整数定数のみが使用されることを保証します。 
次の例は、注釈を作成する手順を示しています。 
import android.support.annotation.IntDef;
public abstract class Car {
    //Define the list of accepted constants
    @IntDef({MICROCAR, CONVERTIBLE, SUPERCAR, MINIVAN, SUV})
    //Tell the compiler not to store annotation data in the .class file
    @Retention(RetentionPolicy.SOURCE)
    //Declare the CarType annotation
    public @interface CarType {}
    //Declare the constants
    public static final int MICROCAR = 0;
    public static final int CONVERTIBLE = 1;
    public static final int SUPERCAR = 2;
    public static final int MINIVAN = 3;
    public static final int SUV = 4;
    @CarType
    private int mType;
    @CarType
    public int getCarType(){
        return mType;
    };
    public void setCarType(@CarType int type){
        mType = type;
    }
}
 また、コード補完により、許可された定数を自動的に提供することもできます。 
このコードを作成すると、typeパラメータが定義された定数のいずれかを参照しない場合に警告が生成されます。 
フラグと定数の結合
 IntDef#flag()属性をtrueに設定すると、複数の定数を組み合わせることができます。 
このトピックの同じ例を使用する:
public abstract class Car {
    //Define the list of accepted constants
    @IntDef(flag=true, value={MICROCAR, CONVERTIBLE, SUPERCAR, MINIVAN, SUV})
    //Tell the compiler not to store annotation data in the .class file
    @Retention(RetentionPolicy.SOURCE)
    .....
}
 ユーザーは、許可された定数をフラグ( | 、 & 、 ^ )と組み合わせることができます。 
Modified text is an extract of the original Stack Overflow Documentation
        ライセンスを受けた CC BY-SA 3.0
        所属していない Stack Overflow