栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

反射相关笔记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

反射相关笔记

例子一

    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());


    }



}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号