Skip to content

Commit 0531e08

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Add an Error Prone check that reimplements javac sunapi warnings
The javac diagnostic can't be configured using `-Xlint:` (https://bugs.openjdk.org/browse/JDK-8148808), and doesn't work when `--system` is configured (https://bugs.openjdk.org/browse/JDK-8332744) PiperOrigin-RevId: 636969056
1 parent 9e0fbf7 commit 0531e08

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2024 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone.bugpatterns;
18+
19+
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
20+
import static com.google.errorprone.matchers.Description.NO_MATCH;
21+
22+
import com.google.errorprone.BugPattern;
23+
import com.google.errorprone.VisitorState;
24+
import com.google.errorprone.bugpatterns.BugChecker.IdentifierTreeMatcher;
25+
import com.google.errorprone.bugpatterns.BugChecker.MemberSelectTreeMatcher;
26+
import com.google.errorprone.matchers.Description;
27+
import com.google.errorprone.util.ASTHelpers;
28+
import com.sun.source.tree.IdentifierTree;
29+
import com.sun.source.tree.ImportTree;
30+
import com.sun.source.tree.MemberSelectTree;
31+
import com.sun.source.tree.Tree;
32+
import com.sun.tools.javac.code.Flags;
33+
import com.sun.tools.javac.code.Symbol;
34+
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
35+
import com.sun.tools.javac.code.Symbol.PackageSymbol;
36+
37+
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
38+
@BugPattern(
39+
summary = "Usage of internal proprietary API which may be removed in a future release",
40+
severity = WARNING)
41+
public class SunApi extends BugChecker implements MemberSelectTreeMatcher, IdentifierTreeMatcher {
42+
@Override
43+
public Description matchIdentifier(IdentifierTree tree, VisitorState state) {
44+
return match(tree, state);
45+
}
46+
47+
@Override
48+
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
49+
return match(tree, state);
50+
}
51+
52+
private Description match(Tree tree, VisitorState state) {
53+
Symbol sym = ASTHelpers.getSymbol(tree);
54+
if (sym == null) {
55+
return NO_MATCH;
56+
}
57+
if (!inJdkUnsupportedModule(sym) && ((sym.flags() & Flags.PROPRIETARY) == 0)) {
58+
return NO_MATCH;
59+
}
60+
if (state.findEnclosing(ImportTree.class) != null) {
61+
return NO_MATCH;
62+
}
63+
return buildDescription(tree)
64+
.setMessage(
65+
String.format(
66+
"%s is an internal JDK API which may be removed in a future release",
67+
sym.getQualifiedName()))
68+
.build();
69+
}
70+
71+
private static boolean inJdkUnsupportedModule(Symbol sym) {
72+
PackageSymbol packageSymbol = ASTHelpers.enclosingPackage(sym);
73+
if (packageSymbol == null) {
74+
return false;
75+
}
76+
ModuleSymbol moduleSymbol = (ModuleSymbol) packageSymbol.getEnclosingElement();
77+
if (moduleSymbol == null) {
78+
return false;
79+
}
80+
return moduleSymbol.name.contentEquals("jdk.unsupported");
81+
}
82+
}

core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java

+2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
import com.google.errorprone.bugpatterns.StringSplitter;
357357
import com.google.errorprone.bugpatterns.StronglyTypeByteString;
358358
import com.google.errorprone.bugpatterns.SubstringOfZero;
359+
import com.google.errorprone.bugpatterns.SunApi;
359360
import com.google.errorprone.bugpatterns.SuperCallToObjectMethod;
360361
import com.google.errorprone.bugpatterns.SuppressWarningsDeprecated;
361362
import com.google.errorprone.bugpatterns.SuppressWarningsWithoutExplanation;
@@ -1210,6 +1211,7 @@ public static ScannerSupplier warningChecks() {
12101211
StringFormatWithLiteral.class,
12111212
StronglyTypeByteString.class,
12121213
StronglyTypeTime.class,
1214+
SunApi.class,
12131215
SuppressWarningsWithoutExplanation.class,
12141216
SwitchDefault.class,
12151217
SymbolToString.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2024 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone.bugpatterns;
18+
19+
import com.google.errorprone.CompilationTestHelper;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.JUnit4;
23+
24+
@RunWith(JUnit4.class)
25+
public class SunApiTest {
26+
27+
private final CompilationTestHelper compilationHelper =
28+
CompilationTestHelper.newInstance(SunApi.class, getClass());
29+
30+
@Test
31+
public void positive() {
32+
compilationHelper
33+
.addSourceLines(
34+
"Test.java", //
35+
"class Test {",
36+
" // BUG: Diagnostic contains: sun.misc.Unsafe",
37+
" sun.misc.Unsafe u;",
38+
"}")
39+
.doTest();
40+
}
41+
42+
@Test
43+
public void positiveSource8() {
44+
compilationHelper
45+
.addSourceLines(
46+
"Test.java", //
47+
"class Test {",
48+
" // BUG: Diagnostic contains: sun.misc.Unsafe",
49+
" sun.misc.Unsafe u;",
50+
"}")
51+
.setArgs("-source", "8", "-target", "8")
52+
.doTest();
53+
}
54+
55+
@Test
56+
public void negative() {
57+
compilationHelper
58+
.addSourceLines(
59+
"Test.java", //
60+
"import jdk.internal.misc.Unsafe;",
61+
"class Test {",
62+
" Unsafe u;",
63+
"}")
64+
.addModules("java.base/jdk.internal.misc")
65+
.doTest();
66+
}
67+
}

docs/bugpattern/SunApi.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Warn on internal, proprietary APIs that may be removed in future JDK versions.
2+
3+
For `sun.misc.Unsafe`, note that the API will be removed from a future version
4+
of the JDK:
5+
[JEP 471: Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal](https://openjdk.org/jeps/471).
6+
7+
This check is a re-implementation of javac's 'sunapi' diagnostic.

0 commit comments

Comments
 (0)