Search in sources :

Example 1 with ClassInfo

use of io.github.classgraph.ClassInfo 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)

Example 2 with ClassInfo

use of io.github.classgraph.ClassInfo in project morphia by mongodb.

the class Mapper method getClasses.

private List<Class> getClasses(ClassLoader loader, String packageName, boolean mapSubPackages) throws ClassNotFoundException {
    final Set<Class> classes = new HashSet<>();
    ClassGraph classGraph = new ClassGraph().addClassLoader(loader).enableAllInfo();
    if (mapSubPackages) {
        classGraph.acceptPackages(packageName);
        classGraph.acceptPackages(packageName + ".*");
    } else {
        classGraph.acceptPackagesNonRecursive(packageName);
    }
    try (ScanResult scanResult = classGraph.scan()) {
        for (ClassInfo classInfo : scanResult.getAllClasses()) {
            classes.add(Class.forName(classInfo.getName(), true, loader));
        }
    }
    return new ArrayList<>(classes);
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassInfo(io.github.classgraph.ClassInfo)

Example 3 with ClassInfo

use of io.github.classgraph.ClassInfo 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)

Aggregations

ClassGraph (io.github.classgraph.ClassGraph)3 ClassInfo (io.github.classgraph.ClassInfo)3 ScanResult (io.github.classgraph.ScanResult)3 ClassLoaderUtil (com.hazelcast.internal.nio.ClassLoaderUtil)1 ExceptionUtil.sneakyThrow (com.hazelcast.internal.util.ExceptionUtil.sneakyThrow)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)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 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Arrays.stream (java.util.Arrays.stream)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1