From 092b3d4a526ee2118793b53fc428cdcc3fddb43d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 8 Jan 2018 13:41:23 +0100 Subject: [PATCH] Warning instead of error for non-present type filter class Issue: SPR-16356 (cherry picked from commit 4adc820) --- .../parsing/CustomProblemReporterTests.java | 9 ++- .../ComponentScanBeanDefinitionParser.java | 59 ++++++++++--------- .../config/JmsListenerContainerParser.java | 4 +- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java index 17455d785c..adfe6176cb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/parsing/CustomProblemReporterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,7 @@ import static org.springframework.tests.TestResourceUtils.*; * @author Chris Beams * @since 2.0 */ -public final class CustomProblemReporterTests { +public class CustomProblemReporterTests { private static final Resource CONTEXT = qualifiedResource(CustomProblemReporterTests.class, "context.xml"); @@ -66,10 +66,9 @@ public final class CustomProblemReporterTests { private static class CollatingProblemReporter implements ProblemReporter { - private List errors = new ArrayList(); - - private List warnings = new ArrayList(); + private final List errors = new ArrayList<>(); + private final List warnings = new ArrayList<>(); @Override public void fatal(Problem problem) { diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java index adc42223b2..97867a06f9 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,6 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.springframework.beans.BeanUtils; -import org.springframework.beans.FatalBeanException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.parsing.BeanComponentDefinition; @@ -40,6 +39,7 @@ import org.springframework.core.type.filter.AspectJTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.RegexPatternTypeFilter; import org.springframework.core.type.filter.TypeFilter; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -212,6 +212,10 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser { scanner.addExcludeFilter(typeFilter); } } + catch (ClassNotFoundException ex) { + parserContext.getReaderContext().warning( + "Ignoring non-present type filter class: " + ex, parserContext.extractSource(element)); + } catch (Exception ex) { parserContext.getReaderContext().error( ex.getMessage(), parserContext.extractSource(element), ex.getCause()); @@ -221,37 +225,34 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser { } @SuppressWarnings("unchecked") - protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader, ParserContext parserContext) { + protected TypeFilter createTypeFilter(Element element, 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); - try { - if ("annotation".equals(filterType)) { - return new AnnotationTypeFilter((Class) 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); - } + if ("annotation".equals(filterType)) { + return new AnnotationTypeFilter((Class) ClassUtils.forName(expression, classLoader)); } - catch (ClassNotFoundException ex) { - throw new FatalBeanException("Type filter class not found: " + expression, ex); + 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); } } diff --git a/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java b/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java index 0e91e2ef36..f804341012 100644 --- a/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java +++ b/spring-jms/src/main/java/org/springframework/jms/config/JmsListenerContainerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -150,7 +150,7 @@ class JmsListenerContainerParser extends AbstractListenerContainerParser { if (!("auto".equals(cache) || "consumer".equals(cache))) { parserContext.getReaderContext().warning( "'cache' attribute not actively supported for listener container of type \"simple\". " + - "Effective runtime behavior will be equivalent to \"consumer\" / \"auto\".", containerEle); + "Effective runtime behavior will be equivalent to \"consumer\" / \"auto\".", containerEle); } } else {