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(); }
}; ScanResult result = scanner.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); }
: new ClassGraph().enableAnnotationInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { scanResult.getClassesWithAnnotation(AtomixResource.class.getName()).forEach(classInfo -> { deployment.getRegistry().addPerInstanceResource(classInfo.loadClass(), "/v1");
/** * Scans the classpath, blocking until the scan is complete. * * @throws RuntimeException * if any of the worker threads throws an uncaught exception, or the scan was interrupted. * @return a {@link ScanResult} object representing the result of the scan. */ public ScanResult scan() { return scan(DEFAULT_NUM_WORKER_THREADS); }
/** * Scans the classpath with the requested number of threads, blocking until the scan is complete. * * @param numThreads * The number of worker threads to start up. * @throws RuntimeException * if any of the worker threads throws an uncaught exception, or the scan was interrupted. * @return a {@link ScanResult} object representing the result of the scan. */ public ScanResult scan(final int numThreads) { try (AutoCloseableExecutorService executorService = new AutoCloseableExecutorService(numThreads)) { return scan(executorService, numThreads); } }
@SuppressWarnings("unchecked") public static Set<AbstractAtlasShellToolsCommand> getSubcommandInstances() { final List<Class<? extends AtlasShellToolsMarkerInterface>> subcommandClasses = new ArrayList<>(); final Set<AbstractAtlasShellToolsCommand> instantiatedCommands = new HashSet<>(); try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { final ClassInfoList classInfoList = scanResult .getClassesImplementing(AtlasShellToolsMarkerInterface.class.getName()); classInfoList.loadClasses().forEach(klass -> subcommandClasses .add((Class<? extends AtlasShellToolsMarkerInterface>) klass)); } subcommandClasses.stream().forEach(klass -> instantiateSubcommand(klass.getName()) .ifPresent(instantiatedCommands::add)); return instantiatedCommands; }
@Override public List<URL> getClasspathURLs() { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .scan()) { return scan.getClasspathURLs(); } }
@Override public List<URL> getClasspathURLs() { try (ScanResult scan = new ClassGraph() .disableNestedJarScanning() .scan()) { return scan.getClasspathURLs(); } }
public Validators(final String packageName) { this.validatorTypes = new EnumMap<>(Validation.class); this.validators = new ValidatorMap(); fillValidatorTypes(this.validatorTypes); final List<Class<?>> klasses = new ArrayList<>(); // Scan all classes in the given package with the Tag annotation try (ScanResult scanResult = new ClassGraph().enableAllInfo().whitelistPackages(packageName) .scan()) { final ClassInfoList tagClassInfoList = scanResult .getClassesWithAnnotation("org.openstreetmap.atlas.tags.annotations.Tag"); tagClassInfoList.loadClasses().forEach(klasses::add); } klasses.stream().forEach(this::processClass); }
public static void checkInitByAnnotatedClass() { if (!annotatedClassInitialized) { // look for classes annotated with @Properties try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { ClassInfoList classInfoList = scanResult.getClassesWithAnnotation(io.github.thingersoft.pm.api.annotations.Properties.class.getName()); for (ClassInfo mappedClassInfo : classInfoList) { Class<?> mappedClass = mappedClassInfo.loadClass(); initByAnnotatedClass(mappedClass); } } catch (IllegalArgumentException e) { throw new RuntimeException("Properties injection mapping failed", e); } } }
private Set<Class<?>> scanClasses(final Set<Class<? extends Annotation>> supportedAnnotations) { Set<Class<?>> result = new LinkedHashSet<>(); ClassGraph classGraph = new ClassGraph() .enableAllInfo() .enableExternalClasses(); List<String> basePackages = this.properties.getBasePackages(); if (basePackages != null && !basePackages.isEmpty()) { classGraph = classGraph.whitelistPackages(basePackages.toArray(new String[0])); } try (ScanResult scanResult = classGraph.scan()) { for (final Class<? extends Annotation> supportedAnnotation : supportedAnnotations) { result.addAll(scanResult.getClassesWithAnnotation(supportedAnnotation.getName()).loadClasses(true)); result.addAll(scanResult.getClassesWithMethodAnnotation(supportedAnnotation.getName()).loadClasses(true)); result.addAll(scanResult.getClassesWithFieldAnnotation(supportedAnnotation.getName()).loadClasses(true)); } } return result; }
public static List<Class<?>> findClasses(Class<? extends Annotation> annotation, String... packages) { Validate.argumentsAreNotNull(annotation, packages); return new ClassGraph() .whitelistPackages(packages) .enableAnnotationInfo() .scan() .getClassesWithAnnotation(annotation.getName()) .loadClasses(); }
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "https://github.com/spotbugs/spotbugs/issues/756") public void scanClasses() throws IOException { ClassGraph classGraph = new ClassGraph() .enableAllInfo() .enableExternalClasses() .enableSystemJarsAndModules(); classGraph = getClassGraphConfigurer().apply(classGraph); try (ScanResult scanResult = classGraph.scan()) { for (ScanResultHandler scanResultHandler : this.scanResultHandlers) { scanResultHandler.handle(scanResult, getBaseDir()); } } } }
@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(); } }
@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(); } } }