use of org.reflections.scanners.Scanner in project reflections by ronmamo.
the class Reflections method scan.
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
Predicate<String> inputsFilter = configuration.getInputsFilter();
String path = file.getRelativePath();
String fqn = path.replace('/', '.');
if (inputsFilter == null || inputsFilter.apply(path) || inputsFilter.apply(fqn)) {
Object classObject = null;
for (Scanner scanner : configuration.getScanners()) {
try {
if (scanner.acceptsInput(path) || scanner.acceptResult(fqn)) {
classObject = scanner.scan(file, classObject);
}
} catch (Exception e) {
if (log != null && log.isDebugEnabled())
log.debug("could not scan file " + file.getRelativePath() + " in url " + url.toExternalForm() + " with scanner " + scanner.getClass().getSimpleName(), e);
}
}
}
}
} finally {
dir.close();
}
}
use of org.reflections.scanners.Scanner in project reflections by ronmamo.
the class ConfigurationBuilder method build.
/** constructs a {@link ConfigurationBuilder} using the given parameters, in a non statically typed way.
* that is, each element in {@code params} is guessed by it's type and populated into the configuration.
* <ul>
* <li>{@link String} - add urls using {@link ClasspathHelper#forPackage(String, ClassLoader...)} ()}</li>
* <li>{@link Class} - add urls using {@link ClasspathHelper#forClass(Class, ClassLoader...)} </li>
* <li>{@link ClassLoader} - use these classloaders in order to find urls in ClasspathHelper.forPackage(), ClasspathHelper.forClass() and for resolving types</li>
* <li>{@link Scanner} - use given scanner, overriding the default scanners</li>
* <li>{@link URL} - add the given url for scanning</li>
* <li>{@code Object[]} - flatten and use each element as above</li>
* </ul>
*
* an input {@link FilterBuilder} will be set according to given packages.
* <p>use any parameter type in any order. this constructor uses instanceof on each param and instantiate a {@link ConfigurationBuilder} appropriately.
* */
@SuppressWarnings("unchecked")
public static ConfigurationBuilder build(@Nullable final Object... params) {
ConfigurationBuilder builder = new ConfigurationBuilder();
//flatten
List<Object> parameters = Lists.newArrayList();
if (params != null) {
for (Object param : params) {
if (param != null) {
if (param.getClass().isArray()) {
for (Object p : (Object[]) param) if (p != null)
parameters.add(p);
} else if (param instanceof Iterable) {
for (Object p : (Iterable) param) if (p != null)
parameters.add(p);
} else
parameters.add(param);
}
}
}
List<ClassLoader> loaders = Lists.newArrayList();
for (Object param : parameters) if (param instanceof ClassLoader)
loaders.add((ClassLoader) param);
ClassLoader[] classLoaders = loaders.isEmpty() ? null : loaders.toArray(new ClassLoader[loaders.size()]);
FilterBuilder filter = new FilterBuilder();
List<Scanner> scanners = Lists.newArrayList();
for (Object param : parameters) {
if (param instanceof String) {
builder.addUrls(ClasspathHelper.forPackage((String) param, classLoaders));
filter.includePackage((String) param);
} else if (param instanceof Class) {
if (Scanner.class.isAssignableFrom((Class) param)) {
try {
builder.addScanners(((Scanner) ((Class) param).newInstance()));
} catch (Exception e) {
/*fallback*/
}
}
builder.addUrls(ClasspathHelper.forClass((Class) param, classLoaders));
filter.includePackage(((Class) param));
} else if (param instanceof Scanner) {
scanners.add((Scanner) param);
} else if (param instanceof URL) {
builder.addUrls((URL) param);
} else if (param instanceof ClassLoader) {
/* already taken care */
} else if (param instanceof Predicate) {
filter.add((Predicate<String>) param);
} else if (param instanceof ExecutorService) {
builder.setExecutorService((ExecutorService) param);
} else if (Reflections.log != null) {
throw new ReflectionsException("could not use param " + param);
}
}
if (builder.getUrls().isEmpty()) {
if (classLoaders != null) {
//default urls getResources("")
builder.addUrls(ClasspathHelper.forClassLoader(classLoaders));
} else {
//default urls getResources("")
builder.addUrls(ClasspathHelper.forClassLoader());
}
}
builder.filterInputsBy(filter);
if (!scanners.isEmpty()) {
builder.setScanners(scanners.toArray(new Scanner[scanners.size()]));
}
if (!loaders.isEmpty()) {
builder.addClassLoaders(loaders);
}
return builder;
}
Aggregations