Skip to content

Commit

Permalink
Fix for #1277: dynamic map property does not apply to implicit-this call
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 13, 2021
1 parent 2e6a12e commit 5cfa5b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ public void testWithAndClosure8() {
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1230
public void testWithAndClosure8a() {
public void testWithAndClosure9() {
//@formatter:off
String contents =
"class C {\n" +
Expand All @@ -1283,6 +1283,24 @@ public void testWithAndClosure8a() {
assertType(contents, "x", "java.lang.Object");
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1277
public void testWithAndClosure10() {
//@formatter:off
String contents =
"class C {\n" +
" Number x\n" +
" def m() {\n" +
" [x:'map'].with {\n" +
" getX()\n" +
" }\n" +
" }\n" +
"}\n";
//@formatter:on

assertDeclaringType(contents, "getX", "C");
assertType(contents, "getX", "java.lang.Number");
}

@Test
public void testClosureReturnType1() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5208,6 +5208,50 @@ public void testGroovyPropertyAccessors5() {
" public void setFlag(boolean arg0);\n");
}

@Test
public void testGroovyPropertyAccessors6() {
//@formatter:off
String[] sources = {
"G.groovy",
"class G {\n" +
" def p = 'G'\n" +
" void test() {\n" +
" [p:'map'].with {\n" +
" print getP()\n" + // resolves to enclosing class not map
" }\n" +
" }\n" +
" static main(args) {\n" +
" new G().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "G");
}

@Test
public void testGroovyPropertyAccessors7() {
//@formatter:off
String[] sources = {
"G.groovy",
"class G {\n" +
" def p = 'G'\n" +
" void test() {\n" +
" [p:'map'].with {\n" +
" print p\n" + // resolves to map not enclosing class
" }\n" +
" }\n" +
" static main(args) {\n" +
" new G().test()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "map");
}

@Test // check no duplicate created for 'String getProp'
public void testGroovyPropertyAccessors_ErrorCases1() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,9 @@ protected ASTNode findDeclaration(final String name, final ClassNode declaringTy
return findDeclaration(name, VariableScope.OBJECT_CLASS_NODE, isLhsExpression, isStaticExpression, 0, methodCallArgumentTypes);
}

if (!isLhsExpression && methodCallArgumentTypes != null) {
boolean isMethodCall = (!isLhsExpression && methodCallArgumentTypes != null);

if (isMethodCall) {
if (!isStaticExpression && declaringType.implementsInterface(ClassHelper.GROOVY_INTERCEPTABLE_TYPE)) {
return declaringType.getMethod("invokeMethod", new Parameter[] {new Parameter(VariableScope.STRING_CLASS_NODE, "name"), new Parameter(VariableScope.OBJECT_CLASS_NODE, "args")});
}
Expand All @@ -631,7 +633,7 @@ protected ASTNode findDeclaration(final String name, final ClassNode declaringTy
// name may still map to something that is callable; keep looking
}

if (!isStaticExpression && directFieldAccess == 0 && isOrImplements(declaringType, VariableScope.MAP_CLASS_NODE)) {
if (!isMethodCall && !isStaticExpression && directFieldAccess == 0 && isOrImplements(declaringType, VariableScope.MAP_CLASS_NODE)) {
ClassNode resolvedType;
if (isLhsExpression) {
resolvedType = VariableScope.VOID_CLASS_NODE;
Expand Down Expand Up @@ -706,8 +708,7 @@ protected ASTNode findDeclaration(final String name, final ClassNode declaringTy
}
}).findFirst();
if (mopMethod.isPresent()) {
return mopMethod.map(mm -> (isLhsExpression || methodCallArgumentTypes == null)
? createDynamicProperty(name, VariableScope.OBJECT_CLASS_NODE, declaringType, isStaticExpression) : mm).get();
return mopMethod.map(mm -> !isMethodCall ? createDynamicProperty(name, VariableScope.OBJECT_CLASS_NODE, declaringType, isStaticExpression) : mm).get();
}
}

Expand Down

0 comments on commit 5cfa5b6

Please sign in to comment.