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);
}
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;
}
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;
}
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();
}
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());
}
Aggregations