反射相关笔记

反射相关笔记,第1张

反射相关笔记 例子一

    org.reflections
    reflections
    0.10.2

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>  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.*;


@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;


public class TestMain {

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

//        Class cls = Class.forName("com.shiro.test.inter.Worker");
//        Method[] method = cls.getMethods();
//        
//        boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
//        
//        if (flag) {
//            TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
//            System.out.println("@TypeAnnotation值:" + typeAnno.value());
//        }
//
//        
//        List list = new ArrayList();
//        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 fieldList = new ArrayList();
//        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());


    }



}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5672516.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存