메타 어노테이션

@Target

 - 어노테이션을 적용할 대상을 지정

 - ElementType 

CONSTRUCTOR         // 생성자 선언부

FIELD                      // enum포함 필드선언부

LOCAL_VARIABLE     // 지역변수 선언부

METHOD                 // 메소드 선언부

PACKAGE                 // 패키지 선언부

PARAMETER             // 파라메터 선언부

TYPE                     // 클래스, 인터페이스, enum 선언부

@Retention

 - 어노테이션 정보가 보관되는 기간을 지정

 - RetentionPolicy

SOURCE    // 어노테이션이 컴파일러에서 버려짐

CLASS    // 클래스안의 어노테이션은 사용되지만 VM에서는 버려짐

RUNTIME    // VM에서 유지가 되므로 리플렉션으로 활용 가능

@Documented

 - Javadoc에 포함

@Inherited

 - 서브클래스 부모 어노테이션을 상속


활용 Exam


@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Handler {

    String value();

    String name();

}


///


private void base(Handler annotation) {

        Preconditions.checkNotNull(annotation);


        name = annotation.name();

        path = annotation.value();

    }



///


public HttpRequestHandler build(Object obj, Method method) {
        Preconditions.checkNotNull(obj);
        Preconditions.checkNotNull(method);

        base(method.getAnnotation(Handler.class));

        Preconditions.checkNotNull(name);
        Preconditions.checkNotNull(path);
        
        String httpMethod = null;
        
        String upperCaseName = name.toUpperCase();
        if(upperCaseName.startsWith("GET")) {
            httpMethod = "GET";
        } else if(upperCaseName.startsWith("POST")) {
            httpMethod = "POST";
        } else if(upperCaseName.startsWith("PUT")) {
            httpMethod = "PUT";
        } else if(upperCaseName.startsWith("DELETE")) {
            httpMethod = "DELETE";
        }
  HttpRequestHandler handler = new HttpRequestHandler(name, path, httpMethod);
        handler.addArgs(args);
        handler.setRaw(obj, method);
        
        return handler;
    }

+ Recent posts