Skip to content

Commit 79f6973

Browse files
ECJ fails to complain about static member being selected from a
parameterized class * Fixes #4702
1 parent 11eccb1 commit 79f6973

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ParameterizedQualifiedTypeReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private TypeBinding internalResolveLeafType(Scope scope, boolean checkBounds) {
244244
if (this.annotations != null)
245245
rejectAnnotationsOnStaticMemberQualififer(scope, currentType, this.annotations[i-1]);
246246
if (typeIsConsistent && currentType.isStatic()
247-
&& (qualifyingType.isParameterizedTypeWithActualArguments() || qualifyingType.isGenericType())) {
247+
&& (qualifyingType.isParameterizedType() || qualifyingType.isGenericType())) {
248248
scope.problemReporter().staticMemberOfParameterizedType(this, currentType, qualifyingType, i);
249249
typeIsConsistent = false;
250250
qualifyingType = qualifyingType.actualType(); // avoid raw/parameterized enclosing of static member

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/QualifiedTypeReference.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ protected TypeBinding getTypeBinding(Scope scope) {
159159
if (this.annotations != null) {
160160
rejectAnnotationsOnStaticMemberQualififer(scope, currentType, this.annotations[i-1]);
161161
}
162+
if (currentType.isStatic() && (qualifyingType.isParameterizedType() || qualifyingType.isGenericType())) {
163+
scope.problemReporter().staticMemberOfParameterizedType(this, currentType, qualifyingType, i);
164+
qualifyingType = qualifyingType.actualType(); // avoid raw/parameterized enclosing of static member
165+
}
162166
ReferenceBinding enclosingType = currentType.enclosingType();
163167
if (enclosingType != null && TypeBinding.notEquals(enclosingType.erasure(), qualifyingType.erasure())) {
164168
qualifyingType = enclosingType; // inherited member type, leave it associated with its enclosing rather than subtype
@@ -178,7 +182,10 @@ protected TypeBinding getTypeBinding(Scope scope) {
178182
}
179183
}
180184
} else {
181-
qualifyingType = currentType.isGenericType() ? (ReferenceBinding)scope.environment().convertToRawType(currentType, false /*do not force conversion of enclosing types*/) : currentType;
185+
qualifyingType = currentType.isGenericType() ?
186+
(ReferenceBinding)scope.environment().convertToRawType(currentType, false /*do not force conversion of enclosing types*/) :
187+
scope.environment().convertToParameterizedType(currentType);
188+
182189
}
183190
recordResolution(scope.environment(), qualifyingType);
184191
}

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,113 @@ interface MyCallable<U, F extends Exception> extends Callable<U> {
20342034
});
20352035
}
20362036

2037+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4702
2038+
// ecj ignores error "cannot select a static class from a parameterized type"
2039+
public void testIssue4702() {
2040+
if (this.complianceLevel < ClassFileConstants.JDK16)
2041+
return;
2042+
runNegativeTest(new String[] {
2043+
"OuterStaticNestedDemo.java",
2044+
"""
2045+
public class OuterStaticNestedDemo<E> {
2046+
class Outer {
2047+
static class StaticNested {}
2048+
}
2049+
2050+
void qualifiedNew(Outer outer) {
2051+
new OuterStaticNestedDemo<String>.Outer.StaticNested();
2052+
}
2053+
}
2054+
"""
2055+
},
2056+
"----------\n" +
2057+
"1. ERROR in OuterStaticNestedDemo.java (at line 7)\n" +
2058+
" new OuterStaticNestedDemo<String>.Outer.StaticNested();\n" +
2059+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
2060+
"The member type OuterStaticNestedDemo.Outer.StaticNested cannot be qualified with a parameterized type, since it is static. Remove arguments from qualifying type OuterStaticNestedDemo<String>.Outer\n" +
2061+
"----------\n");
2062+
}
2063+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4702
2064+
// ecj ignores error "cannot select a static class from a parameterized type"
2065+
public void testIssue4702_1() {
2066+
if (this.complianceLevel < ClassFileConstants.JDK16)
2067+
return;
2068+
runNegativeTest(new String[] {
2069+
"OuterStaticNestedDemo.java",
2070+
"""
2071+
public class OuterStaticNestedDemo<E> {
2072+
class Outer {
2073+
static class StaticNested {}
2074+
}
2075+
2076+
void qualifiedNew(Outer outer) {
2077+
new Outer.StaticNested();
2078+
}
2079+
}
2080+
"""
2081+
},
2082+
"----------\n" +
2083+
"1. ERROR in OuterStaticNestedDemo.java (at line 7)\n" +
2084+
" new Outer.StaticNested();\n" +
2085+
" ^^^^^^^^^^^^^^^^^^\n" +
2086+
"The member type OuterStaticNestedDemo.Outer.StaticNested cannot be qualified with a parameterized type, since it is static. Remove arguments from qualifying type OuterStaticNestedDemo<E>.Outer\n" +
2087+
"----------\n");
2088+
}
2089+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4702
2090+
// ecj ignores error "cannot select a static class from a parameterized type"
2091+
public void testIssue4702_2() {
2092+
if (this.complianceLevel < ClassFileConstants.JDK16)
2093+
return;
2094+
runNegativeTest(new String[] {
2095+
"OuterStaticNestedDemo.java",
2096+
"""
2097+
class OuterStaticNestedBase<E> {
2098+
class Outer {
2099+
static class StaticNested {}
2100+
}
2101+
}
2102+
class OuterStaticNestedDemo<E> extends OuterStaticNestedBase<E> {
2103+
2104+
2105+
void qualifiedNew(Outer outer) {
2106+
new Outer.StaticNested();
2107+
}
2108+
}
2109+
"""
2110+
},
2111+
"----------\n" +
2112+
"1. ERROR in OuterStaticNestedDemo.java (at line 10)\n" +
2113+
" new Outer.StaticNested();\n" +
2114+
" ^^^^^^^^^^^^^^^^^^\n" +
2115+
"The member type OuterStaticNestedBase.Outer.StaticNested cannot be qualified with a parameterized type, since it is static. Remove arguments from qualifying type OuterStaticNestedBase<E>.Outer\n" +
2116+
"----------\n");
2117+
}
2118+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4702
2119+
// ecj ignores error "cannot select a static class from a parameterized type"
2120+
public void testIssue4702_3() {
2121+
if (this.complianceLevel < ClassFileConstants.JDK16)
2122+
return;
2123+
runNegativeTest(new String[] {
2124+
"OuterStaticNestedDemo.java",
2125+
"""
2126+
public class OuterStaticNestedDemo<E> {
2127+
class Outer<T> {
2128+
static class StaticNested {}
2129+
}
2130+
2131+
void qualifiedNew(Outer outer) {
2132+
new Outer.StaticNested();
2133+
}
2134+
}
2135+
"""
2136+
},
2137+
"----------\n" +
2138+
"1. WARNING in OuterStaticNestedDemo.java (at line 6)\n" +
2139+
" void qualifiedNew(Outer outer) {\n" +
2140+
" ^^^^^\n" +
2141+
"OuterStaticNestedDemo.Outer is a raw type. References to generic type OuterStaticNestedDemo<E>.Outer<T> should be parameterized\n" +
2142+
"----------\n");
2143+
}
20372144
public static Class<GenericsRegressionTest_9> testClass() {
20382145
return GenericsRegressionTest_9.class;
20392146
}

0 commit comments

Comments
 (0)