Commit eed58eec authored by Andy Wilkinson's avatar Andy Wilkinson

Improve error handling in EnableAutoConfigurationImportSelector

Previously, EnableAutoConfigurationImportSelector assumed that it
would always find auto-configuration attributes from an
@EnableAutoConfiguration annotation. This assumption does not hold
true in certain circumstances, although exactly what those
circumstances are is unclear. It could occur if the import selector
were used directly, but it's package-private making that unlikey. In
such circumstances a NullPointerException was being thrown.

This commit asserts that the attributes are non-null and, should the
assertion fail, produces an error that is more helpful than an NPE.

Closes gh-1512
parent 468b6cb1
...@@ -31,12 +31,14 @@ import org.springframework.core.annotation.Order; ...@@ -31,12 +31,14 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
/** /**
* {@link DeferredImportSelector} to handle {@link EnableAutoConfiguration * {@link DeferredImportSelector} to handle {@link EnableAutoConfiguration
* auto-configuration}. * auto-configuration}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
* @see EnableAutoConfiguration * @see EnableAutoConfiguration
*/ */
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
...@@ -54,6 +56,10 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector, ...@@ -54,6 +56,10 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
.getAnnotationAttributes(EnableAutoConfiguration.class.getName(), .getAnnotationAttributes(EnableAutoConfiguration.class.getName(),
true)); true));
Assert.notNull(attributes, "No auto-configuration attributes found. Is "
+ metadata.getClassName()
+ " annotated with @EnableAutoConfiguration?");
// Find all possible auto configuration classes, filtering duplicates // Find all possible auto configuration classes, filtering duplicates
List<String> factories = new ArrayList<String>(new LinkedHashSet<String>( List<String> factories = new ArrayList<String>(new LinkedHashSet<String>(
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment