Reflections工具包

工作中配合Google Guice的使用,用到了Reflections工具包,记录一下,备忘。

项目地址: http://code.google.com/p/reflections/

从官方文档上,我们可以了解到它是一个用于Java运行时元数据分析,本着注解搜寻的精神。

用途:

Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project.


Using Reflections you can query your metadata such as:

get all subtypes of some type
get all types/methods/fields annotated with some annotation, w/o annotation parameters matching
get all resources matching matching a regular expression

UseCases:

Java代码 复制代码 收藏代码
  1. some useful use cases below:
  2. bootstrap in a multi module environment
  3. collect pre scanned metadata
  4. serialize Reflections into a java source file, and use it to statically reference java elements
  5. query the store directly, avoid definition of types in class loader
  6. find resources in your classpath (for example all properties files)
  7. optional parallel scanning
  8. bootstrap in a multi module environment
  9. In a multi module project, where each module is responsible for it's properties, jpa entities and maybe guice modules, use Reflections to collect that metadata and bootstrap the application
  10. Reflections reflections = new Reflections(new ConfigurationBuilder()
  11. .addUrls(ClasspathHelper.forPackage("your.package.here"),
  12. ClasspathHelper.forClass(Entity.class),
  13. ClasspathHelper.forClass(Module.class))
  14. .setScanners(new ResourcesScanner(),
  15. new TypeAnnotationsScanner(),
  16. new SubTypesScanner()));
  17. Set<String> propertiesFiles = reflections.getResources(Pattern.compile(".*\\.properties"));
  18. Properties allProperties = createOneBigProperties(propertiesFiles);
  19. Set<Class<?>> jpaEntities = reflections.getTypesAnnotatedWith(Entity.class);
  20. SessionFactory sessionFactory = createOneBigSessionFactory(jpaEntities, allProperties);
  21. Set<Class<? extends Module>> guiceModules = reflections.getSubTypesOf(Module.class);
  22. Injector injector = createOneBigInjector(guiceModules);
  23. collect pre scanned metadata
  24. first configure your project's parent pom in the build.plugins section with Reflections, like this
  25. <plugin>
  26. <groupId>org.reflections</groupId>
  27. <artifactId>reflections-maven</artifactId>
  28. <version>0.9.8</version>
  29. <executions>
  30. <execution>
  31. <goals>
  32. <goal>reflections</goal>
  33. </goals>
  34. <phase>process-classes</phase>
  35. </execution>
  36. </executions>
  37. </plugin>
  38. than, on runtime, collect these pre saved metadata and instantiate Reflections
  39. Reflections reflections =
  40. isProduction() ? Reflections.collect() : new Reflections("your.package.here");
  41. of course, saving the scanned metadata can be done without maven, by simply calling the save method
  42. public static void main(String[] args) {
  43. //from time to time, I run this main to regenerate saved metadata for Reflections
  44. new Reflections(new ConfigurationBuilder()
  45. .setUrls(ClasspathHelper.forPackage("my.project.prefix"))
  46. .setScanners(/*whatever*/))
  47. .save("src/main/resources/resource1-reflections.xml");
  48. new Reflections(new ConfigurationBuilder()
  49. .setUrls(ClasspathHelper.forPackage("my.project.prefix.model"))
  50. .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.project.prefix.model")))
  51. .setScanners(new TypesScanner(), new TypeElementsScanner())
  52. .setSerializer(new JavaCodeSerializer()))
  53. .save("src/main/java/my.project.prefix.model.MyModelStore");
  54. }
  55. serialize Reflections into a java source file, and use it to statically reference java elements
  56. Reflections reflections = new Reflections(new ConfigurationBuilder()
  57. .filterInputsBy(new FilterBuilder().include("model.package"))
  58. .setScanners(new TypesScanner(), new TypeElementsScanner())
  59. .setUrls(asList(ClasspathHelper.forPackage("model.package"))));
  60. String filename = System.getProperty("user.dir") + "/src/test/java/model.package.reflections.MyModelStore";
  61. reflections.save(filename, new JavaCodeSerializer());
  62. replace "model.package" with your model's package prefix
  63. this serializes types and types elements into interfaces respectively to fully qualified name, for example:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值