Skip to content

Commit

Permalink
GRAILS-8426 - Move some code to GrailsASTUtils in preparation for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Scott Brown committed Jan 10, 2014
1 parent 1c65c10 commit 3799b20
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package org.codehaus.groovy.grails.compiler.injection;

import grails.artefact.Artefact;
import grails.artefact.Enhanced;
import grails.util.GrailsUtil;
import groovy.lang.Mixin;

import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -70,7 +68,6 @@ public abstract class AbstractGrailsArtefactTransformer implements GrailsArtefac
private static final String INSTANCE_PREFIX = "instance";
private static final String STATIC_PREFIX = "static";
private static final AnnotationNode AUTO_WIRED_ANNOTATION = new AnnotationNode(new ClassNode(Autowired.class));
private static final ClassNode ENHANCED_CLASS_NODE = new ClassNode(Enhanced.class);

protected static final ClassNode OBJECT_CLASS = new ClassNode(Object.class);

Expand Down Expand Up @@ -206,17 +203,13 @@ protected void performStaticImplementationInjection(ClassNode classNode,
}

protected void addEnhancedAnnotation(ClassNode classNode) {
if (classNode.getAnnotations(ENHANCED_CLASS_NODE).isEmpty()) {
final AnnotationNode annotationNode = new AnnotationNode(ENHANCED_CLASS_NODE);
annotationNode.setMember("version", new ConstantExpression(GrailsUtil.getGrailsVersion()));
classNode.addAnnotation(annotationNode);

AnnotationNode annotation = GrailsASTUtils.findAnnotation(classNode, Mixin.class);
if (annotation != null) {
Expression value = annotation.getMember("value");
if (value != null) {
annotationNode.setMember("mixins", value);
}
final AnnotationNode annotationNode = GrailsASTUtils.addEnhancedAnnotation(classNode);

AnnotationNode annotation = GrailsASTUtils.findAnnotation(classNode, Mixin.class);
if (annotation != null) {
Expression value = annotation.getMember("value");
if (value != null) {
annotationNode.setMember("mixins", value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.codehaus.groovy.grails.compiler.injection;

import grails.artefact.Enhanced;
import grails.build.logging.GrailsConsole;
import grails.persistence.Entity;
import grails.util.GrailsNameUtils;
import grails.util.GrailsUtil;
import groovy.lang.Closure;
import groovy.lang.MissingMethodException;
import groovy.transform.CompileStatic;
Expand Down Expand Up @@ -110,6 +112,7 @@ public class GrailsASTUtils {
public static final Token LOGICAL_AND_OPERATOR = Token.newSymbol("&&", 0, 0);
public static final Token NOT_EQUALS_OPERATOR = Token.newSymbol("!=", 0, 0);

private static final ClassNode ENHANCED_CLASS_NODE = new ClassNode(Enhanced.class);
public static final ClassNode MISSING_METHOD_EXCEPTION = new ClassNode(MissingMethodException.class);
public static final ConstantExpression NULL_EXPRESSION = new ConstantExpression(null);
public static final Token ASSIGNMENT_OPERATOR = Token.newSymbol(Types.ASSIGNMENT_OPERATOR, 0, 0);
Expand Down Expand Up @@ -794,6 +797,20 @@ public static void addAnnotationIfNecessary(ClassNode classNode, Class<Entity> e
if (!foundAnn) classNode.addAnnotation(annotationToAdd);
}
}

public static AnnotationNode addEnhancedAnnotation(ClassNode classNode) {
final AnnotationNode annotationNode;
final List<AnnotationNode> annotations = classNode.getAnnotations(ENHANCED_CLASS_NODE);
if (annotations.isEmpty()) {
annotationNode = new AnnotationNode(ENHANCED_CLASS_NODE);
annotationNode.setMember("version", new ConstantExpression(GrailsUtil.getGrailsVersion()));
classNode.addAnnotation(annotationNode);
} else {
annotationNode = annotations.get(0);
}

return annotationNode;
}

public static AnnotationNode findAnnotation(ClassNode annotationClassNode, List<AnnotationNode> annotations) {
for (AnnotationNode annotation : annotations) {
Expand Down

0 comments on commit 3799b20

Please sign in to comment.