反射相关笔记

例子一

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.10.2</version>
</dependency>
package com.chinaunicom.cnaps.test;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface pay {

    int garid();
}

package com.chinaunicom.cnaps.test;

import org.reflections.Reflections;

import java.util.Set;

@pay(garid = -1)
public class TestMain {

    static {
        System.out.println("什么时候执行");
        Reflections reflections = new Reflections("com.chinaunicom.cnaps.test");
        Set<Class<?>>  classSet = reflections.getTypesAnnotatedWith(pay.class);
        for (Class clazzSet: classSet){
            pay pay = (pay) clazzSet.getAnnotation(pay.class);
            System.out.println(pay.garid());
            //类名
            System.out.println(clazzSet.getCanonicalName());

            try {
                Class cls = Class.forName(clazzSet.getCanonicalName());
                TestMain testMain = (TestMain) cls.newInstance();
                testMain.test();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
    }


    public static void main(String[] args) {
        System.out.println("1`1111");
    }


    public void test(){
        System.out.println("到此一游");
    }

}

例子二

package com.shiro.test.inter;

import java.lang.annotation.*;

/**
 * @Function:
 * @author: 𝕎𝔸ℕ𝔾𝕄𝕌𝕋𝕀𝔸ℕ
 * @create: 2020/9/16 10:06
 **/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {

    String value() default "Is-TypeAnnotation";

    String name() default "我不是田哥";

}
package com.shiro.test.inter;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Function:
 * @author: 𝕎𝔸ℕ𝔾𝕄𝕌𝕋𝕀𝔸ℕ
 * @create: 2020/9/16 10:07
 **/
public class TestMain {

    public static void main(String[] args) throws Exception {

//        Class cls = Class.forName("com.shiro.test.inter.Worker");
//        Method[] method = cls.getMethods();
//        /**判断Worker类上是否有TypeAnnotation注解*/
//        boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
//        /**获取Worker类上是TypeAnnotation注解值*/
//        if (flag) {
//            TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
//            System.out.println("@TypeAnnotation值:" + typeAnno.value());
//        }
//
//        /**方法上注解*/
//        List<Method> list = new ArrayList<Method>();
//        for (int i = 0; i < method.length; i++) {
//            list.add(method[i]);
//        }
//
//        for (Method m : list) {
//            MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
//            if (methodAnno == null)
//                continue;
//            System.out.println( "方法名称:" + m.getName());
//            System.out.println("方法上注解name = " + methodAnno.name());
//            System.out.println("方法上注解url = " + methodAnno.url());
//        }
//        /**属性上注解*/
//        List<Field> fieldList = new ArrayList<Field>();
//        for (Field f : cls.getDeclaredFields()) {// 访问所有字段
//            FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
//            System.out.println( "属性名称:" + f.getName());
//            System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
//        }


        TypeAnnotation typeAnno = Worker.class.getAnnotation(TypeAnnotation.class);
        System.out.println("@TypeAnnotation值:" + typeAnno.name());


        FiledAnnotation filedAno =  Worker.class.getDeclaredField("myfield").getAnnotation(FiledAnnotation.class);
        System.out.println( "属性名称:" + Worker.class.getDeclaredField("myfield").getName());
        System.out.println("属性注解值FiledAnnotation = " + filedAno.value());

        MethodAnnotation methodAnno = Worker.class.getMethod("getDefineInfo").getAnnotation(MethodAnnotation.class);;
        System.out.println( "方法名称:getDefineInfo");
        System.out.println("方法上注解name = " + methodAnno.name());
        System.out.println("方法上注解url = " + methodAnno.url());


    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值