Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
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/constructors/methods/fields annotated with some annotation, optionally with annotation parameters matching
*
get all resources matching matching a regular expression
*
get all methods with specific signature including parameters, parameter annotations and return type
*
get all methods parameter names
*
get all fields/methods/constructors usages in code
*
Basically, to use Reflections first instantiate it with one of the constructors, then depending on the scanners, use the convenient query methods:
*
* Reflections reflections = new Reflections("my.package.prefix");
* //or
* Reflections reflections = new Reflections(ClasspathHelper.forPackage("my.package.prefix"),
* new SubTypesScanner(), new TypesAnnotationScanner(), new FilterBuilder().include(...), ...);
*
* //or using the ConfigurationBuilder
* new Reflections(new ConfigurationBuilder()
* .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.project.prefix")))
* .setUrls(ClasspathHelper.forPackage("my.project.prefix"))
* .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...));
*
You can use other scanners defined in Reflections as well, such as: SubTypesScanner, TypeAnnotationsScanner (both default),
* ResourcesScanner, MethodAnnotationsScanner, ConstructorAnnotationsScanner, FieldAnnotationsScanner,
* MethodParameterScanner, MethodParameterNamesScanner, MemberUsageScanner or any custom scanner.
*
Use {@link #getStore()} to access and query the store directly
*
In order to save the store metadata, use {@link #save(String)} or {@link #save(String, org.reflections8.serializers.Serializer)}
* for example with {@link org.reflections8.serializers.XmlSerializer} or {@link org.reflections8.serializers.JavaCodeSerializer}
*
In order to collect pre saved metadata and avoid re-scanning, use {@link #collect(String, Predicate, org.reflections8.serializers.Serializer...)}}
*
Make sure to scan all the transitively relevant packages.
* for instance, given your class C extends B extends A, and both B and A are located in another package than C,
* when only the package of C is scanned - then querying for sub types of A returns nothing (transitive), but querying for sub types of B returns C (direct).
* In that case make sure to scan all relevant packages a priori.
*
For Javadoc, source code, and more information about Reflections Library, see http://github.com/ronmamo/reflections/
*/
public class Reflections {
public static final Optional log = findLogger(Reflections.class);
protected final transient Configuration configuration;
protected Store store;
/**
* constructs a Reflections instance and scan according to given {@link org.reflections8.Configuration}
*
it is preferred to use {@link org.reflections8.util.ConfigurationBuilder}
*/
public Reflections(final Configuration configuration) {
this.configuration = configuration;
store = new Store(configuration);
if (configuration.getScanners() != null && !configuration.getScanners().isEmpty()) {
//inject to scanners
for (Scanner scanner : configuration.getScanners()) {
scanner.setConfiguration(configuration);
scanner.setStore(store.getOrCreate(index(scanner.getClass())));
}
scan();
if (configuration.shouldExpandSuperTypes()) {
expandSuperTypes();
}
}
}
/**
* a convenient constructor for scanning within a package prefix.
*
this actually create a {@link org.reflections8.Configuration} with:
* - urls that contain resources with name {@code prefix}
* - filterInputsBy where name starts with the given {@code prefix}
* - scanners set to the given {@code scanners}, otherwise defaults to {@link org.reflections8.scanners.TypeAnnotationsScanner} and {@link org.reflections8.scanners.SubTypesScanner}.
* @param prefix package prefix, to be used with {@link org.reflections8.util.ClasspathHelper#forPackage(String, ClassLoader...)} )}
* @param scanners optionally supply scanners, otherwise defaults to {@link org.reflections8.scanners.TypeAnnotationsScanner} and {@link org.reflections8.scanners.SubTypesScanner}
*/
public Reflections(final String prefix, final Scanner... scanners) {
this((Object) prefix, scanners);
}
/**
* a convenient constructor for Reflections, where given {@code Object...} parameter types can be either:
*
*
{@link String} - would add urls using {@link org.reflections8.util.ClasspathHelper#forPackage(String, ClassLoader...)} ()}
*
{@link Class} - would add urls using {@link org.reflections8.util.ClasspathHelper#forClass(Class, ClassLoader...)}
*
{@link ClassLoader} - would use this classloaders in order to find urls in {@link org.reflections8.util.ClasspathHelper#forPackage(String, ClassLoader...)} and {@link org.reflections8.util.ClasspathHelper#forClass(Class, ClassLoader...)}
*
{@link org.reflections8.scanners.Scanner} - would use given scanner, overriding the default scanners
*
{@link java.net.URL} - would add the given url for scanning
*
{@link Object[]} - would use each element as above
*
*
* use any parameter type in any order. this constructor uses instanceof on each param and instantiate a {@link org.reflections8.util.ConfigurationBuilder} appropriately.
* if you prefer the usual statically typed constructor, don't use this, although it can be very useful.
*
*
for example:
*
* new Reflections("my.package", classLoader);
* //or
* new Reflections("my.package", someScanner, anotherScanner, classLoader);
* //or
* new Reflections(myUrl, myOtherUrl);
*
*/
public Reflections(final Object... params) {
this(ConfigurationBuilder.build(params));
}
protected Reflections() {
configuration = new ConfigurationBuilder();
store = new Store(configuration);
}
//
protected void scan() {
if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
if (log.isPresent()) log.get().warn("given scan urls are empty. set urls in the configuration");
return;
}
if (log.isPresent() && log.get().isDebugEnabled()) {
log.get().debug("going to scan these urls:\n{}", Joiner.on("\n").join(configuration.getUrls()));
}
long time = System.currentTimeMillis();
int scannedUrls = 0;
Optional executorService = configuration.getExecutorService();
List> futures = new ArrayList();
for (final URL url : configuration.getUrls()) {
try {
if (executorService.isPresent()) {
futures.add(executorService.get().submit(new Runnable() {
public void run() {
if (log.isPresent()) {
log.get().debug("[{}] scanning {}", Thread.currentThread().toString(), url);
}
scan(url);
}
}));
} else {
scan(url);
}
scannedUrls++;
} catch (ReflectionsException e) {
if (log.isPresent()) {
log.get().warn("could not create Vfs.Dir from url. ignoring the exception and continuing", e);
}
}
}
//todo use CompletionService
if (executorService.isPresent()) {
for (Future future : futures) {
try { future.get(); } catch (Exception e) { throw new RuntimeException(e); }
}
}
time = System.currentTimeMillis() - time;
//gracefully shutdown the parallel scanner executor service.
if (executorService.isPresent()) {
executorService.get().shutdown();
}
if (log.isPresent()) {
int keys = 0;
int values = 0;
for (String index : store.keySet()) {
keys += store.get(index).keySet().size();
for (Collection c: store.get(index).values()) {
values += c.size();
}
}
log.get().trace(format("Reflections took %d ms to scan %d urls, producing %d keys and %d values %s",
time, scannedUrls, keys, values,
executorService.isPresent() && executorService.get() instanceof ThreadPoolExecutor ?
format("[using %d cores]", ((ThreadPoolExecutor) executorService.get()).getMaximumPoolSize()) : ""));
}
}
protected void scan(URL url) {
Vfs.Dir dir = Vfs.fromURL(url);
try {
for (final Vfs.File file : dir.getFiles()) {
// scan if inputs filter accepts file relative path or fqn
Optional> inputsFilter = configuration.getInputsFilter();
String path = file.getRelativePath();
String fqn = path.replace('/', '.');
if (!inputsFilter.isPresent() || inputsFilter.get().test(path) || inputsFilter.get().test(fqn)) {
Optional