|
| 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 | +} |
0 commit comments