java随笔-扫描使用指定注解的类与方法

前几天项目中让扫描出所有使用Restful API的方法。刚开始还想着用python过滤关键字来查找的,后来想想可以使用反射来搞的。主要包含以下三个步骤:

  1. 根据包名解析包的具体路径
  2. 查找指定包下指定注解的类
  3. 在上一步骤中得到的类中,依次扫描包含指定注解的方法

想着写着工具类的形式,代码结构如下:

public class AnnotationScannerUtils {
    private static final Logger logger = LoggerFactory.getLogger(AnnotationScannerUtils.class);
    private static final String EXT = "class
    
    /**
     * 根据包名获取包的URL
     * @param pkgName com.demo.controller
     * @return
     */
    public static String getPkgPath(String pkgName){
        String pkgDirName = pkgName.replace('.', File.separatorChar);
        URL url = Thread.currentThread().getContextClassLoader().getResource(pkgDirName);

        return url == null ? null : url.getFile();
    }

    /**
     * 获取指定包下所有类对象的集合
     * @param pkgName 包名(com.demo.controller)
     * @param pkgPath 包路径(/Users/xxx/workspace/java/project/out/production/classes/com/demo/controller)
     * @param recursive 是否递归遍历子目录
     * @return 类集合
     */
    public static Set> scanClasses(String pkgName, String pkgPath, final boolean recursive){
        Set> classesSet = new HashSet<>();

        Collection allClassFile = getAllClassFile(pkgPath, recursive);

        for (File curFile : allClassFile){
            try {
                classesSet.add(getClassObj(curFile, pkgPath, pkgName));
            } catch (ClassNotFoundException e) {
                logger.error("load class fail", e);
            }
        }

        return classesSet;
    }

    /**
     * 获取指定包下包含指定注解的所有类对象的集合
     * @param pkgName 包名(com.demo.controller)
     * @param pkgPath 包路径(/Users/xxx/workspace/java/project/out/production/classes/com/demo/controller)
     * @param recursive 是否递归遍历子目录
     * @param targetAnnotations 指定注解
     * @return 以注解和对应类集合构成的键值对
     */
    public static Map, Set>> scanClassesByAnnotations(
            String pkgName, String pkgPath, final boolean recursive, List> targetAnnotations){
        Map, Set>> resultMap = new HashMap<>(16);

        Collection allClassFile = getAllClassFile(pkgPath, recursive);

        for (File curFile : allClassFile){
            try {
                Class curClass = getClassObj(curFile, pkgPath, pkgName);
                for (Class annotation : targetAnnotations){
                    if (curClass.isAnnotationPresent(annotation)){
                        if (!resultMap.containsKey(annotation)){
                            resultMap.put(annotation, new HashSet>());
                        }
                        resultMap.get(annotation).add(curClass);
                    }
                }
            } catch (ClassNotFoundException e) {
                logger.error("load class fail", e);
            }
        }

        return resultMap;
    }

    /**
     * 加载类
     * @param file
     * @param pkgPath
     * @param pkgName
     * @return
     * @throws ClassNotFoundException
     */
    private static Class getClassObj(File file, String pkgPath, String pkgName) throws ClassNotFoundException{
        // 考虑class文件在子目录中的情况
        String absPath = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - EXT.length() - 1);
        String className = absPath.substring(pkgPath.length()).replace(File.separatorChar, '.');
        className = className.startsWith(".") ? pkgName + className : pkgName + "." + className;

        return Thread.currentThread().getContextClassLoader().loadClass(className);
    }

    /**
     * 遍历指定目录下所有扩展名为class的文件
     * @param pkgPath 包目录
     * @param recursive 是否递归遍历子目录
     * @return
     */
    private static Collection getAllClassFile(String pkgPath, boolean recursive){
        File fPkgDir = new File(pkgPath);

        if (!(fPkgDir.exists() && fPkgDir.isDirectory())){
            logger.error("the directory to package is empty: {}", pkgPath);

            return null;
        }

       return FileUtils.listFiles(fPkgDir, new String[]{EXT}, recursive);
    }

    /**
     * 查找指定注解的Method
     * @param classes 查找范围
     * @param targetAnnotations 指定的注解
     * @return 以注解和对应Method类集合构成的键值对
     */
    public static Map, Set> scanMethodsByAnnotations(Set> classes,
                                                                                         List> targetAnnotations){
        Map, Set> resultMap = new HashMap<>(16);

        for (Class cls : classes){
            Method[] methods = cls.getMethods();

            for (Class annotation : targetAnnotations){
                for (Method method : methods){
                    if (method.isAnnotationPresent(annotation)){
                        if (!resultMap.containsKey(annotation)){
                            resultMap.put(annotation, new HashSet());
                        }
                        resultMap.get(annotation).add(method);
                    }
                }
            }
        }

        return resultMap;
    }
}
复制代码

具体使用时,可根据具体情况在原方法上二次开发。如果是直接调用,可以实现扫描包含指定注解的类和方法:

    public static void main(String[] args) {
        String pkgName = "com.demo.controller";
        String pkgPath = getPkgPath(pkgName);
        logger.info("pkgPath is {}", pkgName);

        // 查找包含RestController和Controller注解的类
        Map, Set>> classesMap = scanClassesByAnnotations(pkgName, pkgPath, true,
                Arrays.asList(RestController.class, Controller.class));
        if (classesMap.size() == 0){
            logger.error("Not exists any class in {} with the specified annotation", pkgPath);
            return;
        }

        Set> classSet = new HashSet<>();
        classesMap.forEach((k, v) -> {
            logger.info("get {} classes with {}", v.size(), k.getSimpleName());
            classSet.addAll(v);
        });

        // 查找包含GetMapping和PostMapping注解的Method
        Map, Set> methodMap = scanMethodsByAnnotations(classSet, Arrays.asList(GetMapping.class, PostMapping.class));
        if (methodMap.size() == 0){
            logger.error("Not exists any method with the specified annotation");
            return;
        }

        methodMap.forEach((k, v) -> {
            StringBuilder sb = new StringBuilder();
            v.forEach(method -> sb.append(method.getName()+", "));
            logger.info(k.getSimpleName() + ": " + sb.toString());
        });
    }
    
--------------------------output-------------------------
01-20 15:06:02.293 [ INFO] [            m.i.u.AnnotationScannerUtils: 29] - pkgPath is com.demo.controller
01-20 15:06:02.363 [ INFO] [            m.i.u.AnnotationScannerUtils: 41] - get 5 classes with RestController
01-20 15:06:02.374 [ INFO] [            m.i.u.AnnotationScannerUtils: 41] - get 2 classes with Controller
01-20 15:06:02.388 [ INFO] [            m.i.u.AnnotationScannerUtils: 55] - PostMapping: login, addFavorite, addUser, logout, 
01-20 15:06:02.388 [ INFO] [            m.i.u.AnnotationScannerUtils: 55] - GetMapping: webSiteInfo, info, getCategories, favoritesList, getReportByCityId, queryUserById, getReportByCityName, queryBookById, 
复制代码

你可能感兴趣的:(java随笔-扫描使用指定注解的类与方法)