diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheck.java
index d5afd57b8..8c1de3356 100644
--- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheck.java
+++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheck.java
@@ -25,7 +25,6 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
/**
- * Class to check the ordering/grouping of imports. Features are:
*
* - groups imports: ensures that groups of imports come in a specific order
* (e.g., java. comes first, javax. comes second, then everything else)
@@ -54,9 +53,10 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
*
*
*
- * There is always an additional, implied "everything else" package
- * group. If no "groups" property is supplied, all imports belong in
- * this "everything else" group.
+ * There is always a wildcard group to which everything not in a named group
+ * belongs. If an import does not match a named group, the group belongs to
+ * this wildcard group. The wildcard group position can be specified using the
+ * {@code *} character.
*
*
*
@@ -77,12 +77,19 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
* @author Bill Schneider
* @author o_sukhodolsky
* @author David DIDIER
+ * @author Steve McKay
*/
public class ImportOrderCheck
extends AbstractOptionCheck
{
+
+ /** the special wildcard that catches all remaining groups. */
+ private static final String WILDCARD_GROUP_NAME = "*";
+
/** List of import groups specified by the user. */
private String[] mGroups = new String[0];
+ /** The position of the "everything else" group. */
+ private int mWildcardGroupIndex;
/** Require imports in group be separated. */
private boolean mSeparated;
/** Require imports in group. */
@@ -123,7 +130,12 @@ public class ImportOrderCheck
for (int i = 0; i < aGroups.length; i++) {
String pkg = aGroups[i];
- if (!pkg.endsWith(".")) {
+ // if the pkg name is the wildcard, record the
+ // position for later reference
+ if (WILDCARD_GROUP_NAME.equals(pkg)) {
+ mWildcardGroupIndex = i;
+ }
+ else if (!pkg.endsWith(".")) {
pkg = pkg + ".";
}
@@ -333,17 +345,16 @@ public class ImportOrderCheck
*/
private int getGroupNumber(String aName)
{
- int i = 0;
-
// find out what group this belongs in
// loop over mGroups and get index
- for (; i < mGroups.length; i++) {
+ for (int i = 0; i < mGroups.length; i++) {
if (aName.startsWith(mGroups[i])) {
- break;
+ return i;
}
}
- return i;
+ // no match, so we return the wildcard group
+ return mWildcardGroupIndex;
}
/**
diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportOrder_Wildcard.java b/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportOrder_Wildcard.java
new file mode 100644
index 000000000..1b38121c0
--- /dev/null
+++ b/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportOrder_Wildcard.java
@@ -0,0 +1,12 @@
+
+// groups are configured as follows
+// com.puppycrawl,*,java
+// the trailing javax.crypto.Cipher; should be flagged as an error.
+
+import com.puppycrawl.tools.checkstyle.imports.InputImportOrder_Above;
+import javax.crypto.BadPaddingException;
+import java.util.List;
+import javax.crypto.Cipher;
+
+public class InputImportOrder_Wildcard {
+}
diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheckTest.java
index 30c8eb9cd..2cc41ed63 100644
--- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheckTest.java
+++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportOrderCheckTest.java
@@ -141,4 +141,16 @@ public class ImportOrderCheckTest extends BaseCheckTestSupport
verify(checkConfig, getPath("imports" + File.separator + "InputImportOrder_Bottom.java"), expected);
}
+
+ @Test
+ public void testWildcard() throws Exception
+ {
+ final DefaultConfiguration checkConfig = createCheckConfig(ImportOrderCheck.class);
+ checkConfig.addAttribute("groups", "com,*,java");
+ final String[] expected = {
+ "9: Wrong order for 'javax.crypto.Cipher' import."
+ };
+
+ verify(checkConfig, getPath("imports" + File.separator + "InputImportOrder_Wildcard.java"), expected);
+ }
}
diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml
index aa4fbb6e0..a400d82b7 100755
--- a/src/xdocs/releasenotes.xml
+++ b/src/xdocs/releasenotes.xml
@@ -12,6 +12,10 @@
New features:
+ -
+ Enhanced ImportOrder check to support wildcard ('*') groups.
+ Thanks to Steve McKay for patch #2891032.
+
-
Enhanced the naming checks ConstantName, MemberName, MethodName
and StaticVariableName to utilise the access control tuning features