Ensure CLI adds @EnableAutoConfiguration in an appropriate place
Up to now we have been treating the *first* class to be compiled as the "main" application and adding @EnableAutoConfiguration. This isn't always appropriate (e.g. if it's a test case), so now we look for an appropriate annotation before falling back to the old behaviour. In addition ensures classes with a field of type Reactor trigger the reactor auto imports. See gh-969
This commit is contained in:
@@ -34,6 +34,7 @@ import org.codehaus.groovy.ast.expr.MethodCallExpression;
|
||||
import org.codehaus.groovy.ast.stmt.BlockStatement;
|
||||
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
|
||||
import org.codehaus.groovy.ast.stmt.Statement;
|
||||
import org.springframework.util.PatternMatchUtils;
|
||||
|
||||
/**
|
||||
* General purpose AST utilities.
|
||||
@@ -69,7 +70,7 @@ public abstract class AstUtils {
|
||||
String... annotations) {
|
||||
for (AnnotationNode annotationNode : node.getAnnotations()) {
|
||||
for (String annotation : annotations) {
|
||||
if (annotation.equals(annotationNode.getClassNode().getName())) {
|
||||
if (PatternMatchUtils.simpleMatch(annotation, annotationNode.getClassNode().getName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,8 +191,8 @@ public class GroovyCompiler {
|
||||
for (Object loadedClass : collector.getLoadedClasses()) {
|
||||
classes.add((Class<?>) loadedClass);
|
||||
}
|
||||
ClassNode mainClassNode = (ClassNode) compilationUnit.getAST().getClasses()
|
||||
.get(0);
|
||||
ClassNode mainClassNode = getMainClass(compilationUnit);
|
||||
|
||||
Class<?> mainClass = null;
|
||||
for (Class<?> loadedClass : classes) {
|
||||
if (mainClassNode.getName().equals(loadedClass.getName())) {
|
||||
@@ -265,6 +265,7 @@ public class GroovyCompiler {
|
||||
throws CompilationFailedException {
|
||||
|
||||
ImportCustomizer importCustomizer = new ImportCustomizer();
|
||||
ClassNode mainClassNode = getMainClass(source.getAST().getClasses());
|
||||
|
||||
// Additional auto configuration
|
||||
for (CompilerAutoConfiguration autoConfiguration : GroovyCompiler.this.compilerAutoConfigurations) {
|
||||
@@ -273,8 +274,7 @@ public class GroovyCompiler {
|
||||
autoConfiguration.applyImports(importCustomizer);
|
||||
importCustomizer.call(source, context, classNode);
|
||||
}
|
||||
if (source.getAST().getClasses().size() > 0
|
||||
&& classNode.equals(source.getAST().getClasses().get(0))) {
|
||||
if (classNode.equals(mainClassNode)) {
|
||||
autoConfiguration.applyToMainClass(GroovyCompiler.this.loader,
|
||||
GroovyCompiler.this.configuration, context, source,
|
||||
classNode);
|
||||
@@ -290,4 +290,23 @@ public class GroovyCompiler {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static ClassNode getMainClass(CompilationUnit source) {
|
||||
return getMainClass((List<ClassNode>) source.getAST().getClasses());
|
||||
}
|
||||
|
||||
private static ClassNode getMainClass(List<ClassNode> classes) {
|
||||
for (ClassNode node : classes) {
|
||||
if (AstUtils.hasAtLeastOneAnnotation(node, "Enable*AutoConfiguration")) {
|
||||
return null; // No need to enhance this
|
||||
}
|
||||
if (AstUtils.hasAtLeastOneAnnotation(node, "*Controller", "Configuration",
|
||||
"Component", "*Service", "Repository", "Enable*")) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return classes.isEmpty() ? null : classes.get(0);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ReactorCompilerAutoConfiguration extends CompilerAutoConfiguration
|
||||
|
||||
@Override
|
||||
public boolean matches(ClassNode classNode) {
|
||||
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableReactor");
|
||||
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableReactor") || AstUtils.hasAtLeastOneFieldOrMethod(classNode, "Reactor");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.boot.cli.compiler.autoconfigure;
|
||||
import groovy.lang.GroovyClassLoader;
|
||||
|
||||
import org.codehaus.groovy.ast.AnnotationNode;
|
||||
import org.codehaus.groovy.ast.ClassHelper;
|
||||
import org.codehaus.groovy.ast.ClassNode;
|
||||
import org.codehaus.groovy.classgen.GeneratorContext;
|
||||
import org.codehaus.groovy.control.CompilationFailedException;
|
||||
@@ -79,16 +80,9 @@ public class SpringBootCompilerAutoConfiguration extends CompilerAutoConfigurati
|
||||
private void addEnableAutoConfigurationAnnotation(SourceUnit source,
|
||||
ClassNode classNode) {
|
||||
if (!hasEnableAutoConfigureAnnotation(classNode)) {
|
||||
try {
|
||||
Class<?> annotationClass = source.getClassLoader().loadClass(
|
||||
"org.springframework.boot.autoconfigure.EnableAutoConfiguration");
|
||||
AnnotationNode annotationNode = new AnnotationNode(new ClassNode(
|
||||
annotationClass));
|
||||
classNode.addAnnotation(annotationNode);
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
AnnotationNode annotationNode = new AnnotationNode(
|
||||
ClassHelper.make("EnableAutoConfiguration"));
|
||||
classNode.addAnnotation(annotationNode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,21 +35,27 @@ import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SpringTestCompilerAutoConfiguration extends CompilerAutoConfiguration {
|
||||
public class SpringTestCompilerAutoConfiguration extends
|
||||
CompilerAutoConfiguration {
|
||||
|
||||
@Override
|
||||
public boolean matches(ClassNode classNode) {
|
||||
return AstUtils.hasAtLeastOneAnnotation(classNode, "SpringApplicationConfiguration");
|
||||
return AstUtils.hasAtLeastOneAnnotation(classNode,
|
||||
"SpringApplicationConfiguration");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void apply(GroovyClassLoader loader,
|
||||
GroovyCompilerConfiguration configuration,
|
||||
GeneratorContext generatorContext, SourceUnit source,
|
||||
ClassNode classNode) throws CompilationFailedException {
|
||||
if (!AstUtils.hasAtLeastOneAnnotation(classNode, "RunWith")) {
|
||||
AnnotationNode runwith = new AnnotationNode(ClassHelper.make("RunWith"));
|
||||
runwith.addMember("value", new ClassExpression(ClassHelper.make("SpringJUnit4ClassRunner")));
|
||||
AnnotationNode runwith = new AnnotationNode(
|
||||
ClassHelper.make("RunWith"));
|
||||
runwith.addMember(
|
||||
"value",
|
||||
new ClassExpression(ClassHelper
|
||||
.make("SpringJUnit4ClassRunner")));
|
||||
classNode.addAnnotation(runwith);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +65,8 @@ public class SpringTestCompilerAutoConfiguration extends CompilerAutoConfigurati
|
||||
throws CompilationFailedException {
|
||||
imports.addStarImports("org.junit.runner")
|
||||
.addStarImports("org.springframework.boot.test")
|
||||
.addStarImports("org.springframework.test.context.junit4");
|
||||
.addStarImports("org.springframework.test.context.junit4")
|
||||
.addImports(
|
||||
"org.springframework.test.context.web.WebAppConfiguration");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user