Skip to content

Commit

Permalink
Fix Finalize bugpattern to match protected finalize()
Browse files Browse the repository at this point in the history
Object#finalize() is protected, but the Finalize bugpattern would only find overridden PUBLIC finalizers. Fix it to look for protected methods too. The example finalizer in the Object#finalize() javadocs is protected, previously it would not be matched.

https://docs.oracle.com/javase/9/docs/api/java/lang/Object.html#finalize--

Fixes #3652

COPYBARA_INTEGRATE_REVIEW=#3652 from rmuir:finalize_doesnt_match_all_finalizers 99d23d0
PiperOrigin-RevId: 499137662
  • Loading branch information
rmuir authored and Error Prone Team committed Jan 3, 2023
1 parent 6c0d670 commit 8447456
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.MethodTree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.Set;
import javax.lang.model.element.Modifier;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
Expand All @@ -45,8 +45,8 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
if (!isVoidType(getType(tree.getReturnType()), state)) {
return NO_MATCH;
}
MethodSymbol sym = getSymbol(tree);
if (!sym.getModifiers().contains(Modifier.PUBLIC)) {
Set<Modifier> modifiers = getSymbol(tree).getModifiers();
if (!modifiers.contains(Modifier.PROTECTED) && !modifiers.contains(Modifier.PUBLIC)) {
return NO_MATCH;
}
return describeMatch(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void positive() {
"Test.java",
"class Test {",
" // BUG: Diagnostic contains: Do not override finalize",
" public void finalize() {}",
" protected void finalize() {}",
" interface A {",
" // BUG: Diagnostic contains: Do not override finalize",
" void finalize();",
Expand Down

0 comments on commit 8447456

Please sign in to comment.