Repository: checkstyle/checkstyle

Problem statement:
Update AbstractChecks to log DetailAST - NoFinalizer
Child of https://github.com/checkstyle/checkstyle/issues/5777

This issue specifically focuses on NoFinalizer.


Hints:
I am on it.

Patch:
diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNoFinalizerTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNoFinalizerTest.java
new file mode 100644
--- /dev/null
+++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNoFinalizerTest.java
@@ -0,0 +1,98 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2020 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+////////////////////////////////////////////////////////////////////////////////
+
+package org.checkstyle.suppressionxpathfilter;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.checks.coding.NoFinalizerCheck;
+
+public class XpathRegressionNoFinalizerTest extends AbstractXpathTestSupport {
+
+    @Override
+    protected String getCheckName() {
+        return NoFinalizerCheck.class.getSimpleName();
+    }
+
+    @Test
+    public void testOne() throws Exception {
+        final File fileToProcess = new File(
+                getPath("SuppressionXpathRegressionNoFinalizer1.java"));
+
+        final DefaultConfiguration moduleConfig =
+                createModuleConfig(NoFinalizerCheck.class);
+
+        final String[] expectedViolation = {
+            "8:5: " + getCheckMessage(NoFinalizerCheck.class,
+                    NoFinalizerCheck.MSG_KEY),
+        };
+
+        final List<String> expectedXpathQueries = Arrays.asList(
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer1']]"
+                + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]",
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer1']]"
+                + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer1']]"
+                + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/LITERAL_PROTECTED"
+        );
+
+        runVerifications(moduleConfig, fileToProcess, expectedViolation,
+                expectedXpathQueries);
+    }
+
+    @Test
+    public void testTwo() throws Exception {
+        final File fileToProcess = new File(
+                getPath("SuppressionXpathRegressionNoFinalizer2.java"));
+
+        final DefaultConfiguration moduleConfig =
+                createModuleConfig(NoFinalizerCheck.class);
+
+        final String[] expectedViolation = {
+            "10:9: " + getCheckMessage(NoFinalizerCheck.class,
+                    NoFinalizerCheck.MSG_KEY),
+        };
+
+        final List<String> expectedXpathQueries = Arrays.asList(
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer2']]"
+                + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
+                + "METHOD_DEF[./IDENT[@text='finalize']]",
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer2']]"
+                + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
+                + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer2']]"
+                + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
+                + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
+                + "ANNOTATION[./IDENT[@text='Override']]",
+                "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNoFinalizer2']]"
+                + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
+                + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
+                + "ANNOTATION[./IDENT[@text='Override']]/AT"
+        );
+
+        runVerifications(moduleConfig, fileToProcess, expectedViolation,
+                expectedXpathQueries);
+    }
+
+}
diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer1.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer1.java
new file mode 100644
--- /dev/null
+++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer1.java
@@ -0,0 +1,11 @@
+package org.checkstyle.suppressionxpathfilter.nofinalizer;
+
+public class SuppressionXpathRegressionNoFinalizer1 {
+    public static void main(String[] args) {
+        // some code here
+    }
+
+    protected void finalize() throws Throwable { // warn
+
+    }
+}
diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer2.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer2.java
new file mode 100644
--- /dev/null
+++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/nofinalizer/SuppressionXpathRegressionNoFinalizer2.java
@@ -0,0 +1,16 @@
+package org.checkstyle.suppressionxpathfilter.nofinalizer;
+
+public class SuppressionXpathRegressionNoFinalizer2 {
+    public static void doStuff() {
+        // some code here
+    }
+
+    class InnerClass {
+
+        @Override // warn
+        @Deprecated
+        protected void finalize() throws Throwable {
+
+        }
+    }
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheck.java
@@ -73,17 +73,17 @@ public int[] getRequiredTokens() {
     }
 
     @Override
-    public void visitToken(DetailAST aAST) {
-        final DetailAST mid = aAST.findFirstToken(TokenTypes.IDENT);
+    public void visitToken(DetailAST ast) {
+        final DetailAST mid = ast.findFirstToken(TokenTypes.IDENT);
         final String name = mid.getText();
 
         if ("finalize".equals(name)) {
-            final DetailAST params = aAST.findFirstToken(TokenTypes.PARAMETERS);
+            final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
             final boolean hasEmptyParamList =
                 params.findFirstToken(TokenTypes.PARAMETER_DEF) == null;
 
             if (hasEmptyParamList) {
-                log(aAST.getLineNo(), MSG_KEY);
+                log(ast, MSG_KEY);
             }
         }
     }
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java
@@ -134,9 +134,6 @@
  * NeedBraces
  * </li>
  * <li>
- * NoFinalizer
- * </li>
- * <li>
  * NoLineWrap
  * </li>
  * <li>


Test patch:
diff --git a/src/it/java/com/google/checkstyle/test/chapter6programpractice/rule64finalizers/NoFinalizerTest.java b/src/it/java/com/google/checkstyle/test/chapter6programpractice/rule64finalizers/NoFinalizerTest.java
--- a/src/it/java/com/google/checkstyle/test/chapter6programpractice/rule64finalizers/NoFinalizerTest.java
+++ b/src/it/java/com/google/checkstyle/test/chapter6programpractice/rule64finalizers/NoFinalizerTest.java
@@ -37,7 +37,7 @@ public void testNoFinalizerBasic() throws Exception {
         final String msg = getCheckMessage(NoFinalizerCheck.class, "avoid.finalizer.method");
 
         final String[] expected = {
-            "5: " + msg,
+            "5:5: " + msg,
         };
 
         final Configuration checkConfig = getModuleConfig("NoFinalizer");
@@ -52,15 +52,15 @@ public void testNoFinalizerExtended() throws Exception {
         final String msg = getCheckMessage(NoFinalizerCheck.class, "avoid.finalizer.method");
 
         final String[] expected = {
-            "9: " + msg,
-            "21: " + msg,
-            "33: " + msg,
-            "45: " + msg,
-            "57: " + msg,
-            "69: " + msg,
-            "79: " + msg,
-            "119: " + msg,
-            "136: " + msg,
+            "9:5: " + msg,
+            "21:5: " + msg,
+            "33:5: " + msg,
+            "45:5: " + msg,
+            "57:5: " + msg,
+            "69:5: " + msg,
+            "79:9: " + msg,
+            "119:13: " + msg,
+            "136:5: " + msg,
         };
 
         final Configuration checkConfig = getModuleConfig("NoFinalizer");
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoFinalizerCheckTest.java
@@ -57,7 +57,7 @@ public void testHasFinalizer()
         final DefaultConfiguration checkConfig =
             createModuleConfig(NoFinalizerCheck.class);
         final String[] expected = {
-            "5: " + getCheckMessage(MSG_KEY),
+            "5:5: " + getCheckMessage(MSG_KEY),
         };
         verify(checkConfig, getPath("InputNoFinalizerHasFinalizer.java"), expected);
     }
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java
@@ -82,7 +82,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
             "MissingOverride",
             "MissingSwitchDefault",
             "NeedBraces",
-            "NoFinalizer",
             "NoLineWrap",
             "OuterTypeFilename",
             "OverloadMethodsDeclarationOrder",