diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ClosureInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ClosureInferencingTests.java index cfa5ec8a7b..23c3ee5a32 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ClosureInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ClosureInferencingTests.java @@ -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" + @@ -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 diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java index d4f1756376..3b4e2af726 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java @@ -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 diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java index 56399fdd58..e4dec2362b 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java @@ -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")}); } @@ -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; @@ -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(); } }