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

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

根据包名解析包的具体路径

查找指定包下指定注解的类

在上一步骤中获得的类中,依次扫描包含指定注解的方法

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

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 extends Annotation> 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 extends Annotation> 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;

}

}

复制代码

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

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,

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值