java 非常好用的反射框架Reflections

背景
最近在設(shè)計(jì)和開發(fā)部門的基礎(chǔ)架構(gòu),需要利用反射技術(shù)找到classpath目錄下所有包含指定注解的類,然后根據(jù)注解配置完成指定的功能。
最初是想自己寫代碼來實(shí)現(xiàn)這些功能,邊查資料邊寫,整了大半天,寫出來的效果自己都不太滿意。一方面是代碼多,不好維護(hù);另一方面是性能不太好。不過,在查資料的過程中,我發(fā)現(xiàn)了一個(gè)非常好用的反射框架:reflections。
reflections簡單好用,性能也不錯(cuò),很快就完成了我想要的功能,就是這么優(yōu)秀!
簡介
Reflections通過掃描classpath,索引元數(shù)據(jù),并且允許在運(yùn)行時(shí)查詢這些元數(shù)據(jù)。
使用Reflections可以很輕松的獲取以下元數(shù)據(jù)信息:
獲取某個(gè)類型的全部子類
只要類型、構(gòu)造器、方法,字段上帶有特定注解,便能獲取帶有這個(gè)注解的全部信息(類型、構(gòu)造器、方法,字段)
獲取所有能匹配某個(gè)正則表達(dá)式的資源
獲取所有帶有特定簽名的方法,包括參數(shù),參數(shù)注解,返回類型
獲取所有方法的名字
獲取代碼里所有字段、方法名、構(gòu)造器的使用
Maven依賴
在pom.xml中添加reflections的依賴:
dependency>
????<groupId>org.reflectionsgroupId>
????<artifactId>reflectionsartifactId>
????<version>0.9.11version>
dependency>// 實(shí)例化Reflections,并指定要掃描的包名
Reflections reflections = new?Reflections("my.project");
// 獲取某個(gè)類的所有子類
Set> subTypes = reflections.getSubTypesOf(SomeType.class);
// 獲取包含某個(gè)注解的所有類
Set> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class); 使用
//scan urls that contain 'my.package', include inputs starting with 'my.package', use the default?scanners
Reflections reflections = new?Reflections("my.package");
//or?using ConfigurationBuilder
new?Reflections(new?ConfigurationBuilder()
?????.setUrls(ClasspathHelper.forPackage("my.project.prefix"))
?????.setScanners(new?SubTypesScanner(),
??????????????????new?TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...),
?????.filterInputsBy(new?FilterBuilder().includePackage("my.project.prefix"))
?????...);掃描子類
Set<Class extends Module>> modules =
????reflections.getSubTypesOf(com.google.inject.Module.class);掃描注解
//TypeAnnotationsScanner
Set<Class>> singletons =
????reflections.getTypesAnnotatedWith(javax.inject.Singleton.class);掃描資源
//ResourcesScanner
Set<String> properties =
????reflections.getResources(Pattern.compile(".*\\.properties"));掃描方法注解
//MethodAnnotationsScanner
Set resources =
????reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);
Set<Constructor> injectables =
????reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class); 掃描字段注解
//FieldAnnotationsScanner
Set<Field> ids =
????reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);掃描方法參數(shù)
//MethodParameterScanner
Set someMethods =
????reflections.getMethodsMatchParams(long.class, int.class);
Set voidMethods =
????reflections.getMethodsReturn(void.class);
Set pathParamMethods =
????reflections.getMethodsWithAnyParamAnnotated(PathParam.class); 掃描方法參數(shù)名
//MethodParameterNamesScanner
List parameterNames =
????reflections.getMethodParamNames(Method.class) 掃描方法調(diào)用情況
//MemberUsageScanner
Set<Member> usages =
????reflections.getMethodUsages(Method.class)出處:cnblogs.com/panchanggui/p/13188345.html
評(píng)論
圖片
表情
