Search in sources :

Example 1 with ClassInfoList

use of io.github.classgraph.ClassInfoList 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)1 ClassInfoList (io.github.classgraph.ClassInfoList)1 ScanResult (io.github.classgraph.ScanResult)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1 API (org.apiguardian.api.API)1 Status (org.apiguardian.api.API.Status)1 Logger (org.junit.platform.commons.logging.Logger)1