package java.lang.annotation;
public interface Annotation {
boolean equals(Object obj);
int hashCode();
String toString();
//返回该注解的Class,元素使用了多个注解的时候,可以进行输出判断
Class<? extends Annotation> annotationType();
}
package java.lang.reflect;
import java.lang.annotation.Annotation;
public interface AnnotatedElement {
//判断此元素上是否存在指定类型的注解,如果存在则返回true,否则返回false
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return getAnnotation(annotationClass) != null;
}
//返回此元素上存在的指定类型的注解,如果该类型的注解不存在,则返回null
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
//返回此元素上存在的所有注解。
Annotation[] getAnnotations();
//返回此元素上存在的所有注解。不包括继承
Annotation[] getDeclaredAnnotations();
default <T extends Annotation> Annotation getDeclaredAnnotation(Class<T> annotationClass) {
return AnnotatedElements.getDeclaredAnnotation(this, annotationClass);
}
default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
return AnnotatedElements.getDeclaredAnnotationsByType(this, annotationClass);
}
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
return AnnotatedElements.getAnnotationsByType(this, annotationClass);
}
}
@Target(ElementType.METHOD) //修饰方法
@Retention(RetentionPolicy.RUNTIME) //运行时可以获取
public @interface MyTag {
}
public class MyTagParser {
public static void process(Object clazz) {
try {
for (Method method : clazz.getClass().getMethods()) {
if (method.isAnnotationPresent(MyTag.class)) {
//获取到了,输出
Log.e("tag","被mytag注解修饰的方法:" + method.getName());
} else {
Log.e("tag","没有被mytag注解修饰的方法:" + method.getName());
}
}
} catch (Exception en) {
en.printStackTrace();
}
}
}
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用解析器
MyTagParser.process(this);
}
@MyTag
public void testYes(){
}
public void testNo(){
}
}
...... 02-18 15:23:41.622 12446-12446/? E/tag: 没有被mytag注解修饰的方法:stopServiceAsUser 02-18 15:23:41.622 12446-12446/? E/tag: 没有被mytag注解修饰的方法:takeKeyEvents 02-18 15:23:41.622 12446-12446/? E/tag: 没有被mytag注解修饰的方法:testNo 02-18 15:23:41.622 12446-12446/? E/tag: 被mytag注解修饰的方法:testYes 02-18 15:23:41.632 12446-12446/? E/tag: 没有被mytag注解修饰的方法:toString 02-18 15:23:41.632 12446-12446/? E/tag: 没有被mytag注解修饰的方法:triggerSearch 02-18 15:23:41.632 12446-12446/? E/tag: 没有被mytag注解修饰的方法:unbindService .......
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTag {
String name() default "天平";
int age();
}
public class MyTagParser {
public static void parser(Object o){
Class clazz = o.getClass();
for(Method method:clazz.getMethods()){
if(method.isAnnotationPresent(MyTag.class)){
MyTag myTag = method.getAnnotation(MyTag.class);
Log.e("tag","方法名:"+method.getName()+"的注解值为"+myTag.name()+","+myTag.age());
}
}
}
}
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTagParser.parser(this);
}
@MyTag(age = 20)
public void testYes(){
}
}
02-18 16:11:53.493 25662-25662/? E/tag: 方法名:testYes的注解值为天平,20
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有