Fixed exception in AvoidStaticImport check trying to exclude members of inner class. #1209

This commit is contained in:
Aleksandr Ivanov 2015-06-10 14:06:01 +03:00 committed by Roman Ivanov
parent 68dbad5779
commit fd33eb9c26
4 changed files with 44 additions and 5 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;
}
}