自定义注解(仅作为标识,不处理逻辑,逻辑有反射完成)

本文介绍了如何在Java中创建自定义注解`MyAnnotation`,并使用反射来检查类、方法是否携带该注解以及执行注解标记的方法。在`InitDemo`测试类中,展示了如何通过反射获取注解信息,并根据注解处理逻辑执行方法和设置对象属性。
摘要由CSDN通过智能技术生成

一 : 新建MyAnnotation 注解

/**
 * 自定义注解:仅仅是标注,不具有运行逻辑
 * @Target() 指定注解针对的地方
 * @Retention()  指定注解保留区域
 * @MyAnnotation() 自定义注解
 */
//@Target(ElementType.TYPE) // 类、接口
//@Target(ElementType.FIELD) // 成员变量
@Target(ElementType.METHOD) // 成员方法
//@Target({ElementType.METHOD,ElementType.TYPE,ElementType.FIELD}) // 成员方法,类
//@Target(ElementType.PARAMETER) // 方法参数
//@Target(ElementType.CONSTRUCTOR) // 构造器
//@Target(ElementType.PACKAGE) // 包
//@Target(ElementType.ANNOTATION_TYPE) // 注解

//@Retention(RetentionPolicy.SOURCE) // 源代码级别,由编译器处理,处理之后不再保留
//@Retention(RetentionPolicy.CLASS) // 注解信息保留到类对应的class文件中
@Retention(RetentionPolicy.RUNTIME) // 由jvm读取,运行时使用
public @interface MyAnnotation {
   //  String value() default ""; // 如果需要接收值,不需要接受值可以不写这个
}

二 : 新建 测试类 InitDemo ,包含 注解标记的处理逻辑

public class InitDemo {
    @MyAnnotation
    public void init() {
        System.out.println("init方法执行了......");
    }

    public static void main(String[] args) throws Exception {
        /*
          方法注解
         */

        // 通过反射 获取 Class 类
        Class aClass = Class.forName("com.example.Dog");
        // 获取 Class 所有方法
        Method[] methods = aClass.getMethods();
        // 遍历 methods
        if (methods != null) {
            for (Method method : methods) {
                // 判断方法 是否有某个注解
                boolean isMyAnnotation = method.isAnnotationPresent(MyAnnotation.class);
                System.out.println(method.getName() + "方法时候有MyAnnotation注解:" + isMyAnnotation);
                // 拥有注解
                if (isMyAnnotation) {
                    // 调用方法
                    method.invoke(aClass.getConstructor(null).newInstance(null), null);
                }
            }
        }

        /*
            类注解 , 属性注解
         */

        // 通过反射 获取 Class 类
        Class<Dog> dogClass = Dog.class;
        // 判断对象是否有某个注解
        MyAnnotation annotation = dogClass.getAnnotation(MyAnnotation.class);
        if (annotation != null) {
            // 创建对象
            Constructor<Dog> constructor = dogClass.getConstructor(null);// 拿到构造器
            Dog dog = constructor.newInstance(null); // 获取对象
            // 开始赋值
            //dogClass.getField()//  获取公用属性
            Field[] declaredFields = dogClass.getDeclaredFields();// 获取全部私有属性
            for (Field declaredField : declaredFields) {
                MyAnnotation annotation1 = declaredField.getAnnotation(MyAnnotation.class);
                if (annotation1 != null) {
                    declaredField.setAccessible(true); // 暴力反射(否则下面会报异常,因为是私有的)
                    String value = annotation1.value();
                    // 判断属性的类型
                    switch (declaredField.getType().getName()) {
                        case "java.lang.Integer": {
                            Integer val = Integer.parseInt(value);
                            declaredField.set(dog, val); // 给目标对象赋值
                            break;
                        }
                        default:
                            declaredField.set(dog, value); // 给目标对象赋值
                    }

                }
            }
        } else {
            System.out.println(dogClass + "没有添加了MyAnnotation注解");
        }
    }
}

三 : 执行结果

main方法时候有MyAnnotation注解:false
init方法时候有MyAnnotation注解:true
init方法执行了......
wait方法时候有MyAnnotation注解:false
wait方法时候有MyAnnotation注解:false
wait方法时候有MyAnnotation注解:false
equals方法时候有MyAnnotation注解:false
toString方法时候有MyAnnotation注解:false
hashCode方法时候有MyAnnotation注解:false
getClass方法时候有MyAnnotation注解:false
notify方法时候有MyAnnotation注解:false
notifyAll方法时候有MyAnnotation注解:false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值