사용자 도구

사이트 도구


java:annotation

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
java:annotation [2012/01/20 16:26]
kwon37xi 새로 만듦
java:annotation [2023/12/25 15:15] (현재)
kwon37xi
줄 1: 줄 1:
 ====== Java Annotation ====== ====== Java Annotation ======
 +  * [[http://en.wikibooks.org/wiki/Java_Programming/Annotations|Java Annotations]]
 +  * [[http://www.javacodegeeks.com/2014/11/java-annotations-tutorial.html|Java Annotation Tutorial]]
 +  * [[https://dzone.com/articles/all-java-built-in-annotation-examples|All Java Built-In Annotation Examples]]
 +  * [[java:jandex|jandex]]
 +===== Meta Annotation =====
 +  * [[http://en.wikibooks.org/wiki/Java_Programming/Annotations/Meta-Annotations|Java Meta Annotations]]
 +
 +==== @Inherited ====
 +<code java>
 +@Inherited
 +@interface ForEveryone { }
 + 
 +@interface JustForMe { }
 + 
 +@ForEveryone
 +@JustForMe
 +class Superclass { }
 + 
 +class Subclass extends Superclass { }
 +</code>
 +위와 같은 코드에서 ''@Inherited''가 적용된 ''@ForEveryone'' 어노테이션을 Superclass에만 지정했지만 자동으로 그 서브클래스인 Subclass에도 적용된 것으로 간주된다. 그에 반해 ''@JustForMe''는 Subclass로 상속되지 않는다.
  
 ===== Annotation 객체 생성하기 instanciation of annotation ===== ===== Annotation 객체 생성하기 instanciation of annotation =====
 +==== 어노테이션은 인터페이스다! ====
 +  * 어노테이션은 기본적으로 인터페이스이다.
 +  * 인터페이스는 implements 로 구현 클래스를 만든뒤에 객체를 생성해주면 된다. 그걸로 끝!
 +  * <code java>
 +// 아래와 같은 어노테이션이 있을 때!
 +@Target(ElementType.PARAMETER)
 +@Retention(RetentionPolicy.RUNTIME)
 +@Documented
 +public @interface LoginUser {
 + boolean required() default true;
 + SomeValue [] someValues() default {};
 +}
 +
 +// 아래와 같이 클래스로 구현한 뒤에 해당 클래스의 인스턴스를 생성하면 된다.
 +public static class FakeLoginUser implements LoginUser {
 +
 + private boolean requiredValue = true;
 +
 + private SomeValue[] someValueValue = {};
 +
 + public FakeLoginUser(boolean requiredValue, SomeValue[] someValueValue) {
 + this.requiredValue = requiredValue;
 + this.someValueValue = someValueValue;
 + }
 +
 + @Override
 + public Class<? extends Annotation> annotationType() {
 + return LoginUser.class;
 + }
 +
 + @Override
 + public boolean required() {
 + return requiredValue;
 + }
 +
 + @Override
 + public SomeValue[] someValue() {
 + return someValueValue;
 + }
 +}
 +</code>
 +
 +==== 어노테이션을 직접 지정한 뒤에 Reflection으로 생성하기 - 복잡하므로 하지 말 것 ====
   * [[http://stackoverflow.com/questions/266903/create-annotation-instance-with-defaults-in-java|Create Annotation instance with defaults, in Java]]   * [[http://stackoverflow.com/questions/266903/create-annotation-instance-with-defaults-in-java|Create Annotation instance with defaults, in Java]]
   * [[http://docs.jboss.org/cdi/api/1.0/javax/enterprise/util/AnnotationLiteral.html|AnnotationLiteral (CDI APIs 1.0 API)]]   * [[http://docs.jboss.org/cdi/api/1.0/javax/enterprise/util/AnnotationLiteral.html|AnnotationLiteral (CDI APIs 1.0 API)]]
-  * 내가 만들어본 어노테이션 객체 생성하기 +  * 내가 만들어본 어노테이션 객체 생성하기 MyAnn이 required 항목을 가진 어노테이션일 경우<code java> 
-<code java> +private MyAnn requiredTrueMyAnn
-private LoginUser requiredTrueLoginUser+private MyAnn requiredFalseMyAnn;
-private LoginUser requiredFalseLoginUser;+
  
-public void loginUserAnnotationSupports(@LoginUser Object requiredTrueLoginUser, +public void myAnnSupports(@MyAnn Object requiredTrueObject, @MyAnn(required = false) Object requiredFalseObject) throws SecurityException, NoSuchMethodException {
-        @LoginUser(required = false) Object requiredFalseLoginUser) throws SecurityException, NoSuchMethodException {+
  
     Annotation[][] parameterAnnotations = this.getClass()     Annotation[][] parameterAnnotations = this.getClass()
-            .getMethod("loginUserAnnotationSupports", Object.class, Object.class).getParameterAnnotations();+            .getMethod("myAnnSupports", Object.class, Object.class).getParameterAnnotations();
  
-    requiredTrueLoginUser = parameterAnnotations[0][0]; +    requiredTrueMyAnn= parameterAnnotations[0][0]; // requiredTrueMyAnn.required() == true 
-    requiredFalseLoginUser = parameterAnnotations[1][0];+    requiredFalseMyAnn= parameterAnnotations[1][0]; // requiredFalseMyAnn.required() == false
 } }
 </code> </code>
java/annotation.1327044413.txt.gz · 마지막으로 수정됨: 2012/01/20 16:26 저자 kwon37xi