ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 15. demo1.annotation - (10) NotNullAnnotationProcessor Demo ( 어노테이션2 )
    java/코드 리뷰 2020. 4. 27. 14:09
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package demo1.annotation;
     
    import static java.lang.annotation.ElementType.FIELD;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
     
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
     
    @Retention(RUNTIME)
    @Target(FIELD)
    public @interface NotNull {
        
        // 어노테이션 요소 정의
        String message();
     
    }
     
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    package demo1.app;
     
    import demo1.annotation.NotNull;
     
    public class User {
        
        @NotNull(message = "이름은 필수 입력값입니다.")
        String name;
     
        @NotNull(message = "이메일은 필수 입력값입니다.")
        String email;
        
        String tel;
        
        public User() {
            
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getEmail() {
            return email;
        }
     
        public void setEmail(String email) {
            this.email = email;
        }
     
        public String getTel() {
            return tel;
        }
     
        public void setTel(String tel) {
            this.tel = tel;
        }
        
    }
     
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package demo1.app;
     
    import java.lang.reflect.Field;
     
    import demo1.annotation.NotNull;
     
    public class NotNullAnnotationProcessor {
        
        public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
            User user = new User();
            user.setName("홍길동");
            
            Class clz = user.getClass();
            
            // User 객체에 선언된 멤버변수(필드) 모두 조회
            Field[] fields = clz.getDeclaredFields();
            
            for (Field field : fields) {
                // 필드명 조회
                String fieldName = field.getName();
                // 해당 필드가 @NotNull 에노테이션이 부착되어 있는지 확인
                boolean isNotNullField = field.isAnnotationPresent(NotNull.class);
                
                if (isNotNullField) {
                    // 해당 필드에 저장된 값 조회
                    String value = (String) field.get(user);
                    System.out.println("필드명: " + fieldName + ", " + value);
                    
                    // 필드의 값이 null 이라면
                    if (value == null) {
                        // 필드에 부착된 NotNull 어노테이션 객체 획득
                        NotNull annotation = field.getAnnotation(NotNull.class);
                        // 어노테이션의 구성요소 중 message의 값 조회
                        String message = annotation.message();
                        // 메시지값 화면에 출력
                        System.out.println("에러 메시지 " + message);
                    }
                    
                    field.getClass();
                }
                
            }
            
        }
    }
     
     
Designed by Tistory.