-
Notifications
You must be signed in to change notification settings - Fork 940
Replace Xlint deprecation check with custom errorprone check #8061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jack-berg
merged 6 commits into
open-telemetry:main
from
trask:deprecate-with-errorprone
Feb 13, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
custom-checks/src/main/java/io/opentelemetry/gradle/customchecks/OtelDeprecatedApiUsage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checkDeprecated(sym, tree, state); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchNewClass(NewClassTree tree, VisitorState state) { | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checkDeprecated(sym, tree, state); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checkDeprecated(sym, tree, state); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checkDeprecated(sym, tree, state); | ||
| } | ||
|
|
||
| @Override | ||
| public Description matchIdentifier(IdentifierTree tree, VisitorState state) { | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return checkDeprecated(sym, tree, state); | ||
| } | ||
|
|
||
| private Description checkDeprecated(Symbol sym, Tree tree, VisitorState state) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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(); | ||
jack-berg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (sym != null && isDeprecated(sym, state)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static boolean isInSameTopLevelClass(Symbol sym, VisitorState state) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Get the top-level class containing the deprecated symbol | ||
| Symbol.ClassSymbol deprecatedTopLevel = getTopLevelClass(sym); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| boolean first = true; | ||
| for (Tree tree : state.getPath()) { | ||
| if (first) { | ||
| first = false; | ||
| continue; | ||
| } | ||
| Symbol treeSym = ASTHelpers.getSymbol(tree); | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (treeSym instanceof Symbol.ClassSymbol classSymbol) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| usageTopLevel = getTopLevelClass(classSymbol); | ||
| if (usageTopLevel != null) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (usageTopLevel == null) { | ||
| return false; | ||
| } | ||
|
|
||
| return deprecatedTopLevel.equals(usageTopLevel); | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Symbol.ClassSymbol getTopLevelClass(Symbol sym) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Symbol current = sym; | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| while (current != null) { | ||
| if (current instanceof Symbol.ClassSymbol classSymbol) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Symbol owner = classSymbol.owner; | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Top-level class is owned by a package | ||
| if (owner instanceof Symbol.PackageSymbol) { | ||
trask marked this conversation as resolved.
Show resolved
Hide resolved
trask marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return classSymbol; | ||
| } | ||
| } | ||
| current = current.owner; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-checks/src/main/resources/META-INF/services/com.google.errorprone.bugpatterns.BugChecker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| io.opentelemetry.gradle.customchecks.OtelDeprecatedApiUsage | ||
| io.opentelemetry.gradle.customchecks.OtelInternalJavadoc | ||
| io.opentelemetry.gradle.customchecks.OtelPrivateConstructorForUtilityClass |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's slightly more strict than
javachere, but this does align with Intellij's warnings, so seems good