Skip to content

Commit 9a38166

Browse files
committed
solution for GRAILS-8652 "classes lose their mixins during unit tests"
1 parent 21d9965 commit 9a38166

File tree

4 files changed

+215
-1
lines changed

4 files changed

+215
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 grails.util;
18+
19+
import org.codehaus.groovy.grails.compiler.injection.MixinTransformation;
20+
import org.codehaus.groovy.transform.GroovyASTTransformationClass;
21+
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
@java.lang.annotation.Documented
28+
@Retention(RetentionPolicy.SOURCE)
29+
@Target(ElementType.TYPE)
30+
@GroovyASTTransformationClass("org.codehaus.groovy.grails.compiler.injection.MixinTransformation")
31+
public @interface Mixin {
32+
Class [] value ();
33+
}

grails-core/src/main/groovy/org/codehaus/groovy/grails/commons/GrailsClassUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,10 @@ public static String getPropertyForGetter(String getterName) {
939939
}
940940

941941
private static String convertPropertyName(String prop) {
942-
if (Character.isUpperCase(prop.charAt(0)) && Character.isUpperCase(prop.charAt(1))) {
942+
if(prop.length() == 1) {
943+
return String.valueOf(Character.toLowerCase(prop.charAt(0)));
944+
}
945+
else if (Character.isUpperCase(prop.charAt(0)) && Character.isUpperCase(prop.charAt(1))) {
943946
return prop;
944947
}
945948
if (Character.isDigit(prop.charAt(0))) {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+

grails-plugin-testing/src/test/groovy/grails/test/mixin/MetaClassCleanupSpec.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import grails.test.mixin.support.GrailsUnitTestMixin
55
import org.junit.After
66
import org.junit.AfterClass
77
import org.junit.Assert
8+
import grails.util.Mixin
89

910

1011
@TestMixin(GrailsUnitTestMixin)
@@ -55,6 +56,27 @@ class MetaClassCleanupSpec extends Specification {
5556
greeting == 'goodbye'
5657
}
5758

59+
60+
def "Test that mixins are re-applied after cleanup - step 1"() {
61+
given:"A mixin class"
62+
def a = new A()
63+
64+
when:"A method is called that uses a mixin"
65+
def rs = a.doStuff()
66+
67+
then:"The the mixin method works"
68+
rs == "A with mixin: mixMe from AMixin mix static"
69+
}
70+
71+
def "Test that mixins are re-applied after cleanup - step 2"() {
72+
given:"A mixin class"
73+
def a = new A()
74+
when:"A method is called that uses a mixin"
75+
def rs = a.doStuff()
76+
77+
then:"The the mixin method works"
78+
rs == "A with mixin: mixMe from AMixin mix static"
79+
}
5880
@AfterClass
5981
static void checkCleanup() {
6082
def a = new Author()
@@ -86,3 +108,16 @@ class HelloService {
86108
}
87109

88110
}
111+
112+
class AMixin {
113+
String prop = "foo"
114+
String mixMe() {"mixMe from AMixin"}
115+
static mixStatic() { "mix static"}
116+
}
117+
118+
@Mixin(AMixin)
119+
class A {
120+
String doStuff() {
121+
"A with mixin: ${mixMe()} ${mixStatic()}"
122+
}
123+
}

0 commit comments

Comments
 (0)