|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.regex.Pattern; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 25 | +import org.springframework.core.annotation.AnnotationAttributes; |
| 26 | +import org.springframework.core.env.Environment; |
| 27 | +import org.springframework.core.io.ResourceLoader; |
| 28 | +import org.springframework.core.type.filter.AnnotationTypeFilter; |
| 29 | +import org.springframework.core.type.filter.AspectJTypeFilter; |
| 30 | +import org.springframework.core.type.filter.AssignableTypeFilter; |
| 31 | +import org.springframework.core.type.filter.RegexPatternTypeFilter; |
| 32 | +import org.springframework.core.type.filter.TypeFilter; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * Collection of utilities for working with {@link ComponentScan @ComponentScan} |
| 37 | + * {@linkplain ComponentScan.Filter type filters}. |
| 38 | + * |
| 39 | + * @author Chris Beams |
| 40 | + * @author Juergen Hoeller |
| 41 | + * @author Sam Brannen |
| 42 | + * @since 5.3.13 |
| 43 | + * @see ComponentScan.Filter |
| 44 | + * @see org.springframework.core.type.filter.TypeFilter |
| 45 | + */ |
| 46 | +public abstract class TypeFilterUtils { |
| 47 | + |
| 48 | + /** |
| 49 | + * Create {@linkplain TypeFilter type filters} from the supplied |
| 50 | + * {@link AnnotationAttributes}, such as those sourced from |
| 51 | + * {@link ComponentScan#includeFilters()} or {@link ComponentScan#excludeFilters()}. |
| 52 | + * <p>Each {@link TypeFilter} will be instantiated using an appropriate |
| 53 | + * constructor, with {@code BeanClassLoaderAware}, {@code BeanFactoryAware}, |
| 54 | + * {@code EnvironmentAware}, and {@code ResourceLoaderAware} contracts |
| 55 | + * invoked if they are implemented by the type filter. |
| 56 | + * @param filterAttributes {@code AnnotationAttributes} for a |
| 57 | + * {@link ComponentScan.Filter @Filter} declaration |
| 58 | + * @param environment the {@code Environment} to make available to filters |
| 59 | + * @param resourceLoader the {@code ResourceLoader} to make available to filters |
| 60 | + * @param registry the {@code BeanDefinitionRegistry} to make available to filters |
| 61 | + * as a {@link org.springframework.beans.factory.BeanFactory} if applicable |
| 62 | + * @return a list of instantiated and configured type filters |
| 63 | + * @see TypeFilter |
| 64 | + * @see AnnotationTypeFilter |
| 65 | + * @see AssignableTypeFilter |
| 66 | + * @see AspectJTypeFilter |
| 67 | + * @see RegexPatternTypeFilter |
| 68 | + * @see org.springframework.beans.factory.BeanClassLoaderAware |
| 69 | + * @see org.springframework.beans.factory.BeanFactoryAware |
| 70 | + * @see org.springframework.context.EnvironmentAware |
| 71 | + * @see org.springframework.context.ResourceLoaderAware |
| 72 | + */ |
| 73 | + public static List<TypeFilter> createTypeFiltersFor(AnnotationAttributes filterAttributes, Environment environment, |
| 74 | + ResourceLoader resourceLoader, BeanDefinitionRegistry registry) { |
| 75 | + |
| 76 | + List<TypeFilter> typeFilters = new ArrayList<>(); |
| 77 | + FilterType filterType = filterAttributes.getEnum("type"); |
| 78 | + |
| 79 | + for (Class<?> filterClass : filterAttributes.getClassArray("classes")) { |
| 80 | + switch (filterType) { |
| 81 | + case ANNOTATION: |
| 82 | + Assert.isAssignable(Annotation.class, filterClass, |
| 83 | + "@ComponentScan ANNOTATION type filter requires an annotation type"); |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + Class<Annotation> annotationType = (Class<Annotation>) filterClass; |
| 86 | + typeFilters.add(new AnnotationTypeFilter(annotationType)); |
| 87 | + break; |
| 88 | + case ASSIGNABLE_TYPE: |
| 89 | + typeFilters.add(new AssignableTypeFilter(filterClass)); |
| 90 | + break; |
| 91 | + case CUSTOM: |
| 92 | + Assert.isAssignable(TypeFilter.class, filterClass, |
| 93 | + "@ComponentScan CUSTOM type filter requires a TypeFilter implementation"); |
| 94 | + TypeFilter filter = ParserStrategyUtils.instantiateClass(filterClass, TypeFilter.class, |
| 95 | + environment, resourceLoader, registry); |
| 96 | + typeFilters.add(filter); |
| 97 | + break; |
| 98 | + default: |
| 99 | + throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for (String expression : filterAttributes.getStringArray("pattern")) { |
| 104 | + switch (filterType) { |
| 105 | + case ASPECTJ: |
| 106 | + typeFilters.add(new AspectJTypeFilter(expression, resourceLoader.getClassLoader())); |
| 107 | + break; |
| 108 | + case REGEX: |
| 109 | + typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression))); |
| 110 | + break; |
| 111 | + default: |
| 112 | + throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return typeFilters; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments