Skip to content

Commit

Permalink
Bug 533327 - [9] Implement JEP 211 (#2680)
Browse files Browse the repository at this point in the history
Don't raise warnings on imports (deprecation, restricted access) 

fixes #2679
  • Loading branch information
stephan-herrmann authored Jul 9, 2024
1 parent 803b5c8 commit eb2e0c6
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1099,11 +1099,13 @@ private int checkAndRecordImportBinding(
final char[] name = importReference.getSimpleName();
if (importBinding instanceof ReferenceBinding || conflictingType != null) {
ReferenceBinding referenceBinding = conflictingType == null ? (ReferenceBinding) importBinding : conflictingType;
ReferenceBinding typeToCheck = referenceBinding.problemId() == ProblemReasons.Ambiguous
? ((ProblemReferenceBinding) referenceBinding).closestMatch
: referenceBinding;
if (importReference.isTypeUseDeprecated(typeToCheck, this))
problemReporter().deprecatedType(typeToCheck, importReference);
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2014 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -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,53 +59,55 @@ public void test001() {
" }\n" +
"}\n",
},
(isJDK9 ? "" :
"----------\n" +
"1. WARNING in Y.java (at line 1)\n" +
" import p.X;\n" +
" ^^^\n" +
"The type X<T> is deprecated\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" +
"----------\n" +
"3. WARNING in Y.java (at line 5)\n" +
"2. WARNING in Y.java (at line 5)\n" +
" X x;\n" +
" ^\n" +
"The type X<T> is deprecated\n" +
"----------\n" +
"4. WARNING in Y.java (at line 5)\n" +
"3. WARNING in Y.java (at line 5)\n" +
" X x;\n" +
" ^\n" +
"X is a raw type. References to generic type X<T> should be parameterized\n" +
"----------\n" +
"5. WARNING in Y.java (at line 6)\n" +
"4. WARNING in Y.java (at line 6)\n" +
" X[] xs = { x };\n" +
" ^\n" +
"The type X<T> is deprecated\n" +
"----------\n" +
"6. WARNING in Y.java (at line 6)\n" +
"5. WARNING in Y.java (at line 6)\n" +
" X[] xs = { x };\n" +
" ^\n" +
"X is a raw type. References to generic type X<T> should be parameterized\n" +
"----------\n" +
"7. WARNING in Y.java (at line 9)\n" +
"6. WARNING in Y.java (at line 9)\n" +
" p.X x;\n" +
" ^^^\n" +
"X is a raw type. References to generic type X<T> should be parameterized\n" +
"----------\n" +
"8. WARNING in Y.java (at line 9)\n" +
"7. WARNING in Y.java (at line 9)\n" +
" p.X x;\n" +
" ^\n" +
"The type X<T> is deprecated\n" +
"----------\n" +
"9. WARNING in Y.java (at line 10)\n" +
"8. WARNING in Y.java (at line 10)\n" +
" p.X[] xs = { x };\n" +
" ^^^\n" +
"X is a raw type. References to generic type X<T> should be parameterized\n" +
"----------\n" +
"10. WARNING in Y.java (at line 10)\n" +
"9. WARNING in Y.java (at line 10)\n" +
" p.X[] xs = { x };\n" +
" ^\n" +
"The type X<T> is deprecated\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,7 @@ public void testBug534304() throws Exception {
"}\n"
},
"----------\n" +
"1. WARNING in p1\\C1.java (at line 3)\n" +
" import pdep.Dep1;\n" +
" ^^^^^^^^^\n" +
"The type Dep1 is deprecated since version 13\n" +
"----------\n" +
"2. WARNING in p1\\C1.java (at line 6)\n" +
"1. WARNING in p1\\C1.java (at line 6)\n" +
" Dep1 f;\n" +
" ^^^^\n" +
"The type Dep1 is deprecated since version 13\n" +
Expand Down Expand Up @@ -1060,6 +1055,77 @@ public class AnnotatedClass {}
};
runner.runConformTest();
}
public void testJEP211_2() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"p1/C1.java",
"""
package p1;
public class C1 {
@Deprecated public class CInner {}
@Deprecated(forRemoval=true) public static int ZERO = 0;
}
""",
"Test.java",
"""
import p1.C1.CInner;
import static p1.C1.ZERO;
public class Test {
CInner c;
int z = ZERO;
}
"""
};
runner.expectedCompilerLog = """
----------
1. WARNING in Test.java (at line 4)
CInner c;
^^^^^^
The type C1.CInner is deprecated
----------
2. WARNING in Test.java (at line 5)
int z = ZERO;
^^^^
The field C1.ZERO has been deprecated and marked for removal
----------
""";
runner.runWarningTest();
}
public void testJEP211_3() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"p1/C1.java",
"""
package p1;
public class C1 {
@Deprecated public static int ZERO = 0;
@Deprecated public static int nothing() { return 0; };
}
""",
"Test.java",
"""
import static p1.C1.*;
public class Test {
int z = ZERO;
int zz = nothing();
}
"""
};
runner.expectedCompilerLog = """
----------
1. WARNING in Test.java (at line 3)
int z = ZERO;
^^^^
The field C1.ZERO is deprecated
----------
2. WARNING in Test.java (at line 4)
int zz = nothing();
^^^^^^^^^
The method nothing() from the type C1 is deprecated
----------
""";
runner.runWarningTest();
}
public static Class<?> testClass() {
return Deprecated9Test.class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -618,33 +618,35 @@ public void test014() {
" }\n" +
"}\n",
},
(this.complianceLevel >= ClassFileConstants.JDK9 ? "" :
"----------\n" +
"1. WARNING in Y.java (at line 1)\n" +
" import p.X;\n" +
" ^^^\n" +
"The type X is deprecated\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" +
"----------\n" +
"3. WARNING in Y.java (at line 5)\n" +
"2. WARNING in Y.java (at line 5)\n" +
" X x;\n" +
" ^\n" +
"The type X is deprecated\n" +
"----------\n" +
"4. WARNING in Y.java (at line 6)\n" +
"3. WARNING in Y.java (at line 6)\n" +
" X[] xs = { x };\n" +
" ^\n" +
"The type X is deprecated\n" +
"----------\n" +
"5. WARNING in Y.java (at line 9)\n" +
"4. WARNING in Y.java (at line 9)\n" +
" p.X x;\n" +
" ^\n" +
"The type X is deprecated\n" +
"----------\n" +
"6. WARNING in Y.java (at line 10)\n" +
"5. WARNING in Y.java (at line 10)\n" +
" p.X[] xs = { x };\n" +
" ^\n" +
"The type X is deprecated\n" +
Expand Down Expand Up @@ -966,6 +968,50 @@ public void test020() {
// javac options
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",
"""
package p1;
@Deprecated public class C1 {}
""",
"Test.java",
"""
import p1.C1;
public class Test {
C1 c;
}
"""
};
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;
^^
The type C1 is deprecated
----------
""";
runner.runWarningTest();
}
public static Class testClass() {
return DeprecatedTest.class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2018 IBM Corporation and others.
* Copyright (c) 2006, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -5365,58 +5365,60 @@ public void test135() {
" }\n" +
"}", // =================,
},
(this.complianceLevel >= ClassFileConstants.JDK9 ? "" :
"----------\n" +
"1. WARNING in p\\X.java (at line 2)\n" +
" import p.A;\n" +
" ^^^\n" +
"The type A is deprecated\n" +
"The type A is deprecated\n"
) +
"----------\n" +
"2. WARNING in p\\X.java (at line 6)\n" +
"1. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^\n" +
"The type A is deprecated\n" +
"----------\n" +
"3. WARNING in p\\X.java (at line 6)\n" +
"2. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^^\n" +
"The type A.M2 is deprecated\n" +
"----------\n" +
"4. WARNING in p\\X.java (at line 6)\n" +
"3. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^^^\n" +
"The type A.M1.MM1 is deprecated\n" +
"----------\n" +
"5. WARNING in p\\X.java (at line 6)\n" +
"4. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^\n" +
"The type A is deprecated\n" +
"----------\n" +
"6. WARNING in p\\X.java (at line 6)\n" +
"5. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^^\n" +
"The type A.M2 is deprecated\n" +
"----------\n" +
"7. WARNING in p\\X.java (at line 6)\n" +
"6. WARNING in p\\X.java (at line 6)\n" +
" A.M2.MM1 mm1 = (A.M2.MM1) o;\n" +
" ^^^\n" +
"The type A.M1.MM1 is deprecated\n" +
"----------\n" +
"8. WARNING in p\\X.java (at line 7)\n" +
"7. WARNING in p\\X.java (at line 7)\n" +
" A.M2.MM1[] mm1s = (A.M2.MM1[]) os;\n" +
" ^\n" +
"The type A is deprecated\n" +
"----------\n" +
"9. WARNING in p\\X.java (at line 7)\n" +
"8. WARNING in p\\X.java (at line 7)\n" +
" A.M2.MM1[] mm1s = (A.M2.MM1[]) os;\n" +
" ^^\n" +
"The type A.M2 is deprecated\n" +
"----------\n" +
"10. WARNING in p\\X.java (at line 7)\n" +
"9. WARNING in p\\X.java (at line 7)\n" +
" A.M2.MM1[] mm1s = (A.M2.MM1[]) os;\n" +
" ^^^\n" +
"The type A.M1.MM1 is deprecated\n" +
"----------\n" +
"11. WARNING in p\\X.java (at line 7)\n" +
"10. WARNING in p\\X.java (at line 7)\n" +
" A.M2.MM1[] mm1s = (A.M2.MM1[]) os;\n" +
" ^\n" +
"The type A is deprecated\n" +
Expand All @@ -5431,7 +5433,7 @@ public void test135() {
" ^^^\n" +
"The type A.M1.MM1 is deprecated\n" +
"----------\n" +
"14. ERROR in p\\X.java (at line 16)\n" +
"13. ERROR in p\\X.java (at line 16)\n" +
" Zork z;\n" +
" ^^^^\n" +
"Zork cannot be resolved to a type\n" +
Expand Down
Loading

0 comments on commit eb2e0c6

Please sign in to comment.