use of com.google.common.reflect.ClassPath.ClassInfo in project LogisticsPipes by RS485.
the class PacketHandler method initialize.
/*
* enumerates all ModernPackets, sets their IDs and populate packetlist/packetmap
*/
@SuppressWarnings("unchecked")
@SneakyThrows({ IOException.class, InvocationTargetException.class, IllegalAccessException.class, InstantiationException.class, IllegalArgumentException.class, NoSuchMethodException.class, SecurityException.class })
public static final // Suppression+sneakiness because these shouldn't ever fail, and if they do, it needs to fail.
void initialize() {
final List<ClassInfo> classes = new ArrayList<>(ClassPath.from(PacketHandler.class.getClassLoader()).getTopLevelClassesRecursive("logisticspipes.network.packets"));
Collections.sort(classes, (o1, o2) -> o1.getSimpleName().compareTo(o2.getSimpleName()));
PacketHandler.packetlist = new ArrayList<>(classes.size());
PacketHandler.packetmap = new HashMap<>(classes.size());
int currentid = 0;
for (ClassInfo c : classes) {
final Class<?> cls = c.load();
final ModernPacket instance = (ModernPacket) cls.getConstructor(int.class).newInstance(currentid);
PacketHandler.packetlist.add(instance);
PacketHandler.packetmap.put((Class<? extends ModernPacket>) cls, instance);
currentid++;
}
}
use of com.google.common.reflect.ClassPath.ClassInfo 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;
}
use of com.google.common.reflect.ClassPath.ClassInfo in project closure-templates by google.
the class CopyPolicyTest method testCopy.
@Test
public void testCopy() throws IOException {
// We use top level classes to ignore node types defined as inner classes for tests.
ImmutableSet<ClassInfo> topLevelClasses = ClassPath.from(ClassLoader.getSystemClassLoader()).getTopLevelClasses();
Set<String> errors = new LinkedHashSet<>();
for (ClassInfo clazz : topLevelClasses) {
if (clazz.getPackageName().startsWith("com.google.template.soy")) {
Class<?> cls = clazz.load();
if (Node.class.isAssignableFrom(cls)) {
if (cls.isInterface()) {
continue;
}
if (Modifier.isAbstract(cls.getModifiers())) {
checkAbstractNode(cls, errors);
} else {
checkConcreteNode(cls, errors);
}
}
}
}
if (!errors.isEmpty()) {
fail("Copy policy failure:\n" + Joiner.on('\n').join(errors));
}
}
use of com.google.common.reflect.ClassPath.ClassInfo in project runelite by runelite.
the class PluginManagerTest method before.
@Before
public void before() throws IOException {
RuneLite.setOptions(mock(OptionSet.class));
Injector injector = Guice.createInjector(new RuneLiteModule(), BoundFieldModule.of(this));
RuneLite.setInjector(injector);
runelite = injector.getInstance(RuneLite.class);
// Find plugins we expect to have
pluginClasses = new HashSet<>();
Set<ClassInfo> classes = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(PLUGIN_PACKAGE);
for (ClassInfo classInfo : classes) {
Class<?> clazz = classInfo.load();
PluginDescriptor pluginDescriptor = clazz.getAnnotation(PluginDescriptor.class);
if (pluginDescriptor != null) {
pluginClasses.add(clazz);
}
}
}
use of com.google.common.reflect.ClassPath.ClassInfo 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);
}
Aggregations