This commit is contained in:
Phillip Webb
2013-10-08 21:17:39 -07:00
parent b772f7c2e4
commit af0d08c998
14 changed files with 151 additions and 147 deletions

View File

@@ -27,9 +27,7 @@ public interface ArtifactCoordinatesResolver {
/**
* Gets the group id of the artifact identified by the given {@code artifactId}.
* Returns {@code null} if the artifact is unknown to the resolver.
*
* @param artifactId The id of the artifact
*
* @return The group id of the artifact
*/
String getGroupId(String artifactId);
@@ -37,9 +35,7 @@ public interface ArtifactCoordinatesResolver {
/**
* Gets the version of the artifact identified by the given {@code artifactId}.
* Returns {@code null} if the artifact is unknown to the resolver.
*
* @param artifactId The id of the artifact
*
* @return The version of the artifact
*/
String getVersion(String artifactId);

View File

@@ -18,7 +18,6 @@ package org.springframework.boot.cli.compiler;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.codehaus.groovy.ast.AnnotatedNode;
@@ -36,13 +35,29 @@ import org.codehaus.groovy.ast.MethodNode;
*/
public abstract class AstUtils {
/**
* Determine if a {@link ClassNode} has one or more of the specified annotations on
* the class or any of its methods. N.B. the type names are not normally fully
* qualified.
*/
public static boolean hasAtLeastOneAnnotation(ClassNode node, String... annotations) {
if (hasAtLeastOneAnnotation((AnnotatedNode) node, annotations)) {
return true;
}
for (MethodNode method : node.getMethods()) {
if (hasAtLeastOneAnnotation(method, annotations)) {
return true;
}
}
return false;
}
/**
* Determine if an {@link AnnotatedNode} has one or more of the specified annotations.
* N.B. the annotation type names are not normally fully qualified.
*/
public static boolean hasAtLeastOneAnnotation(AnnotatedNode node,
String... annotations) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
@@ -50,75 +65,40 @@ public abstract class AstUtils {
}
}
}
return false;
}
/**
* Determine if a {@link ClassNode} has one or more of the specified annotations on the class
* or any of its methods.
* N.B. the type names are not normally fully qualified.
*/
public static boolean hasAtLeastOneAnnotation(ClassNode node, String... annotations) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}
List<MethodNode> methods = node.getMethods();
for (MethodNode method : methods) {
for (AnnotationNode annotationNode : method.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}
}
return false;
}
/**
* Determine if a {@link ClassNode} has one or more fields of the specified types or
* method returning one or more of the specified types. N.B. the type names are not
* normally fully qualified.
*/
public static boolean hasAtLeastOneFieldOrMethod(ClassNode node, String... types) {
Set<String> set = new HashSet<String>(Arrays.asList(types));
List<FieldNode> fields = node.getFields();
for (FieldNode field : fields) {
if (set.contains(field.getType().getName())) {
Set<String> typesSet = new HashSet<String>(Arrays.asList(types));
for (FieldNode field : node.getFields()) {
if (typesSet.contains(field.getType().getName())) {
return true;
}
}
List<MethodNode> methods = node.getMethods();
for (MethodNode method : methods) {
if (set.contains(method.getReturnType().getName())) {
for (MethodNode method : node.getMethods()) {
if (typesSet.contains(method.getReturnType().getName())) {
return true;
}
}
return false;
}
/**
* Determine if a {@link ClassNode} subclasses any of the specified types
* N.B. the type names are not normally fully qualified.
*/
public static boolean subclasses(ClassNode node, String... types) {
for (String type : types) {
if (node.getSuperClass().getName().equals(type)) {
return true;
}
}
return false;
}
/**
* Determine if a {@link ClassNode} subclasses any of the specified types N.B. the
* type names are not normally fully qualified.
*/
public static boolean subclasses(ClassNode node, String... types) {
for (String type : types) {
if (node.getSuperClass().getName().equals(type)) {
return true;
}
}
return false;
}
}

View File

@@ -64,6 +64,7 @@ import org.codehaus.groovy.transform.ASTTransformationVisitor;
*
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
*/
public class GroovyCompiler {
@@ -73,6 +74,8 @@ public class GroovyCompiler {
private ArtifactCoordinatesResolver artifactCoordinatesResolver;
private final ASTTransformation dependencyCoordinatesTransformation = new DefaultDependencyCoordinatesAstTransformation();
/**
* Create a new {@link GroovyCompiler} instance.
* @param configuration the compiler configuration
@@ -168,7 +171,6 @@ public class GroovyCompiler {
try {
Field field = CompilationUnit.class.getDeclaredField("phaseOperations");
field.setAccessible(true);
LinkedList[] phaseOperations = (LinkedList[]) field.get(compilationUnit);
processConversionOperations(phaseOperations[Phases.CONVERSION]);
}
@@ -186,13 +188,10 @@ public class GroovyCompiler {
if (operation.getClass().getName()
.startsWith(ASTTransformationVisitor.class.getName())) {
conversionOperations.add(i, new CompilationUnit.SourceUnitOperation() {
private final ASTTransformation transformation = new DefaultDependencyCoordinatesAstTransformation();
@Override
public void call(SourceUnit source) throws CompilationFailedException {
this.transformation.visit(new ASTNode[] { source.getAST() },
source);
GroovyCompiler.this.dependencyCoordinatesTransformation.visit(
new ASTNode[] { source.getAST() }, source);
}
});
break;
@@ -312,6 +311,7 @@ public class GroovyCompiler {
.getGroupId(module));
grabAnnotation.setMember("group", groupIdExpression);
}
if (grabAnnotation.getMember("version") == null) {
ConstantExpression versionExpression = new ConstantExpression(
GroovyCompiler.this.artifactCoordinatesResolver

View File

@@ -24,6 +24,11 @@ import java.net.URL;
import java.util.Collections;
import java.util.Properties;
/**
* {@link ArtifactCoordinatesResolver} backed by a properties file.
*
* @author Andy Wilkinson
*/
final class PropertiesArtifactCoordinatesResolver implements ArtifactCoordinatesResolver {
private final GroovyClassLoader loader;
@@ -60,7 +65,7 @@ final class PropertiesArtifactCoordinatesResolver implements ArtifactCoordinates
try {
properties.load(inputStream);
}
catch (IOException ioe) {
catch (IOException ex) {
// Swallow and continue
}
finally {
@@ -68,7 +73,7 @@ final class PropertiesArtifactCoordinatesResolver implements ArtifactCoordinates
}
}
}
catch (IOException e) {
catch (IOException ex) {
// Swallow and continue
}
this.properties = properties;

View File

@@ -25,28 +25,26 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer;
/**
* {@link CompilerAutoConfiguration} for JUnit
*
*
* @author Greg Turnquist
*/
public class JUnitCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "Test");
}
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "Test");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("junit").add("spring-test").add("hamcrest-library");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.junit")
.addStaticStars("org.junit.Assert").addImports()
.addStaticStars("org.hamcrest.MatcherAssert")
.addStaticStars("org.hamcrest.Matchers");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("junit").add("spring-test").add("hamcrest-library");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.junit").addStaticStars("org.junit.Assert")
.addStaticStars("org.hamcrest.MatcherAssert")
.addStaticStars("org.hamcrest.Matchers");
}
}

View File

@@ -25,25 +25,25 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer;
/**
* {@link CompilerAutoConfiguration} for Spock test framework
*
*
* @author Greg Turnquist
*/
public class SpockCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.subclasses(classNode, "Specification");
}
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.subclasses(classNode, "Specification");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("spock-core");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("spock-core");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("spock.lang");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("spock.lang");
}
}