org.springframework.core.type.filter.RegexPatternTypeFilter Java Examples

The following examples show how to use org.springframework.core.type.filter.RegexPatternTypeFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComponentScanBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected TypeFilter createTypeFilter(Element element, @Nullable ClassLoader classLoader,
		ParserContext parserContext) throws ClassNotFoundException {

	String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
	String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
	expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression);
	if ("annotation".equals(filterType)) {
		return new AnnotationTypeFilter((Class<Annotation>) ClassUtils.forName(expression, classLoader));
	}
	else if ("assignable".equals(filterType)) {
		return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader));
	}
	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 = ClassUtils.forName(expression, classLoader);
		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);
	}
}
 
Example #2
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void excludeFilterWithScan() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
	provider.setResourceLoader(new DefaultResourceLoader(
			CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
	provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
	testExclude(provider, ScannedGenericBeanDefinition.class);
}
 
Example #3
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void excludeFilterWithIndex() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
	provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
	provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
	testExclude(provider, AnnotatedGenericBeanDefinition.class);
}
 
Example #4
Source File: ComponentScanBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected TypeFilter createTypeFilter(Element element, @Nullable ClassLoader classLoader,
		ParserContext parserContext) throws ClassNotFoundException {

	String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
	String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
	expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression);
	if ("annotation".equals(filterType)) {
		return new AnnotationTypeFilter((Class<Annotation>) ClassUtils.forName(expression, classLoader));
	}
	else if ("assignable".equals(filterType)) {
		return new AssignableTypeFilter(ClassUtils.forName(expression, classLoader));
	}
	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 = ClassUtils.forName(expression, classLoader);
		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);
	}
}
 
Example #5
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void excludeFilterWithScan() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
	provider.setResourceLoader(new DefaultResourceLoader(
			CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())));
	provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
	testExclude(provider, ScannedGenericBeanDefinition.class);
}
 
Example #6
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void excludeFilterWithIndex() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
	provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
	provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*")));
	testExclude(provider, AnnotatedGenericBeanDefinition.class);
}
 
Example #7
Source File: ComponentScanBeanDefinitionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #8
Source File: SpringClassUtils.java    From jkes with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: ComponentScanBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #10
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #11
Source File: ComponentScanAnnotationParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
	List<TypeFilter> typeFilters = new ArrayList<>();
	FilterType filterType = filterAttributes.getEnum("type");

	for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
		switch (filterType) {
			case ANNOTATION:
				Assert.isAssignable(Annotation.class, filterClass,
						"@ComponentScan ANNOTATION type filter requires an annotation type");
				@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,
						"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
				TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
				ParserStrategyUtils.invokeAwareMethods(
						filter, this.environment, this.resourceLoader, this.registry);
				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;
}
 
Example #12
Source File: ComponentScanAnnotationParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
	List<TypeFilter> typeFilters = new ArrayList<>();
	FilterType filterType = filterAttributes.getEnum("type");

	for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
		switch (filterType) {
			case ANNOTATION:
				Assert.isAssignable(Annotation.class, filterClass,
						"@ComponentScan ANNOTATION type filter requires an annotation type");
				@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,
						"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
				TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
				ParserStrategyUtils.invokeAwareMethods(
						filter, this.environment, this.resourceLoader, this.registry);
				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;
}
 
Example #13
Source File: SpringRestConfiguration.java    From galeb with Apache License 2.0 4 votes vote down vote up
private Set<BeanDefinition> allBeansDomain() {
    final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*")));
    return provider.findCandidateComponents("io.galeb.core.entity");
}
 
Example #14
Source File: ComponentScanAnnotationParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
	List<TypeFilter> typeFilters = new ArrayList<TypeFilter>();
	FilterType filterType = filterAttributes.getEnum("type");

	for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
		switch (filterType) {
			case ANNOTATION:
				Assert.isAssignable(Annotation.class, filterClass,
						"@ComponentScan ANNOTATION type filter requires an annotation type");
				@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,
						"@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
				TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
				ParserStrategyUtils.invokeAwareMethods(
						filter, this.environment, this.resourceLoader, this.registry);
				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;
}
 
Example #15
Source File: ComponentScanAnnotationParser.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #16
Source File: IbisDocPipe.java    From iaf with Apache License 2.0 4 votes vote down vote up
private static void addExcludeFilter(ClassPathBeanDefinitionScanner scanner, String excludeFilter) {
	scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(excludeFilter)));
}