From 97f93bfa645c4d26892f272752bce41cd0e5cf33 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 10 Oct 2013 15:04:04 +0100 Subject: [PATCH] Support simple @Grab annotations everywhere Previously, simple @Grab annotations only worked on classes. This commit updates the simple @Grab support so that they can be used on anything that can be annotated with @Grab. The simplified @Grab support relies upon springcli.properties having been generated. This commit adds an M2E lifecycle mapping for the antrun plugin so that springcli.properties is generated as part of an Eclipse build, thereby making it easier to run tests in Eclipse that rely upon the simplified @Grab support. --- spring-boot-cli/pom.xml | 30 +++++++++++++++++ spring-boot-cli/samples/jms.groovy | 3 +- .../boot/cli/compiler/GroovyCompiler.java | 33 +++++++++++++++---- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/spring-boot-cli/pom.xml b/spring-boot-cli/pom.xml index 436779bd7d..9848d2826b 100644 --- a/spring-boot-cli/pom.xml +++ b/spring-boot-cli/pom.xml @@ -205,6 +205,36 @@ + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + [1.7,) + + run + + + + + + + + + + + + diff --git a/spring-boot-cli/samples/jms.groovy b/spring-boot-cli/samples/jms.groovy index df2a62d475..998bbc450b 100644 --- a/spring-boot-cli/samples/jms.groovy +++ b/spring-boot-cli/samples/jms.groovy @@ -1,8 +1,7 @@ package org.test @Grab("org.apache.activemq:activemq-all:5.4.0") -@Grab("org.apache.activemq:activemq-pool:5.4.0") - +@Grab("activemq-pool") import java.util.concurrent.CountDownLatch @Log diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java index e3be5742a2..18359fc60c 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GroovyCompiler.java @@ -31,8 +31,11 @@ import java.util.List; import java.util.ServiceLoader; import org.codehaus.groovy.ast.ASTNode; +import org.codehaus.groovy.ast.AnnotatedNode; import org.codehaus.groovy.ast.AnnotationNode; +import org.codehaus.groovy.ast.ClassCodeVisitorSupport; import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.ImportNode; import org.codehaus.groovy.ast.ModuleNode; import org.codehaus.groovy.ast.expr.ConstantExpression; import org.codehaus.groovy.ast.expr.Expression; @@ -263,13 +266,31 @@ public class GroovyCompiler { for (ASTNode node : nodes) { if (node instanceof ModuleNode) { ModuleNode module = (ModuleNode) node; - for (ClassNode classNode : module.getClasses()) { - for (AnnotationNode annotationNode : classNode.getAnnotations()) { - if (isGrabAnnotation(annotationNode)) { - transformGrabAnnotationIfNecessary(annotationNode); - } - } + for (ImportNode importNode : module.getImports()) { + visitAnnotatedNode(importNode); } + for (ClassNode classNode : module.getClasses()) { + visitAnnotatedNode(classNode); + classNode.visitContents(new ClassCodeVisitorSupport() { + @Override + protected SourceUnit getSourceUnit() { + return source; + } + + @Override + public void visitAnnotations(AnnotatedNode node) { + visitAnnotatedNode(node); + } + }); + } + } + } + } + + private void visitAnnotatedNode(AnnotatedNode annotatedNode) { + for (AnnotationNode annotationNode : annotatedNode.getAnnotations()) { + if (isGrabAnnotation(annotationNode)) { + transformGrabAnnotationIfNecessary(annotationNode); } } }