运行时的Android注释
我想用自定义注释对类列表进行注释,这是我在运行时需要的 我的注解:运行时的Android注释,android,annotations,Android,Annotations,我想用自定义注释对类列表进行注释,这是我在运行时需要的 我的注解: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface CustomAnnotation { String value() default "defaultValue"; } 我的自定义类是: @CustomAnnotation public class MyClass { } 现在,我想从一个类名中读取注释,我把
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {
String value() default "defaultValue";
}
我的自定义类是:
@CustomAnnotation
public class MyClass {
}
现在,我想从一个类名中读取注释,我把它作为字符串。代码没有错误处理(便于处理):
为什么clazz.getAnnotation()为空?有人有想法吗?刚刚测试了你的代码-它对我有用。 只需要调用getAnnotations() 这个问题肯定超出了你发布的代码范围
String className = "com.example.MyClass";
Class clazz = Class.forName(className);
Annotation[] annotation = clazz.getAnnotation(); // is empty