자바 5.0의 Enum, Annotation Test Code이다.

실행결과는 따로 적지 않았다. 대충 만든거라, 약간 어설플 수도 있다.

 


import java.lang.annotation.*;
import java.lang.reflect.*;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @interface debug {
    boolean devbuild() default false;
    int counter();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Java {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface  ID {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Desc {
    String value();
}

@ID("1")
public class MetaTest {

    @Java("123712831783")
    public String a;

    /*
    public enum Type {
        E1("aa"), E2("bb");
        private final String value;
        Type(String value) { this.value = value; }
        public String value() { return value; }
    }
    public Type xType = Type.E1;
    */
    public enum DescEnum {  X1,  X2 };
    public DescEnum de = DescEnum.X1;

    public void de(DescEnum m) {
        if (DescEnum.X1.equals(m)) {
            System.out.println("changed X1");
        } else {
            System.out.println("changed X2");
        }
    }
    /*final boolean production = true;
    @debug(devbuild = production, counter=1) public void testMethod() {}

    public MetaTest() {
        System.out.println(this);
    }*/

    public static void main(String[] args) {
        MetaTest mt = new MetaTest();
        ///////////////////////////////////////////////////////////////
        // de 필드에 enum element의 특정값 넣기.?
        ///////////////////////////////////////////////////////////////

        try {
            Field field = mt.getClass().getField("de");
            System.out.println(field.getType());
            Class cls = field.getType();
            if (cls.isEnum()) {
                System.out.println("c");
            }
            Method method = cls.getMethod("valueOf", String.class);
            Object o = method.invoke(cls, new Object[] { "X2" });
            System.out.println("name : f.getName() : " + o);
            field.set(mt, o);
            System.out.println("result : " + mt.de);

            //------------------------------------------------------
            Method method2 = mt.getClass().getMethod("de", cls);
            Class cls2 = method2.getParameterTypes()[0];
            System.out.println("oj : " + cls2);
            if (cls2.isEnum()) {
                System.out.println("xx");
                Method mtd = cls2.getMethod("valueOf", String.class);
                Object o2 = mtd.invoke(cls2, new Object[] { "X1" });
                System.out.println("o : " + o2);
                method2.invoke(mt, o2);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        ///////////////////////////////////////////////////////////////
        // 
        ///////////////////////////////////////////////////////////////

        /*
        ///////////////////////////////////////////////////////////////
        //  de 필드를 통해서 해당 값이 어떤것인지 알아오기.
        ///////////////////////////////////////////////////////////////

        try {
            Field field = mt.getClass().getField("de");
            System.out.println(field.getType());

            Class cls = field.getType();
            Field f = cls.getField("X1");
            Annotation a = f.getAnnotation(Desc.class);
            Method[] methods = a.getClass().getMethods();

            System.out.println(a);

            for (Method method : methods) {
                if (method.getName().equals("value")) {
                    System.out.println("**************find************");
                    try {
                        Object val = method.invoke(a, new Object[0]);
                        System.out.println("value: " + val);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }*/
        ///////////////////////////////////////////////////////////////
        //
        ///////////////////////////////////////////////////////////////

        /*try {
            Annotation[] a =
                mt.getClass().getMethod("testMethod").getAnnotations();
            for (int i = 0 ; i < a.length ; i++) {
                System.out.println("a[" + i + "]" + a[i]);
            }

        } catch (NoSuchMethodException e) {
            System.out.println(e);
        }
        String id = "1";
        System.out.println(id == "" ? "" : "2");

        Annotation ab = mt.getClass().getAnnotation(ID.class);
        System.out.println(ab);
        System.out.println("11");
        Field[] f = mt.getClass().getFields();
        System.out.println("22");
        Annotation a = f[0].getAnnotation(Java.class);
        Method[] methods = a.getClass().getMethods();
        System.out.println("num : " + methods.length);

        for (Method i : methods) {Type
            System.out.println("method = " + i.getName());
            //System.out.println("methods : " + i);

            if (i.getName().equals("value")) {
                System.out.println("**************find************");
                try {
                    Object val = i.invoke(a, new Object[0]);
                    System.out.println("value: " + val);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        System.out.println(a.toString()) ;
        System.out.println(a.value());

        Package p = mt.getClass().getPackage();
        System.out.println("aaa : " + p);
        try {
            Field field = mt.getClass().getField("a");
            System.out.println(field.getType());
            if (field.getType() == String.class) {
                System.out.println("equals to String");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } */

 

    }
}

 

'java core' 카테고리의 다른 글

java 5.0 enum.  (0) 2005.05.12
jdk 5.0 static import test code  (0) 2005.05.11
jdk 5.0 Annotation, Enum Test code  (0) 2005.05.11
jdk 5.0 Annotation 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
Posted by 김용환 '김용환'

댓글을 달아 주세요