annotation

Jang Seok Woo·2021년 7월 9일
0

실무

목록 보기
33/136

public class LoginAppbarDeco extends AppbarDecorator {

public LoginAppbarDeco(@StringRes int titleRes, @DrawableRes int imageTitleRes, Runnable leftRunnable) {
    super(R.color.white, R.drawable.btn_top_back, titleRes, imageTitleRes, leftRunnable);
}

}

이런 코드가 있다.

@StringRes의 역할이 알고싶어졌다.

우선 명칭은 Annotation
3가지 형태가 있다.

  1. Nullness annotattions

  2. Resource type annotations

  3. IntDef and StringDef annotations

  1. Nullness annotations

@Nonnull

이 녀석은 전달되는 param 이 null 이어서는 안 된다는 것을 의미한다.

만약 명시적으로 null 이 전달되는 case 라면 warning 을 표시하게 된다.

반대로 @Nullable도 있다.

  1. Resource type annotations

전달받는 resource 의 type 을 명시할 수 있다.

void sayHello(@StringRes int id){

     ...

}

만약 여기에 전달되는 id 가 string res 가 아니라면 error 를 표시한다.

@StringRes, @DrawableRes, @ColorRes, @InterpolatorRes 등등이 있다.

@AnyRes 도 가능하다.

  1. IntDef and StringDef annotations

IntDef 나 StringDef 는 Annotation 을 정의해서 사용할 때 전달되는 값을 validate 할 수 있어서 좋다.

public class IceCreamFlavourManager {
    private int flavour;
    public static final int VANILLA = 0;
    public static final int CHOCOLATE = 1;
    public static final int STRAWBERRY = 2;
    @IntDef({VANILLA, CHOCOLATE, STRAWBERRY})
    public @interface Flavour {
    }
    @Flavour
    public int getFlavour() {
        return flavour;
    }
    public void setFlavour(@Flavour int flavour) {
        this.flavour = flavour;
    }
}

위와 같이 Flavour annotation 을 정의하여 사용하면,

param 으로 전달되는 값이 정의한 3가지 값이 아니라면, error 를 표시한다.

덧붙여 IDE 에서 suggest argument 로도 작동을 한다.

출처: https://aroundck.tistory.com/4637 [돼지왕 놀이터]

그냥 전반적으로 해당 값을 annotation

profile
https://github.com/jsw4215

0개의 댓글