Search in sources :

Example 21 with ClassGraph

use of io.github.classgraph.ClassGraph in project junit5 by junit-team.

the class ApiReportGenerator method generateReport.

// -------------------------------------------------------------------------
ApiReport generateReport(String... packages) {
    Logger logger = LoggerFactory.getLogger(ApiReportGenerator.class);
    String EOL = System.lineSeparator();
    ClassGraph classGraph = // 
    new ClassGraph().acceptPackages(// 
    packages).disableNestedJarScanning().enableAnnotationInfo();
    String apiClasspath = System.getProperty("api.classpath");
    if (apiClasspath != null) {
        classGraph = classGraph.overrideClasspath(apiClasspath);
    }
    // Scan packages
    try (ScanResult scanResult = classGraph.scan()) {
        // Collect names
        ClassInfoList classesWithApiAnnotation = scanResult.getClassesWithAnnotation(API.class.getCanonicalName());
        List<String> names = classesWithApiAnnotation.getNames();
        logger.debug(() -> {
            StringBuilder builder = new StringBuilder(names.size() + " @API declarations (including meta) found in class-path:");
            builder.append(EOL);
            scanResult.getClasspathURLs().forEach(e -> builder.append(e).append(EOL));
            return builder.toString();
        });
        // Collect types
        List<Class<?>> types = classesWithApiAnnotation.loadClasses();
        // only retain directly annotated types
        types.removeIf(c -> !c.isAnnotationPresent(API.class));
        types.sort(Comparator.comparing(Class::getName));
        logger.debug(() -> {
            StringBuilder builder = new StringBuilder("Listing of all " + types.size() + " annotated types:");
            builder.append(EOL);
            types.forEach(e -> builder.append(e).append(EOL));
            return builder.toString();
        });
        // Build map
        Map<Status, List<Class<?>>> declarationsMap = new EnumMap<>(Status.class);
        for (Status status : Status.values()) {
            declarationsMap.put(status, new ArrayList<>());
        }
        types.forEach(type -> declarationsMap.get(type.getAnnotation(API.class).status()).add(type));
        // Create report
        return new ApiReport(types, declarationsMap);
    }
}
Also used : Status(org.apiguardian.api.API.Status) ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) Logger(org.junit.platform.commons.logging.Logger) ClassInfoList(io.github.classgraph.ClassInfoList) API(org.apiguardian.api.API) ArrayList(java.util.ArrayList) ClassInfoList(io.github.classgraph.ClassInfoList) List(java.util.List) EnumMap(java.util.EnumMap)

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