Repository: checkstyle/checkstyle

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


Hints:
I am on it


Patch:
diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionAvoidStaticImportTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionAvoidStaticImportTest.java
new file mode 100644
--- /dev/null
+++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionAvoidStaticImportTest.java
@@ -0,0 +1,82 @@
+////////////////////////////////////////////////////////////////////////////////
+// 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.Collections;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
+import com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck;
+
+public class XpathRegressionAvoidStaticImportTest
+        extends AbstractXpathTestSupport {
+
+    private static final Class<AvoidStaticImportCheck> CLASS =
+        AvoidStaticImportCheck.class;
+
+    @Override
+    protected String getCheckName() {
+        return CLASS.getSimpleName();
+    }
+
+    @Test
+    public void testOne() throws Exception {
+        final File fileToProcess = new File(getPath(
+            "SuppressionXpathRegressionAvoidStaticImport1.java"));
+
+        final DefaultConfiguration moduleConfig =
+                createModuleConfig(CLASS);
+
+        final String[] expectedViolation = {
+            "3:42: " + getCheckMessage(CLASS,
+                AvoidStaticImportCheck.MSG_KEY, "javax.swing.WindowConstants.*"),
+        };
+
+        final List<String> expectedXpathQueries = Collections.singletonList(
+                "/STATIC_IMPORT/DOT"
+        );
+
+        runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
+    }
+
+    @Test
+    public void testTwo() throws Exception {
+        final File fileToProcess = new File(getPath(
+            "SuppressionXpathRegressionAvoidStaticImport2.java"));
+
+        final DefaultConfiguration moduleConfig =
+                createModuleConfig(CLASS);
+
+        final String[] expectedViolation = {
+            "3:27: " + getCheckMessage(CLASS,
+                AvoidStaticImportCheck.MSG_KEY, "java.io.File.createTempFile"),
+        };
+
+        final List<String> expectedXpathQueries = Collections.singletonList(
+                "/STATIC_IMPORT/DOT[./IDENT[@text='createTempFile']]"
+        );
+
+        runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
+    }
+
+}
diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport1.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport1.java
new file mode 100644
--- /dev/null
+++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport1.java
@@ -0,0 +1,6 @@
+package org.checkstyle.suppressionxpathfilter.avoidstaticimport;
+
+import static javax.swing.WindowConstants.*; // warn
+
+public class SuppressionXpathRegressionAvoidStaticImport1 {
+}
diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport2.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport2.java
new file mode 100644
--- /dev/null
+++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/avoidstaticimport/SuppressionXpathRegressionAvoidStaticImport2.java
@@ -0,0 +1,6 @@
+package org.checkstyle.suppressionxpathfilter.avoidstaticimport;
+
+import static java.io.File.createTempFile; // warn
+
+public class SuppressionXpathRegressionAvoidStaticImport2 {
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java
@@ -125,7 +125,7 @@ public void visitToken(final DetailAST ast) {
         final FullIdent name = FullIdent.createFullIdent(startingDot);
 
         if (!isExempt(name.getText())) {
-            log(startingDot.getLineNo(), MSG_KEY, name.getText());
+            log(startingDot, MSG_KEY, name.getText());
         }
     }
 
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
@@ -56,9 +56,6 @@
  * AvoidStarImport
  * </li>
  * <li>
- * AvoidStaticImport
- * </li>
- * <li>
  * CommentsIndentation
  * </li>
  * <li>


Test patch:
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheckTest.java
@@ -50,17 +50,16 @@ public void testDefaultOperation()
         final DefaultConfiguration checkConfig =
             createModuleConfig(AvoidStaticImportCheck.class);
         final String[] expected = {
-            "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
-            "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
-            "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
-            "29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
-            "30: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
-            "31: " + getCheckMessage(MSG_KEY,
+            "23:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
+            "25:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
+            "26:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
+            "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
+            "28:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
+            "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
+            "30:113: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
-            "32: " + getCheckMessage(MSG_KEY,
+            "31:124: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
         };
@@ -76,14 +75,13 @@ public void testStarExcludes()
         checkConfig.addAttribute("excludes", "java.io.File.*,sun.net.ftpclient.FtpClient.*");
         // allow the "java.io.File.*" AND "sun.net.ftpclient.FtpClient.*" star imports
         final String[] expected = {
-            "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
-            "30: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
-            "31: " + getCheckMessage(MSG_KEY,
+            "25:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
+            "28:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
+            "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
+            "30:113: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
-            "32: " + getCheckMessage(MSG_KEY,
+            "31:124: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
         };
@@ -98,15 +96,14 @@ public void testMemberExcludes()
         checkConfig.addAttribute("excludes", "java.io.File.listRoots,java.lang.Math.E");
         // allow the java.io.File.listRoots and java.lang.Math.E member imports
         final String[] expected = {
-            "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
-            "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
-            "30: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
-            "31: " + getCheckMessage(MSG_KEY,
+            "25:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
+            "26:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
+            "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
+            "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
+            "30:113: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
-            "32: " + getCheckMessage(MSG_KEY,
+            "31:124: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
         };
@@ -126,17 +123,16 @@ public void testBogusMemberExcludes()
             + "sun.net.ftpclient.FtpClient.*FtpClient, sun.net.ftpclient.FtpClientjunk,"
             + " java.io.File.listRootsmorejunk");
         final String[] expected = {
-            "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
-            "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
-            "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
-            "29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
-            "30: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
-            "31: " + getCheckMessage(MSG_KEY,
+            "23:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
+            "25:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
+            "26:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
+            "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
+            "28:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
+            "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
+            "30:113: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
-            "32: " + getCheckMessage(MSG_KEY,
+            "31:124: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.one"),
         };
@@ -156,14 +152,13 @@ public void testInnerClassMemberExcludesStar()
             "com.puppycrawl.tools.checkstyle.checks.imports."
                 + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass.*");
         final String[] expected = {
-            "23: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
-            "25: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "26: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
-            "27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
-            "28: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
-            "29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
-            "30: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
-            "31: " + getCheckMessage(MSG_KEY,
+            "23:27: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
+            "25:42: " + getCheckMessage(MSG_KEY, "javax.swing.WindowConstants.*"),
+            "26:27: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
+            "27:27: " + getCheckMessage(MSG_KEY, "java.io.File.pathSeparator"),
+            "28:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.E"),
+            "29:29: " + getCheckMessage(MSG_KEY, "java.lang.Math.sqrt"),
+            "30:113: " + getCheckMessage(MSG_KEY,
                 "com.puppycrawl.tools.checkstyle.checks.imports."
                     + "avoidstaticimport.InputAvoidStaticImportNestedClass.InnerClass"),
         };
@@ -181,3 +176,4 @@ public void testGetAcceptableTokens() {
     }
 
 }
+
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
@@ -56,7 +56,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
             "AnnotationUseStyle",
             "AvoidEscapedUnicodeCharacters",
             "AvoidStarImport",
-            "AvoidStaticImport",
             "CommentsIndentation",
             "CustomImportOrder",
             "EmptyLineSeparator",
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/avoidstaticimport/InputAvoidStaticImportDefault.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/avoidstaticimport/InputAvoidStaticImportDefault.java
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/avoidstaticimport/InputAvoidStaticImportDefault.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/imports/avoidstaticimport/InputAvoidStaticImportDefault.java
@@ -22,7 +22,6 @@
 import javax.swing.BorderFactory;
 import static java.io.File.listRoots;
 
-import static javax.swing.WindowConstants.*;
 import static javax.swing.WindowConstants.*;
 import static java.io.File.createTempFile;
 import static java.io.File.pathSeparator;