달력

5

« 2024/5 »

  • 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

'Annotations'에 해당되는 글 1

  1. 2007.08.14 JDK 5.0의 새로운 기능 Annotation
2007. 8. 14. 10:57

JDK 5.0의 새로운 기능 Annotation 그거/Java2007. 8. 14. 10:57

o annotation
  : source code에 사용하는 meta-tag

  @MyAnnotation(doSomething="What to do")
  public void myMethod() {
      ....
  }

  - JDK5.0에서 사용가능한 2가지 Annotation

    Simple Annotations : custom annotation type은 생성할 수 없는 기본으로 제공되는 Annotation.
    Meta Annotations : annotation-type 선언에 대한 annotation. 즉, annotation의 annotation

  - Simple Annotations

    기본으로 제공되는 annotation에는 3가지가 있다.

    1) Override
    super class의 method를 꼭 override 해야 됨을 표시.
    이 annotation을 사용한 method가 super class의 method를 override 하지 않으면, compile시 오류 발생

    Example:

    @Override
    public Strig tostring() {
       return super.toString() + " Testing annotation name : 'Override'";
    }

    위 예제처럼 Object class의 toString() method를 override하기 위해 annotation @Override를 사용한 후에 compile을 하면
    아래와 같은 오류가 발생한다.

       Compiling 1 source file to D:tempNew Folder (2)
                                     TestJavaApplication1buildclasses
       D:tempNew Folder (2)TestJavaApplication1srctest
          myannotationTest_Override.java:24: method does not override
                       a method from its superclass
       @Override
       1 error

 2) Deprecated
    code에 deprecated된 method등을 사용시에 compiler가 deprecated 된 부분에 대해서 warning을 보여주도록 함.

    Example:

    public class TestDeprecated {
       
@Deprecated
        public void doSomething() {
            System.out.println("Testing annotation name : 'Deprecated'");
        }
    }

    public class TestAnnotations {
        public static void main(String[] args) {
            new TestAnnotations();
        }

        public TestAnnotations() {
            TestDeprecated t2 = new TestDeprecated();
                  t2.doSomething();
        }
    }

    위 소스를 컴파일 해보면 둘 다 문제없이 compile이 되지만, TestAnnotations class는 compile시
    t2.doSomething() 부분에서 deprecated 된 method가 사용되고 있다고 warning을 보여준다.

 3) SuppressWarnings
    compiler에게 warning에 대해서 무시하도록 지시.

    Example:

    public class TestAnnotations {
        public static void main(String[] args) {
            new TestAnnotations().doSomeTestNow();
        }

      
@SuppressWarnings("deprecation")
       public void doSomeTestNow() {
           TestDeprecated t2 = new TestDeprecated();
           t2.doSomething();
       }
    }

   Deprecated annotation에서 사용했던 예제를 위와 같이 수정한 후에 compile 하면,
   전에 보였던 warning message가 보이지 않는 것을 확인할 수 있다.

o annotation type
  : annotation을 정의하는 type. custom annotation을 정의할 때 사용

  public @interface MyAnnotation {
      String toSomething();
  }

  - 3 annotation types

 1) Marker annotation
    annotation에 element 없이 이름 자체만으로 사용하는 경우

    Example:

       public @interface MyAnnotation {
    }

    Usage:

    @MyAnnotation
    public void myMethod() {
    }

 2) Single-Element
    annotation에 element를 하나 사용하는 경우
    data=value 또는 value 만 () 안에 사용

    Example:

    public @interface MyAnnotation {
      String doSomething();
      }

      Usage:

    @MyAnnotation("What to do")
    public void myMethod() {
        ....
    }

 3) Full-value or Multi-value
    annotation에 data를 여러개 사용하는 경우
    data=value 형식으로 ()안에 사용

    Example:

    public @interface MyAnnotation {
     int count;
     String date;
        String doSomething();
    }

    Usage:

    @MyAnnotation(doSomething="What to do", count=1, date="09-09-2005")
    public void myMethod() {
        ....
    }


  - Annotation type 정의 규칙
    Annotation 선언시 '@'다음에 interface keyword, '@' 다음에 annotation 이름의 형식으로 '@'로 시작해야 한다.
    method 선언에 대해서는 parameter, throws 절이 있어선 안된다.
    method의 반환 타입은 다음 중 하나여야 한다.
      primitives
      String
      Class
      enum
      array of the above types


- 4 meta-annotation

 1) Target

class의 어떤 element에 annotation이 적용 가능한지를 나타내는 annotation

   - Target annotation의 value
     @Target(Element.TYPE)                : class의 어떤 element 에나 적용 가능함
     @Target(Element.FIELD)               : field 또는 property에만 적용 가능함
     @Target(Element.METHOD)              : method level에서 적용 가능함
     @Target(Element.PARAMETER)           : method의 parameter에 적용 가능함
     @Target(Element.CONSTRUCTOR)         : 생성자에 적용 가능함
     @Target(Element.LOCAL_VARIABLE)      : local variables에 적용 가능함
     @Target(Element.ANNOTATION_TYPE)     : 선언된 유형 자체가 annotation 임을 가르킴

  
   Example:

   // annotation 정의
   @Target(ElementType.METHOD)
   public @interface Test_Target {
          public String doTestTarget();
   }

   // 위에서 정의한 annotation을 사용하는 class
   public class TetAnnotations {
       public static void main(String[] args) {
           new TestAnnotations().doTestTarget();
       }

      @Test_Target(doTestTarget="Hello World!")
       public void doTestTarget() {
           System.out.println("Testing Target Annotation");
       }
   }

   위 예제를 보면 Test_Target 이라는 custom annotation이 method level(ElementType.METHOD)로 정의되어 있기 때문에
      @Test_Target(doTestTarget="Hello World!") 를 아래와 같이 method가 아닌 field level로 이동하면 오류가 발생한다.

   public class TetAnnotations {
       @Test_Target(doTestTarget="Hello World!")
       private String str;

       public static void main(String[] args) {
           new TestAnnotations().doTestTarget();
       }

       public void doTestTarget() {
           System.out.println("Testing Target Annotation");
       }
   }


 2) Retention

annotation이 얼마 동안 유지되는지 여부를 나타내는 annotation

   - Retention annotation의 value
     RetentionPolicy.SOURCE    : source level에서 유지되며, compiler에서는 무시됨
     RetentionPolicy.CLASS     : compiler에 의해서 compile time에 유지됨
     RetentionPolicy.RUNTIME   : VM에 의해 유지되고, runtime에만 읽을 수 있음.

   Example:

   @Retention(RetentionPolicy.RUNTIME)
   public @interface TestRetention {
       String doSomeTestRetention();
   }


 3) Documented

   javadoc tool에 의해서 문서로 만들어져야 되는 부분을 나타냄


   Example:

   @Documented
   public @interface TestDocumented {
       String getDocumented();
   }

   public class TestAnnotations {
       public static void main(String[] args) {
        new TestAnnotations().doSomeTestRetention();
        new TestAnnotations().doSomeTestDocumented();
    }

    @TestRetention(doTestRetention='Hello retention test")
    public void doSomeTestRetention() {
        System.out.println("Test annotation 'Retention'");
    }

    @TestDocumented(doTestDocumented='Hello Documented")
    public void doSomeTestDocumented() {
        System.out.println("Test annotation 'Documented'");
    }
   }

 4) Inherited

 

  이 annotation이 붙으면 해당 class는 자동으로 상속된 class 임을 나타낸다.

   Example:

  @Inherited
   public @interface TestInherited {
       boolean isInherited() default true;
    String doSomething() default "Do What?";
   }

   @TestInherited
   public class TestAnnotations {
       // no implementation for TestInherited class
   }

   위 예를 보면 TestAnnotations class는 TestInherited의 method를 implements할 필요가 없다.
   @TestInherited annotation을 통해 자동으로 상속이 이루어지기 때문이다.

   old-style-java way

   public class TestAnnotations implements TestInherited {
       public boolean isInherited() {
        return false;
    }

    public String doSomething() {
        return "";
    }

    public boolean equals(Object obj) {
        return false;
    }

    public int hashCode() {
        return 0;
    }

    public String toString() {
        return "";
    }

    public Class annotationClass() {
        return null;
    }
   }

   위 예제를 보면 TestInherited의 method 뿐만 아니라, Object class의 method인 equals(), hashCode(), toString() method와
   java.lang.annotation.Annotation class의 method인 annotationClass() method까지 원하던 원하지 않던 override를 해야 한다.


 

'그거 > Java' 카테고리의 다른 글

[Java Quiz] 주민등록 번호와 바코드 유효성 체크  (0) 2007.09.12
Java Anti-Pattern  (2) 2007.08.20
JDK 5.0의 새로운 기능 Generics  (1) 2007.08.13
java Collection 들  (0) 2007.08.10
객체의 hashcode에 대한 고찰  (0) 2007.08.09
:
Posted by 뽀기