Skip to content

Commit

Permalink
GROOVY-10544
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 28, 2022
1 parent 7aa55b0 commit 50f6eb9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String> list = []\n" +
Expand All @@ -1325,8 +1316,15 @@ public void testJavaInterfaceWithDefaultMethod3() {
//@formatter:on

int offset = contents.indexOf("toArray");
MethodNode method = assertDeclaration(contents, offset, offset + 7, "java.util.Collection<java.lang.String>", "toArray", DeclarationKind.METHOD);
assertEquals(jdkCollectToArray3 ? "java.util.function.IntFunction<T[]>" : "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<java.lang.String>", "toArray", DeclarationKind.METHOD);
assertEquals("java.util.function.IntFunction<T[]>", printTypeName(method.getParameters()[0].getType()));
assertType(contents, "n", "java.lang.Integer");
} catch (Exception e) {
MethodNode method = assertDeclaration(contents, offset, offset + 7, "java.util.List<java.lang.String>", "toArray", DeclarationKind.METHOD);
assertEquals("T[]", printTypeName(method.getParameters()[0].getType()));
}
}

@Test // GRECLIPSE-1105
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<T> {\n" +
" def <U> Iterable<U> m(Function<? super T, ? extends U> f)\n" +
"}\n" +
"interface J<T> extends I<T> {\n" +
" def <U> List<U> m(Function<? super T, ? extends U> f)\n" +
"}\n" +
"void test(J<String> j) {\n" +
" def list = j.m{s -> java.util.regex.Pattern.compile(s)}\n" +
"}\n";
assertType(contents, "list", "java.util.List<java.util.regex.Pattern>");
}

@Test // GRECLIPSE-1129: Static generic method type inference with @CompileStatic
public void testStaticMethod1() {
String contents =
Expand Down Expand Up @@ -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<E extends java.lang.Object>", toFind, DeclarationKind.METHOD);
MethodNode m = assertDeclaration(contents, start, end, "java.util.List<java.lang.String>", toFind, DeclarationKind.METHOD);
assertEquals("java.util.Collection<?>", printTypeName(m.getParameters()[0].getType()));
}

Expand All @@ -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<java.lang.String>", toFind, DeclarationKind.METHOD);
MethodNode m = assertDeclaration(contents, start, end, "java.util.List<java.lang.String>", toFind, DeclarationKind.METHOD);
assertEquals("Parameter type should be resolved", "java.util.Collection<? extends java.lang.String>", printTypeName(m.getParameters()[0].getType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 50f6eb9

Please sign in to comment.