Skip to content

Commit

Permalink
GROOVY-9059
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 19, 2022
1 parent 9cb4c71 commit 0d91486
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ public void testReferencingFieldsGenerics_GreferingToJ() {
}

@Test
public void testAbstractCovariance_1() {
public void testAbstractCovariance1() {
//@formatter:off
String[] sources = {
"Foo.groovy",
Expand All @@ -2091,7 +2091,7 @@ public void testAbstractCovariance_1() {
}

@Test
public void testAbstractCovariance_2() {
public void testAbstractCovariance2() {
//@formatter:off
String[] sources = {
"Foo.groovy",
Expand All @@ -2110,25 +2110,19 @@ public void testAbstractCovariance_2() {
"----------\n");
}

@Test
public void testAbstractCovariance_3() {
@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9059
public void testAbstractCovariance3() {
//@formatter:off
String[] sources = {
"Face1.java",
"@FunctionalInterface\n" +
"interface Face1<I, O> {\n" +
" O apply(I in);\n" +
"}\n",

"Face2.java",
"interface Face2<X, Y> extends Face1<X, Y> {\n" +
" Object another();\n" +
"Face.java",
"interface Face<T> {\n" +
" <O extends T> O process(O o);\n" +
"}\n",

"Impl.groovy",
"class Impl implements Face2<Number, String> {\n" +
" @Override String apply(Number n) { '' }\n" +
" @Override Object another() { null }\n" +
"class Impl implements Face<CharSequence> { \n" +
" @Override\n" +
" def <Chars extends CharSequence> Chars process(Chars chars) { chars }\n" +
"}\n",
};
//@formatter:on
Expand All @@ -2137,7 +2131,7 @@ public void testAbstractCovariance_3() {
}

@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9059
public void testAbstractCovariance_GROOVY9059() {
public void testAbstractCovariance4() {
//@formatter:off
String[] sources = {
"Face.java",
Expand All @@ -2146,9 +2140,9 @@ public void testAbstractCovariance_GROOVY9059() {
"}\n",

"Impl.groovy",
"class Impl implements Face<CharSequence> { \n" +
"def impl = new Face<CharSequence>() { \n" +
" @Override\n" +
" public <Chars extends CharSequence> Chars process(Chars chars) { chars }\n" +
" def <Chars extends CharSequence> Chars process(Chars chars) { chars }\n" +
"}\n",
};
//@formatter:on
Expand All @@ -2157,7 +2151,7 @@ public void testAbstractCovariance_GROOVY9059() {
}

@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9059
public void testAbstractCovariance_GROOVY9059a() {
public void testAbstractCovariance5() {
//@formatter:off
String[] sources = {
"Face.java",
Expand All @@ -2167,8 +2161,8 @@ public void testAbstractCovariance_GROOVY9059a() {

"Impl.groovy",
"def impl = new Face<CharSequence>() { \n" +
" @Override\n" +
" public <Chars extends CharSequence> Chars process(Chars chars) { chars }\n" +
" @Override @SuppressWarnings('unchecked')\n" +
" CharSequence process(CharSequence chars) { chars }\n" +
"}\n",
};
//@formatter:on
Expand All @@ -2177,7 +2171,7 @@ public void testAbstractCovariance_GROOVY9059a() {
}

@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9059
public void testAbstractCovariance_GROOVY9059b() {
public void testAbstractCovariance6() {
//@formatter:off
String[] sources = {
"Face.java",
Expand All @@ -2186,9 +2180,9 @@ public void testAbstractCovariance_GROOVY9059b() {
"}\n",

"Impl.groovy",
"def impl = new Face<CharSequence>() { \n" +
"def impl = new Face<String>() { \n" +
" @Override @SuppressWarnings('unchecked')\n" +
" public CharSequence process(CharSequence chars) { chars }\n" +
" String process(String string) { string }\n" +
"}\n",
};
//@formatter:on
Expand All @@ -2197,18 +2191,44 @@ public void testAbstractCovariance_GROOVY9059b() {
}

@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-9059
public void testAbstractCovariance_GROOVY9059c() {
public void testAbstractCovariance7() {
//@formatter:off
String[] sources = {
"Face.java",
"Face.groovy",
"interface Face<T> {\n" +
" <O extends T> O process(O o);\n" +
" def <O extends T> O process(O o)\n" +
"}\n",

"Impl.groovy",
"def impl = new Face<String>() { \n" +
" @Override @SuppressWarnings('unchecked')\n" +
" public String process(String string) { string }\n" +
" String process(String string) { string }\n" +
"}\n",
};
//@formatter:on

runWarningFreeTest(sources);
}

@Test // https://issues.apache.org/jira/projects/GROOVY/issues/GROOVY-10675
public void testAbstractCovariance8() {
//@formatter:off
String[] sources = {
"Face1.java",
"@FunctionalInterface\n" +
"interface Face1<I, O> {\n" +
" O apply(I in);\n" +
"}\n",

"Face2.java",
"interface Face2<X, Y> extends Face1<X, Y> {\n" +
" Object another();\n" +
"}\n",

"Impl.groovy",
"class Impl implements Face2<Number, String> {\n" +
" @Override String apply(Number n) { '' }\n" +
" @Override Object another() { null }\n" +
"}\n",
};
//@formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static ClassNode parameterizeType(final ClassNode hint, final ClassNode t
}
return target;
}
// GRECLIPSE add -- GROOVY-8984, GROOVY-9891
// GRECLIPSE add -- GROOVY-8984
if (hint.isGenericsPlaceHolder()) {
ClassNode bound = hint.redirect();
return parameterizeType(bound, target);
Expand Down Expand Up @@ -402,16 +402,17 @@ public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> gene
}
*/
if (type != null && type.isGenericsPlaceHolder()) {
if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
if (type.getGenericsTypes() == null) {
// correct "T -> U" (no generics)
ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
placeholder.setGenericsPlaceHolder(true);
return makeClassSafeWithGenerics(type, new GenericsType(placeholder));
} else if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
}
// GRECLIPSE end
Expand Down Expand Up @@ -457,15 +458,14 @@ public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpe
if (type.isPlaceholder()) {
String name = type.getName();
ret = genericsSpec.get(name);
}
// GRECLIPSE add -- GROOVY-8984, GROOVY-9891
else if (type.isWildcard()) {
//ret = type.getLowerBound(); // use lower or upper
} else if (type.isWildcard()) { // GROOVY-9891
/* GRECLIPSE edit -- GROOVY-8984: "? super T" RHS
ret = type.getLowerBound(); // use lower or upper
*/
if (ret == null && type.getUpperBounds() != null) {
ret = type.getUpperBounds()[0]; // ? supports 1
}
}
// GRECLIPSE end
if (ret == null) ret = type.getType();
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,23 +382,22 @@ public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> gene
exclusions = plus(exclusions, name); // GROOVY-7722
type = genericsSpec.get(name);
if (type != null && type.isGenericsPlaceHolder()) {
// GRECLIPSE add -- GROOVY-9059
if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
// GRECLIPSE end
if (type.getGenericsTypes() == null) {
// correct "T -> U" (no generics)
ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
placeholder.setGenericsPlaceHolder(true);
return makeClassSafeWithGenerics(type, new GenericsType(placeholder));
/* GRECLIPSE edit -- GROOVY-9059
} else if (!name.equals(type.getUnresolvedName())) {
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
*/
} else if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
// GRECLIPSE end
}
}
if (type == null) type = ClassHelper.OBJECT_TYPE;
Expand Down Expand Up @@ -445,7 +444,7 @@ public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpe
String name = type.getName();
ret = genericsSpec.get(name);
} else if (type.isWildcard()) { // GROOVY-9891
/* GRECLIPSE edit -- GROOVY-8984
/* GRECLIPSE edit -- GROOVY-8984: "? super T" RHS
ret = type.getLowerBound(); // use lower or upper
*/
if (ret == null && type.getUpperBounds() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,23 +389,22 @@ public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> gene
exclusions = plus(exclusions, name); // GROOVY-7722
type = genericsSpec.get(name);
if (type != null && type.isGenericsPlaceHolder()) {
// GRECLIPSE add -- GROOVY-9059
if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
// GRECLIPSE end
if (type.getGenericsTypes() == null) {
// correct "T -> U" (no generics)
ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
placeholder.setGenericsPlaceHolder(true);
return makeClassSafeWithGenerics(type, new GenericsType(placeholder));
/* GRECLIPSE edit -- GROOVY-9059
} else if (!name.equals(type.getUnresolvedName())) {
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
*/
} else if (type.hasMultiRedirect()) {
// convert "S -> T -> U" to "T -> U"
type = type.asGenericsType().getUpperBounds()[0];
// continue to resolve "T -> U" if "T" is a placeholder
return correctToGenericsSpecRecurse(genericsSpec, type, exclusions);
}
// GRECLIPSE end
}
}
if (type == null) type = ClassHelper.OBJECT_TYPE.getPlainNodeReference();
Expand Down

0 comments on commit 0d91486

Please sign in to comment.