Search in sources :

Example 21 with ClassPath

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

the class GuavaCollectionBuildersTest method testImmutableBuilders.

@Test
public void testImmutableBuilders() throws Exception {
    ClassPath classPath = ClassPath.from(getClass().getClassLoader());
    ImmutableSet<ClassPath.ClassInfo> classes = classPath.getAllClasses();
    int checked = 0;
    for (ClassPath.ClassInfo classInfo : classes) {
        if (classInfo.getPackageName().equals("com.google.common.collect") && classInfo.getSimpleName().startsWith("Immutable") && !NON_BUILDABLE_COLLECTIONS.contains(classInfo.getSimpleName())) {
            Class<?> c = Class.forName(classInfo.getName());
            if (Modifier.isPublic(c.getModifiers())) {
                checked++;
                checkImmutableClass(c);
            }
        }
    }
    expect.that(checked).isGreaterThan(10);
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Test(org.junit.Test)

Example 22 with ClassPath

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

the class PluginManager method scanAndInstantiate.

List<Plugin> scanAndInstantiate(ClassLoader classLoader, String packageName) throws IOException {
    boolean developerPlugins = RuneLite.getOptions().has("developer-mode");
    MutableGraph<Class<? extends Plugin>> graph = GraphBuilder.directed().build();
    List<Plugin> scannedPlugins = new ArrayList<>();
    ClassPath classPath = ClassPath.from(classLoader);
    ImmutableSet<ClassInfo> classes = packageName == null ? classPath.getAllClasses() : classPath.getTopLevelClassesRecursive(packageName);
    for (ClassInfo classInfo : classes) {
        Class<?> clazz = classInfo.load();
        PluginDescriptor pluginDescriptor = clazz.getAnnotation(PluginDescriptor.class);
        if (pluginDescriptor == null) {
            if (clazz.getSuperclass() == Plugin.class) {
                log.warn("Class {} is a plugin, but has no plugin descriptor", clazz);
            }
            continue;
        }
        if (clazz.getSuperclass() != Plugin.class) {
            log.warn("Class {} has plugin descriptor, but is not a plugin", clazz);
            continue;
        }
        if (!pluginDescriptor.loadWhenOutdated() && isOutdated) {
            continue;
        }
        if (pluginDescriptor.developerPlugin() && !developerPlugins) {
            continue;
        }
        Class<Plugin> pluginClass = (Class<Plugin>) clazz;
        graph.addNode(pluginClass);
    }
    // Build plugin graph
    for (Class<? extends Plugin> pluginClazz : graph.nodes()) {
        PluginDependency[] pluginDependencies = pluginClazz.getAnnotationsByType(PluginDependency.class);
        for (PluginDependency pluginDependency : pluginDependencies) {
            graph.putEdge(pluginClazz, pluginDependency.value());
        }
    }
    if (Graphs.hasCycle(graph)) {
        throw new RuntimeException("Plugin dependency graph contains a cycle!");
    }
    List<Class<? extends Plugin>> sortedPlugins = topologicalSort(graph);
    sortedPlugins = Lists.reverse(sortedPlugins);
    for (Class<? extends Plugin> pluginClazz : sortedPlugins) {
        Plugin plugin;
        try {
            plugin = instantiate(scannedPlugins, (Class<Plugin>) pluginClazz);
        } catch (PluginInstantiationException ex) {
            log.warn("Error instantiating plugin!", ex);
            continue;
        }
        scannedPlugins.add(plugin);
    }
    return scannedPlugins;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Example 23 with ClassPath

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

the class FaultsAndWarningsTranspiler method getEnums.

@SuppressWarnings("unchecked")
private static Set<Class<ThingStateEnum>> getEnums() throws ReflectionException {
    String topLevelPackage = "io.openems.impl";
    Class<ThingStateEnum> searchClazz = ThingStateEnum.class;
    Set<Class<ThingStateEnum>> clazzes = new HashSet<>();
    try {
        ClassPath classpath = ClassPath.from(ClassLoader.getSystemClassLoader());
        for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(topLevelPackage)) {
            Class<?> thisClazz = classInfo.load();
            if (searchClazz.isAssignableFrom(thisClazz)) {
                clazzes.add((Class<ThingStateEnum>) thisClazz);
            }
        }
    } 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) ThingStateEnum(io.openems.api.channel.thingstate.ThingStateEnum) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 24 with ClassPath

use of com.google.common.reflect.ClassPath in project commons by twitter.

the class Configuration method getLiveResources.

/**
 * Gets all relevant resources from our package.
 *
 * This filters classnames that actually exist, to avoid including Configuration
 * for classes that were removed.
 */
private static ImmutableMap<String, URL> getLiveResources() throws IOException {
    ClassLoader classLoader = Configuration.class.getClassLoader();
    ClassPath classPath = ClassPath.from(classLoader);
    ImmutableMap.Builder<String, URL> resources = new ImmutableMap.Builder<String, URL>();
    for (ClassPath.ResourceInfo resourceInfo : classPath.getResources()) {
        String name = resourceInfo.getResourceName();
        // Find relevant resource files.
        if (name.startsWith(DEFAULT_RESOURCE_PREFIX) && name.endsWith(DEFAULT_RESOURCE_SUFFIX)) {
            String className = name.substring(DEFAULT_RESOURCE_PREFIX.length(), name.length() - DEFAULT_RESOURCE_SUFFIX.length());
            // Include only those resources for live classes.
            if (classExists(classLoader, className)) {
                resources.put(className, resourceInfo.url());
            }
        }
    }
    return resources.build();
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ToStringBuilder(org.apache.commons.lang.builder.ToStringBuilder) HashCodeBuilder(org.apache.commons.lang.builder.HashCodeBuilder) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) URL(java.net.URL)

Example 25 with ClassPath

use of com.google.common.reflect.ClassPath in project druid by druid-io.

the class FirehoseModuleTest method getFirehoseFactoryClassesInPackage.

// for ClassPath
@SuppressWarnings("UnstableApiUsage")
private static Set<Class> getFirehoseFactoryClassesInPackage(String packageName) throws IOException {
    // workaround for Guava 16, which can only parse the classpath from URLClassLoaders
    // requires Guava 28 or later to work properly with the system class loader in Java 9 and above
    URLClassLoader classloader = new URLClassLoader(JvmUtils.systemClassPath().toArray(new URL[0]));
    ClassPath classPath = ClassPath.from(classloader);
    return classPath.getTopLevelClasses(packageName).stream().map(ClassPath.ClassInfo::load).filter(IS_FIREHOSE_FACTORY).collect(Collectors.toSet());
}
Also used : ClassPath(com.google.common.reflect.ClassPath) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

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