Android 自定义注解之运行时注解(RetentionPolicy.RUNTIME)
RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在 获取注解的常用方法: /** * 获取指定类型的注解 */ public <A extends Annotation> A getAnnotation(Class<A> annotationType); /** * 获取所有注解,如果有的话 */ public Annotation[] getAnnotations(); /** * 获取所有注解,忽略继承的注解 */ public Annotation[] getDeclaredAnnotations(); /** * 指定注解是否存在该元素上,如果有则返回true,否则false */ public boolean isAnnotationPresent(Class<? extends Annotation> annotationType); /** * 获取Method中参数的所有注解 */ public Annotation[][] getParameterAnnotations(); 1. 定义注解 /** * @author mazaiting * @date 2018/1/16 */ public class RuntimeAnnotation { /** * 适用类、接口(包括注解类型)或枚举 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ClassInfo { String value(); } /** * 适用变量,也包括枚举常量 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface FieldInfo { int[] value(); } /** * 适用方法 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodInfo { String name() default "long"; String data(); int age() default 24; } } 2. 使用注解 /** * @author mazaiting * @date 2018/1/16 */ @RuntimeAnnotation.ClassInfo("TestRuntimeAnnotation class") public class TestRuntimeAnnotation { @RuntimeAnnotation.FieldInfo(value = {1, 2}) public String fieldnfo = "FieldInfo"; @RuntimeAnnotation.FieldInfo(value = {10086}) public int i = 100; @RuntimeAnnotation.MethodInfo(name = "mazaiting", data = "big") public static String getMethodInfo() { return TestRuntimeAnnotation.class.getSimpleName(); } } 3. 解析注解 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) findViewById(R.id.textView); /**运行时注解*/ textView.setText(parse()); } /** * 解析运行时注解 */ private String parse() { // 创建一个线程安全的字符串拼接对象 StringBuffer sb = new StringBuffer(); // 获取类字节码 Class cls = TestRuntimeAnnotation.class; // 获取构造方法 Constructor[] constructors = cls.getConstructors(); // 获取指定类型的注解 // 获取ClassInfo注解 sb.append("Class注解:").append("\n"); RuntimeAnnotation.ClassInfo classInfo = (RuntimeAnnotation.ClassInfo) cls.getAnnotation(RuntimeAnnotation.ClassInfo.class); if (null != classInfo) { // 拼接访问修饰符和类名 sb.append(Modifier.toString(cls.getModifiers())).append(" ") .append(cls.getSimpleName()).append("\n"); sb.append("注解值:").append(classInfo.value()).append("\n\n"); } // 获取FieldInfo注解 Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { RuntimeAnnotation.FieldInfo fieldInfo = field.getAnnotation(RuntimeAnnotation.FieldInfo.class); if (null != fieldInfo) { // 拼接访问修饰符,类型名,字段名 sb.append(Modifier.toString(field.getModifiers())).append(" ") .append(field.getType().getSimpleName()).append(" ") .append(field.getName()).append("\n"); sb.append("注解值:").append(Arrays.toString(fieldInfo.value())).append("\n\n"); } } // 获取MethodInfo注解 sb.append("Method注解:").append("\n"); Method[] methods = cls.getDeclaredMethods(); for (Method method: methods) { RuntimeAnnotation.MethodInfo methodInfo = method.getAnnotation(RuntimeAnnotation.MethodInfo.class); if (null != methodInfo) { // 拼接访问修饰符、返回值类型、方法名 sb.append(Modifier.toString(method.getModifiers())).append(" ") .append(method.getReturnType().getSimpleName()).append(" ") .append(method.getName()).append("\n"); // 注解值 sb.append("注解值:").append("\n"); sb.append("name: ").append(methodInfo.name()).append("\n"); sb.append("data: ").append(methodInfo.data()).append("\n"); sb.append("age: ").append(methodInfo.age()).append("\n"); } } return sb.toString(); } } 效果图: 效果图.png