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

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

Introduction

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

Prototype

public ImmutableSet<ClassInfo> getAllClasses() 

Source Link

Document

Returns all classes loadable from the current class path.

Usage

From source file:com.vmware.photon.controller.common.xenon.migration.MigrationUtils.java

/**
 * This method searches the class path to identify each class definition that extends {@link ServiceDocument}
 * that is part of the Photon Controller code base.
 * It selects all {@link ServiceDocument} with the {@link MigrateDuringDeployment} annotation and record the necessary
 * upgrade information./*from w  ww.  j av  a  2  s.  c  o  m*/
 *
 * @return list of {@link DeploymentMigrationInformation} objects describing each service document that needs to be
 * migrated during deployment.
 */
@SuppressWarnings("unchecked")
public static List<DeploymentMigrationInformation> findAllMigrationServices() {
    List<DeploymentMigrationInformation> infoEntries = new ArrayList<>();
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    ClassPath classPath;
    try {
        classPath = ClassPath.from(cl);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    for (ClassInfo classFile : classPath.getAllClasses()) {
        if (classFile.getName().contains(PHOTON_CONTROLLER_PACKAGE)) {
            Class<?> type = classFile.load();
            if (type.getSuperclass() != null && type.getSuperclass() == ServiceDocument.class) {
                for (Annotation a : type.getAnnotations()) {
                    if (a.annotationType() == MigrateDuringDeployment.class) {
                        MigrateDuringDeployment u = (MigrateDuringDeployment) a;

                        DeploymentMigrationInformation info = new DeploymentMigrationInformation(
                                u.factoryServicePath(), u.serviceName(),
                                (Class<? extends ServiceDocument>) type);

                        infoEntries.add(info);
                    }
                }
            }
        }
    }
    return infoEntries;
}

From source file:com.vmware.photon.controller.common.xenon.migration.MigrationUtils.java

/**
 * This method searches the class path to identify each class definition that extends {@link ServiceDocument}
 * that is part of the Photon Controller code base.
 * It selects all {@link ServiceDocument} with the {@link MigrateDuringUpgrade} annotation and record the necessary
 * upgrade information./*from   w  w w  .j a v a  2  s .  co m*/
 *
 * @return list of {@link UpgradeInfromation} objects describing each service document that needs to be migrated
 * during upgrade.
 */
@SuppressWarnings("unchecked")
public static List<UpgradeInformation> findAllUpgradeServices() {
    if (cachedList != null) {
        return cachedList;
    }
    List<UpgradeInformation> infoEntries = new ArrayList<>();
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    ClassPath classPath;
    try {
        classPath = ClassPath.from(cl);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    for (ClassInfo classFile : classPath.getAllClasses()) {
        if (classFile.getName().contains(PHOTON_CONTROLLER_PACKAGE)) {
            Class<?> type = classFile.load();
            if (type.getSuperclass() != null && type.getSuperclass() == ServiceDocument.class) {
                for (Annotation a : type.getAnnotations()) {
                    if (a.annotationType() == MigrateDuringUpgrade.class) {
                        MigrateDuringUpgrade u = (MigrateDuringUpgrade) a;

                        UpgradeInformation info = new UpgradeInformation(u.sourceFactoryServicePath(),
                                u.destinationFactoryServicePath(), u.serviceName(),
                                u.transformationServicePath(), (Class<? extends ServiceDocument>) type);

                        infoEntries.add(info);
                    }
                }
            }
        }
    }
    cachedList = infoEntries;
    return infoEntries;
}

From source file:com.itametis.jsonconverter.classpathscan.JsonMappingPackage.java

/**
 * Get all classes from class path depending on the declaration of a specific classpath.
 *
 * @param classpath the class path to investigate.
 *
 * @return the classes.//from   w ww  . j  ava2 s .  co m
 */
private ImmutableSet<ClassInfo> getClassesFromClassPath(ClassPath classpath) {
    if (this.packageToScan == null || this.packageToScan.isEmpty()) {
        return classpath.getAllClasses();
    } else {
        return classpath.getTopLevelClassesRecursive(this.packageToScan);
    }
}

From source file:com.softwarewerke.htdbc.App.java

@Override
public Set<Class<?>> getClasses() {
    Logger log = Logger.getLogger(getClass());

    Set set = new HashSet();

    ClassPath cp;
    try {// w w  w  .  java 2s .com
        cp = ClassPath.from(getClass().getClassLoader());
    } catch (IOException ex) {
        log.fatal(ex);
        return set;
    }

    String p = getClass().getPackage().getName();

    for (ClassPath.ClassInfo n : cp.getAllClasses()) {
        if (!n.getPackageName().startsWith(p)) {
            continue;
        }
        try {
            Class cl = n.load();

            if (cl.getAnnotation(Path.class) == null) {
                continue;
            }
            if (cl.getAnnotation(Singleton.class) != null) {
                continue;
            }
            if (!Main.isDevelopment() && cl.getAnnotation(IsDev.class) != null) {
                continue;
            }
            set.add(cl);
        } catch (ClassFormatError ignore) {
        } catch (Exception ex) {
            log.error(ex);
        }
    }

    return set;
}

From source file:com.softwarewerke.htdbc.App.java

@Override
public Set getSingletons() {

    Logger log = Logger.getLogger(getClass());

    Set set = new HashSet();

    ClassPath cp;
    try {/*from   w w w  .j a  v a 2s. c om*/
        cp = ClassPath.from(getClass().getClassLoader());
    } catch (IOException ex) {
        log.fatal(ex);
        return set;
    }
    String p = getClass().getPackage().getName();

    for (ClassPath.ClassInfo n : cp.getAllClasses()) {
        if (!n.getPackageName().startsWith(p)) {
            continue;
        }
        try {
            Class cl = n.load();

            if (cl.getAnnotation(Path.class) == null) {
                continue;
            }
            if (cl.getAnnotation(Singleton.class) == null) {
                continue;
            }
            if (!Main.isDevelopment() && cl.getAnnotation(IsDev.class) != null) {
                continue;
            }
            set.add(cl.newInstance());
        } catch (ClassFormatError ignore) {
        } catch (Exception ex) {
            log.error(ex);
        }
    }

    set.add(new AppException());

    return set;
}

From source file:net.revelc.code.apilyzer.maven.plugin.AnalyzeMojo.java

private void buildPublicSet(ClassPath classPath, List<Class<?>> publicApiClasses, TreeSet<String> publicSet) {
    for (ClassInfo classInfo : classPath.getAllClasses()) {
        // TODO handle empty includes case; maybe?
        for (String includePattern : includes) {
            if (classInfo.getName().matches(includePattern)) {
                boolean exclude = false;
                for (String excludePattern : excludes) {
                    if (classInfo.getName().matches(excludePattern)) {
                        exclude = true;/*  ww w  .  ja  v a  2 s.  c om*/
                        break;
                    }
                }
                if (!exclude) {
                    Class<?> clazz = classInfo.load();
                    if (isPublicOrProtected(clazz)) {
                        publicApiClasses.add(clazz);
                        publicSet.add(clazz.getName());
                    }
                }
                break;
            }
        }
    }
}