Java 类org.springframework.core.type.filter.RegexPatternTypeFilter 实例源码

项目:iVIS    文件:DocumentationUtil.java   
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;
}
项目:lams    文件:ComponentScanBeanDefinitionParser.java   
@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);
    }
}
项目:jkes    文件:SpringClassUtils.java   
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);
}
项目:yadaframework    文件:YadaUtil.java   
/**
 * 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;
}
项目:spring4-understanding    文件:ComponentScanBeanDefinitionParser.java   
@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);
    }
}
项目:spring4-understanding    文件:ClassPathScanningCandidateComponentProviderTests.java   
@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());
}
项目:my-spring-cache-redis    文件:ComponentScanBeanDefinitionParser.java   
@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);
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:FilterAnnotations.java   
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);
}
项目:spring    文件:ComponentScanBeanDefinitionParser.java   
@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);
    }
}
项目:spring-boot-concourse    文件:FilterAnnotations.java   
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);
}
项目:class-guard    文件:ComponentScanBeanDefinitionParser.java   
@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);
    }
}
项目:class-guard    文件:ClassPathScanningCandidateComponentProviderTests.java   
@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());
}
项目:lams    文件:ComponentScanAnnotationParser.java   
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;
}
项目:dwr    文件:AnnotationScannerParser.java   
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;
}
项目:spring4-understanding    文件:ComponentScanAnnotationParser.java   
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;
}
项目:my-spring-cache-redis    文件:ComponentScanAnnotationParser.java   
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;
}
项目:spring    文件:ComponentScanAnnotationParser.java   
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;
}