public Set<Class<?>> scanClasspathForAnnotation(Class<?> annotation, String... onlyFromPackages) { ClassInfoList classInfoList = scanResult.getClassesWithAnnotation(annotation.getName()); classInfoList = classInfoList .filter(ci -> Arrays.stream(onlyFromPackages) .anyMatch(p -> classMatchesPackage(ci.getName(), p))); return classInfoList.loadClasses().stream().collect(toSet()); }
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()); }
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); } }
/** * Convert this list of {@link ClassInfo} objects to a list of {@code Class<?>} objects. Causes the classloader * to load the class named by each {@link ClassInfo} object, if it is not already loaded. * * @throws IllegalArgumentException * if an exception or error was thrown while trying to load any of the classes. * @return The loaded {@code Class<?>} objects corresponding to each {@link ClassInfo} object in this list. */ public List<Class<?>> loadClasses() { return loadClasses(/* ignoreExceptions = */ false); }
/** * Convert this list of {@link ClassInfo} objects to a list of {@code Class<?>} objects, casting each item in * the list to the requested superclass or interface type. Causes the classloader to load the class named by * each {@link ClassInfo} object, if it is not already loaded. * * <p> * <b>Important note:</b> since {@code superclassOrInterfaceType} is a class reference for an already-loaded * class, it is critical that {@code superclassOrInterfaceType} is loaded by the same classloader as the class * referred to by this {@code ClassInfo} object, otherwise the class cast will fail. * * @param <T> * The superclass or interface. * @param superclassOrInterfaceType * The superclass or interface class reference to cast each loaded class to. * @throws IllegalArgumentException * if an exception or error was thrown while trying to load or cast any of the classes. * @return The loaded {@code Class<?>} objects corresponding to each {@link ClassInfo} object in this list. */ public <T> List<Class<T>> loadClasses(final Class<T> superclassOrInterfaceType) { return loadClasses(superclassOrInterfaceType, /* ignoreExceptions = */ false); }
@Override protected void handleScanResult(ScanResult scanResult) { super.handleScanResult(scanResult); Map<Class<? extends Annotation>, Set<Class<?>>> annotatedClasses = new HashMap<>(); Arrays.asList( ManagedBean.class, FacesComponent.class, FacesBehavior.class, FacesConverter.class, NamedEvent.class, FacesRenderer.class, FacesBehaviorRenderer.class, FacesValidator.class ).forEach(annotationClass -> { List<Class<?>> classes = scanResult.getClassesWithAnnotation(annotationClass.getName()).loadClasses(); annotatedClasses.put(annotationClass, new HashSet<>(classes)); }); JoinFacesAnnotationProvider.setAnnotatedClasses(annotatedClasses); } }
@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") 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; }
/** * Returns all the class types annotated with an annotation * * @param annotation * The given annotations to find * * @return The set of classes that have the types annotated */ @NotNull @SuppressWarnings({"unchecked", "WeakerAccess"}) public Set<Class> getTypesAnnotatedWith(Class<? extends Annotation> annotation) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getClassesWithAnnotation(annotation.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
/** * Returns all the class types annotated with an annotation * * @param annotation * The given annotations to find * * @return The set of classes that have the types annotated */ @NotNull @SuppressWarnings({"unchecked", "WeakerAccess"}) public Set<Class> getTypesAnnotatedWith(Class<? extends Annotation> annotation) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getClassesWithAnnotation(annotation.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
/** * Returns all the subtypes (interface or abstract) of a given class type * * @param <T> * The type to check * @param type * variable * * @return A set of classes matching */ @NotNull @SuppressWarnings("unchecked") public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getSubclasses(type.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
@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(); } }
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; }
/** * Returns all the class types annotated with an annotation * * @param annotation * The given annotations to find * * @return The set of classes that have the types annotated */ @NotNull @SuppressWarnings({"unchecked", "WeakerAccess"}) public Set<Class> getTypesAnnotatedWith(Class<? extends Annotation> annotation) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getClassesWithAnnotation(annotation.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
/** * Returns all the subtypes (interface or abstract) of a given class type * * @param <T> * The type to check * @param type * variable * * @return A set of classes matching */ @NotNull @SuppressWarnings("unchecked") public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getSubclasses(type.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
/** * 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(); }
/** * Returns all the subtypes (interface or abstract) of a given class type * * @param <T> * The type to check * @param type * variable * * @return A set of classes matching */ @NotNull @SuppressWarnings("unchecked") public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type) { ClassInfoList subtypes = GuiceContext.instance() .getScanResult() .getSubclasses(type.getCanonicalName()); return new HashSet(subtypes.loadClasses()); }
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); } }
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 List<Class<?>> findClasses(Class<? extends Annotation> annotation, String... packages) { Validate.argumentsAreNotNull(annotation, packages); return new ClassGraph() .whitelistPackages(packages) .enableAnnotationInfo() .scan() .getClassesWithAnnotation(annotation.getName()) .loadClasses(); }