日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java扫描指定package注解_java随笔-扫描使用指定注解的类与方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前幾天項目中讓掃描出所有使用Restful API的方法。剛開始還想著用python過濾關鍵字來查找的,后來想想可以使用反射來搞的。主要包含以下三個步驟:

根據(jù)包名解析包的具體路徑

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

在上一步驟中得到的類中,依次掃描包含指定注解的方法

想著寫著工具類的形式,代碼結構如下:

public class AnnotationScannerUtils {

private static final Logger logger = LoggerFactory.getLogger(AnnotationScannerUtils.class);

private static final String EXT = "class

/**

* 根據(jù)包名獲取包的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;

}

}

復制代碼

具體使用時,可根據(jù)具體情況在原方法上二次開發(fā)。如果是直接調用,可以實現(xiàn)掃描包含指定注解的類和方法:

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扫描指定package注解_java随笔-扫描使用指定注解的类与方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。