use of org.reflections.ReflectionsException in project reflections by ronmamo.
the class XmlSerializer method save.
public File save(final Reflections reflections, final String filename) {
File file = Utils.prepareFile(filename);
try {
Document document = createDocument(reflections);
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file), OutputFormat.createPrettyPrint());
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
throw new ReflectionsException("could not save to file " + filename, e);
} catch (Throwable e) {
throw new RuntimeException("Could not save to file " + filename + ". Make sure relevant dependencies exist on classpath.", e);
}
return file;
}
use of org.reflections.ReflectionsException 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;
}
use of org.reflections.ReflectionsException in project reflections by ronmamo.
the class JarInputDir method getFiles.
public Iterable<Vfs.File> getFiles() {
return new Iterable<Vfs.File>() {
public Iterator<Vfs.File> iterator() {
return new AbstractIterator<Vfs.File>() {
{
try {
jarInputStream = new JarInputStream(url.openConnection().getInputStream());
} catch (Exception e) {
throw new ReflectionsException("Could not open url connection", e);
}
}
protected Vfs.File computeNext() {
while (true) {
try {
ZipEntry entry = jarInputStream.getNextJarEntry();
if (entry == null) {
return endOfData();
}
long size = entry.getSize();
//JDK-6916399
if (size < 0)
size = 0xffffffffl + size;
nextCursor += size;
if (!entry.isDirectory()) {
return new JarInputFile(entry, JarInputDir.this, cursor, nextCursor);
}
} catch (IOException e) {
throw new ReflectionsException("could not get next zip entry", e);
}
}
}
};
}
};
}
use of org.reflections.ReflectionsException in project reflections by ronmamo.
the class JavaCodeSerializer method resolveAnnotation.
public static Annotation resolveAnnotation(Class annotation) {
try {
String name = annotation.getSimpleName().replace(pathSeparator, dotSeparator);
Class<?> declaringClass = annotation.getDeclaringClass().getDeclaringClass();
Class<?> aClass = resolveClassOf(declaringClass);
Class<? extends Annotation> aClass1 = (Class<? extends Annotation>) ReflectionUtils.forName(name);
Annotation annotation1 = aClass.getAnnotation(aClass1);
return annotation1;
} catch (Exception e) {
throw new ReflectionsException("could not resolve to annotation " + annotation.getName(), e);
}
}
use of org.reflections.ReflectionsException in project reflections by ronmamo.
the class JavaCodeSerializer method resolveMethod.
public static Method resolveMethod(final Class aMethod) {
String methodOgnl = aMethod.getSimpleName();
try {
String methodName;
Class<?>[] paramTypes;
if (methodOgnl.contains(tokenSeparator)) {
methodName = methodOgnl.substring(0, methodOgnl.indexOf(tokenSeparator));
String[] params = methodOgnl.substring(methodOgnl.indexOf(tokenSeparator) + 1).split(doubleSeparator);
paramTypes = new Class<?>[params.length];
for (int i = 0; i < params.length; i++) {
String typeName = params[i].replace(arrayDescriptor, "[]").replace(pathSeparator, dotSeparator);
paramTypes[i] = ReflectionUtils.forName(typeName);
}
} else {
methodName = methodOgnl;
paramTypes = null;
}
Class<?> declaringClass = aMethod.getDeclaringClass().getDeclaringClass();
return resolveClassOf(declaringClass).getDeclaredMethod(methodName, paramTypes);
} catch (Exception e) {
throw new ReflectionsException("could not resolve to method " + aMethod.getName(), e);
}
}
Aggregations