Support concise @Grab on all types of imports

Previously, the automatic addition of the group and version to a
@Grab annotation based on the module name would only work on standard
import statements. This commit adds support for this functionality
on wildcard imports, static imports and wildcard static imports.

All of the following are now supported:

@Grab('spring-core')
import org.springframework.util.Assert

@Grab('spring-core')
import org.springframework.util.*

@Grab('spring-core')
import static org.springframework.util.Assert.isTrue

@Grab('spring-core')
import static org.springframework.util.Assert.*
This commit is contained in:
Andy Wilkinson
2013-11-18 12:40:57 +00:00
parent 640b9d2680
commit fd2583ed28
2 changed files with 256 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ import groovy.lang.Grab;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.codehaus.groovy.ast.ASTNode;
@@ -60,9 +61,24 @@ public class ResolveDependencyCoordinatesTransformation implements ASTTransforma
for (ASTNode node : nodes) {
if (node instanceof ModuleNode) {
ModuleNode module = (ModuleNode) node;
visitAnnotatedNode(module.getPackage());
for (ImportNode importNode : module.getImports()) {
visitAnnotatedNode(importNode);
}
for (ImportNode importNode : module.getStarImports()) {
visitAnnotatedNode(importNode);
}
for (Map.Entry<String, ImportNode> entry : module.getStaticImports()
.entrySet()) {
visitAnnotatedNode(entry.getValue());
}
for (Map.Entry<String, ImportNode> entry : module.getStaticStarImports()
.entrySet()) {
visitAnnotatedNode(entry.getValue());
}
for (ClassNode classNode : module.getClasses()) {
visitAnnotatedNode(classNode);
classNode.visitContents(classVisitor);
@@ -72,9 +88,12 @@ public class ResolveDependencyCoordinatesTransformation implements ASTTransforma
}
private void visitAnnotatedNode(AnnotatedNode annotatedNode) {
for (AnnotationNode annotationNode : annotatedNode.getAnnotations()) {
if (GRAB_ANNOTATION_NAMES.contains(annotationNode.getClassNode().getName())) {
transformGrabAnnotation(annotationNode);
if (annotatedNode != null) {
for (AnnotationNode annotationNode : annotatedNode.getAnnotations()) {
if (GRAB_ANNOTATION_NAMES.contains(annotationNode.getClassNode()
.getName())) {
transformGrabAnnotation(annotationNode);
}
}
}
}
@@ -138,5 +157,6 @@ public class ResolveDependencyCoordinatesTransformation implements ASTTransforma
public void visitAnnotations(AnnotatedNode node) {
visitAnnotatedNode(node);
}
}
}