Example usage for com.google.common.reflect ClassPath getTopLevelClasses

List of usage examples for com.google.common.reflect ClassPath getTopLevelClasses

Introduction

In this page you can find the example usage for com.google.common.reflect ClassPath getTopLevelClasses.

Prototype

public ImmutableSet<ClassInfo> getTopLevelClasses() 

Source Link

Document

Returns all top level classes loadable from the current class path.

Usage

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:l2server.util.ClassPathUtil.java

public static <T> List<Class<T>> getAllClassesExtending(Class<T> targetClass) throws IOException {
    final ClassPath classPath = ClassPath.from(ClassLoader.getSystemClassLoader());
    //@formatter:off
    return classPath.getTopLevelClasses().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: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   ww  w .  j  ava  2s . com*/
    } 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:com.facebook.buck.cli.Main.java

private void loadListenersFromBuckConfig(ImmutableList.Builder<BuckEventListener> eventListeners,
        ProjectFilesystem projectFilesystem, BuckConfig config) {
    final ImmutableSet<String> paths = config.getListenerJars();
    if (paths.isEmpty()) {
        return;//from  w  w  w.ja v a 2s.  c  o m
    }

    URL[] urlsArray = new URL[paths.size()];
    try {
        int i = 0;
        for (String path : paths) {
            String urlString = "file://" + projectFilesystem.getAbsolutifier().apply(Paths.get(path));
            urlsArray[i] = new URL(urlString);
            i++;
        }
    } catch (MalformedURLException e) {
        throw new HumanReadableException(e.getMessage());
    }

    // This ClassLoader is disconnected to allow searching the JARs (and just the JARs) for classes.
    ClassLoader isolatedClassLoader = URLClassLoader.newInstance(urlsArray, null);

    ImmutableSet<ClassPath.ClassInfo> classInfos;
    try {
        ClassPath classPath = ClassPath.from(isolatedClassLoader);
        classInfos = classPath.getTopLevelClasses();
    } catch (IOException e) {
        throw new HumanReadableException(e.getMessage());
    }

    // This ClassLoader will actually work, because it is joined to the parent ClassLoader.
    URLClassLoader workingClassLoader = URLClassLoader.newInstance(urlsArray);

    for (ClassPath.ClassInfo classInfo : classInfos) {
        String className = classInfo.getName();
        try {
            Class<?> aClass = Class.forName(className, true, workingClassLoader);
            if (BuckEventListener.class.isAssignableFrom(aClass)) {
                BuckEventListener listener = aClass.asSubclass(BuckEventListener.class).newInstance();
                eventListeners.add(listener);
            }
        } catch (ReflectiveOperationException e) {
            throw new HumanReadableException("Error loading event listener class '%s': %s: %s", className,
                    e.getClass(), e.getMessage());
        }
    }
}