Search in sources :

Example 6 with ReflectionsException

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;
}
Also used : ReflectionsException(org.reflections.ReflectionsException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 7 with ReflectionsException

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;
}
Also used : ReflectionsException(org.reflections.ReflectionsException) Scanner(org.reflections.scanners.Scanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) ReflectionsException(org.reflections.ReflectionsException) URL(java.net.URL) Predicate(com.google.common.base.Predicate) ExecutorService(java.util.concurrent.ExecutorService)

Example 8 with ReflectionsException

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);
                        }
                    }
                }
            };
        }
    };
}
Also used : ReflectionsException(org.reflections.ReflectionsException) JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) AbstractIterator(com.google.common.collect.AbstractIterator) IOException(java.io.IOException) ReflectionsException(org.reflections.ReflectionsException) IOException(java.io.IOException)

Example 9 with ReflectionsException

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);
    }
}
Also used : ReflectionsException(org.reflections.ReflectionsException) Annotation(java.lang.annotation.Annotation) ReflectionsException(org.reflections.ReflectionsException) IOException(java.io.IOException)

Example 10 with ReflectionsException

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);
    }
}
Also used : ReflectionsException(org.reflections.ReflectionsException) ReflectionsException(org.reflections.ReflectionsException) IOException(java.io.IOException)

Aggregations

ReflectionsException (org.reflections.ReflectionsException)10 IOException (java.io.IOException)5 Document (org.dom4j.Document)2 Predicate (com.google.common.base.Predicate)1 AbstractIterator (com.google.common.collect.AbstractIterator)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 URL (java.net.URL)1 ExecutorService (java.util.concurrent.ExecutorService)1 JarInputStream (java.util.jar.JarInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 XMLWriter (org.dom4j.io.XMLWriter)1 Reflections (org.reflections.Reflections)1 Scanner (org.reflections.scanners.Scanner)1 SubTypesScanner (org.reflections.scanners.SubTypesScanner)1