forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOtelDeprecatedApiUsage.java
More file actions
189 lines (166 loc) · 5.98 KB
/
OtelDeprecatedApiUsage.java
File metadata and controls
189 lines (166 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.gradle.customchecks;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol;
import javax.annotation.Nullable;
/**
* Error Prone check that detects usage of deprecated APIs.
*
* <p>This is similar to javac's -Xlint:deprecation but properly honors {@code @SuppressWarnings}
* (including on import statements, which javac doesn't support with --release 8 due to <a
* href="https://bugs.openjdk.org/browse/JDK-8032211">JDK-8032211</a>).
*
* <p>Can be suppressed with {@code @SuppressWarnings("deprecation")}.
*/
@BugPattern(
summary = "Use of deprecated API",
severity = ERROR,
linkType = NONE,
altNames = "deprecation", // so it can be suppressed with @SuppressWarnings("deprecation")
suppressionAnnotations = SuppressWarnings.class)
public class OtelDeprecatedApiUsage extends BugChecker
implements BugChecker.MethodInvocationTreeMatcher,
BugChecker.NewClassTreeMatcher,
BugChecker.MemberSelectTreeMatcher,
BugChecker.MemberReferenceTreeMatcher,
BugChecker.IdentifierTreeMatcher {
private static final long serialVersionUID = 1L;
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}
@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}
@Override
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}
@Override
public Description matchIdentifier(IdentifierTree tree, VisitorState state) {
Symbol sym = ASTHelpers.getSymbol(tree);
return checkDeprecated(sym, tree, state);
}
private Description checkDeprecated(Symbol sym, Tree tree, VisitorState state) {
if (sym == null) {
return Description.NO_MATCH;
}
// Don't warn on import statements
if (isInsideImport(state)) {
return Description.NO_MATCH;
}
// Check if the symbol itself is deprecated
if (!isDeprecated(sym, state)) {
return Description.NO_MATCH;
}
// Don't warn if we're inside a deprecated context (class, method, etc.)
if (isInsideDeprecatedCode(state)) {
return Description.NO_MATCH;
}
// Don't warn if the deprecated symbol is in the same top-level class (matches javac behavior)
if (isInSameTopLevelClass(sym, state)) {
return Description.NO_MATCH;
}
return buildDescription(tree).setMessage("Use of deprecated API: " + sym).build();
}
private static boolean isInsideImport(VisitorState state) {
for (Tree tree : state.getPath()) {
if (tree instanceof ImportTree) {
return true;
}
}
return false;
}
private static boolean isDeprecated(Symbol sym, VisitorState state) {
// First try the symbol's isDeprecated() method
if (sym.isDeprecated()) {
return true;
}
// Also check for @Deprecated annotation (some symbols may not have flag set)
return ASTHelpers.hasAnnotation(sym, "java.lang.Deprecated", state);
}
private static boolean isInsideDeprecatedCode(VisitorState state) {
// Check enclosing elements (method, class) for @Deprecated
// Skip the first element which is the current node being checked
boolean first = true;
for (Tree tree : state.getPath()) {
if (first) {
first = false;
continue;
}
Symbol sym = ASTHelpers.getSymbol(tree);
if (sym != null && isDeprecated(sym, state)) {
return true;
}
}
return false;
}
private static boolean isInSameTopLevelClass(Symbol sym, VisitorState state) {
// Get the top-level class containing the deprecated symbol
Symbol.ClassSymbol deprecatedTopLevel = getTopLevelClass(sym);
if (deprecatedTopLevel == null) {
return false;
}
// Get the top-level class containing the current usage by walking the tree path
// Skip the first element (the node being checked) to find the enclosing class
Symbol.ClassSymbol usageTopLevel = null;
boolean first = true;
for (Tree tree : state.getPath()) {
if (first) {
first = false;
continue;
}
Symbol treeSym = ASTHelpers.getSymbol(tree);
if (treeSym instanceof Symbol.ClassSymbol classSymbol) {
usageTopLevel = getTopLevelClass(classSymbol);
if (usageTopLevel != null) {
break;
}
}
}
if (usageTopLevel == null) {
return false;
}
return deprecatedTopLevel.equals(usageTopLevel);
}
@Nullable
private static Symbol.ClassSymbol getTopLevelClass(Symbol sym) {
Symbol current = sym;
while (current != null) {
if (current instanceof Symbol.ClassSymbol classSymbol) {
Symbol owner = classSymbol.owner;
// Top-level class is owned by a package
if (owner instanceof Symbol.PackageSymbol) {
return classSymbol;
}
}
current = current.owner;
}
return null;
}
}