자바 5.0의 Annotation에 대한 샘플 예제를 만들어 보았다.
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.*;
@Retention(RetentionPolicy.RUNTIME)
@interface Title {
String value();
}
public class AnnotationTest {
public enum DescEnum { @Title("1") X1, X2 , X3 };
public static EnumSet<DescEnum> de = EnumSet.of(DescEnum.X1, DescEnum.X2);
public static void main(String[] args) {
AnnotationTest mt = new AnnotationTest();
///////////////////////////////////////////////////////////////
// EnumSet
///////////////////////////////////////////////////////////////
try {
Field field = mt.getClass().getField("de");
System.out.println(field.getGenericType()) ;
ParameterizedType tv = (ParameterizedType) field.getGenericType();
System.out.println(tv);
System.out.println("rawtype : " + tv.getRawType());
System.out.println("getOwnerType : " + tv.getOwnerType());
System.out.println("getReal : " + tv.getActualTypeArguments());
for (Type t : tv.getActualTypeArguments()) {
System.out.println("t : " + t);
//System.out.println("class name: " + c.getName());
Field[] enumFields = ((Class)t).getFields();
for (Field f : enumFields) {
System.out.println("enum element name : " + f.getName());
}
}
Iterator iter = de.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
실행결과
$ java AnnotationTest
java.util.EnumSet<AnnotationTest$DescEnum>
java.util.EnumSet<AnnotationTest$DescEnum>
rawtype : class java.util.EnumSet
getOwnerType : null
getReal : [Ljava.lang.reflect.Type;@66848c
t : class AnnotationTest$DescEnum
enum element name : X1
enum element name : X2
enum element name : X3
X1
X2
'java core' 카테고리의 다른 글
jdk 5.0 static import test code (0) | 2005.05.11 |
---|---|
jdk 5.0 Annotation, Enum Test code (0) | 2005.05.11 |
[펌] RMI(Remote Method Invocation) (0) | 2005.05.11 |
jdk 5.0) In runtime, java compile. (0) | 2005.04.25 |
jar (0) | 2005.04.25 |