public Set<Class<?>> scanPackagesForClassesImplementing(Class<?> implementedInterface, String... onlyFromPackages) { ClassInfoList classInfoList = scanResult.getClassesImplementing(implementedInterface.getName()) .filter(ci -> !ci.isAbstract() && !ci.isInterface() && ci.isPublic()); classInfoList = classInfoList .filter(ci -> Arrays.stream(onlyFromPackages) .anyMatch(p -> classMatchesPackage(ci.getName(), p))); return classInfoList.loadClasses().stream().collect(toSet()); }
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; } }
.filter(I) .filter(type -> type != Jooby.Module.class && type != Module.class && type != Service.class) .forEach(i -> result.getClassesImplementing(i.getName()) .stream() .filter(inPackage) result.getClassesImplementing(Module.class.getName()) .stream() .filter(inPackage) result.getClassesImplementing(Service.class.getName()) .stream() .filter(inPackage)
@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); }
private static <T> List<Class<?>> searchForPublicConcreteSubTypesOf(final Class<T> type) { String typeName = type.getCanonicalName(); ClassInfoList subTypes = type.isInterface() ? scanResult.getClassesImplementing(typeName) : scanResult.getSubclasses(typeName); List<Class<?>> loadedSubTypes = subTypes.filter(subType -> subType.isPublic() && !subType.isAbstract()).loadClasses(true); return Collections.unmodifiableList(loadedSubTypes); } }
@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; }
@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(); } }
/** * Default behavior finds all FlexibleSubCommands in the classpath. Override to restrict the set * of classes returned * * @return a stream containing all of the subcommands we want to support */ @SuppressWarnings("unchecked") protected Stream<Class<? extends FlexibleSubCommand>> getSupportedCommands() { final List<Class<? extends FlexibleSubCommand>> returnValue = new ArrayList<>(); try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { final ClassInfoList classInfoList = scanResult .getClassesImplementing(FlexibleSubCommand.class.getName()); classInfoList.loadClasses() .forEach(klass -> returnValue.add((Class<? extends FlexibleSubCommand>) klass)); } return returnValue.stream(); }
private static <T> List<Class<?>> searchForPublicConcreteSubTypesOf(final Class<T> type) { String typeName = type.getCanonicalName(); ClassInfoList subTypes = type.isInterface() ? scanResult.getClassesImplementing(typeName) : scanResult.getSubclasses(typeName); List<Class<?>> loadedSubTypes = subTypes.filter(subType -> subType.isPublic() && !subType.isAbstract()).loadClasses(true); return Collections.unmodifiableList(loadedSubTypes); } }
@Override public void handle(ScanResult scanResult, File joinfacesBaseDir) throws IOException { ClassInfoList annotationHandlers = scanResult.getClassesImplementing(REWRITE_ANNOTATION_HANDLER);
@Override public void handle(ScanResult scanResult, File joinfacesBaseDir) throws IOException { ClassInfoList scis = scanResult.getClassesImplementing(SERVLET_CONTAINER_INITIALIZER) .filter(classInfo -> classInfo.hasAnnotation(HANDLES_TYPES)); for (ClassInfo sciClassInfo : scis) { List<ClassInfo> handledTypes = resolveHandledTypes(sciClassInfo); SortedSet<String> classes = findHandledClasses(scanResult, handledTypes); File resultFile = new File(joinfacesBaseDir, sciClassInfo.getName() + ".classes"); writeClassList(resultFile, classes); } }
final List<SourceType<Type>> c = fromClassNames( classesImplementingInterfaces.stream() .flatMap(interf -> scanResult.getClassesImplementing(interf).getNames() .stream()) .distinct()
@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); }
classes.addAll(scanResult.getClassesImplementing(name).getNames());
classInfos = scanResult.getClassesImplementing(handledType.getName());