public static Set<Class<? extends Object>> getAllClassesFromPackage(String packageName) { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*"))); Set<BeanDefinition> classes = provider.findCandidateComponents(packageName); Set<Class<?>> allClasses = classes.stream() .map(bean -> { try { return Class.forName(bean.getBeanClassName()); } catch (ClassNotFoundException e) { LoggerFactory.getLogger(DocumentationUtil.class).error(e.getMessage(), e.getCause()); } return null; }) .collect(Collectors.toSet()); return allClasses; }
@SuppressWarnings("unchecked") protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader) { String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); try { if ("annotation".equals(filterType)) { return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression)); } else if ("assignable".equals(filterType)) { return new AssignableTypeFilter(classLoader.loadClass(expression)); } else if ("aspectj".equals(filterType)) { return new AspectJTypeFilter(expression, classLoader); } else if ("regex".equals(filterType)) { return new RegexPatternTypeFilter(Pattern.compile(expression)); } else if ("custom".equals(filterType)) { Class<?> filterClass = classLoader.loadClass(expression); if (!TypeFilter.class.isAssignableFrom(filterClass)) { throw new IllegalArgumentException( "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); } return (TypeFilter) BeanUtils.instantiateClass(filterClass); } else { throw new IllegalArgumentException("Unsupported filter type: " + filterType); } } catch (ClassNotFoundException ex) { throw new FatalBeanException("Type filter class not found: " + expression, ex); } }
private static Set<BeanDefinition> findCandidateComponents(String regex, String packageName) { final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); // add include filters which matches all the classes (or use your own) provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(regex))); // get matching classes defined in the package return provider.findCandidateComponents(packageName); }
/** * Return all the classes of a given package. * @param thePackage * @return * @see http://stackoverflow.com/a/21430849/587641 */ public static List<Class> getClassesInPackage(Package thePackage) { List<Class> result = new ArrayList<>(); ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*"))); final Set<BeanDefinition> classes = provider.findCandidateComponents(thePackage.getName()); for (BeanDefinition bean: classes) { try { result.add(Class.forName(bean.getBeanClassName())); } catch (ClassNotFoundException e) { log.debug("Class not found for bean {} (ignored)", bean); } } return result; }
@SuppressWarnings("unchecked") protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader, ParserContext parserContext) { String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression); try { if ("annotation".equals(filterType)) { return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression)); } else if ("assignable".equals(filterType)) { return new AssignableTypeFilter(classLoader.loadClass(expression)); } else if ("aspectj".equals(filterType)) { return new AspectJTypeFilter(expression, classLoader); } else if ("regex".equals(filterType)) { return new RegexPatternTypeFilter(Pattern.compile(expression)); } else if ("custom".equals(filterType)) { Class<?> filterClass = classLoader.loadClass(expression); if (!TypeFilter.class.isAssignableFrom(filterClass)) { throw new IllegalArgumentException( "Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression); } return (TypeFilter) BeanUtils.instantiateClass(filterClass); } else { throw new IllegalArgumentException("Unsupported filter type: " + filterType); } } catch (ClassNotFoundException ex) { throw new FatalBeanException("Type filter class not found: " + expression, ex); } }
@Test public void testWithPackageExcludeFilter() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*"))); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(0, candidates.size()); }
private TypeFilter createTypeFilter(FilterType filterType, String pattern) { switch (filterType) { case ASPECTJ: return new AspectJTypeFilter(pattern, this.classLoader); case REGEX: return new RegexPatternTypeFilter(Pattern.compile(pattern)); } throw new IllegalArgumentException( "Filter type not supported with String pattern: " + filterType); }
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) { List<TypeFilter> typeFilters = new ArrayList<TypeFilter>(); FilterType filterType = filterAttributes.getEnum("type"); for (Class<?> filterClass : filterAttributes.getClassArray("value")) { switch (filterType) { case ANNOTATION: Assert.isAssignable(Annotation.class, filterClass, "An error occured while processing a @ComponentScan ANNOTATION type filter: "); @SuppressWarnings("unchecked") Class<Annotation> annotationType = (Class<Annotation>) filterClass; typeFilters.add(new AnnotationTypeFilter(annotationType)); break; case ASSIGNABLE_TYPE: typeFilters.add(new AssignableTypeFilter(filterClass)); break; case CUSTOM: Assert.isAssignable(TypeFilter.class, filterClass, "An error occured while processing a @ComponentScan CUSTOM type filter: "); typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class)); break; default: throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType); } } for (String expression : filterAttributes.getStringArray("pattern")) { switch (filterType) { case ASPECTJ: typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader())); break; case REGEX: typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression))); break; default: throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType); } } return typeFilters; }
public BeanDefinition parse(Element element, ParserContext parserContext) { ClassPathBeanDefinitionScanner scanner = new DwrClassPathBeanDefinitionScanner(parserContext.getRegistry()); String basePackage = element.getAttribute("base-package"); // Override - By default Spring uses a name generator that uses AnnotationBeanNameGenerator which uses the name specified on the Component // annotation and if not present uses the simple name. Since our annotation-scanner doesn't scan @Component if two classes with the // same simple name exist in different packages there will be issues. See https://directwebremoting.atlassian.net/browse/DWR-651. scanner.setBeanNameGenerator(new DefaultBeanNameGenerator()); if (!hasText(basePackage)) { if (log.isInfoEnabled()) { log.info("No base package defined for classpath scanning. Traversing the whole JVM classpath"); } } String regex = element.getAttribute("regex"); if (hasText(regex)) { scanner.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(regex))); } String proxies = element.getAttribute("scanRemoteProxy"); if (hasText(proxies) && ("TRUE".equals(proxies.toUpperCase()) || "FALSE".equals(proxies.toUpperCase()))) { scanProxies = Boolean.parseBoolean(proxies); } if (scanProxies) { scanner.addIncludeFilter(new AnnotationTypeFilter(RemoteProxy.class)); } String conv = element.getAttribute("scanDataTransferObject"); if (hasText(conv) && ("TRUE".equals(conv.toUpperCase()) || "FALSE".equals(conv.toUpperCase()))) { scanConverters = Boolean.parseBoolean(conv); } if (scanConverters) { scanner.addIncludeFilter(new AnnotationTypeFilter(DataTransferObject.class)); } String filters = element.getAttribute("scanGlobalFilter"); if (hasText(filters) && ("TRUE".equals(filters.toUpperCase()) || "FALSE".equals(filters.toUpperCase()))) { scanFilters = Boolean.parseBoolean(filters); } if (scanFilters) { scanner.addIncludeFilter(new AnnotationTypeFilter(GlobalFilter.class)); } if (scanProxies | scanConverters | scanFilters) { scanner.scan(basePackage == null ? "" : basePackage); } else { log.warn("Scan is not required if all @RemoteProxy, @DataTransferObject and @GlobalFilter are disabled. Skipping detection"); } return null; }
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) { List<TypeFilter> typeFilters = new ArrayList<TypeFilter>(); FilterType filterType = filterAttributes.getEnum("type"); for (Class<?> filterClass : filterAttributes.getAliasedClassArray("classes", ComponentScan.Filter.class, null)) { switch (filterType) { case ANNOTATION: Assert.isAssignable(Annotation.class, filterClass, "An error occured while processing a @ComponentScan ANNOTATION type filter: "); @SuppressWarnings("unchecked") Class<Annotation> annotationType = (Class<Annotation>) filterClass; typeFilters.add(new AnnotationTypeFilter(annotationType)); break; case ASSIGNABLE_TYPE: typeFilters.add(new AssignableTypeFilter(filterClass)); break; case CUSTOM: Assert.isAssignable(TypeFilter.class, filterClass, "An error occured while processing a @ComponentScan CUSTOM type filter: "); typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class)); break; default: throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType); } } for (String expression : filterAttributes.getStringArray("pattern")) { switch (filterType) { case ASPECTJ: typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader())); break; case REGEX: typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression))); break; default: throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType); } } return typeFilters; }
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) { List<TypeFilter> typeFilters = new ArrayList<TypeFilter>(); FilterType filterType = filterAttributes.getEnum("type"); for (Class<?> filterClass : filterAttributes.getAliasedClassArray("classes", ComponentScan.Filter.class, null)) { switch (filterType) { case ANNOTATION: Assert.isAssignable(Annotation.class, filterClass, "An error occured while processing a @ComponentScan ANNOTATION type filter: "); @SuppressWarnings("unchecked") Class<Annotation> annotationType = (Class<Annotation>) filterClass; typeFilters.add(new AnnotationTypeFilter(annotationType)); break; case ASSIGNABLE_TYPE: typeFilters.add(new AssignableTypeFilter(filterClass)); break; case CUSTOM: Assert.isAssignable(TypeFilter.class, filterClass, "An error occured while processing a @ComponentScan CUSTOM type filter: "); TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class); invokeAwareMethods(filter); typeFilters.add(filter); break; default: throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType); } } for (String expression : filterAttributes.getStringArray("pattern")) { switch (filterType) { case ASPECTJ: typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader())); break; case REGEX: typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression))); break; default: throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType); } } return typeFilters; }