List of usage examples for com.google.common.reflect ClassPath from
public static ClassPath from(ClassLoader classloader) throws IOException
From source file:io.flood.rpc.registry.ResourceRegistry.java
private synchronized static Registry getRegistry() { final ClassLoader loader = Thread.currentThread().getContextClassLoader(); try {//from w w w. j a v a 2s . co m ImmutableSet<ClassPath.ClassInfo> classes = ClassPath.from(loader).getTopLevelClasses("io.flood"); Optional<ClassPath.ClassInfo> registryClassInfoOp = classes.stream().filter(classInfo -> { if (classInfo.getClass().getAnnotation(RegistryAction.class) != null) { return true; } return false; }).sorted(((cla, clb) -> { RegistryAction aa = cla.getClass().getAnnotation(RegistryAction.class); RegistryAction ab = clb.getClass().getAnnotation(RegistryAction.class); return aa.order() - ab.order(); })).findFirst(); ClassPath.ClassInfo registryClassInfo = registryClassInfoOp.orElseThrow(Exception::new); RegistryAction action = registryClassInfo.getClass().getAnnotation(RegistryAction.class); Registry registry = (Registry) Class.forName(registryClassInfo.getName()).<Registry>newInstance(); Configuration.configObject(registry); LOG.info("instance {} registry", action.value()); return registry; } catch (Exception e) { LOG.error(""); System.exit(-1); } return null; }
From source file:rocnikovyprojekt.Conversions.java
public Conversions() { System.out.println("Initializing Conversions..."); ArrayList<Class<? extends Conversion>> list = new ArrayList<>(); try {//from w ww . j ava 2 s. c o m ClassPath classpath = ClassPath.from(Thread.currentThread().getContextClassLoader()); /*Naplnenie list-u triedami implementujucimi Conversion*/ for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive("rocnikovyprojekt")) { Class<?> c = Class.forName(classInfo.getName()); if (Conversion.class.isAssignableFrom(c) && !c.equals(Conversion.class)) { System.out.println("Found " + classInfo.getName()); list.add((Class<? extends Conversion>) c); } } /*Naplnenie grafu*/ graph = new Graph(); for (Class<? extends Conversion> c : list) { Conversion conv = c.newInstance(); graph.addEdge(conv); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Initialization complete."); }
From source file:com.facebook.buck.tools.documentation.generator.skylark.SignatureCollector.java
/** * Returns a stream of Skylark function signatures (identified by {@link SkylarkCallable} * annotation found in the current classpath. * * @param classInfoPredicate predicate to use in order to filter out classes that should not be * loaded. It's best to make it as precise as possible to avoid expensive loading - checking * for class name and package is ideal. */// w w w . j a v a2 s . c o m public static Stream<SkylarkCallable> getSkylarkCallables(Predicate<ClassInfo> classInfoPredicate) throws IOException { return ClassPath.from(ClassPath.class.getClassLoader()).getAllClasses().stream().filter(classInfoPredicate) .map(ClassInfo::load).flatMap(clazz -> Arrays.stream(clazz.getMethods())) .map(field -> field.getAnnotation(SkylarkCallable.class)).filter(Objects::nonNull); }
From source file:org.apache.beam.sdk.util.ApiSurface.java
/** * Returns an {@link ApiSurface} like this one, but also including the named package and all of * its subpackages.//from ww w. j av a2 s.co m */ public ApiSurface includingPackage(String packageName, ClassLoader classLoader) throws IOException { ClassPath classPath = ClassPath.from(classLoader); Set<Class<?>> newRootClasses = Sets.newHashSet(); for (ClassPath.ClassInfo classInfo : classPath.getTopLevelClassesRecursive(packageName)) { Class clazz = null; try { clazz = classInfo.load(); } catch (NoClassDefFoundError e) { // TODO: Ignore any NoClassDefFoundError errors as a workaround. (BEAM-2231) LOG.warn("Failed to load class: {}", classInfo.toString(), e); continue; } if (exposed(clazz.getModifiers())) { newRootClasses.add(clazz); } } LOG.debug("Including package {} and subpackages: {}", packageName, newRootClasses); newRootClasses.addAll(rootClasses); return new ApiSurface(newRootClasses, patternsToPrune); }
From source file:com.bj58.oceanus.exchange.nodes.Analyzers.java
@SuppressWarnings("unchecked") private static void initAnalizers() { ImmutableSet<ClassInfo> classInfos = null; try {//from w ww .j a v a2s . co m classInfos = ClassPath.from(Analyzers.class.getClassLoader()) .getTopLevelClassesRecursive(Analyzers.class.getPackage().getName()); for (ClassInfo classInfo : classInfos) { Class<?> claz = classInfo.load(); try { if (NodeAnalyzer.class.isAssignableFrom(claz) && !claz.isInterface() && !Modifier.isAbstract(claz.getModifiers())) { register((NodeAnalyzer<QueryTreeNode, AnalyzeResult>) claz.newInstance()); } } catch (Exception e) { // TODO LOG Auto-generated catch block e.printStackTrace(); System.out.println(claz); } } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:org.apache.accumulo.iteratortest.IteratorTestCaseFinder.java
/** * Instantiates all test cases provided. * * @return A list of {@link IteratorTestCase}s. *//*from ww w. j a v a 2s. com*/ 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; }
From source file:de.mineformers.gui.api.loader.UILoader.java
public static void loadDefaultAliases() { if (aliases == null) aliases = new HashMap<String, Class<? extends UIComponent>>(); if (aliases.isEmpty()) try {//from w w w . j a v a 2 s . c o m String currentPackage = Reflection.getPackageName(UILoader.class); ImmutableSet<ClassPath.ClassInfo> classes = ClassPath.from(UILoader.class.getClassLoader()) .getTopLevelClassesRecursive( currentPackage.substring(0, currentPackage.lastIndexOf(".")) + ".component"); for (ClassPath.ClassInfo clazz : classes) { if (!clazz.getName().endsWith("UIComponent")) { addAlias(clazz.getSimpleName(), (Class<? extends UIComponent>) Class.forName(clazz.getName())); } } } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }
From source file:de.andreasgiemza.mangadownloader.sites.SiteHelper.java
public static List<Site> getSites() { List<Site> sites = new LinkedList<>(); try {// w w w. ja v a 2 s . c om final ClassLoader loader = Thread.currentThread().getContextClassLoader(); for (final ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses()) { if (info.getName().startsWith(implementationsPackage)) { sites.add((Site) info.load().newInstance()); } } } catch (InstantiationException | IllegalAccessException | IOException ex) { } Collections.sort(sites, new Comparator<Site>() { @Override public int compare(Site site1, Site site2) { return site1.getName().compareTo(site2.getName()); } }); return sites; }
From source file:l2server.util.ClassPathUtil.java
public static <T> List<Class<T>> getAllClassesExtending(String packageName, Class<T> targetClass) throws IOException { final ClassPath classPath = ClassPath.from(ClassLoader.getSystemClassLoader()); //@formatter:off return classPath.getTopLevelClassesRecursive(packageName).stream().map(ClassInfo::load) .filter(clazz -> targetClass.isAssignableFrom(clazz)) .filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())) .filter(clazz -> !Modifier.isInterface(clazz.getModifiers())).map(clazz -> (Class<T>) clazz) .collect(Collectors.toList()); //@formatter:on }
From source file:org.apache.ambari.server.cleanup.ClasspathScannerUtils.java
/** * Scans the classpath for classes based on the provided arguments * * @param packageName the package to be scanned * @param exclusions a list with classes excluded from the result * @param selectors a list with annotation and interface classes that identify classes to be found (lookup criteria) * @return a list of classes from the classpath that match the lookup criteria *//*from w w w .j a v a 2 s . c om*/ public static Set<Class> findOnClassPath(String packageName, List<Class> exclusions, List<Class> selectors) { Set<Class> bindingSet = new HashSet<>(); try { ClassPath classpath = ClassPath.from(ClasspathScannerUtils.class.getClassLoader()); LOGGER.info("Checking package [{}] for binding candidates.", packageName); for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(packageName)) { Class candidate = classInfo.load(); if (exclusions.contains(candidate)) { LOGGER.debug("Candidate [{}] is excluded excluded.", candidate); continue; } if (isEligible(candidate, selectors)) { LOGGER.info("Found class [{}]", candidate); bindingSet.add(candidate); } else { LOGGER.debug("Candidate [{}] doesn't match.", candidate); } } } catch (IOException e) { LOGGER.error("Failure during configuring JUICE bindings.", e); throw new IllegalArgumentException(e); } return bindingSet; }