From bbf7cd951536ed85ef02f1f94badbcab972ae616 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Mon, 31 Jul 2023 09:49:23 -0700 Subject: [PATCH] Add support in ASTHelpersSuggestions for getEnclosedElements See #4026. `Symbol.getEnclosedElements()` is another method that can cause compatibility issues across JDK versions (https://github.com/google/error-prone/issues/3895). Fixes #4027 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/4027 from msridhar:asthelperssuggestions-getenclosedelements b4c01ba852b482c0bfd39352276ebdd070a4ba9e PiperOrigin-RevId: 552510760 --- .../bugpatterns/ASTHelpersSuggestions.java | 3 ++- .../ASTHelpersSuggestionsTest.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java b/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java index d375a117687..b75e4e485e4 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java @@ -50,7 +50,8 @@ public class ASTHelpersSuggestions extends BugChecker implements MethodInvocatio anyOf( instanceMethod() .onDescendantOf("com.sun.tools.javac.code.Symbol") - .namedAnyOf("isDirectlyOrIndirectlyLocal", "isLocal", "packge"), + .namedAnyOf( + "isDirectlyOrIndirectlyLocal", "isLocal", "packge", "getEnclosedElements"), instanceMethod() .onClass((t, s) -> isSubtype(MODULE_SYMBOL.get(s), t, s)) .namedAnyOf("isStatic")); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java index e0be410138b..cfc4ad9e499 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java @@ -87,4 +87,29 @@ public void onSymbolSubtype() { "jdk.compiler/com.sun.tools.javac.code", "jdk.compiler/com.sun.tools.javac.util") .doTest(); } + + @Test + public void symbolGetEnclosedElements() { + testHelper + .addInputLines( + "Test.java", + "import com.sun.tools.javac.code.Symbol.ClassSymbol;", + "class Test {", + " void f(ClassSymbol s) {", + " s.getEnclosedElements();", + " }", + "}") + .addOutputLines( + "Test.java", + "import static com.google.errorprone.util.ASTHelpers.getEnclosedElements;", + "import com.sun.tools.javac.code.Symbol.ClassSymbol;", + "class Test {", + " void f(ClassSymbol s) {", + " getEnclosedElements(s);", + " }", + "}") + .addModules( + "jdk.compiler/com.sun.tools.javac.code", "jdk.compiler/com.sun.tools.javac.util") + .doTest(); + } }