Skip to content

Commit 6cf95da

Browse files
committed
GROOVY-8049, GROOVY-9386, GROOVY-11641: SC: dynamic resolution
- don't transform property with `DYNAMIC_RESOLUTION` to a method call
1 parent f1b3f72 commit 6cf95da

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/main/java/org/codehaus/groovy/transform/sc/transformers/PropertyExpressionTransformer.java

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class PropertyExpressionTransformer {
4040
}
4141

4242
Expression transformPropertyExpression(final PropertyExpression pe) {
43+
if (pe.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION) != null) {
44+
return pe.transformExpression(scTransformer); // GROOVY-11641, etc.
45+
}
46+
4347
MethodNode dmct = pe.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
4448
// NOTE: BinaryExpressionTransformer handles the setter
4549
if (dmct != null && dmct.getParameters().length == 0) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public Expression transform(final Expression exp) {
122122
propertyExpression.getProperty().setSourcePosition(exp);
123123
return propertyExpression;
124124
}
125-
} else if (accessedVariable instanceof DynamicVariable && !inClosure) { // GROOVY-9386
125+
} else if (accessedVariable instanceof DynamicVariable && !inClosure) { // GROOVY-8049, GROOVY-9386
126126
PropertyExpression propertyExpression = propX(varX(weaved), vexp.getName());
127127
propertyExpression.putNodeMetaData(DYNAMIC_RESOLUTION, Boolean.TRUE);
128128
propertyExpression.getProperty().setSourcePosition(exp);

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

+38-8
Original file line numberDiff line numberDiff line change
@@ -3749,43 +3749,73 @@ final class TraitASTTransformationTest {
37493749
}
37503750

37513751
// GROOVY-9386
3752-
@Test
3753-
void testTraitPropertyInitializedByTap() {
3754-
assertScript shell, '''
3752+
@CompileModesTest
3753+
void testTraitPropertyInitializedByTap(String mode) {
3754+
assertScript shell, """
37553755
class P {
37563756
int prop
37573757
}
3758+
$mode
37583759
trait T {
37593760
P pogo = new P().tap {
37603761
prop = 42 // MissingPropertyException: No such property: prop for class: C
37613762
}
37623763
}
3764+
$mode
37633765
class C implements T {
37643766
}
37653767
37663768
def pogo = new C().pogo
37673769
assert pogo.prop == 42
3768-
'''
3770+
"""
37693771
}
37703772

37713773
// GROOVY-9386
3772-
@Test
3773-
void testTraitPropertyInitializedByWith() {
3774-
assertScript shell, '''
3774+
@CompileModesTest
3775+
void testTraitPropertyInitializedByWith(String mode) {
3776+
assertScript shell, """
37753777
class P {
37763778
int prop
37773779
}
3780+
$mode
37783781
trait T {
37793782
P pogo = new P().with {
37803783
prop = 42 // MissingPropertyException: No such property: prop for class: C
37813784
return it
37823785
}
37833786
}
3787+
$mode
37843788
class C implements T {
37853789
}
3790+
37863791
def pogo = new C().pogo
37873792
assert pogo.prop == 42
3788-
'''
3793+
"""
3794+
}
3795+
3796+
// GROOVY-8049
3797+
@CompileModesTest
3798+
void testTraitPropertyAsReceiverForWith(String mode) {
3799+
assertScript shell, """
3800+
interface I {
3801+
String getJay()
3802+
}
3803+
$mode
3804+
trait T {
3805+
abstract I getEye()
3806+
String m() {
3807+
eye.with {
3808+
jay.toUpperCase()
3809+
}
3810+
}
3811+
}
3812+
$mode
3813+
class C implements T {
3814+
I eye = { -> 'works' }
3815+
}
3816+
3817+
assert new C().m() == 'WORKS'
3818+
"""
37893819
}
37903820

37913821
// GROOVY-8000

0 commit comments

Comments
 (0)