Extract closure grabber into AstUtils

pull/946/merge
Dave Syer 11 years ago
parent b71f07932e
commit f1f36cd002

@ -16,6 +16,7 @@
package org.springframework.boot.cli.compiler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@ -25,6 +26,14 @@ import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
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;
/**
* General purpose AST utilities.
@ -101,4 +110,52 @@ public abstract class AstUtils {
return false;
}
public static boolean hasAtLeastOneInterface(ClassNode classNode, String... types) {
Set<String> typesSet = new HashSet<String>(Arrays.asList(types));
for (ClassNode inter : classNode.getInterfaces()) {
if (typesSet.contains(inter.getName())) {
return true;
}
}
return false;
}
/**
* Extract a top-level <code>name</code> closure from inside this block if there is
* one. Removes it from the block at the same time.
* @param block a block statement (class definition)
* @return a beans Closure if one can be found, null otherwise
*/
public static ClosureExpression getClosure(BlockStatement block, String name,
boolean remove) {
for (Statement statement : new ArrayList<Statement>(block.getStatements())) {
if (statement instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) statement).getExpression();
if (expression instanceof MethodCallExpression) {
MethodCallExpression call = (MethodCallExpression) expression;
Expression methodCall = call.getMethod();
if (methodCall instanceof ConstantExpression) {
ConstantExpression method = (ConstantExpression) methodCall;
if (name.equals(method.getValue())) {
ArgumentListExpression arguments = (ArgumentListExpression) call
.getArguments();
if (remove) {
block.getStatements().remove(statement);
}
ClosureExpression closure = (ClosureExpression) arguments
.getExpression(0);
return closure;
}
}
}
}
}
return null;
}
public static ClosureExpression getClosure(BlockStatement block, String name) {
return getClosure(block, name, false);
}
}

@ -25,14 +25,8 @@ import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
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.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
@ -105,29 +99,7 @@ public class GroovyBeansTransformation implements ASTTransformation {
* @return a beans Closure if one can be found, null otherwise
*/
private ClosureExpression beans(BlockStatement block) {
for (Statement statement : new ArrayList<Statement>(block.getStatements())) {
if (statement instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) statement)
.getExpression();
if (expression instanceof MethodCallExpression) {
MethodCallExpression call = (MethodCallExpression) expression;
Expression methodCall = call.getMethod();
if (methodCall instanceof ConstantExpression) {
ConstantExpression method = (ConstantExpression) methodCall;
if (BEANS.equals(method.getValue())) {
ArgumentListExpression arguments = (ArgumentListExpression) call
.getArguments();
block.getStatements().remove(statement);
ClosureExpression closure = (ClosureExpression) arguments
.getExpression(0);
return closure;
}
}
}
}
}
return null;
return AstUtils.getClosure(block, BEANS, true);
}
}
}

Loading…
Cancel
Save