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.jensfendler.ebeanng.ReflectionsHelper.java

static public Set<String> findAllClassesInPackage(String packageName) {
    try {/* w w  w  .  j  av  a2s  .c  o  m*/
        ClassPath cp = ClassPath.from(ReflectionsHelper.class.getClassLoader());
        ImmutableSet<ClassPath.ClassInfo> classes = cp.getTopLevelClasses(packageName);

        Set<String> classNames = new LinkedHashSet<>();

        for (ClassPath.ClassInfo ci : classes) {
            classNames.add(ci.getName());
        }

        return classNames;
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:objenome.util.Packatainer.java

public static Set<ClassPath.ClassInfo> getPackageClasses(String packege) throws Exception {
    //https://code.google.com/p/guava-libraries/wiki/ReflectionExplained#ClassPath
    ClassPath classpath = ClassPath.from(Packatainer.class.getClassLoader());

    return classpath.getTopLevelClasses(packege);
}

From source file:com.digitalpetri.opcua.stack.core.serialization.DelegateRegistry.java

private static void loadGeneratedClasses(ClassLoader classLoader) throws IOException, ClassNotFoundException {
    ClassPath classPath = ClassPath.from(classLoader);

    ImmutableSet<ClassInfo> structures = classPath
            .getTopLevelClasses("com.digitalpetri.opcua.stack.core.types.structured");

    ImmutableSet<ClassInfo> enumerations = classPath
            .getTopLevelClasses("com.digitalpetri.opcua.stack.core.types.enumerated");

    for (ClassInfo classInfo : Sets.union(structures, enumerations)) {
        Class<?> clazz = classInfo.load();
        Class.forName(clazz.getName(), true, classLoader);
    }/*from  ww  w  .  j a  v  a2s.c o m*/
}

From source file:com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtils.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)/*from  ww w.  ja  va  2s  .  c  o  m*/
 */
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(ModuleReflectionUtils::isCheckstyleModule)
            .collect(Collectors.toSet());
}

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)//from w  ww  . j  av  a2  s.co  m
 */
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:org.eclipse.milo.opcua.stack.core.serialization.DelegateRegistry.java

private static void loadGeneratedClasses(ClassLoader classLoader) throws IOException, ClassNotFoundException {
    ClassPath classPath = ClassPath.from(classLoader);

    ImmutableSet<ClassInfo> structures = classPath
            .getTopLevelClasses("org.eclipse.milo.opcua.stack.core.types.structured");

    ImmutableSet<ClassInfo> enumerations = classPath
            .getTopLevelClasses("org.eclipse.milo.opcua.stack.core.types.enumerated");

    for (ClassInfo classInfo : Sets.union(structures, enumerations)) {
        Class<?> clazz = classInfo.load();
        Class.forName(clazz.getName(), true, classLoader);
    }//  w  ww.j a  v  a 2s. co m
}

From source file:org.gbif.registry.ws.resources.EnumerationResource.java

private static Map<String, Enum<?>[]> enumerations() {
    try {//from  w ww  . j  av a  2 s  .co  m
        ClassPath cp = ClassPath.from(EnumerationResource.class.getClassLoader());
        ImmutableMap.Builder<String, Enum<?>[]> builder = ImmutableMap.builder();

        List<ClassInfo> 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.getSimpleName(), 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:org.apache.accumulo.iteratortest.IteratorTestCaseFinder.java

/**
 * Instantiates all test cases provided.
 *
 * @return A list of {@link IteratorTestCase}s.
 *///from   w ww .  j  ava 2s  .c o m
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:org.gbif.api.util.VocabularyUtils.java

/**
 * Utility method to get a map of all enumerations within a package.
 * The map will use the enumeration class simple name as key and the enum itself as value.
 *
 * @return a map of all enumeration within the package or an empty map in all other cases.
 *///from w ww  .j  a  va2 s  . c  o m
public static Map<String, Enum<?>[]> listEnumerations(String packageName) {
    try {
        ClassPath cp = ClassPath.from(VocabularyUtils.class.getClassLoader());
        ImmutableMap.Builder<String, Enum<?>[]> builder = ImmutableMap.builder();

        List<ClassPath.ClassInfo> infos = cp.getTopLevelClasses(packageName).asList();
        for (ClassPath.ClassInfo info : infos) {
            Class<? extends Enum<?>> vocab = lookupVocabulary(info.getName());
            // verify that it is an Enumeration
            if (vocab != null && vocab.getEnumConstants() != null) {
                builder.put(info.getSimpleName(), 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:org.gbif.registry2.ws.resources.EnumerationResource.java

private static Map<String, Enum<?>[]> enumerations() {
    try {//from  w  w  w  .j  av a  2s.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
    }
}