Search in sources :

Example 11 with ClassPath

use of com.google.common.reflect.ClassPath in project calcite-avatica by apache.

the class TestRunner method getAllTestClasses.

/**
 * Finds all tests to run for the TCK.
 *
 * @return A list of test classes to run.
 */
List<Class<?>> getAllTestClasses() {
    try {
        ClassPath cp = ClassPath.from(getClass().getClassLoader());
        ImmutableSet<ClassInfo> classes = cp.getTopLevelClasses("org.apache.calcite.avatica.tck.tests");
        List<Class<?>> testClasses = new ArrayList<>(classes.size());
        for (ClassInfo classInfo : classes) {
            if (classInfo.getSimpleName().equals("package-info")) {
                continue;
            }
            Class<?> clz = Class.forName(classInfo.getName());
            if (Modifier.isAbstract(clz.getModifiers())) {
                // Ignore abstract classes
                continue;
            }
            testClasses.add(clz);
        }
        return testClasses;
    } catch (Exception e) {
        LOG.error("Failed to instantiate test classes", e);
        Unsafe.systemExit(TestRunnerExitCodes.TEST_CASE_INSTANTIATION.ordinal());
        // Unreachable..
        return null;
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Example 12 with ClassPath

use of com.google.common.reflect.ClassPath in project j2objc by google.

the class ASTClassInfoPrinter method main.

public static void main(String... args) {
    // Clear saved state for new calls.
    tree = TreeMultimap.create();
    astLookup = Sets.newHashSet();
    ClassPath cp = null;
    try {
        cp = ClassPath.from(ClassLoader.getSystemClassLoader());
    } catch (IOException e) {
        ErrorUtil.error(e.getMessage());
        System.exit(1);
    }
    for (ClassInfo c : cp.getTopLevelClasses("org.eclipse.jdt.core.dom")) {
        astLookup.add(c.getSimpleName());
    }
    for (ClassInfo ci : cp.getTopLevelClasses("com.google.devtools.j2objc.ast")) {
        // Ignore package-info and JUnit tests.
        if (ci.getSimpleName().equals("package-info") || TestCase.class.isAssignableFrom(ci.load())) {
            continue;
        }
        walkSuperclassHierarchy(ci.load());
    }
    // Print hierarchy descending from Object.
    printClassHierarchy("Object", "");
}
Also used : ClassPath(com.google.common.reflect.ClassPath) TestCase(junit.framework.TestCase) IOException(java.io.IOException) ClassInfo(com.google.common.reflect.ClassPath.ClassInfo)

Example 13 with ClassPath

use of com.google.common.reflect.ClassPath in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ClassScanner method getClassesForPackage.

public List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
    final List<Class<?>> classes = new ArrayList<>();
    try {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        ClassPath classPath = ClassPath.from(classLoader);
        // Some anonymous classes don't return true when calling isAnonymousClass(), but they
        // always seem to be nested anonymous classes like com.android.settings.Foo$1$2. In
        // general we don't want any anonymous classes so we just filter these out by searching
        // for $[0-9] in the name.
        Pattern anonymousClassPattern = Pattern.compile(".*\\$\\d+.*");
        Matcher anonymousClassMatcher = anonymousClassPattern.matcher("");
        for (ClassPath.ClassInfo info : classPath.getAllClasses()) {
            if (info.getPackageName().startsWith(packageName)) {
                try {
                    Class clazz = classLoader.loadClass(info.getName());
                    if (clazz.isAnonymousClass() || anonymousClassMatcher.reset(clazz.getName()).matches()) {
                        continue;
                    }
                    classes.add(clazz);
                } catch (NoClassDefFoundError e) {
                // do nothing. this class hasn't been found by the
                // loader, and we don't care.
                }
            }
        }
    } catch (final IOException e) {
        throw new ClassNotFoundException("Error when parsing " + packageName, e);
    }
    return classes;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 14 with ClassPath

use of com.google.common.reflect.ClassPath in project alluxio by Alluxio.

the class AllocatorContractTest method before.

/**
 *  Try to find all implementation classes of {@link Allocator} in the same package.
 */
@Before
@Override
public void before() throws Exception {
    super.before();
    mStrategies = new ArrayList<>();
    try {
        String packageName = Reflection.getPackageName(Allocator.class);
        ClassPath path = ClassPath.from(Thread.currentThread().getContextClassLoader());
        List<ClassPath.ClassInfo> clazzInPackage = new ArrayList<>(path.getTopLevelClassesRecursive(packageName));
        for (ClassPath.ClassInfo clazz : clazzInPackage) {
            Set<Class<?>> interfaces = new HashSet<>(Arrays.asList(clazz.load().getInterfaces()));
            if (interfaces.size() > 0 && interfaces.contains(Allocator.class)) {
                mStrategies.add(clazz.getName());
            }
        }
    } catch (Exception e) {
        fail("Failed to find implementation of allocate strategy");
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 15 with ClassPath

use of com.google.common.reflect.ClassPath in project Openfire by igniterealtime.

the class XMPPServer method scanForSystemPropertyClasses.

private void scanForSystemPropertyClasses() {
    try {
        final Set<ClassPath.ClassInfo> classesInPackage = ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive("org.jivesoftware.openfire");
        for (final ClassPath.ClassInfo classInfo : classesInPackage) {
            final String className = classInfo.getName();
            if (!CLASSES_TO_EXCLUDE.contains(className)) {
                try {
                    final Class<?> clazz = classInfo.load();
                    final Field[] fields = clazz.getDeclaredFields();
                    for (final Field field : fields) {
                        if (field.getType().equals(SystemProperty.class)) {
                            try {
                                field.setAccessible(true);
                                logger.info("Accessing SystemProperty field {}#{}", className, field.getName());
                                field.get(null);
                            } catch (final Throwable t) {
                                logger.warn("Unable to access field {}#{}", className, field.getName(), t);
                            }
                        }
                    }
                } catch (final Throwable t) {
                    logger.warn("Unable to load class {}", className, t);
                }
            }
        }
    } catch (final Throwable t) {
        logger.warn("Unable to scan classpath for SystemProperty classes", t);
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Field(java.lang.reflect.Field)

Aggregations

ClassPath (com.google.common.reflect.ClassPath)26 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)8 ClassInfo (com.google.common.reflect.ClassPath.ClassInfo)5 URL (java.net.URL)3 List (java.util.List)3 ReflectionException (io.openems.api.exception.ReflectionException)2 URLClassLoader (java.net.URLClassLoader)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BuckEventListener (com.facebook.buck.event.BuckEventListener)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 Collections2 (com.google.common.collect.Collections2)1