From f1f36cd00260a3c241e565529c9f95382284150b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sun, 30 Mar 2014 07:55:30 +0100 Subject: [PATCH] Extract closure grabber into AstUtils --- .../boot/cli/compiler/AstUtils.java | 57 +++++++++++++++++++ .../compiler/GroovyBeansTransformation.java | 30 +--------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/AstUtils.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/AstUtils.java index a8e60c5bfe..9eaba3eb41 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/AstUtils.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/AstUtils.java @@ -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 typesSet = new HashSet(Arrays.asList(types)); + for (ClassNode inter : classNode.getInterfaces()) { + if (typesSet.contains(inter.getName())) { + return true; + } + } + return false; + } + + /** + * Extract a top-level name 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(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); + } + } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java index a534244cda..04c3a2e3a6 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyBeansTransformation.java @@ -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(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); } } }