Search in sources :

Example 11 with ClassInfo

use of com.google.common.reflect.ClassPath.ClassInfo 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 12 with ClassInfo

use of com.google.common.reflect.ClassPath.ClassInfo in project guava by google.

the class ClassPathTest method testGetSimpleName.

public void testGetSimpleName() {
    ClassLoader classLoader = getClass().getClassLoader();
    assertEquals("Foo", new ClassInfo(FILE, "Foo.class", classLoader).getSimpleName());
    assertEquals("Foo", new ClassInfo(FILE, "a/b/Foo.class", classLoader).getSimpleName());
    assertEquals("Foo", new ClassInfo(FILE, "a/b/Bar$Foo.class", classLoader).getSimpleName());
    assertEquals("", new ClassInfo(FILE, "a/b/Bar$1.class", classLoader).getSimpleName());
    assertEquals("Foo", new ClassInfo(FILE, "a/b/Bar$Foo.class", classLoader).getSimpleName());
    assertEquals("", new ClassInfo(FILE, "a/b/Bar$1.class", classLoader).getSimpleName());
    assertEquals("Local", new ClassInfo(FILE, "a/b/Bar$1Local.class", classLoader).getSimpleName());
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Example 13 with ClassInfo

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

the class ChannelAndServerBuilderTest method params.

/**
 * Javadoc.
 */
@Parameters(name = "class={0}")
public static Collection<Object[]> params() throws Exception {
    ClassLoader loader = ChannelAndServerBuilderTest.class.getClassLoader();
    Collection<ClassInfo> classInfos = ClassPath.from(loader).getTopLevelClassesRecursive("io.grpc");
    // Java 9 doesn't expose the URLClassLoader, which breaks searching through the classpath
    if (classInfos.isEmpty()) {
        return new ArrayList<>();
    }
    List<Object[]> classes = new ArrayList<>();
    for (ClassInfo classInfo : classInfos) {
        String className = classInfo.getName();
        if (className.contains("io.grpc.netty.shaded.io.netty")) {
            continue;
        }
        Class<?> clazz = Class.forName(className, false, /*initialize*/
        loader);
        if (ServerBuilder.class.isAssignableFrom(clazz) && clazz != ServerBuilder.class) {
            classes.add(new Object[] { clazz });
        } else if (ManagedChannelBuilder.class.isAssignableFrom(clazz) && clazz != ManagedChannelBuilder.class) {
            classes.add(new Object[] { clazz });
        }
    }
    Truth.assertWithMessage("Unable to find any builder classes").that(classes).isNotEmpty();
    return classes;
}
Also used : ArrayList(java.util.ArrayList) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

ClassInfo (com.google.common.reflect.ClassPath.ClassInfo)13 ArrayList (java.util.ArrayList)7 ClassPath (com.google.common.reflect.ClassPath)5 IOException (java.io.IOException)3 SneakyThrows (lombok.SneakyThrows)2 GWTTestCase (com.google.gwt.junit.client.GWTTestCase)1 GWTTestSuite (com.google.gwt.junit.tools.GWTTestSuite)1 Injector (com.google.inject.Injector)1 URLClassLoader (java.net.URLClassLoader)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 OptionSet (joptsimple.OptionSet)1 TestCase (junit.framework.TestCase)1 GuiProvider (logisticspipes.network.abstractguis.GuiProvider)1 PopupGuiProvider (logisticspipes.network.abstractguis.PopupGuiProvider)1 ModernPacket (logisticspipes.network.abstractpackets.ModernPacket)1 Mixin (net.runelite.api.mixins.Mixin)1