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 index 1a95cca9e..6f8f3466d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportCheck.java @@ -122,7 +122,8 @@ public class AvoidStaticImportCheck //a starred import final String excludeMinusDotStar = exclude.substring(0, exclude.length() - 2); - if (classOrStaticMember.startsWith(excludeMinusDotStar)) { + if (classOrStaticMember.startsWith(excludeMinusDotStar) + && !classOrStaticMember.equals(excludeMinusDotStar)) { final String member = classOrStaticMember.substring( excludeMinusDotStar.length() + 1); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportTest.java index 1a3fa96a4..1d8304f14 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/AvoidStaticImportTest.java @@ -42,6 +42,8 @@ public class AvoidStaticImportTest "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, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass"), + "30: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one"), }; verify(checkConfig, getPath("imports" + File.separator + "InputAvoidStaticImportCheck.java"), expected); @@ -53,10 +55,12 @@ public class AvoidStaticImportTest final DefaultConfiguration checkConfig = createCheckConfig(AvoidStaticImportCheck.class); checkConfig.addAttribute("excludes", "java.io.File.*,sun.net.ftpclient.FtpClient.*"); - // allow the java.io.File.*/sun.net.ftpclient.FtpClient.* star imports + // 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, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass"), + "30: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one"), }; verify(checkConfig, getPath("imports" + File.separator + "InputAvoidStaticImportCheck.java"), expected); } @@ -73,6 +77,8 @@ public class AvoidStaticImportTest "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, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass"), + "30: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one"), }; verify(checkConfig, getPath("imports" + File.separator + "InputAvoidStaticImportCheck.java"), expected); } @@ -83,16 +89,36 @@ public class AvoidStaticImportTest final DefaultConfiguration checkConfig = createCheckConfig(AvoidStaticImportCheck.class); checkConfig.addAttribute( - "excludes", - "java.io.File.listRoots.listRoots, javax.swing.WindowConstants," + "excludes", //should NOT mask anything + "java.io.File.listRoots.listRoots, javax.swing.WindowConstants, javax.swing.*," + "sun.net.ftpclient.FtpClient.*FtpClient, sun.net.ftpclient.FtpClientjunk, java.io.File.listRootsmorejunk"); - // allow the java.io.File.listRoots member imports 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, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass"), + "30: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one"), + }; + verify(checkConfig, getPath("imports" + File.separator + "InputAvoidStaticImportCheck.java"), expected); + } + + @Test + public void testInnerClassMemberExcludesStar() + throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(AvoidStaticImportCheck.class); + checkConfig.addAttribute( + "excludes", //should mask com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one + "com.puppycrawl.tools.checkstyle.imports.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, "com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass"), }; verify(checkConfig, getPath("imports" + File.separator + "InputAvoidStaticImportCheck.java"), expected); } @@ -106,4 +132,5 @@ public class AvoidStaticImportTest assertArrayEquals(expected, actual); } + } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportCheck.java b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportCheck.java index 75515237e..209a3fff0 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportCheck.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportCheck.java @@ -26,6 +26,8 @@ import static javax.swing.WindowConstants.*; import static javax.swing.WindowConstants.*; import static java.io.File.createTempFile; import static java.io.File.pathSeparator; +import static com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass; +import static com.puppycrawl.tools.checkstyle.imports.InputAvoidStaticImportNestedClass.InnerClass.one; import java.awt.Component; import java.awt.Graphics2D; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportNestedClass.java b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportNestedClass.java new file mode 100644 index 000000000..8e9cbabf0 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/imports/InputAvoidStaticImportNestedClass.java @@ -0,0 +1,9 @@ +package com.puppycrawl.tools.checkstyle.imports; + +public class InputAvoidStaticImportNestedClass{ + public static Integer zero=0; + + public static class InnerClass { + public static Integer one=1; + } +}