|
| 1 | +package 注解学习.all; |
| 2 | + |
| 3 | +import 注解学习.all.annotation.*; |
| 4 | + |
| 5 | +import java.lang.annotation.Annotation; |
| 6 | +import java.lang.annotation.ElementType; |
| 7 | +import java.lang.annotation.Target; |
| 8 | +import java.lang.reflect.Constructor; |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.lang.reflect.Method; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by fuzhipeng on 2016/10/28. |
| 14 | + */ |
| 15 | +public class TestMain { |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + try { |
| 19 | + annotationTypeAnnotation(); |
| 20 | + classAnnotation(); |
| 21 | + constructorAnnotation(); |
| 22 | + fieldAnnotation(); |
| 23 | + methodAnnotation(); |
| 24 | + parameterAnnotation(); |
| 25 | + |
| 26 | + //这个特殊 需要创建 package-info.java |
| 27 | + // 然后在内里添加个包 然后在包上添加注解 |
| 28 | + packageAnnotation(); |
| 29 | + //暂时没什么用 |
| 30 | +// 但是目前的javac不会在bytecode中的local variable中保存annotation信息, |
| 31 | +// 所以就无法在runtime时获取该annotaion。 |
| 32 | +// 也就是说ElementType.LOCAL_VARIABLE只能用在RetentionPolicy.SOURCE情况下。 |
| 33 | + localVariableAnnnotaiton(); |
| 34 | + |
| 35 | + } catch (Exception e) { |
| 36 | + e.printStackTrace(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static void packageAnnotation() { |
| 41 | + Package p = Package.getPackage("注解学习.all.annotation"); |
| 42 | + //判断构造方法中是否有指定注解类型的注解 |
| 43 | +// boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class); |
| 44 | + if(p!=null && p.isAnnotationPresent(PackageBinds.class)){ |
| 45 | + PackageBinds nav = p.getAnnotation(PackageBinds.class); |
| 46 | + if(nav !=null){ |
| 47 | + System.out.println("name:"+"======================="+nav.value()); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static void localVariableAnnnotaiton() { |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + private static void annotationTypeAnnotation() { |
| 57 | + // @Target这个就是 此类型 |
| 58 | + ClassBinds classBinds = Test.class.getAnnotation(ClassBinds.class); |
| 59 | + if (classBinds != null) { |
| 60 | + Annotation[] annotations = classBinds.annotationType().getAnnotations(); |
| 61 | + Target target = classBinds.annotationType().getAnnotation(Target.class); |
| 62 | + if (target != null) |
| 63 | + System.out.print("ANNOTATION_TYPE---->targets:" ); |
| 64 | + for (ElementType elementType : target.value()) { |
| 65 | + System.out.print("\t elementType:"+elementType); |
| 66 | + } |
| 67 | + System.out.println(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + private static void parameterAnnotation() throws NoSuchMethodException { |
| 74 | + Method method = Test.class.getMethod("use", int.class, String.class); |
| 75 | + //双维 因为 一个参数可以多个 注解; |
| 76 | + Annotation[][] parameterAnnotations = method.getParameterAnnotations(); |
| 77 | + if (parameterAnnotations == null || parameterAnnotations.length == 0) { |
| 78 | + return; |
| 79 | + } |
| 80 | + for (int i = 0; i < parameterAnnotations.length; i++) { |
| 81 | + System.out.printf("第%s个参数注解-->", i); |
| 82 | + for (Annotation annotation : parameterAnnotations[i]) { |
| 83 | + if (annotation instanceof ParameterBinds) |
| 84 | + System.out.println("value:" + ((ParameterBinds) annotation).value()); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static void methodAnnotation() { |
| 90 | + Method[] methods = Test.class.getMethods(); |
| 91 | + for (Method method : methods) { |
| 92 | + MethodBinds methodBinds = method.getAnnotation(MethodBinds.class); |
| 93 | + if (methodBinds != null) |
| 94 | + System.out.println("name:" + methodBinds.value()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void fieldAnnotation() throws NoSuchFieldException { |
| 99 | + Field field = Test.class.getField("tag"); |
| 100 | + FieldBinds fieldBinds = field.getAnnotation(FieldBinds.class); |
| 101 | + if (fieldBinds != null) |
| 102 | + System.out.println("name:" + fieldBinds.name()); |
| 103 | + } |
| 104 | + |
| 105 | + //构造器注解 需要得到构造器 ,构造器getAnnotation |
| 106 | + private static void constructorAnnotation() throws NoSuchMethodException { |
| 107 | + Test test = new Test(); |
| 108 | + Constructor<? extends Test> cons = test.getClass().getConstructor(); |
| 109 | + ConstructorBinds constructorBinds = cons.getAnnotation(ConstructorBinds.class); |
| 110 | + if (constructorBinds != null) |
| 111 | + System.out.println("name:" + constructorBinds.name()); |
| 112 | + } |
| 113 | + |
| 114 | + //class注解: class.getAnnotation |
| 115 | + private static void classAnnotation() { |
| 116 | + ClassBinds classBinds = Test.class.getAnnotation(ClassBinds.class); |
| 117 | + if (classBinds != null) |
| 118 | + System.out.println("name:" + classBinds.value() + "\t name:" + classBinds.name()); |
| 119 | + } |
| 120 | + |
| 121 | + @ClassBinds(value = {"类注解_Zone", "非主流_Zone"}, name = "kb_Zone") |
| 122 | + public static class Test { |
| 123 | + @FieldBinds(name = "field注解_Zone") |
| 124 | + public String tag = "test"; |
| 125 | + |
| 126 | + @ConstructorBinds(name = "构造注解_Zone") |
| 127 | + public Test() { |
| 128 | + } |
| 129 | + |
| 130 | + @MethodBinds("方法注解_Zone") |
| 131 | + public void use(@ParameterBinds("par1_Zone") int par1, @ParameterBinds("par2_Zone") String par2) { |
| 132 | + @LocalVariableBinds |
| 133 | + String infoInner = "sa"; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments