Skip to content

Commit

Permalink
GROOVY-5568, GROOVY-10815
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 9, 2022
1 parent 160d839 commit 36bdce2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7954,4 +7954,27 @@ public void testCompileStatic10807() {

runConformTest(sources, "[bar, baz, foo]");
}

@Test
public void testCompileStatic10815() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"void test() {\n" +
" Pogo pogo = new Pogo(name:'Frank Grimes')\n" +
" def props = pogo.properties\n" +
" print props.keySet().sort()\n" +
"}\n" +
"test()\n",

"Pogo.groovy",
"class Pogo {\n" +
" String name\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "[class, name]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@

import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.BigInteger_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Boolean_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.CLASS_Type;
import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.GROOVY_OBJECT_TYPE;
Expand All @@ -90,7 +89,6 @@
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;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isClassClassNodeWrappingConcreteType;
Expand Down Expand Up @@ -214,7 +212,7 @@ public void makeGetPropertySite(Expression receiver, final String propertyName,
}

}

/* GRECLIPSE edit
// GROOVY-5568, we would be facing a DGM call, but instead of foo.getText(), have foo.text
List<MethodNode> methods = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, getterName, ClassNode.EMPTY_ARRAY);
for (MethodNode m: findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, altGetterName, ClassNode.EMPTY_ARRAY)) {
Expand All @@ -237,7 +235,7 @@ public void makeGetPropertySite(Expression receiver, final String propertyName,
return;
}
}

*/
if (!isStaticProperty && isOrImplements(receiverType, LIST_TYPE)) {
writeListDotProperty(receiver, propertyName, safe);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import static org.apache.groovy.util.BeanUtils.capitalize;
import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.BigInteger_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.Boolean_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.CLASS_Type;
import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.GROOVY_OBJECT_TYPE;
Expand Down Expand Up @@ -90,7 +89,6 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isClassClassNodeWrappingConcreteType;
Expand Down Expand Up @@ -201,7 +199,7 @@ public void makeGetPropertySite(final Expression receiver, final String property
return;
}
}

/* GRECLIPSE edit
// GROOVY-5568: we would be facing a DGM call, but instead of foo.getText(), have foo.text
List<MethodNode> methods = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, getterName, ClassNode.EMPTY_ARRAY);
for (MethodNode dgm : findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, altGetterName, ClassNode.EMPTY_ARRAY)) {
Expand All @@ -222,7 +220,7 @@ public void makeGetPropertySite(final Expression receiver, final String property
return;
}
}

*/
if (!isStaticProperty && isOrImplements(receiverType, LIST_TYPE)) {
writeListDotProperty(receiver, propertyName, safe);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public Expression transform(final Expression expr) {

MethodCallExpression mce = new MethodCallExpression(transform(pe.getObjectExpression()), pe.getPropertyAsString(), MethodCallExpression.NO_ARGUMENTS);
mce.setImplicitThis(pe.isImplicitThis());
mce.setSpreadSafe(pe.isSpreadSafe());
mce.setMethodTarget(dmct);
mce.setSourcePosition(pe);
mce.setSpreadSafe(pe.isSpreadSafe());
mce.setSafe(pe.isSafe());
mce.copyNodeMetaData(pe);
return mce;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import static org.codehaus.groovy.ast.ClassHelper.isClassType;
import static org.codehaus.groovy.ast.ClassHelper.isGeneratedFunction;
import static org.codehaus.groovy.ast.ClassHelper.isObjectType;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
import static org.codehaus.groovy.ast.ClassHelper.isStringType;
import static org.codehaus.groovy.ast.ClassHelper.isWrapperInteger;
Expand All @@ -91,7 +90,6 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isClassClassNodeWrappingConcreteType;
import static groovyjarjarasm.asm.Opcodes.AALOAD;
Expand Down Expand Up @@ -225,24 +223,6 @@ public void makeGetPropertySite(final Expression receiver, final String property
}
}

// GROOVY-5568: we would be facing a DGM call, but instead of foo.getText(), have foo.text
List<MethodNode> methods = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, isserName, ClassNode.EMPTY_ARRAY);
methods.removeIf(dgm -> !isPrimitiveBoolean(dgm.getReturnType()));
findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, getterName, ClassNode.EMPTY_ARRAY, methods);
if (!methods.isEmpty()) {
List<MethodNode> methodNodes = chooseBestMethod(receiverType, methods, ClassNode.EMPTY_ARRAY);
if (methodNodes.size() == 1) {
MethodNode getter = methodNodes.get(0);
MethodCallExpression call = callX(receiver, getter.getName());
call.setImplicitThis(false);
call.setMethodTarget(getter);
call.setSafe(safe);
call.setSourcePosition(receiver);
call.visit(controller.getAcg());
return;
}
}

if (!isStaticProperty && isOrImplements(receiverType, LIST_TYPE)) {
writeListDotProperty(receiver, propertyName, safe);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,17 @@ class PropertyExpressionTransformer {
}

Expression transformPropertyExpression(final PropertyExpression pe) {
if (isThisOrSuper(pe.getObjectExpression())) { // TODO: all obj exp
MethodNode dmct = pe.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
// NOTE: BinaryExpressionTransformer handles the setter
if (dmct != null && dmct.getParameters().length == 0) {
MethodCallExpression mce = callX(scTransformer.transform(pe.getObjectExpression()), pe.getPropertyAsString());
mce.setImplicitThis(pe.isImplicitThis());
mce.setSpreadSafe(pe.isSpreadSafe());
mce.setMethodTarget(dmct);
mce.setSourcePosition(pe);
mce.copyNodeMetaData(pe);
mce.setSafe(pe.isSafe());
return mce;
}
MethodNode dmct = pe.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
// NOTE: BinaryExpressionTransformer handles the setter
if (dmct != null && dmct.getParameters().length == 0) {
MethodCallExpression mce = callX(scTransformer.transform(pe.getObjectExpression()), pe.getPropertyAsString());
mce.setImplicitThis(pe.isImplicitThis());
mce.setMethodTarget(dmct);
mce.setSourcePosition(pe);
mce.setSpreadSafe(pe.isSpreadSafe());
mce.setSafe(pe.isSafe());
mce.copyNodeMetaData(pe);
return mce;
}

return scTransformer.superTransform(pe);
Expand Down

0 comments on commit 36bdce2

Please sign in to comment.