From 50f6eb9b4655784f2c0dec28b2112fdcc555d922 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Mon, 28 Mar 2022 14:09:16 -0500 Subject: [PATCH] GROOVY-10544 --- .../search/DeclarationInferencingTests.java | 22 ++++++++---------- .../tests/search/GenericInferencingTests.java | 23 +++++++++++++++---- .../jdt/groovy/search/SimpleTypeLookup.java | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DeclarationInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DeclarationInferencingTests.java index 81b032150a..52bd0b063a 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DeclarationInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DeclarationInferencingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2020 the original author or authors. + * Copyright 2009-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1307,15 +1307,6 @@ public void testJavaInterfaceWithDefaultMethod2() { @Test // https://github.com/groovy/groovy-eclipse/issues/1047 public void testJavaInterfaceWithDefaultMethod3() { - // Java 11 adds default method toArray(IntFunction) to the Collection interface - boolean jdkCollectToArray3; - try { - java.util.Collection.class.getDeclaredMethod("toArray", java.util.function.IntFunction.class); - jdkCollectToArray3 = true; - } catch (Exception e) { - jdkCollectToArray3 = false; - } - //@formatter:off String contents = "List list = []\n" + @@ -1325,8 +1316,15 @@ public void testJavaInterfaceWithDefaultMethod3() { //@formatter:on int offset = contents.indexOf("toArray"); - MethodNode method = assertDeclaration(contents, offset, offset + 7, "java.util.Collection", "toArray", DeclarationKind.METHOD); - assertEquals(jdkCollectToArray3 ? "java.util.function.IntFunction" : "T[]", printTypeName(method.getParameters()[0].getType())); + try { // Java 11 adds default method toArray(IntFunction) to the Collection interface + java.util.Collection.class.getDeclaredMethod("toArray", java.util.function.IntFunction.class); + MethodNode method = assertDeclaration(contents, offset, offset + 7, "java.util.Collection", "toArray", DeclarationKind.METHOD); + assertEquals("java.util.function.IntFunction", printTypeName(method.getParameters()[0].getType())); + assertType(contents, "n", "java.lang.Integer"); + } catch (Exception e) { + MethodNode method = assertDeclaration(contents, offset, offset + 7, "java.util.List", "toArray", DeclarationKind.METHOD); + assertEquals("T[]", printTypeName(method.getParameters()[0].getType())); + } } @Test // GRECLIPSE-1105 diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/GenericInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/GenericInferencingTests.java index fa522dfe8c..ba31484121 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/GenericInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/GenericInferencingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2021 the original author or authors. + * Copyright 2009-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1283,6 +1283,22 @@ public void testMethod6() { assertType(contents, "n", "java.lang.Byte"); } + @Test // GROOVY-10544 + public void testMethod7() { + String contents = + "import java.util.function.Function\n" + + "interface I {\n" + + " def Iterable m(Function f)\n" + + "}\n" + + "interface J extends I {\n" + + " def List m(Function f)\n" + + "}\n" + + "void test(J j) {\n" + + " def list = j.m{s -> java.util.regex.Pattern.compile(s)}\n" + + "}\n"; + assertType(contents, "list", "java.util.List"); + } + @Test // GRECLIPSE-1129: Static generic method type inference with @CompileStatic public void testStaticMethod1() { String contents = @@ -1431,8 +1447,7 @@ public void testStaticMethod11() { String toFind = "removeAll"; int start = contents.indexOf(toFind), end = start + toFind.length(); assertType(contents, start, end, "java.lang.Boolean"); - // Is there any reason to fully resolve the declaring type in this case? - MethodNode m = assertDeclaration(contents, start, end, "java.util.Collection", toFind, DeclarationKind.METHOD); + MethodNode m = assertDeclaration(contents, start, end, "java.util.List", toFind, DeclarationKind.METHOD); assertEquals("java.util.Collection", printTypeName(m.getParameters()[0].getType())); } @@ -1443,7 +1458,7 @@ public void testStaticMethod12() { String toFind = "addAll"; int start = contents.indexOf(toFind), end = start + toFind.length(); assertType(contents, start, end, "java.lang.Boolean"); - MethodNode m = assertDeclaration(contents, start, end, "java.util.Collection", toFind, DeclarationKind.METHOD); + MethodNode m = assertDeclaration(contents, start, end, "java.util.List", toFind, DeclarationKind.METHOD); assertEquals("Parameter type should be resolved", "java.util.Collection", printTypeName(m.getParameters()[0].getType())); } 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 1de2f9fb41..d9dc65f8f2 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 @@ -793,7 +793,7 @@ protected MethodNode findMethodDeclaration(final String name, final ClassNode de return innerCandidate; } if (argumentTypes.size() == methodParameters.length) { - outerCandidate = innerCandidate; + outerCandidate = closer(innerCandidate, outerCandidate, argumentTypes); Boolean suitable = isTypeCompatible(argumentTypes, methodParameters); if (Boolean.FALSE.equals(suitable)) {