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(String packageName) 

Source Link

Document

Returns all top level classes whose package name is packageName .

Usage

From source file:com.analog.lyric.collect.ConstructorRegistry.java

/**
 * Preloads all subclasses of declared superclass {@code T} found in registry's packages.
 * <p>//from w w w  .jav  a 2 s .  co m
 * Searches all of the packages in {@link #getPackages()} for subclasses of {@code T} and adds
 * then to the registry.
 * <p>
 * 
 * @since 0.07
 */
public synchronized void loadAll() {
    ClassLoader loader = getClass().getClassLoader();

    ClassPath path;
    try {
        path = ClassPath.from(loader);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    for (String packageName : _packages) {
        for (ClassPath.ClassInfo info : path.getTopLevelClasses(packageName)) {
            addConstructorsFrom(info.load(), true);
        }
    }
}

From source file:org.eclipse.m2e.editor.xml.mojo.PlexusConfigHelper.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Class> getCandidateClasses(ClassRealm realm, Class enclosingClass, Class paramClass) {

    String name = enclosingClass.getName();
    int dot = name.lastIndexOf('.');
    if (dot > 0) {
        String pkg = name.substring(0, dot);

        List<Class> candidateClasses = null;

        ClassPath cp;
        try {/*  w  w  w  .j a  v a 2 s .c om*/
            cp = ClassPath.from(realm);
        } catch (IOException e) {
            log.error(e.getMessage());
            return Collections.singletonList(enclosingClass);
        }

        for (ClassInfo ci : cp.getTopLevelClasses(pkg)) {
            Class clazz;
            try {
                clazz = realm.loadClass(ci.getName());
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
                continue;
            }

            if ((clazz.getModifiers() & (Modifier.ABSTRACT)) != 0) {
                continue;
            }

            if (!paramClass.isAssignableFrom(clazz)) {
                continue;
            }

            // skip classes without no-arg constructors
            try {
                clazz.getConstructor(new Class[0]);
            } catch (NoSuchMethodException ex) {
                continue;
            }

            if (candidateClasses == null) {
                candidateClasses = new ArrayList<Class>();
            }
            candidateClasses.add(clazz);

        }

        if (candidateClasses != null) {
            return candidateClasses;
        }
    }

    return Collections.singletonList(paramClass);
}