Skip to content

Commit

Permalink
Re-add warning at compliance 8-
Browse files Browse the repository at this point in the history
Adjust also AccessRestrictionsTests
  • Loading branch information
stephan-herrmann committed Jul 9, 2024
1 parent a3ec75b commit 5024190
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,14 @@ private int checkAndRecordImportBinding(
final char[] name = importReference.getSimpleName();
if (importBinding instanceof ReferenceBinding || conflictingType != null) {
ReferenceBinding referenceBinding = conflictingType == null ? (ReferenceBinding) importBinding : conflictingType;
if (compilerOptions().complianceLevel <= ClassFileConstants.JDK1_8) { // not any more since JEP 211 / JDK 9
ReferenceBinding typeToCheck = referenceBinding.problemId() == ProblemReasons.Ambiguous
? ((ProblemReferenceBinding) referenceBinding).closestMatch
: referenceBinding;
if (importReference.isTypeUseDeprecated(typeToCheck, this))
problemReporter().deprecatedType(typeToCheck, importReference);
}

ReferenceBinding existingType = typesBySimpleNames.get(name);
if (existingType != null) {
// duplicate test above should have caught this case, but make sure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import junit.framework.Test;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand All @@ -34,6 +35,7 @@ public static Test suite() {
public void test001() {
Map options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.WARNING);
boolean isJDK9 = this.complianceLevel >= ClassFileConstants.JDK9;
this.runNegativeTest(
new String[] {
"p/X.java",
Expand All @@ -57,8 +59,15 @@ public void test001() {
" }\n" +
"}\n",
},
(isJDK9 ? "" :
"----------\n" +
"1. ERROR in Y.java (at line 3)\n" +
"1. WARNING in Y.java (at line 1)\n" +
" import p.X;\n" +
" ^^^\n" +
"The type X<T> is deprecated\n"
) +
"----------\n" +
"2. ERROR in Y.java (at line 3)\n" +
" Zork z;\n" +
" ^^^^\n" +
"Zork cannot be resolved to a type\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,15 @@ public void test014() {
" }\n" +
"}\n",
},
(this.complianceLevel >= ClassFileConstants.JDK9 ? "" :
"----------\n" +
"1. ERROR in Y.java (at line 3)\n" +
"1. WARNING in Y.java (at line 1)\n" +
" import p.X;\n" +
" ^^^\n" +
"The type X is deprecated\n"
) +
"----------\n" +
"2. ERROR in Y.java (at line 3)\n" +
" Zork z;\n" +
" ^^^^\n" +
"Zork cannot be resolved to a type\n" +
Expand Down Expand Up @@ -962,6 +969,8 @@ public void test020() {
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError /* javac test options */);
}
public void testJEP211_1() {
if (this.complianceLevel < ClassFileConstants.JDK1_5)
return;
Runner runner = new Runner();
runner.testFiles = new String[] {
"p1/C1.java",
Expand All @@ -977,7 +986,23 @@ public class Test {
}
"""
};
runner.expectedCompilerLog = """
runner.expectedCompilerLog =
this.complianceLevel < ClassFileConstants.JDK9 ?
"""
----------
1. WARNING in Test.java (at line 1)
import p1.C1;
^^^^^
The type C1 is deprecated
----------
2. WARNING in Test.java (at line 3)
C1 c;
^^
The type C1 is deprecated
----------
"""
:
"""
----------
1. WARNING in Test.java (at line 3)
C1 c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,4 +1202,90 @@ public void testBug545766() throws CoreException {
deleteProjects(new String[] {"P1", "P2"});
}
}
public void testBug545766_JEP211() throws CoreException {
// no warnings on imports anymore since JDK 9 (deprecation & access restriction)
ICompilationUnit x1 = null, z = null;
try {
createJavaProject(
"P1",
new String[] {"src"},
new String[] {"JCL15_LIB"},
"bin",
"1.5");
createFolder("/P1/src/p");
createFile("/P1/src/p/X1.java",
"package p;\n" +
"public class X1 {\n" +
" public enum E {" +
" E1(), E2();" +
" }\n" +
"}"
);
IJavaProject p2 = createJavaProject("P2", new String[] {"src"},
new String[] {"JCL9_LIB"}, "bin", "9");
IClasspathEntry[] classpath = p2.getRawClasspath();
int length = classpath.length;
System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length);
classpath[length] = createSourceEntry("P2", "/P1", "-p/X1");
p2.setRawClasspath(classpath, null);

String src =
"package p2;\n" +
"import p.X1;\n" +
"public class Z {\n" +
" X1.E e = X1.E.E1;" +
"}";
String expectedProblems =
"1. ERROR in /P2/src/p2/Z.java (at line 4)\n" +
" X1.E e = X1.E.E1;}\n" +
" ^^^^\n" +
"Access restriction: The type \'X1\' is not API (restriction on required project \'P1\')\n" +
"----------\n" +
"2. ERROR in /P2/src/p2/Z.java (at line 4)\n" +
" X1.E e = X1.E.E1;}\n" +
" ^^^^\n" +
"Access restriction: The type \'X1.E\' is not API (restriction on required project \'P1\')\n" +
"----------\n" +
"3. ERROR in /P2/src/p2/Z.java (at line 4)\n" +
" X1.E e = X1.E.E1;}\n" +
" ^^^^^^^\n" +
"Access restriction: The type \'X1\' is not API (restriction on required project \'P1\')\n" +
"----------\n" +
"4. ERROR in /P2/src/p2/Z.java (at line 4)\n" +
" X1.E e = X1.E.E1;}\n" +
" ^^^^^^^\n" +
"Access restriction: The type \'X1.E\' is not API (restriction on required project \'P1\')\n" +
"----------\n" +
"5. ERROR in /P2/src/p2/Z.java (at line 4)\n" +
" X1.E e = X1.E.E1;}\n" +
" ^^\n" +
"Access restriction: The field \'X1.E.E1\' is not API (restriction on required project \'P1\')\n" +
"----------\n";
this.problemRequestor = new ProblemRequestor(src);
z = getWorkingCopy("/P2/src/p2/Z.java", src);
assertProblems("Unexpected problems value", "----------\n" + expectedProblems);

int start = src.indexOf("E1");
IJavaElement[] elements = z.codeSelect(start, 2);
assertElementsEqual("Unexpected elements", "E1 [in E [in X1 [in X1.java [in p [in src [in P1]]]]]]", elements);

createFolder("/P2/src/p2");
createFile("/P2/src/p2/Z.java", src);
ASTParser parser = ASTParser.newParser(AST_INTERNAL_LATEST);
parser.setProject(p2);
parser.setSource((ITypeRoot)p2.findElement(new Path("p2/Z.java")));
parser.setResolveBindings(true);
ASTNode ast = parser.createAST(null); // <== NPE was thrown here
assertProblems("unexpected problems",
expectedProblems,
((CompilationUnit) ast).getProblems(),
src.toCharArray());
} finally {
if (x1 != null)
x1.discardWorkingCopy();
if (z != null)
z.discardWorkingCopy();
deleteProjects(new String[] {"P1", "P2"});
}
}
}

0 comments on commit 5024190

Please sign in to comment.