Skip to content

Commit

Permalink
refactor compile time transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Brown committed Apr 30, 2012
1 parent af7e7e5 commit 709a6c5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/java/grails/plugin/cache/CacheEvict.java
Expand Up @@ -19,7 +19,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheEvictTransformation")
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface CacheEvict {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/java/grails/plugin/cache/CachePut.java
Expand Up @@ -21,7 +21,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CachePutTransformation")
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface CachePut {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/java/grails/plugin/cache/Cacheable.java
Expand Up @@ -20,7 +20,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheableTransformation")
@GroovyASTTransformationClass("grails.plugin.cache.compiler.CacheTransformation")
public @interface Cacheable {

/**
Expand Down

This file was deleted.

37 changes: 0 additions & 37 deletions src/java/grails/plugin/cache/compiler/CachePutTransformation.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/java/grails/plugin/cache/compiler/CacheTransformation.java
@@ -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;
}
}
37 changes: 0 additions & 37 deletions src/java/grails/plugin/cache/compiler/CacheableTransformation.java

This file was deleted.

0 comments on commit 709a6c5

Please sign in to comment.