Skip to content

Commit

Permalink
GROOVY-9771
Browse files Browse the repository at this point in the history
for #1411
  • Loading branch information
eric-milles committed Oct 22, 2022
1 parent 587103b commit b7f9539
Show file tree
Hide file tree
Showing 10 changed files with 1,316 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,30 @@ public void testCompileStatic28() {
runConformTest(sources);
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1411
public void testCompileStatic29() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"void test(Pogo pogo) {\n" +
" def key = 'aaa'\n" +
" pogo.map[key] = 1\n" +
"}\n" +
"test(new Pogo())\n",

"Pogo.groovy",
"class Pogo {\n" +
" Map<String,?> getMap() {\n" +
" return [:]\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources);
}

@Test
public void testCompileStatic1505() {
//@formatter:off
Expand Down
1 change: 1 addition & 0 deletions base/org.codehaus.groovy25/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<file-match-pattern match-pattern="groovy/classgen/asm/(Optimizing)?StatementWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(CallSite|Statement)Writer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/ErrorCollector.java" include-pattern="false" />
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.groovy.ast.tools.ExpressionUtils;
import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.InnerClassNode;
Expand Down Expand Up @@ -58,7 +57,6 @@
import groovyjarjarasm.asm.MethodVisitor;
import groovyjarjarasm.asm.Opcodes;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -86,7 +84,9 @@
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
import static org.codehaus.groovy.ast.ClassHelper.make;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
Expand Down Expand Up @@ -378,39 +378,38 @@ private boolean makeGetPrivateFieldWithBridgeMethod(final Expression receiver, f
if (field == null && implicitThis && outerClass != null && !receiverType.isStaticClass()) {
Expression pexp;
if (controller.isInClosure()) {
MethodCallExpression mce = new MethodCallExpression(
new VariableExpression("this"),
"getThisObject",
ArgumentListExpression.EMPTY_ARGUMENTS
);
mce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getOutermostClass());
mce.setImplicitThis(true);
mce.setMethodTarget(CLOSURE_GETTHISOBJECT_METHOD);
pexp = new CastExpression(controller.getOutermostClass(),mce);
MethodCallExpression call = callThisX("getThisObject");
call.setImplicitThis(true);
call.setMethodTarget(CLOSURE_GETTHISOBJECT_METHOD);
call.setNodeMetaData(StaticTypesMarker.INFERRED_TYPE, controller.getOutermostClass());
pexp = new CastExpression(controller.getOutermostClass(), call);
} else {
pexp = new PropertyExpression(
new ClassExpression(outerClass),
"this"
);
((PropertyExpression)pexp).setImplicitThis(true);
pexp = new PropertyExpression(classX(outerClass), "this");
}
pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, outerClass);
pexp.setSourcePosition(receiver);
return makeGetPrivateFieldWithBridgeMethod(pexp, outerClass, fieldName, safe, true);
}
ClassNode classNode = controller.getClassNode();
if (field != null && Modifier.isPrivate(field.getModifiers()) && !receiverType.equals(classNode)
&& (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode) || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode,receiverType))) {
if (field != null && field.isPrivate() && !receiverType.equals(classNode)
&& (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)
|| StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode, receiverType))) {
Map<String, MethodNode> accessors = receiverType.redirect().getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS);
if (accessors!=null) {
if (accessors != null) {
MethodNode methodNode = accessors.get(fieldName);
if (methodNode!=null) {
MethodCallExpression mce = new MethodCallExpression(receiver, methodNode.getName(),
new ArgumentListExpression(field.isStatic() ? new ConstantExpression(null) : receiver));
mce.setMethodTarget(methodNode);
mce.setSafe(safe);
mce.setImplicitThis(implicitThis);
mce.visit(controller.getAcg());
if (methodNode != null) {
Expression thisObject;
if (field.isStatic()) {
thisObject = nullX();
} else if (!ExpressionUtils.isThisExpression(receiver)) {
thisObject = receiver;
} else { // GROOVY-7304, GROOVY-9771, GROOVY-9872, et al.
thisObject = new PropertyExpression(classX(receiverType), "this");
}

MethodCallExpression methodCall = callX(classX(receiverType), methodNode.getName(), thisObject);
methodCall.setMethodTarget(methodNode);
methodCall.visit(controller.getAcg());
return true;
}
}
Expand Down Expand Up @@ -583,7 +582,7 @@ boolean makeGetField(final Expression receiver, final ClassNode receiverType, fi
}
mv.visitFieldInsn(GETFIELD, BytecodeHelper.getClassInternalName(field.getOwner()), fieldName, BytecodeHelper.getTypeDescription(replacementType));
if (safe) {
if (ClassHelper.isPrimitiveType(replacementType)) {
if (isPrimitiveType(replacementType)) {
operandStack.replace(replacementType);
operandStack.box();
replacementType = operandStack.getTopOperand();
Expand Down
1 change: 1 addition & 0 deletions base/org.codehaus.groovy30/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<file-match-pattern match-pattern="groovy/classgen/asm/CompileStack.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesBinaryExpressionMultiTypeDispatcher.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypes(CallSite|MethodReferenceExpression|Statement)Writer.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
Expand Down
Loading

0 comments on commit b7f9539

Please sign in to comment.