public Collection<String> findFunctionsInJar(File jarFile) throws IOException { ClassGraph fastClasspathScanner = new ClassGraph().disableDirScanning() .removeTemporaryFilesAfterScan().overrideClasspath(jarFile.getAbsolutePath()); ScanResult scanResult = fastClasspathScanner.enableClassInfo().scan(); Set<String> functionClasses = new HashSet<>(); functionClasses.addAll(scanResult.getClassesImplementing(Function.class.getName()).getNames()); functionClasses.addAll(scanResult.getSubclasses(FunctionAdapter.class.getName()).getNames()); return functionClasses; } }
public ClasspathScanLoadHelper(Collection<String> packagesToScan) { scanResult = new ClassGraph().whitelistPackages(packagesToScan.toArray(new String[] {})) .enableClassInfo() .enableAnnotationInfo().scan(); }
public ClasspathScanningPrimitiveTypeRegistry(ClassLoader classLoader) { Map<String, PrimitiveType> types = CACHE.computeIfAbsent(classLoader, cl -> { final Map<String, PrimitiveType> result = new ConcurrentHashMap<>(); final String[] whitelistPackages = StringUtils.split(System.getProperty("io.atomix.whitelistPackages"), ","); final ClassGraph classGraph = whitelistPackages != null ? new ClassGraph().enableClassInfo().whitelistPackages(whitelistPackages).addClassLoader(classLoader) : new ClassGraph().enableClassInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { scanResult.getClassesImplementing(PrimitiveType.class.getName()).forEach(classInfo -> { if (classInfo.isInterface() || classInfo.isAbstract() || Modifier.isPrivate(classInfo.getModifiers())) { return; } final PrimitiveType primitiveType = newInstance(classInfo.loadClass()); final PrimitiveType oldPrimitiveType = result.put(primitiveType.name(), primitiveType); if (oldPrimitiveType != null) { LOGGER.warn("Found multiple primitives types name={}, classes=[{}, {}]", primitiveType.name(), oldPrimitiveType.getClass().getName(), primitiveType.getClass().getName()); } }); } return Collections.unmodifiableMap(result); }); primitiveTypes.putAll(types); }
@SuppressWarnings("unchecked") private ClasspathScanningRegistry(ClassLoader classLoader, Set<String> whitelistPackages) { final Map<Class<? extends NamedType>, Map<String, NamedType>> registrations = CACHE.computeIfAbsent(classLoader, cl -> { final ClassGraph classGraph = !whitelistPackages.isEmpty() ? new ClassGraph().enableClassInfo().whitelistPackages(whitelistPackages.toArray(new String[0])).addClassLoader(classLoader) : new ClassGraph().enableClassInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { final Map<Class<? extends NamedType>, Map<String, NamedType>> result = new ConcurrentHashMap<>(); scanResult.getClassesImplementing(NamedType.class.getName()).forEach(classInfo -> { if (classInfo.isInterface() || classInfo.isAbstract() || Modifier.isPrivate(classInfo.getModifiers())) { return; } final Class<?> type = classInfo.loadClass(); final Class<? extends NamedType> classType = getClassType(type); final NamedType instance = newInstance(type); final NamedType oldInstance = result.computeIfAbsent(classType, t -> new HashMap<>()).put(instance.name(), instance); if (oldInstance != null) { LOGGER.warn("Found multiple types with name={}, classes=[{}, {}]", instance.name(), oldInstance.getClass().getName(), instance.getClass().getName()); } }); return result; } }); this.registrations.putAll(registrations); }
/** * Causes ClassGraph to return classes that are not in the whitelisted packages, but that are directly referred * to by classes within whitelisted packages as a superclass, implemented interface or annotation. * (Automatically calls {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph enableExternalClasses() { enableClassInfo(); scanSpec.enableExternalClasses = true; return this; }
/** * Enables the saving of method info during the scan. This information can be obtained using * {@link ClassInfo#getMethodInfo()} etc. By default, method info is not scanned. (Automatically calls * {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph enableMethodInfo() { enableClassInfo(); scanSpec.enableMethodInfo = true; return this; }
/** * Enables the saving of field info during the scan. This information can be obtained using * {@link ClassInfo#getFieldInfo()}. By default, field info is not scanned. (Automatically calls * {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph enableFieldInfo() { enableClassInfo(); scanSpec.enableFieldInfo = true; return this; }
/** * Causes only runtime visible annotations to be scanned (causes runtime invisible annotations to be ignored). * (Automatically calls {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph disableRuntimeInvisibleAnnotations() { enableClassInfo(); scanSpec.disableRuntimeInvisibleAnnotations = true; return this; }
/** * Causes class visibility to be ignored, enabling private, package-private and protected classes to be scanned. * By default, only public classes are scanned. (Automatically calls {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph ignoreClassVisibility() { enableClassInfo(); scanSpec.ignoreClassVisibility = true; return this; }
/** * Enables the saving of annotation info (for class, field, method and method parameter annotations) during the * scan. This information can be obtained using {@link ClassInfo#getAnnotationInfo()}, * {@link FieldInfo#getAnnotationInfo()}, and {@link MethodParameterInfo#getAnnotationInfo()}. By default, * annotation info is not scanned. (Automatically calls {@link #enableClassInfo()}.) * * @return this (for method chaining). */ public ClassGraph enableAnnotationInfo() { enableClassInfo(); scanSpec.enableAnnotationInfo = true; return this; }
/** * Enables the scanning of system packages ({@code "java.*"}, {@code "javax.*"}, {@code "javafx.*"}, * {@code "jdk.*"}, {@code "oracle.*"}, {@code "sun.*"}) -- these are not scanned by default for speed. * * <p> * N.B. Automatically calls {@link #enableClassInfo()}. * * @return this (for method chaining). */ public ClassGraph enableSystemJarsAndModules() { enableClassInfo(); scanSpec.enableSystemJarsAndModules = true; return this; }
/** * Enables the saving of static final field constant initializer values. By default, constant initializer values * are not scanned. Automatically calls {@link #enableClassInfo()} and {@link #enableFieldInfo()}. * * @return this (for method chaining). */ public ClassGraph enableStaticFinalFieldConstantInitializerValues() { enableClassInfo(); enableFieldInfo(); scanSpec.enableStaticFinalFieldConstantInitializerValues = true; return this; }
/** * Causes field visibility to be ignored, enabling private, package-private and protected fields to be scanned. * By default, only public fields are scanned. (Automatically calls {@link #enableClassInfo()} and * {@link #enableFieldInfo()}.) * * @return this (for method chaining). */ public ClassGraph ignoreFieldVisibility() { enableClassInfo(); enableFieldInfo(); scanSpec.ignoreFieldVisibility = true; return this; }
/** * Causes method visibility to be ignored, enabling private, package-private and protected methods to be * scanned. By default, only public methods are scanned. (Automatically calls {@link #enableClassInfo()} and * {@link #enableMethodInfo()}.) * * @return this (for method chaining). */ public ClassGraph ignoreMethodVisibility() { enableClassInfo(); enableMethodInfo(); scanSpec.ignoreMethodVisibility = true; return this; }
@Override public List<String> getClassNamesForClasspath(URL[] urls) { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .enableClassInfo() .ignoreClassVisibility() .overrideClasspath(urls) .scan()) { return scan.getAllClasses().getNames(); } }
@Override public List<String> getClassNamesForClasspath(URL[] urls) { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .enableClassInfo() .ignoreClassVisibility() .overrideClasspath(urls) .scan()) { return scan.getAllClasses().getNames(); } }
/** * Enables the determination of inter-class dependencies, which may be read by calling * {@link ClassInfo#getClassDependencies()}, {@link ScanResult#getClassDependencyMap()} or * {@link ScanResult#getReverseClassDependencyMap()}. (Automatically calls {@link #enableClassInfo()}, * {@link #enableFieldInfo()}, {@link #enableMethodInfo()}, {@link #enableAnnotationInfo()}, * {@link #ignoreClassVisibility()}, {@link #ignoreFieldVisibility()} and {@link #ignoreMethodVisibility()}.) * */ public ClassGraph enableInterClassDependencies() { enableClassInfo(); enableFieldInfo(); enableMethodInfo(); enableAnnotationInfo(); ignoreClassVisibility(); ignoreFieldVisibility(); ignoreMethodVisibility(); scanSpec.enableInterClassDependencies = true; return this; }
@Override public List<String> getClassNamesForPackage(String packageName, URL url) { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .enableClassInfo() .ignoreClassVisibility() .overrideClasspath(url) .whitelistPackagesNonRecursive(packageName) .scan()) { return scan.getAllClasses().getNames(); } } }
@Override public List<String> getClassNamesForPackage(String packageName, URL url) { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .enableClassInfo() .ignoreClassVisibility() .overrideClasspath(url) .whitelistPackagesNonRecursive(packageName) .scan()) { return scan.getAllClasses().getNames(); } } }
/** * Enables the scanning of all classes, fields, methods, annotations, and static final field constant * initializer values, and ignores all visibility modifiers, so that both public and non-public classes, fields * and methods are all scanned. * * <p> * Calls {@link #enableClassInfo()}, {@link #enableFieldInfo()}, {@link #enableMethodInfo()}, * {@link #enableAnnotationInfo()}, {@link #enableStaticFinalFieldConstantInitializerValues()}, * {@link #ignoreClassVisibility()}, {@link #ignoreFieldVisibility()}, and {@link #ignoreMethodVisibility()}. * * @return this (for method chaining). */ public ClassGraph enableAllInfo() { enableClassInfo(); enableFieldInfo(); enableMethodInfo(); enableAnnotationInfo(); enableStaticFinalFieldConstantInitializerValues(); ignoreClassVisibility(); ignoreFieldVisibility(); ignoreMethodVisibility(); return this; }