Search in sources :

Example 6 with ScanResult

use of io.github.classgraph.ScanResult 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 7 with ScanResult

use of io.github.classgraph.ScanResult 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)7 ScanResult (io.github.classgraph.ScanResult)7 ClassInfo (io.github.classgraph.ClassInfo)3 HashSet (java.util.HashSet)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 ClassLoaderUtil (com.hazelcast.internal.nio.ClassLoaderUtil)1 ExceptionUtil.sneakyThrow (com.hazelcast.internal.util.ExceptionUtil.sneakyThrow)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 Modifier (java.lang.reflect.Modifier)1 Arrays.stream (java.util.Arrays.stream)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1