Search in sources :

Example 6 with ClassPath

use of com.google.common.reflect.ClassPath in project accumulo by apache.

the class IteratorTestCaseFinder method findAllTestCases.

/**
 * Instantiates all test cases provided.
 *
 * @return A list of {@link IteratorTestCase}s.
 */
public static List<IteratorTestCase> findAllTestCases() {
    log.info("Searching {}", IteratorTestCase.class.getPackage().getName());
    ClassPath cp;
    try {
        cp = ClassPath.from(IteratorTestCaseFinder.class.getClassLoader());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ImmutableSet<ClassInfo> classes = cp.getTopLevelClasses(IteratorTestCase.class.getPackage().getName());
    final List<IteratorTestCase> testCases = new ArrayList<>();
    // final Set<Class<? extends IteratorTestCase>> classes = reflections.getSubTypesOf(IteratorTestCase.class);
    for (ClassInfo classInfo : classes) {
        Class<?> clz;
        try {
            clz = Class.forName(classInfo.getName());
        } catch (Exception e) {
            log.warn("Could not get class for " + classInfo.getName(), e);
            continue;
        }
        if (clz.isInterface() || Modifier.isAbstract(clz.getModifiers()) || !IteratorTestCase.class.isAssignableFrom(clz)) {
            log.debug("Skipping " + clz);
            continue;
        }
        try {
            testCases.add((IteratorTestCase) clz.newInstance());
        } catch (IllegalAccessException | InstantiationException e) {
            log.warn("Could not instantiate {}", clz, e);
        }
    }
    return testCases;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) IteratorTestCase(org.apache.accumulo.iteratortest.testcases.IteratorTestCase) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Example 7 with ClassPath

use of com.google.common.reflect.ClassPath in project openems by OpenEMS.

the class ConfigUtils method getAvailableClasses.

public static Set<Class<? extends Thing>> getAvailableClasses(String topLevelPackage, Class<? extends Thing> clazz, String suffix) throws ReflectionException {
    Set<Class<? extends Thing>> clazzes = new HashSet<>();
    try {
        ClassPath classpath = ClassPath.from(ClassLoader.getSystemClassLoader());
        for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(topLevelPackage)) {
            if (classInfo.getName().endsWith(suffix)) {
                Class<?> thisClazz = classInfo.load();
                if (clazz.isAssignableFrom(thisClazz)) {
                    @SuppressWarnings("unchecked") Class<? extends Thing> thisThingClazz = (Class<? extends Thing>) thisClazz;
                    clazzes.add(thisThingClazz);
                }
            }
        }
    } catch (IllegalArgumentException | IOException e) {
        throw new ReflectionException(e.getMessage());
    }
    return clazzes;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ReflectionException(io.openems.api.exception.ReflectionException) IOException(java.io.IOException) Thing(io.openems.api.thing.Thing) HashSet(java.util.HashSet)

Example 8 with ClassPath

use of com.google.common.reflect.ClassPath in project stripe-java by stripe.

the class StandardizationTest method getAllModels.

private Collection<Class> getAllModels() throws IOException {
    Class<Charge> chargeClass = Charge.class;
    ClassPath classPath = ClassPath.from(chargeClass.getClassLoader());
    ImmutableSet<ClassPath.ClassInfo> topLevelClasses = classPath.getTopLevelClasses(chargeClass.getPackage().getName());
    List<Class> classList = Lists.newArrayListWithExpectedSize(topLevelClasses.size());
    for (ClassPath.ClassInfo classInfo : topLevelClasses) {
        Class c = classInfo.load();
        // Skip things that aren't APIResources
        if (!APIResource.class.isAssignableFrom(c)) {
            continue;
        }
        // Skip the APIResource itself
        if (APIResource.class == c) {
            continue;
        }
        classList.add(classInfo.load());
    }
    return classList;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) APIResource(com.stripe.net.APIResource)

Example 9 with ClassPath

use of com.google.common.reflect.ClassPath in project raml-module-builder by folio-org.

the class InterfaceToImpl method convert2Impl.

/**
 * Return the implementing class.
 *
 * @param implDir
 *          - package name where to search
 * @param interface2check
 *          - class name of the required interface
 * @return implementing class/es
 * @throws IOException
 *           - if the attempt to read class path resources (jar files or directories) failed.
 * @throws ClassNotFoundException
 *           - if no class in implDir implements the interface
 */
public static ArrayList<Class<?>> convert2Impl(String implDir, String interface2check, boolean allowMultiple) throws IOException, ClassNotFoundException {
    ArrayList<Class<?>> impl = new ArrayList<>();
    ArrayList<Class<?>> cachedClazz = clazzCache.get(implDir, interface2check);
    if (cachedClazz != null) {
        log.debug("returned " + cachedClazz.size() + " class/es from cache");
        return cachedClazz;
    }
    ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
    ImmutableSet<ClassPath.ClassInfo> classes = classPath.getTopLevelClasses(implDir);
    Class<?> userImpl = null;
    /**
     * iterate over all classes in the org.folio.rest.impl package to find the one implementing the
     * requested interface
     */
    for (ClassPath.ClassInfo info : classes) {
        if (userImpl != null && impl.size() == 1) {
            /**
             * we found a user impl that matches the interface2check, we are done, since we can only have one of these
             */
            break;
        }
        try {
            Class<?> clazz = Class.forName(info.getName());
            if (!clazz.getSuperclass().getName().equals("java.lang.Object")) {
                // NOSONAR
                /**
                 * user defined class which overrides one of the out of the box RMB implementations
                 * set the clazz to the interface. find the correct implementation below
                 */
                userImpl = clazz;
                clazz = clazz.getSuperclass();
            }
            /**
             * loop over all interfaces the class implements
             */
            for (Class<?> anInterface : clazz.getInterfaces()) {
                if (!anInterface.getName().equals(interface2check)) {
                    // NOSONAR
                    /**
                     *if userImpl != null here then a user impl exists but does not match the requested interface, so set to null
                     */
                    userImpl = null;
                    /**
                     * class doesnt implement the requested package, continue to check if it implements other packages
                     */
                    continue;
                }
                if (userImpl != null) {
                    /**
                     * we are here if we found a user impl (extends) of an RMB existing interface implementation
                     *  load the user implementation and remove previous impl if it was added in previous loop iterations
                     * there can only be one impl of an override, but we dont know the order we will get from the classloader
                     * will the default RMB impl be passed in here or the user implementation - so once we hit a class whose
                     * super class extends a generated interface - clear out the previous impl if any from the array and add
                     * the user's impl - once found we are done
                     */
                    impl.clear();
                    impl.add(userImpl);
                    break;
                } else if (!allowMultiple && impl.size() > 0) {
                    throw new RuntimeException("Duplicate implementation of " + interface2check + " in " + implDir + ": " + impl.get(0).getName() + ", " + clazz.getName());
                } else {
                    /**
                     * return the class found that implements the requested package
                     */
                    impl.add(clazz);
                }
            }
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage(), e);
        }
    }
    if (impl.isEmpty()) {
        throw new ClassNotFoundException("Implementation of " + interface2check + " not found in " + implDir);
    }
    clazzCache.put(implDir, interface2check, impl);
    return impl;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ArrayList(java.util.ArrayList)

Example 10 with ClassPath

use of com.google.common.reflect.ClassPath in project runelite by runelite.

the class MixinInjector method inject.

public void inject() throws InjectionException {
    ClassPath classPath;
    try {
        classPath = ClassPath.from(this.getClass().getClassLoader());
    } catch (IOException ex) {
        throw new InjectionException(ex);
    }
    // key: mixin class
    // value: mixin targets
    Map<Class<?>, List<ClassFile>> mixinClasses = new HashMap<>();
    // Find mixins and populate mixinClasses
    for (ClassInfo classInfo : classPath.getTopLevelClasses(MIXIN_BASE)) {
        Class<?> mixinClass = classInfo.load();
        List<ClassFile> mixinTargets = new ArrayList<>();
        for (Mixin mixin : mixinClass.getAnnotationsByType(Mixin.class)) {
            Class<?> implementInto = mixin.value();
            ClassFile targetCf = inject.findVanillaForInterface(implementInto);
            if (targetCf == null) {
                throw new InjectionException("No class implements " + implementInto + " for mixin " + mixinClass);
            }
            mixinTargets.add(targetCf);
        }
        mixinClasses.put(mixinClass, mixinTargets);
    }
    inject(mixinClasses);
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ClassFile(net.runelite.asm.ClassFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mixin(net.runelite.api.mixins.Mixin) ArrayList(java.util.ArrayList) List(java.util.List) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Aggregations

ClassPath (com.google.common.reflect.ClassPath)26 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)8 ClassInfo (com.google.common.reflect.ClassPath.ClassInfo)5 URL (java.net.URL)3 List (java.util.List)3 ReflectionException (io.openems.api.exception.ReflectionException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BuckEventListener (com.facebook.buck.event.BuckEventListener)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 Collections2 (com.google.common.collect.Collections2)1