public ClasspathScanLoadHelper(Collection<String> packagesToScan) { scanResult = new ClassGraph().whitelistPackages(packagesToScan.toArray(new String[] {})) .enableClassInfo() .enableAnnotationInfo().scan(); }
.whitelistPackages(spec.toArray(new String[spec.size()]));
@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); }
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); }
final String[] whitelistPackages = StringUtils.split(System.getProperty("io.atomix.whitelistPackages"), ","); final ClassGraph classGraph = whitelistPackages != null ? new ClassGraph().enableAnnotationInfo().whitelistPackages(whitelistPackages).addClassLoader(classLoader) : new ClassGraph().enableAnnotationInfo().addClassLoader(classLoader);
@SuppressWarnings("unchecked") SimpleConversionCallback(String... packages) { // Scan the packages for the converters. try (ScanResult scanResult = new ClassGraph().enableAllInfo().whitelistPackages(packages).scan()) { ClassInfoList controlClasses = scanResult.getClassesImplementing(AttributeConverter.class.getName()); List<Class<?>> controlClassRefs = controlClasses.loadClasses(); this.converters = controlClassRefs.stream() .map(clazz -> (Class<AttributeConverter>) clazz) .collect(Collectors.toMap( converter -> (ParameterizedType) Arrays.stream(converter.getGenericInterfaces()) .filter(type -> type instanceof ParameterizedType) .filter(type -> ((ParameterizedType) type).getRawType().equals(AttributeConverter.class)) .findFirst() .orElseThrow(() -> new IllegalArgumentException( format("The converter %s must implement AttributeConverter interface", converter.getSimpleName()))), converter -> converter )); } }
@SuppressWarnings("unchecked") @Override protected Stream<Class<? extends FlexibleSubCommand>> getSupportedCommands() { final List<Class<? extends FlexibleSubCommand>> returnValue = new ArrayList<>(); try (ScanResult scanResult = new ClassGraph().enableAllInfo() .whitelistPackages(AtlasReader.class.getPackage().getName()).scan()) { final ClassInfoList classInfoList = scanResult .getClassesImplementing(FlexibleSubCommand.class.getName()); classInfoList.loadClasses() .forEach(klass -> returnValue.add((Class<? extends FlexibleSubCommand>) klass)); } return returnValue.stream(); } }
/** * Searches for the implementations/subtypes of the given class. Only the matching classes are loaded. * * @param superType The type the implementations/subtypes of which are to be searched for * @param packages The packages to limit the search to * * @return A collection of classes discovered that implementation/extend {@code superType} */ public List<Class<?>> findImplementations(Class superType, Predicate<ClassInfo> filter, String... packages) { String[] scanPackages = Utils.emptyIfNull(packages); String cacheKey = Arrays.stream(scanPackages).sorted().collect(Collectors.joining()); ScanResult scanResults = cache.computeIfAbsent(cacheKey, k -> new ClassGraph() .whitelistPackages(packages) .enableAllInfo() .initializeLoadedClasses() .scan()); try { return scanResults.getAllClasses().stream() .filter(impl -> superType.isInterface() ? impl.implementsInterface(superType.getName()) : impl.extendsSuperclass(superType.getName())) .filter(filter == null ? info -> true : filter) .flatMap(info -> loadClass(info, superType)) .collect(Collectors.toList()); } catch (Exception e) { log.error("Failed to auto discover the subtypes of " + superType.getName() + ". Error encountered while scanning the classpath/modulepath.", e); return Collections.emptyList(); } }
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); }
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; }
try (ScanResult scanResult = new ClassGraph().enableAllInfo().whitelistPackages(packageName) .scan())
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(); }
@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); }
final String[] whitelistPackages = StringUtils.split(System.getProperty("io.atomix.whitelistPackages"), ","); final ClassGraph classGraph = whitelistPackages != null ? new ClassGraph().enableAnnotationInfo().whitelistPackages(whitelistPackages).addClassLoader(classLoader) : new ClassGraph().enableAnnotationInfo().addClassLoader(classLoader);
if (packages.length != 0) scanner.whitelistPackages(packages);
.whitelistPackages( selected_packages ); // Scan com.xyz and subpackages
if (packages.length != 0) graph.whitelistPackages(packages);
if (packages.length != 0) graph.whitelistPackages(packages);