Skip to content

Commit b7f9539

Browse files
committedOct 22, 2022
GROOVY-9771
for #1411
1 parent 587103b commit b7f9539

File tree

10 files changed

+1316
-35
lines changed

10 files changed

+1316
-35
lines changed
 

Diff for: ‎base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,30 @@ public void testCompileStatic28() {
754754
runConformTest(sources);
755755
}
756756

757+
@Test // https://github.com/groovy/groovy-eclipse/issues/1411
758+
public void testCompileStatic29() {
759+
//@formatter:off
760+
String[] sources = {
761+
"Main.groovy",
762+
"@groovy.transform.CompileStatic\n" +
763+
"void test(Pogo pogo) {\n" +
764+
" def key = 'aaa'\n" +
765+
" pogo.map[key] = 1\n" +
766+
"}\n" +
767+
"test(new Pogo())\n",
768+
769+
"Pogo.groovy",
770+
"class Pogo {\n" +
771+
" Map<String,?> getMap() {\n" +
772+
" return [:]\n" +
773+
" }\n" +
774+
"}\n",
775+
};
776+
//@formatter:on
777+
778+
runConformTest(sources);
779+
}
780+
757781
@Test
758782
public void testCompileStatic1505() {
759783
//@formatter:off

Diff for: ‎base/org.codehaus.groovy25/.checkstyle

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<file-match-pattern match-pattern="groovy/classgen/asm/(Optimizing)?StatementWriter.java" include-pattern="false" />
4444
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
4545
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(CallSite|Statement)Writer.java" include-pattern="false" />
46+
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java" include-pattern="false" />
4647
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
4748
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
4849
<file-match-pattern match-pattern="groovy/control/ErrorCollector.java" include-pattern="false" />

Diff for: ‎base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java

+424
Large diffs are not rendered by default.

Diff for: ‎base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java

+26-27
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.groovy.ast.tools.ExpressionUtils;
2323
import org.codehaus.groovy.GroovyBugError;
2424
import org.codehaus.groovy.ast.ASTNode;
25-
import org.codehaus.groovy.ast.ClassHelper;
2625
import org.codehaus.groovy.ast.ClassNode;
2726
import org.codehaus.groovy.ast.FieldNode;
2827
import org.codehaus.groovy.ast.InnerClassNode;
@@ -58,7 +57,6 @@
5857
import groovyjarjarasm.asm.MethodVisitor;
5958
import groovyjarjarasm.asm.Opcodes;
6059

61-
import java.lang.reflect.Modifier;
6260
import java.util.ArrayList;
6361
import java.util.Collection;
6462
import java.util.List;
@@ -86,7 +84,9 @@
8684
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
8785
import static org.codehaus.groovy.ast.ClassHelper.make;
8886
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
87+
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
8988
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
89+
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
9090
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
9191
import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
9292
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
@@ -378,39 +378,38 @@ private boolean makeGetPrivateFieldWithBridgeMethod(final Expression receiver, f
378378
if (field == null && implicitThis && outerClass != null && !receiverType.isStaticClass()) {
379379
Expression pexp;
380380
if (controller.isInClosure()) {
381-
MethodCallExpression mce = new MethodCallExpression(
382-
new VariableExpression("this"),
383-
"getThisObject",
384-
ArgumentListExpression.EMPTY_ARGUMENTS
385-
);
386-
mce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getOutermostClass());
387-
mce.setImplicitThis(true);
388-
mce.setMethodTarget(CLOSURE_GETTHISOBJECT_METHOD);
389-
pexp = new CastExpression(controller.getOutermostClass(),mce);
381+
MethodCallExpression call = callThisX("getThisObject");
382+
call.setImplicitThis(true);
383+
call.setMethodTarget(CLOSURE_GETTHISOBJECT_METHOD);
384+
call.setNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getOutermostClass());
385+
pexp = new CastExpression(controller.getOutermostClass(), call);
390386
} else {
391-
pexp = new PropertyExpression(
392-
new ClassExpression(outerClass),
393-
"this"
394-
);
395-
((PropertyExpression)pexp).setImplicitThis(true);
387+
pexp = new PropertyExpression(classX(outerClass), "this");
396388
}
397389
pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, outerClass);
398390
pexp.setSourcePosition(receiver);
399391
return makeGetPrivateFieldWithBridgeMethod(pexp, outerClass, fieldName, safe, true);
400392
}
401393
ClassNode classNode = controller.getClassNode();
402-
if (field != null && Modifier.isPrivate(field.getModifiers()) && !receiverType.equals(classNode)
403-
&& (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode) || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode,receiverType))) {
394+
if (field != null && field.isPrivate() && !receiverType.equals(classNode)
395+
&& (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)
396+
|| StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode, receiverType))) {
404397
Map<String, MethodNode> accessors = receiverType.redirect().getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS);
405-
if (accessors!=null) {
398+
if (accessors != null) {
406399
MethodNode methodNode = accessors.get(fieldName);
407-
if (methodNode!=null) {
408-
MethodCallExpression mce = new MethodCallExpression(receiver, methodNode.getName(),
409-
new ArgumentListExpression(field.isStatic() ? new ConstantExpression(null) : receiver));
410-
mce.setMethodTarget(methodNode);
411-
mce.setSafe(safe);
412-
mce.setImplicitThis(implicitThis);
413-
mce.visit(controller.getAcg());
400+
if (methodNode != null) {
401+
Expression thisObject;
402+
if (field.isStatic()) {
403+
thisObject = nullX();
404+
} else if (!ExpressionUtils.isThisExpression(receiver)) {
405+
thisObject = receiver;
406+
} else { // GROOVY-7304, GROOVY-9771, GROOVY-9872, et al.
407+
thisObject = new PropertyExpression(classX(receiverType), "this");
408+
}
409+
410+
MethodCallExpression methodCall = callX(classX(receiverType), methodNode.getName(), thisObject);
411+
methodCall.setMethodTarget(methodNode);
412+
methodCall.visit(controller.getAcg());
414413
return true;
415414
}
416415
}
@@ -583,7 +582,7 @@ boolean makeGetField(final Expression receiver, final ClassNode receiverType, fi
583582
}
584583
mv.visitFieldInsn(GETFIELD, BytecodeHelper.getClassInternalName(field.getOwner()), fieldName, BytecodeHelper.getTypeDescription(replacementType));
585584
if (safe) {
586-
if (ClassHelper.isPrimitiveType(replacementType)) {
585+
if (isPrimitiveType(replacementType)) {
587586
operandStack.replace(replacementType);
588587
operandStack.box();
589588
replacementType = operandStack.getTopOperand();

Diff for: ‎base/org.codehaus.groovy30/.checkstyle

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<file-match-pattern match-pattern="groovy/classgen/asm/CompileStack.java" include-pattern="false" />
4545
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
4646
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
47+
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java" include-pattern="false" />
4748
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(CallSite|MethodReferenceExpression|Statement)Writer.java" include-pattern="false" />
4849
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
4950
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />

0 commit comments

Comments
 (0)
Please sign in to comment.