|
| 1 | +/* |
| 2 | + * Copyright 2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.codehaus.groovy.grails.compiler.injection; |
| 18 | + |
| 19 | +import grails.util.GrailsNameUtils; |
| 20 | +import grails.util.Mixin; |
| 21 | +import groovy.lang.GroovyObjectSupport; |
| 22 | +import org.codehaus.groovy.ast.*; |
| 23 | +import org.codehaus.groovy.ast.expr.ClassExpression; |
| 24 | +import org.codehaus.groovy.ast.expr.Expression; |
| 25 | +import org.codehaus.groovy.ast.expr.ListExpression; |
| 26 | +import org.codehaus.groovy.ast.expr.VariableExpression; |
| 27 | +import org.codehaus.groovy.control.CompilePhase; |
| 28 | +import org.codehaus.groovy.control.SourceUnit; |
| 29 | +import org.codehaus.groovy.transform.ASTTransformation; |
| 30 | +import org.codehaus.groovy.transform.GroovyASTTransformation; |
| 31 | + |
| 32 | +import java.lang.reflect.Modifier; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * The logic for the {@link grails.util.Mixin} location transform. |
| 37 | + * |
| 38 | + * @author Graeme Rocher |
| 39 | + * @since 2.1.2 |
| 40 | + */ |
| 41 | +@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION) |
| 42 | +public class MixinTransformation implements ASTTransformation { |
| 43 | + |
| 44 | + public static final ClassNode GROOVY_OBJECT_CLASS_NODE = new ClassNode(GroovyObjectSupport.class); |
| 45 | + private static final ClassNode MY_TYPE = new ClassNode(Mixin.class); |
| 46 | + private static final String MY_TYPE_NAME = "@" + MY_TYPE.getNameWithoutPackage(); |
| 47 | + public static final String OBJECT_CLASS = "java.lang.Object"; |
| 48 | + |
| 49 | + public void visit(ASTNode[] astNodes, SourceUnit source) { |
| 50 | + if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) { |
| 51 | + throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class"); |
| 52 | + } |
| 53 | + |
| 54 | + AnnotatedNode parent = (AnnotatedNode) astNodes[1]; |
| 55 | + AnnotationNode node = (AnnotationNode) astNodes[0]; |
| 56 | + if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + ClassNode classNode = (ClassNode) parent; |
| 61 | + String cName = classNode.getName(); |
| 62 | + if (classNode.isInterface()) { |
| 63 | + throw new RuntimeException("Error processing interface '" + cName + "'. " + |
| 64 | + MY_TYPE_NAME + " not allowed for interfaces."); |
| 65 | + } |
| 66 | + |
| 67 | + ListExpression values = getListOfClasses(node); |
| 68 | + |
| 69 | + weaveMixinsIntoClass(classNode, values); |
| 70 | + |
| 71 | + } |
| 72 | + public void weaveMixinsIntoClass(ClassNode classNode, ListExpression values) { |
| 73 | + if (values != null) { |
| 74 | + for (Expression current : values.getExpressions()) { |
| 75 | + if (current instanceof ClassExpression) { |
| 76 | + ClassExpression ce = (ClassExpression) current; |
| 77 | + |
| 78 | + ClassNode mixinClassNode = ce.getType(); |
| 79 | + |
| 80 | + final String fieldName = '$' + GrailsNameUtils.getPropertyName(mixinClassNode.getName()); |
| 81 | + |
| 82 | + GrailsASTUtils.addFieldIfNonExistent(classNode, mixinClassNode, fieldName); |
| 83 | + VariableExpression fieldReference = new VariableExpression(fieldName); |
| 84 | + |
| 85 | + while (!mixinClassNode.getName().equals(OBJECT_CLASS)) { |
| 86 | + final List<MethodNode> mixinMethods = mixinClassNode.getMethods(); |
| 87 | + |
| 88 | + for (MethodNode mixinMethod : mixinMethods) { |
| 89 | + if (isCandidateMethod(mixinMethod) && !hasDeclaredMethod(classNode, mixinMethod)) { |
| 90 | + if (mixinMethod.isStatic()) { |
| 91 | + MethodNode methodNode = GrailsASTUtils.addDelegateStaticMethod(classNode, mixinMethod); |
| 92 | + } |
| 93 | + else { |
| 94 | + MethodNode methodNode = GrailsASTUtils.addDelegateInstanceMethod(classNode, fieldReference, mixinMethod, false); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + List<PropertyNode> properties = mixinClassNode.getProperties(); |
| 100 | + System.out.println("properties = " + properties); |
| 101 | + for (PropertyNode property : properties) { |
| 102 | + System.out.println("property = " + property); |
| 103 | + } |
| 104 | + |
| 105 | + mixinClassNode = mixinClassNode.getSuperClass(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + protected boolean hasDeclaredMethod(ClassNode classNode, MethodNode mixinMethod) { |
| 113 | + return classNode.hasDeclaredMethod(mixinMethod.getName(), mixinMethod.getParameters()); |
| 114 | + } |
| 115 | + protected ListExpression getListOfClasses(AnnotationNode node) { |
| 116 | + Expression value = node.getMember("value"); |
| 117 | + ListExpression values = null; |
| 118 | + if (value instanceof ListExpression) { |
| 119 | + values = (ListExpression) value; |
| 120 | + } else if (value instanceof ClassExpression) { |
| 121 | + values = new ListExpression(); |
| 122 | + values.addExpression(value); |
| 123 | + } |
| 124 | + |
| 125 | + return values; |
| 126 | + } |
| 127 | + |
| 128 | + protected boolean isCandidateMethod(MethodNode declaredMethod) { |
| 129 | + return isAddableMethod(declaredMethod); |
| 130 | + } |
| 131 | + |
| 132 | + public static boolean isAddableMethod(MethodNode declaredMethod) { |
| 133 | + ClassNode groovyMethods = GROOVY_OBJECT_CLASS_NODE; |
| 134 | + String methodName = declaredMethod.getName(); |
| 135 | + return !declaredMethod.isSynthetic() && |
| 136 | + !methodName.contains("$") && |
| 137 | + Modifier.isPublic(declaredMethod.getModifiers()) && |
| 138 | + !Modifier.isAbstract(declaredMethod.getModifiers()) && |
| 139 | + !groovyMethods.hasMethod(declaredMethod.getName(), declaredMethod.getParameters()); |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | + |
0 commit comments