Skip to content

Commit 695af25

Browse files
committed
GROOVY-7456, GROOVY-7797: trait method invocation within closure
1 parent d03b94b commit 695af25

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
5353
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
5454
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
55+
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
5556
import static org.codehaus.groovy.ast.tools.GeneralUtils.isInstanceOfX;
5657
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
5758
import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX;
@@ -217,7 +218,7 @@ private Expression transformBinaryExpression(final BinaryExpression exp, final C
217218
super.transform(rightExpression)
218219
);
219220
mce.setImplicitThis(false);
220-
mce.setSourcePosition(exp);
221+
mce.setSourcePosition(leftExpression instanceof PropertyExpression ? ((PropertyExpression) leftExpression).getProperty() : leftExpression);
221222
markDynamicCall(mce, staticField, isStatic);
222223
return mce;
223224
}
@@ -240,7 +241,7 @@ private Expression transformFieldReference(final Expression exp, final FieldNode
240241

241242
MethodCallExpression mce = callX(receiver, Traits.helperGetterName(fn));
242243
mce.setImplicitThis(false);
243-
mce.setSourcePosition(exp);
244+
mce.setSourcePosition(exp instanceof PropertyExpression ? ((PropertyExpression) exp).getProperty() : exp);
244245
markDynamicCall(mce, fn, isStatic);
245246
return mce;
246247
}
@@ -288,9 +289,9 @@ private Expression transformSuperMethodCall(final MethodCallExpression call) {
288289
Traits.getSuperTraitMethodName(traitClass, method),
289290
superCallArgs
290291
);
291-
newCall.setSourcePosition(call);
292-
newCall.setSafe(call.isSafe());
292+
newCall.getMethod().setSourcePosition(call.getMethod());
293293
newCall.setSpreadSafe(call.isSpreadSafe());
294+
newCall.setSafe(call.isSafe());
294295
newCall.setImplicitThis(false);
295296
return newCall;
296297
}
@@ -304,9 +305,9 @@ private Expression transformMethodCallOnThis(final MethodCallExpression call) {
304305
// GROOVY-7191, GROOVY-7213, GROOVY-7214, GROOVY-8282, GROOVY-8854, GROOVY-8859, et al.
305306
for (MethodNode methodNode : traitClass.getDeclaredMethods(call.getMethodAsString())) {
306307
if (methodNode.isPrivate()) {
307-
// this.m(x) --> this.m($self or $static$self or (Class) $self.getClass(), x)
308+
// this.m(x) --> (this or T$Trait$Helper).m($self or $static$self or (Class) $self.getClass(), x)
308309
Expression selfClassOrObject = methodNode.isStatic() && !ClassHelper.isClassType(weaved.getOriginType()) ? castX(ClassHelper.CLASS_Type.getPlainNodeReference(), callX(weaved, "getClass")) : weaved;
309-
MethodCallExpression newCall = callX(thisExpr, method, createArgumentList(selfClassOrObject, arguments));
310+
MethodCallExpression newCall = callX(inClosure ? classX(traitHelperClass) : thisExpr, method, createArgumentList(selfClassOrObject, arguments));
310311
newCall.setGenericsTypes(call.getGenericsTypes());
311312
newCall.setImplicitThis(call.isImplicitThis());
312313
newCall.setSpreadSafe(call.isSpreadSafe());

src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ final class TraitWithClosureOrLambda {
2727

2828
private final GroovyShell shell = GroovyShell.withConfig {
2929
ast groovy.transform.CompileStatic
30+
imports {
31+
star 'groovy.transform'
32+
}
3033
}
3134

3235
@Test
@@ -179,11 +182,12 @@ final class TraitWithClosureOrLambda {
179182
'''
180183
}
181184

182-
// GROOVY-7456, GROOVY-7797
185+
// GROOVY-7456, GROOVY-7797
183186
@Test
184187
void testInvokePrivateTraitMethodFromTraitClosure() {
185188
assertScript shell, '''
186189
trait T {
190+
@CompileDynamic
187191
def f() {
188192
['a'].collect { String s -> g(s) }
189193
}
@@ -390,7 +394,7 @@ final class TraitWithClosureOrLambda {
390394
void testInvokeTraitStaticMethodFromTraitClosure() {
391395
assertScript shell, '''
392396
trait T {
393-
@groovy.transform.CompileDynamic
397+
@CompileDynamic
394398
static one() {
395399
def me = this
396400
two('good') + ' ' +

0 commit comments

Comments
 (0)