List of usage examples for com.google.common.reflect ClassPath from
public static ClassPath from(ClassLoader classloader) throws IOException
From source file:buildcraftAdditions.compat.ModuleManager.java
public void setupModules() { try {//from ww w . ja va 2 s .com for (ClassPath.ClassInfo info : ClassPath.from(ModuleManager.class.getClassLoader()) .getTopLevelClassesRecursive(ModuleManager.class.getPackage().getName())) if (info.getSimpleName().startsWith("Compat") && !info.getSimpleName().startsWith("CompatModule")) registerModule(info); } catch (IOException e) { throw new RuntimeException("Unknown error while searching for modules!", e); } }
From source file:com.art4ul.loadinstead.bean.ClassMapper.java
private ClassMapper(List<String> packs) throws IOException { classMap = new HashMap<Class<?>, Class<?>>(); for (String pack : packs) { ClassPath classPath = ClassPath.from(getClass().getClassLoader()); Set<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClasses(pack); for (ClassPath.ClassInfo info : classInfos) { processClass(info.load());//from w ww . j ava 2 s. com } } }
From source file:com.rhythm.louie.Classes.java
private static List<Class<?>> findTypesAnnotatedWith(List<String> packages, Class<? extends Annotation> annotation, boolean recursive, List<String> whitelist, List<String> blacklist) throws IOException { ClassPath cp = ClassPath.from(Thread.currentThread().getContextClassLoader()); List<Class<?>> found = new ArrayList<Class<?>>(); Set<ClassInfo> infos = new HashSet<ClassInfo>(); if (packages == null || packages.isEmpty()) { infos = cp.getTopLevelClasses(); } else if (recursive) { for (String pkg : packages) { infos.addAll(cp.getTopLevelClassesRecursive(pkg)); }//from w w w. j a v a 2 s . c o m } else { for (String pkg : packages) { infos.addAll(cp.getTopLevelClasses(pkg)); } } for (ClassInfo info : infos) { if (blacklist != null && blacklist.contains(info.getPackageName())) { if (whitelist != null) { if (!whitelist.contains(info.getName())) { continue; //blacklisted, but not whitelisted } } else { continue; } } try { Class<?> cl = info.load(); Annotation ann = cl.getAnnotation(annotation); if (ann != null) { found.add(cl); } } catch (Exception e) { LoggerFactory.getLogger(Classes.class).warn("Error Checking Class: " + info.getName(), e); } } return found; }
From source file:net.lm.seriesfreak.database.implementation.TypeLoader.java
public <T> List<Class<? extends T>> getClasses(Class<T> superType) throws IOException { classPath = ClassPath.from(this.getClass().getClassLoader()); ImmutableSet<ClassPath.ClassInfo> classInfos = classPath.getTopLevelClasses(packageName); List<Class<? extends T>> classes = new ArrayList<>(); for (ClassPath.ClassInfo classInfo : classInfos) { if (classInfo.load().isAnnotationPresent(annotationType)) { try { classes.add(classInfo.load().asSubclass(superType)); } catch (ClassCastException ex) { //Checks that the class is castable before casring it. }/*from w ww . ja va 2 s. c o m*/ } } return classes; }
From source file:br.com.blackhubos.eventozero.hook.Hooks.java
@SuppressWarnings("unchecked") public static void hookAll() { try {// www. j a v a 2 s.c o m ImmutableSet<ClassInfo> classes = ClassPath.from(classLoader) .getTopLevelClasses("br.com.blackhubos.eventozero.hook.hooks"); classes.forEach(cls -> { try { Class<?> hookClass = Class.forName(cls.getName()); if (!Hook.class.isAssignableFrom(hookClass)) { return; } Hook hookInst = (Hook) hookClass.newInstance(); if (hookInst.canBeHooked()) { hookInst.hook(); hooks.put((Class<Hook>) hookClass, hookInst); logger.info(String.format("[Hooks] %s carregado com sucesso.", cls.getSimpleName())); } } catch (Exception e) { logger.log(Level.SEVERE, "Could not load hook " + cls.getName()); logger.log(Level.SEVERE, "See the stacktrace: "); e.printStackTrace(); } }); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.gbif.registry2.ws.resources.EnumerationResource.java
private static Map<String, Enum<?>[]> enumerations() { try {// w w w. j a v a 2 s. co m ClassPath cp = ClassPath.from(EnumerationResource.class.getClassLoader()); ImmutableMap.Builder<String, Enum<?>[]> builder = ImmutableMap.builder(); // TODO: When registry2 moves to proper package, this will throw compile time error, and this should be removed List<ClassInfo> infos = cp.getTopLevelClasses(ParticipationStatus.class.getPackage().getName()) .asList(); for (ClassInfo info : infos) { Class<? extends Enum<?>> vocab = VocabularyUtils.lookupVocabulary(info.getName()); // verify that it is an Enumeration if (vocab != null && vocab.getEnumConstants() != null) { builder.put(info.getName(), vocab.getEnumConstants()); } } // END TODO (delete this whole block, and verify result with http://localhost:8080/enumeration ) infos = cp.getTopLevelClasses(Country.class.getPackage().getName()).asList(); for (ClassInfo info : infos) { Class<? extends Enum<?>> vocab = VocabularyUtils.lookupVocabulary(info.getName()); // verify that it is an Enumeration if (vocab != null && vocab.getEnumConstants() != null) { builder.put(info.getName(), vocab.getEnumConstants()); } } return builder.build(); } catch (Exception e) { LOG.error("Unable to read the classpath for enumerations", e); return ImmutableMap.<String, Enum<?>[]>of(); // empty } }
From source file:io.nosorog.core.internal.Importer.java
public Importer(Collection<ImportDeclaration> nodes, ClassLoader classLoader) throws IOException { classpath = ClassPath.from(classLoader); this.nodes = nodes; }
From source file:l2server.util.ClassPathUtil.java
public static List<Method> getAllMethodsAnnotatedWith(Class<? extends Annotation> annotationClass) throws IOException { final ClassPath classPath = ClassPath.from(ClassLoader.getSystemClassLoader()); //@formatter:off return classPath.getTopLevelClasses().stream().map(ClassInfo::load) .flatMap(clazz -> Arrays.stream(clazz.getDeclaredMethods())) .filter(method -> method.isAnnotationPresent(annotationClass)).collect(Collectors.toList()); //@formatter:on }
From source file:com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil.java
/** * Gets checkstyle's modules (directly, not recursively) in the given packages. * @param packages the collection of package names to use * @param loader the class loader used to load Checkstyle package names * @return the set of checkstyle's module classes * @throws IOException if the attempt to read class path resources failed * @see #isCheckstyleModule(Class)/* w w w .j av a 2 s . com*/ */ public static Set<Class<?>> getCheckstyleModules(Collection<String> packages, ClassLoader loader) throws IOException { final ClassPath classPath = ClassPath.from(loader); return packages.stream().flatMap(pkg -> classPath.getTopLevelClasses(pkg).stream()) .map(ClassPath.ClassInfo::load).filter(ModuleReflectionUtil::isCheckstyleModule) .collect(Collectors.toSet()); }
From source file:de.andreasgiemza.mangadownloader.sites.SiteHelper.java
public static Site getInstance(String source) { final ClassLoader loader = Thread.currentThread().getContextClassLoader(); try {//from w w w . j ava2 s . c o m for (final ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses()) { if (info.getName().startsWith(implementationsPackage)) { final Class<?> clazz = info.load(); if (clazz.getSimpleName().equals(source)) { try { return (Site) clazz.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { } } } } } catch (IOException ex) { } return null; }