package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) //该注解被标记在属性上 public @interface TestAnnField { public String value() default ""; }
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) //该注解被标记在方法上 public @interface TestAnnoMethod { String types() default "";//设置默认值后,在使用注解时可以选择性的不写 types属性,如@TestAnnoMethod(username = "cyss") , 只写一个属性 String username() ; }
package com.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) // 用于描述类型, 注释在类上面。 @Target(ElementType.FIELD) FIELD:用于描述域 ,注释在属性上面 /* * 1.CONSTRUCTOR:用于描述构造器 2.FIELD:用于描述域 3.LOCAL_VARIABLE:用于描述局部变量 * 4.METHOD:用于描述方法 5.PACKAGE:用于描述包 6.PARAMETER:用于描述参数 * 7.TYPE:用于描述类、接口(包括注解类型) 或enum声明 */ public @interface TestAnnoType { Class<?> type(); String method();//反回类型只能使用基本数据类型和数组 Class<?>[] args(); String value() default ""; }
package com.annotation; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; @TestAnnoType(args = { Test.class,Test2.class }, method = "getUserAll", type = Test.class) public class Test { @TestAnnField(value = "苹果") private String name; @TestAnnField(value = "sfd") private String age; @TestAnnField(value = "男") private String sex; private String adr; @TestAnnoMethod(username = "cyss") public void methodAnno(){ System.out.println("Method||01"); } @TestAnnoMethod(username = "xxxxxx") public void methodAnno2(){ System.out.println("Method||02"); } public static void main(String[] args){ Test.aminType(); try { Class<Test> c = Test.class; //获取类中指定的属性 Field field = c.getDeclaredField("name"); //判断是否包含此注解 if(field.isAnnotationPresent(TestAnnField.class)){ System.out.println("true"); } Test s = (Test)Class.forName("com.annotation.Test").newInstance(); System.out.println("属性:"+s.name); } catch (Exception e) { System.out.println("NO"); e.printStackTrace(); } } /** * 获取类上注解的值 */ public static void aminType(){ Test test = new Test(); try { Class<Test> c = Test.class; Class classs = Class.forName("com.annotation.Test"); TestAnnoType annoType = (TestAnnoType) classs.getAnnotation(TestAnnoType.class); String methodStr = annoType.method(); Class classType = annoType.type(); Class[] classTypes = annoType.args(); System.out.println("&&"+methodStr); System.out.println("&&"+classType); System.out.println("&&"+classTypes[0]); System.out.println("&&"+classTypes[1]); } catch (Exception e) { e.printStackTrace(); } } /** * 获取方法上注解的值 */ public static void mainMethod() { Test test = new Test(); try { //获取指定方法的注解值 Class<Test> c = Test.class; Method method = c.getMethod("methodAnno", new Class[] {}); TestAnnoMethod anoMethod = method.getAnnotation(TestAnnoMethod.class); String name = anoMethod.username(); System.out.println("methodAnno方法注解值:"+name); //所有方法的注解值 for (Method method2 : c.getDeclaredMethods()) { TestAnnoMethod anoMethod2 = method2.getAnnotation(TestAnnoMethod.class); if (anoMethod2 != null) { System.out.println("方法名:"+method2.getName()+"注解值:"+anoMethod2.username()); } } // 获取方法上的所有注解 Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println("方法上的所有注解"+annotation); } } catch (Exception e) { e.printStackTrace(); } test.methodAnno(); } /** * 设置属性的值 */ public static void mainFields() { Test test = new Test(); try { Field[] methods = Test.class.getDeclaredFields(); for (Field method : methods) { boolean hasAnnotation = method .isAnnotationPresent(TestAnnField.class); if (hasAnnotation) { TestAnnField value = method .getAnnotation(TestAnnField.class); method.set(test, value.value()); } } } catch (Exception e) { e.printStackTrace(); } System.out.println("||"+test.name); System.out.println("||"+test.age); System.out.println("||"+test.sex); System.out.println("||"+test.adr); } }