没有任何数据可供显示
| 开源项目社区 | 当前位置 : |
|
| www.trustie.net/open_source_projects | 主页 > 开源项目社区 > reflections |
|
reflections
|
| 4 | 0 | 71 |
| 贡献者 | 讨论 | 代码提交 |
version 0.9.5 - added Maven repository, UseCases, javadoc, JavaCodeSerializer, getResources, Vfs
A Java runtime metadata analysis, in the spirit of ScannotationsReflections 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
How to use?a typical use of Reflections would be:
Reflections reflections = new Reflections("my.project.prefix");
Set> subTypes = reflections.getSubTypesOf(SomeClassOrInterface.class);
Set> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
Set> annotated1 = reflections.getTypesAnnotatedWith(
new SomeAnnotation() {public String value() {return "1";}
public Class annotationType() {return SomeAnnotation.class;}});
Set propertiesFiles = reflections.getResources(Pattern.compile(".*\\.properties")); //depends on ResourcesScanner configured, as on next examplebasically, to use Reflections for scanning and querying, instantiate it with a Configuration, for example
new Reflections(new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix("my.project.prefix")))
.setUrls(ClasspathHelper.getUrlsForPackagePrefix("my.project.prefix"))
.setScanners(new SubTypesScanner(),
new TypeAnnotationsScanner().filterResultsBy(filter),
new ResourcesScanner()));than use the convenient methods to query the metadata, such as getSubTypesOf, getTypesAnnotatedWith, getMethodsAnnotatedWith and more.
You can use other scanners defined in Reflections as well, such as: SubTypesScanner, TypeAnnotationsScanner, FieldAnnotationsScanner, MethodAnnotationsScanner, ResourcrsScanner and more. Than use the equivalent query method in the Reflections object.
browse the javadoc for more info
also, browse the tests directory to see some more examples
MavenRepoto use Reflections via a maven repository, configure your pom.xml with
reflections-repo
Reflections Maven2 Repository
http://reflections.googlecode.com/svn/repo
than add a dependency to Reflections in your pom.xml
org.reflections
reflections
the latest version...
UseCasesReflections can also:
save scanned metadata and collect it later on runtime, for quick bootstrap of your application save your model entities metadata - fully qualified name of packages and types, fields and methods - so you can later reference these in a static manner find all resources matching a regular expression (for example find all .properties files or .xml files)
see the UseCases wiki page
Reflections Maven pluginThere is the ReflectionsMojo Maven plugin available for your project. With simple configuration you can save all scanned metadata into xml files upon compiling. Later on, when your project is bootstrapping you can let Reflections collect all those resources and re-create that metadata for you, making it available at runtime without re-scanning the classpath - thus reducing the bootstrapping time.
Use this maven configuration in your pom file:
org.reflections
reflections-maven
the latest version...
reflections
process-classes
than, on runtime:
Reflections reflections =
isProduction() ? Reflections.collect() : new Reflections("your.package.here");Check out in the ReflectionsMojo wiki page
Extending ReflectionsYou can easily extend Reflections by :
create your specialized scan class, should implement Scanner provide a query method within that scan class next, use that scanner instance to first configure Reflections and than to query
patches and extension are welcomed!
Cheers
标签:
