扫描指定包下的所有类,并通过反射获取类上的所有注解

扫描指定包下的所有类,并通过反射获取类上的所有注解,第1张

扫描指定包下的所有类,并通过反射获取类上的所有注解
package com.doria.electronicContract;

import com.sun.javaws.Main;

import java.io.File;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

public class ScanClass {

    public static void main(String[] args) throws ClassNotFoundException {
        // 指定包名
        String basePack = "com.doria.electronicContract.controller";
        // 包名转换为路径,先得到项目的classpath
        String classPath = Main.class.getResource("/").getPath();
        // 把我们的包名转换为路径名
        basePack = basePack.replace(".", File.separator);
        // 把classPath和basePack合并
        String searchPath = classPath + basePack;
        // 这个时候我们已经得到了指定包下所有的类的绝对路径了。我们现在利用这些绝对路径和java的反射机制得到他们的类对象
        List classPathList = doPath(new File(searchPath), new ArrayList<>());
        for (String zClass : classPathList) {
            // 把D:workcodeax.class 这样的绝对路径转换为全类名com.doria.xxxx
            String replacePath = zClass.replace(classPath.replace("/", "\").replaceFirst("\\", ""), "").replace("\", ".").replace(".class", "");
            Class aClass = Class.forName(replacePath);
            Annotation[] annotations = aClass.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
                System.out.println(annotation.annotationType());
                System.out.println(annotation.getClass());
            }
        }
    }

    
    public static List doPath(File file, List classList) {
        // 如果是文件夹就递归调用
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) return classList;
            for (File f : files) {
                classList=doPath(f, classList);
            }
        } else {
            // 不是文件夹就判断是不是class文件,如果是就把他放进集合
            if (file.getName().endsWith(".class")) {
                classList.add(file.getPath());
            }
        }
        return classList;
    }
}

后续追加;通过注解,将所有被注解标注的类注入倒map容器中并通过bean类型或者名称取出

public class ScanClass {


    public static TreeMap beanMap=new TreeMap<>();

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        // 指定包名
        String basePack = "com.doria.electronicContract.controller";
        // 包名转换为路径,先得到项目的classpath
        String classPath = Main.class.getResource("/").getPath();
        // 把我们的包名转换为路径名
        basePack = basePack.replace(".", File.separator);
        // 把classPath和basePack合并
        String searchPath = classPath + basePack;
        // 这个时候我们已经得到了指定包下所有的类的绝对路径了。我们现在利用这些绝对路径和java的反射机制得到他们的类对象
        List classPathList = doPath(new File(searchPath), new ArrayList<>());
        for (String zClass : classPathList) {
            // 把D:workcodeax.class 这样的绝对路径转换为全类名com.doria.xxxx
            String replacePath = zClass.replace(classPath.replace("/", "\").replaceFirst("\\", ""), "").replace("\", ".").replace(".class", "");
            Class aClass = Class.forName(replacePath);
            // 获取ScanMe注解如果不为空证明找到了,这个类被这个注解注释了,那就吧class实例化存入treeMap
            ScanMe scanMe = aClass.getAnnotation(ScanMe.class);
            if (scanMe!=null) {
                String typeName = aClass.getTypeName();
                Object o = aClass.newInstance();
                beanMap.put(typeName,o);
            }
            // 获得所有注解
            Annotation[] annotations = aClass.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("类注解:"+annotation.annotationType());
            }

             // 获得所有方法(含父类)
            Method[] methods = aClass.getMethods();
            // 获得所有方法
            Method[] declaredMethods = aClass.getDeclaredMethods();
            // 遍历得到单个方法
            for (Method method : methods) {
                // 获取方法上的注解
                Annotation[] methodAnnotations = method.getDeclaredAnnotations();
                for (Annotation methodAnnotation : methodAnnotations) {
                    System.out.println("方法注解:"+methodAnnotation.annotationType());
                }
                System.out.println("############遍历完一个方法的方法注解##########");
                // 获得方法的全部参数注解数组[一个参数有一组]
                Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                for (Annotation[] parameterAnnotation : parameterAnnotations) {
                    // 得到注解数组,遍历注解数组
                    for (Annotation annotation : parameterAnnotation) {
                        System.out.println("方法参数注解: "+annotation.annotationType());
                    }
                    System.out.println("------------遍历完一组方法参数注解----------");
                }
                System.out.println("================遍历完一个方法的所有参数注解================");
            }
        }
        // 遍历集合看看存进去没
        beanMap.forEach((b,c)->{
            System.out.println(b);
            System.out.println(c);
        });

        // 取出来试试
        TestController testController = (TestController)beanMap.get("com.doria.electronicContract.controller.TestController");
        testController.Test();
    }

    
    public static List doPath(File file, List classList) {
        // 如果是文件夹就递归调用
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) return classList;
            for (File f : files) {
                classList=doPath(f, classList);
            }
        } else {
            // 不是文件夹就判断是不是class文件,如果是就把他放进集合
            if (file.getName().endsWith(".class")) {
                classList.add(file.getPath());
            }
        }
        return classList;
    }
}

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

原文地址: https://outofmemory.cn/zaji/5672958.html

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

发表评论

登录后才能评论

评论列表(0条)

保存