Search in sources :

Example 1 with ClassGraph

use of io.github.classgraph.ClassGraph in project symja_android_library by axkr.

the class Table method autoRegisterReadersAndWriters.

private static void autoRegisterReadersAndWriters() {
    try (ScanResult scanResult = new ClassGraph().enableAllInfo().whitelistPackages("tech.tablesaw.io").scan()) {
        List<String> classes = new ArrayList<>();
        classes.addAll(scanResult.getClassesImplementing(DataWriter.class.getName()).getNames());
        classes.addAll(scanResult.getClassesImplementing(DataReader.class.getName()).getNames());
        for (String clazz : classes) {
            try {
                Class.forName(clazz);
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException(e);
            }
        }
    }
}
Also used : ScanResult(io.github.classgraph.ScanResult) DataReader(tech.tablesaw.io.DataReader) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) DataWriter(tech.tablesaw.io.DataWriter)

Example 2 with ClassGraph

use of io.github.classgraph.ClassGraph in project beam by apache.

the class ClasspathScanningResourcesDetectorTest method shouldNotDetectClassPathResourceThatIsNotAFile.

@Test
public void shouldNotDetectClassPathResourceThatIsNotAFile() throws Exception {
    String url = "http://www.google.com/all-the-secrets.jar";
    ClassLoader classLoader = new URLClassLoader(new URL[] { new URL(url) });
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> result = detector.detect(classLoader);
    assertThat(result, not(hasItem(containsString(url))));
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URL(java.net.URL) Test(org.junit.Test)

Example 3 with ClassGraph

use of io.github.classgraph.ClassGraph in project beam by apache.

the class ClasspathScanningResourcesDetectorTest method shouldDetectClassPathResourceFromJavaClassPathEnvVariable.

@Test
public void shouldDetectClassPathResourceFromJavaClassPathEnvVariable() throws IOException {
    String path = tmpFolder.newFolder("folder").getAbsolutePath();
    System.setProperty("java.class.path", path);
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> resources = detector.detect(null);
    assertThat(resources, hasItems(containsString(path)));
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with ClassGraph

use of io.github.classgraph.ClassGraph in project beam by apache.

the class ClasspathScanningResourcesDetectorTest method shouldDetectDirectories.

@Test
public void shouldDetectDirectories() throws Exception {
    File folder = tmpFolder.newFolder("folder1");
    ClassLoader classLoader = new URLClassLoader(new URL[] { folder.toURI().toURL() });
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> result = detector.detect(classLoader);
    assertThat(result, hasItem(containsString(folder.getAbsolutePath())));
}
Also used : URLClassLoader(java.net.URLClassLoader) ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 5 with ClassGraph

use of io.github.classgraph.ClassGraph in project hazelcast by hazelcast.

the class ReflectionUtils method nestedClassesOf.

@Nonnull
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive on try-with-resources as of JDK11")
public static Collection<Class<?>> nestedClassesOf(Class<?>... classes) {
    ClassGraph classGraph = new ClassGraph().enableClassInfo().ignoreClassVisibility();
    stream(classes).map(Class::getClassLoader).distinct().forEach(classGraph::addClassLoader);
    stream(classes).map(ReflectionUtils::toPackageName).distinct().forEach(classGraph::whitelistPackages);
    try (ScanResult scanResult = classGraph.scan()) {
        Set<String> classNames = stream(classes).map(Class::getName).collect(toSet());
        return concat(stream(classes), scanResult.getAllClasses().stream().filter(classInfo -> classNames.contains(classInfo.getName())).flatMap(classInfo -> classInfo.getInnerClasses().stream()).map(ClassInfo::loadClass)).collect(toList());
    }
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) Character.toUpperCase(java.lang.Character.toUpperCase) ClassInfo(io.github.classgraph.ClassInfo) ClassLoaderUtil(com.hazelcast.internal.nio.ClassLoaderUtil) URL(java.net.URL) Collection(java.util.Collection) Set(java.util.Set) Field(java.lang.reflect.Field) Objects(java.util.Objects) ExceptionUtil.sneakyThrow(com.hazelcast.internal.util.ExceptionUtil.sneakyThrow) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Stream.concat(java.util.stream.Stream.concat) ScanResult(io.github.classgraph.ScanResult) Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) Arrays.stream(java.util.Arrays.stream) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) ClassInfo(io.github.classgraph.ClassInfo) Nonnull(javax.annotation.Nonnull) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ClassGraph (io.github.classgraph.ClassGraph)18 Test (org.junit.Test)8 ScanResult (io.github.classgraph.ScanResult)7 URLClassLoader (java.net.URLClassLoader)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 File (java.io.File)4 URL (java.net.URL)4 ClassInfo (io.github.classgraph.ClassInfo)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Nonnull (javax.annotation.Nonnull)2 ClassLoaderUtil (com.hazelcast.internal.nio.ClassLoaderUtil)1 ExceptionUtil.sneakyThrow (com.hazelcast.internal.util.ExceptionUtil.sneakyThrow)1 DefaultQueryMetadata (com.querydsl.core.DefaultQueryMetadata)1 GroupExpression (com.querydsl.core.group.GroupExpression)1 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)1 Character.toUpperCase (java.lang.Character.toUpperCase)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1