Search in sources :

Example 16 with ClassGraph

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

the class ClasspathScanningResourcesDetectorTest method shouldStillDetectResourcesEvenIfClassloaderIsUseless.

/*
   * ClassGraph library that is used in the tested algorithm can still detect resources from
   * "java.class.path" env variable. Even in case the classloader that is passed is of no use we
   * will still be able to detect and load resource paths from the env variable.
   */
@Test
public void shouldStillDetectResourcesEvenIfClassloaderIsUseless() {
    ClassLoader uselessClassLoader = Mockito.mock(ClassLoader.class);
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> detectedResources = detector.detect(uselessClassLoader);
    assertFalse(detectedResources.isEmpty());
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) URLClassLoader(java.net.URLClassLoader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 17 with ClassGraph

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

the class ClasspathScanningResourcesDetectorTest method shouldDetectJarFiles.

@Test
public void shouldDetectJarFiles() throws Exception {
    File jarFile = createTestTmpJarFile("test");
    ClassLoader classLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() });
    ClasspathScanningResourcesDetector detector = new ClasspathScanningResourcesDetector(new ClassGraph());
    List<String> result = detector.detect(classLoader);
    assertThat(result, hasItem(containsString(jarFile.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 18 with ClassGraph

use of io.github.classgraph.ClassGraph in project dhis2-core by dhis2.

the class PreheatStrategyScanner method scanSupplierStrategies.

public Map<String, String> scanSupplierStrategies() {
    Map<String, String> classMap = new HashMap<>();
    final String pkg = getCurrentPackage();
    final String annotation = StrategyFor.class.getName();
    try (ScanResult scanResult = new ClassGraph().enableClassInfo().acceptPackages(pkg).enableAnnotationInfo().scan()) {
        for (ClassInfo classInfo : scanResult.getClassesWithAnnotation(annotation)) {
            classMap.put(getTargetClass(classInfo, annotation), classInfo.getSimpleName());
        }
    }
    return classMap;
}
Also used : ScanResult(io.github.classgraph.ScanResult) HashMap(java.util.HashMap) ClassGraph(io.github.classgraph.ClassGraph) ClassInfo(io.github.classgraph.ClassInfo)

Example 19 with ClassGraph

use of io.github.classgraph.ClassGraph in project swagger-core by swagger-api.

the class GenericOpenApiScanner method classes.

@Override
public Set<Class<?>> classes() {
    ClassGraph graph = new ClassGraph().enableAllInfo();
    Set<String> acceptablePackages = new HashSet<>();
    Set<Class<?>> output = new HashSet<>();
    boolean allowAllPackages = false;
    // if classes are passed, use them
    if (openApiConfiguration.getResourceClasses() != null && !openApiConfiguration.getResourceClasses().isEmpty()) {
        for (String className : openApiConfiguration.getResourceClasses()) {
            if (!isIgnored(className)) {
                try {
                    output.add(Class.forName(className));
                } catch (ClassNotFoundException e) {
                    LOGGER.warn("error loading class from resourceClasses: " + e.getMessage(), e);
                }
            }
        }
        return output;
    }
    if (openApiConfiguration.getResourcePackages() != null && !openApiConfiguration.getResourcePackages().isEmpty()) {
        for (String pkg : openApiConfiguration.getResourcePackages()) {
            if (!isIgnored(pkg)) {
                acceptablePackages.add(pkg);
                graph.whitelistPackages(pkg);
            }
        }
    } else {
        allowAllPackages = true;
    }
    // this is generic, specific Jaxrs scanner will also look for @Path
    final Set<Class<?>> classes;
    try (ScanResult scanResult = graph.scan()) {
        classes = new HashSet<>(scanResult.getClassesWithAnnotation(OpenAPIDefinition.class.getName()).loadClasses());
    }
    for (Class<?> cls : classes) {
        if (allowAllPackages) {
            output.add(cls);
        } else {
            for (String pkg : acceptablePackages) {
                if (cls.getPackage().getName().startsWith(pkg)) {
                    output.add(cls);
                }
            }
        }
    }
    return output;
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) HashSet(java.util.HashSet)

Example 20 with ClassGraph

use of io.github.classgraph.ClassGraph in project swagger-core by swagger-api.

the class JaxrsAnnotationScanner method classes.

@Override
public Set<Class<?>> classes() {
    if (openApiConfiguration == null) {
        openApiConfiguration = new SwaggerConfiguration();
    }
    ClassGraph graph = new ClassGraph().enableAllInfo();
    Set<String> acceptablePackages = new HashSet<>();
    Set<Class<?>> output = new HashSet<>();
    // if classes are passed, use them
    if (openApiConfiguration.getResourceClasses() != null && !openApiConfiguration.getResourceClasses().isEmpty()) {
        for (String className : openApiConfiguration.getResourceClasses()) {
            if (!isIgnored(className)) {
                try {
                    output.add(Class.forName(className));
                } catch (ClassNotFoundException e) {
                    LOGGER.warn("error loading class from resourceClasses: " + e.getMessage(), e);
                }
            }
        }
        return output;
    }
    boolean allowAllPackages = false;
    if (openApiConfiguration.getResourcePackages() != null && !openApiConfiguration.getResourcePackages().isEmpty()) {
        for (String pkg : openApiConfiguration.getResourcePackages()) {
            if (!isIgnored(pkg)) {
                acceptablePackages.add(pkg);
                graph.whitelistPackages(pkg);
            }
        }
    } else {
        if (!onlyConsiderResourcePackages) {
            allowAllPackages = true;
        }
    }
    final Set<Class<?>> classes;
    try (ScanResult scanResult = graph.scan()) {
        classes = new HashSet<>(scanResult.getClassesWithAnnotation(javax.ws.rs.Path.class.getName()).loadClasses());
        classes.addAll(new HashSet<>(scanResult.getClassesWithAnnotation(OpenAPIDefinition.class.getName()).loadClasses()));
        if (Boolean.TRUE.equals(openApiConfiguration.isAlwaysResolveAppPath())) {
            classes.addAll(new HashSet<>(scanResult.getClassesWithAnnotation(ApplicationPath.class.getName()).loadClasses()));
        }
    }
    for (Class<?> cls : classes) {
        if (allowAllPackages) {
            output.add(cls);
        } else {
            for (String pkg : acceptablePackages) {
                if (cls.getPackage().getName().startsWith(pkg)) {
                    output.add(cls);
                }
            }
        }
    }
    LOGGER.trace("classes() - output size {}", output.size());
    return output;
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) HashSet(java.util.HashSet)

Aggregations

ClassGraph (io.github.classgraph.ClassGraph)21 ScanResult (io.github.classgraph.ScanResult)8 Test (org.junit.Test)8 URLClassLoader (java.net.URLClassLoader)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 File (java.io.File)5 URL (java.net.URL)5 ArrayList (java.util.ArrayList)4 ClassInfo (io.github.classgraph.ClassInfo)3 HashSet (java.util.HashSet)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 URI (java.net.URI)2 StringJoiner (java.util.StringJoiner)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 DiagnosticSource (com.sun.tools.javac.util.DiagnosticSource)1 DiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition)1