Permalink
Browse files

refactor compile time transformation

  • Loading branch information...
1 parent af7e7e5 commit 709a6c5fa479ab7df88e64b71741497bd6d08fd4 @jeffbrown jeffbrown committed Apr 30, 2012
@@ -19,7 +19,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
-@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheEvictTransformation")
+@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface CacheEvict {
/**
@@ -21,7 +21,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
-@GroovyASTTransformationClass("grails.plugin.cache.compiler.CachePutTransformation")
+@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface CachePut {
/**
@@ -20,7 +20,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
-@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheableTransformation")
+@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface Cacheable {
/**
@@ -1,38 +0,0 @@
-package grails.plugin.cache.compiler;
-
-import java.util.Map;
-
-import org.codehaus.groovy.ast.ASTNode;
-import org.codehaus.groovy.ast.AnnotatedNode;
-import org.codehaus.groovy.ast.AnnotationNode;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.control.CompilePhase;
-import org.codehaus.groovy.control.SourceUnit;
-import org.codehaus.groovy.transform.ASTTransformation;
-import org.codehaus.groovy.transform.GroovyASTTransformation;
-
-
-@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
-public class CacheEvictTransformation implements ASTTransformation{
-
- public void visit(final ASTNode[] astNodes, final SourceUnit sourceUnit) {
-
- final ASTNode firstNode = astNodes[0];
- final ASTNode secondNode = astNodes[1];
- if (!(firstNode instanceof AnnotationNode) || !(secondNode instanceof AnnotatedNode)) {
- throw new RuntimeException("Internal error: wrong types: " + firstNode.getClass().getName() + " / " + secondNode.getClass().getName());
- }
-
- final AnnotationNode grailsCachableAnnotationNode = (AnnotationNode) firstNode;
- final AnnotatedNode parent = (AnnotatedNode) secondNode;
- final Map<String, Expression> grailsAnnotationMembers = grailsCachableAnnotationNode.getMembers();
-
- final AnnotationNode springCacheEvictAnnotationNode = new AnnotationNode(new ClassNode(org.springframework.cache.annotation.CacheEvict.class));
- for(Map.Entry<String, Expression> entry : grailsAnnotationMembers.entrySet()) {
- springCacheEvictAnnotationNode.addMember(entry.getKey(), entry.getValue());
- }
- parent.addAnnotation(springCacheEvictAnnotationNode);
- }
-}
-
@@ -1,37 +0,0 @@
-package grails.plugin.cache.compiler;
-
-import java.util.Map;
-
-import org.codehaus.groovy.ast.ASTNode;
-import org.codehaus.groovy.ast.AnnotatedNode;
-import org.codehaus.groovy.ast.AnnotationNode;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.control.CompilePhase;
-import org.codehaus.groovy.control.SourceUnit;
-import org.codehaus.groovy.transform.ASTTransformation;
-import org.codehaus.groovy.transform.GroovyASTTransformation;
-
-@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
-public class CachePutTransformation implements ASTTransformation{
-
- public void visit(final ASTNode[] astNodes, final SourceUnit sourceUnit) {
-
- final ASTNode firstNode = astNodes[0];
- final ASTNode secondNode = astNodes[1];
- if (!(firstNode instanceof AnnotationNode) || !(secondNode instanceof AnnotatedNode)) {
- throw new RuntimeException("Internal error: wrong types: " + firstNode.getClass().getName() + " / " + secondNode.getClass().getName());
- }
-
- final AnnotationNode grailsCachableAnnotationNode = (AnnotationNode) firstNode;
- final AnnotatedNode parent = (AnnotatedNode) secondNode;
- final Map<String, Expression> grailsAnnotationMembers = grailsCachableAnnotationNode.getMembers();
-
- final AnnotationNode springCachePutAnnotationNode = new AnnotationNode(new ClassNode(org.springframework.cache.annotation.CachePut.class));
- for(Map.Entry<String, Expression> entry : grailsAnnotationMembers.entrySet()) {
- springCachePutAnnotationNode.addMember(entry.getKey(), entry.getValue());
- }
- parent.addAnnotation(springCachePutAnnotationNode);
- }
-
-}
@@ -0,0 +1,50 @@
+package grails.plugin.cache.compiler;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.groovy.ast.ASTNode;
+import org.codehaus.groovy.ast.AnnotatedNode;
+import org.codehaus.groovy.ast.AnnotationNode;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.control.CompilePhase;
+import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.transform.ASTTransformation;
+import org.codehaus.groovy.transform.GroovyASTTransformation;
+
+@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
+public class CacheTransformation implements ASTTransformation {
+
+ @SuppressWarnings("serial")
+ private static final Map<ClassNode, Class<? extends Annotation>> GRAILS_ANNOTATION_CLASS_NODE_TO_SPRING_ANNOTATION = new HashMap<ClassNode, Class<? extends Annotation>>(){{
+ put(new ClassNode(grails.plugin.cache.Cacheable.class), org.springframework.cache.annotation.Cacheable.class);
+ put(new ClassNode(grails.plugin.cache.CachePut.class), org.springframework.cache.annotation.CachePut.class);
+ put(new ClassNode(grails.plugin.cache.CacheEvict.class), org.springframework.cache.annotation.CacheEvict.class);
+ }};
+
+ public void visit(final ASTNode[] astNodes, final SourceUnit sourceUnit) {
+ final ASTNode firstNode = astNodes[0];
+ final ASTNode secondNode = astNodes[1];
+ if (!(firstNode instanceof AnnotationNode) || !(secondNode instanceof AnnotatedNode)) {
+ throw new RuntimeException("Internal error: wrong types: " + firstNode.getClass().getName() + " / " + secondNode.getClass().getName());
+ }
+
+ final AnnotationNode grailsCacheAnnotationNode = (AnnotationNode) firstNode;
+ final AnnotatedNode parent = (AnnotatedNode) secondNode;
+ final AnnotationNode springCacheableAnnotationNode = getCorrespondingSpringAnnotation(grailsCacheAnnotationNode);
+ parent.addAnnotation(springCacheableAnnotationNode);
+ }
+
+ protected AnnotationNode getCorrespondingSpringAnnotation(final AnnotationNode grailsCacheAnnotationNode) {
+ final Map<String, Expression> grailsAnnotationMembers = grailsCacheAnnotationNode.getMembers();
+
+ final Class<? extends Annotation> springAnnotationClass = GRAILS_ANNOTATION_CLASS_NODE_TO_SPRING_ANNOTATION.get(grailsCacheAnnotationNode.getClassNode());
+ final AnnotationNode springCacheableAnnotationNode = new AnnotationNode(new ClassNode(springAnnotationClass));
+ for(Map.Entry<String, Expression> entry : grailsAnnotationMembers.entrySet()) {
+ springCacheableAnnotationNode.addMember(entry.getKey(), entry.getValue());
+ }
+ return springCacheableAnnotationNode;
+ }
+}
@@ -1,37 +0,0 @@
-package grails.plugin.cache.compiler;
-
-import java.util.Map;
-
-import org.codehaus.groovy.ast.ASTNode;
-import org.codehaus.groovy.ast.AnnotatedNode;
-import org.codehaus.groovy.ast.AnnotationNode;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.control.CompilePhase;
-import org.codehaus.groovy.control.SourceUnit;
-import org.codehaus.groovy.transform.ASTTransformation;
-import org.codehaus.groovy.transform.GroovyASTTransformation;
-
-@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
-public class CacheableTransformation implements ASTTransformation{
-
- public void visit(final ASTNode[] astNodes, final SourceUnit sourceUnit) {
-
- final ASTNode firstNode = astNodes[0];
- final ASTNode secondNode = astNodes[1];
- if (!(firstNode instanceof AnnotationNode) || !(secondNode instanceof AnnotatedNode)) {
- throw new RuntimeException("Internal error: wrong types: " + firstNode.getClass().getName() + " / " + secondNode.getClass().getName());
- }
-
- final AnnotationNode grailsCachableAnnotationNode = (AnnotationNode) firstNode;
- final AnnotatedNode parent = (AnnotatedNode) secondNode;
- final Map<String, Expression> grailsAnnotationMembers = grailsCachableAnnotationNode.getMembers();
-
- final AnnotationNode springCacheableAnnotationNode = new AnnotationNode(new ClassNode(org.springframework.cache.annotation.Cacheable.class));
- for(Map.Entry<String, Expression> entry : grailsAnnotationMembers.entrySet()) {
- springCacheableAnnotationNode.addMember(entry.getKey(), entry.getValue());
- }
- parent.addAnnotation(springCacheableAnnotationNode);
- }
-
-}

0 comments on commit 709a6c5

Please sign in to comment.