diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index 1e4082b67..364f9c0ab 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -856,17 +856,6 @@ class Verifier **/ void reportImport(int aLineNo, String aType) { - if (!mConfig.isIgnoreImports()) { - // Check for a duplicate import - final Iterator it = mImports.iterator(); - while (it.hasNext()) { - final LineText lt = (LineText) it.next(); - if (aType.equals(lt.getText())) { - mMessages.add(aLineNo, "import.duplicate", - new Integer(lt.getLineNo())); - } - } - } // Add to list to check for duplicates, usage and instantiation checks mImports.add(new LineText(aLineNo, aType)); } @@ -879,9 +868,6 @@ class Verifier **/ void reportStarImport(int aLineNo, String aPkg) { - if (!mConfig.isIgnoreImports()) { - mMessages.add(aLineNo, "import.avoidStar"); - } mImports.add(new LineText(aLineNo, aPkg)); } @@ -1420,16 +1406,7 @@ class Verifier while (it.hasNext()) { final LineText imp = (LineText) it.next(); - if (fromPackage(imp.getText(), "java.lang")) { - mMessages.add(imp.getLineNo(), "import.lang"); - } - else if (fromPackage(imp.getText(), mPkgName)) { - mMessages.add(imp.getLineNo(), "import.same"); - } - else if (!isReferencedImport(imp)) { - mMessages.add(imp.getLineNo(), "import.unused", imp.getText()); - } - else if (isIllegalImport(imp.getText())) { + if (isIllegalImport(imp.getText())) { mMessages.add(imp.getLineNo(), "import.illegal", imp.getText()); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RedundantImportCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RedundantImportCheck.java index 6a1ddf83a..643aa4bc9 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RedundantImportCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RedundantImportCheck.java @@ -23,10 +23,23 @@ import com.puppycrawl.tools.checkstyle.JavaTokenTypes; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FullIdent; +import java.util.Iterator; +import java.util.Set; +import java.util.HashSet; + public class RedundantImportCheck extends ImportCheck { private String mPkgName; + /** set of the imports */ + private final Set mImports = new HashSet(); + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void beginTree() + { + mPkgName = null; + mImports.clear(); + } /** @see com.puppycrawl.tools.checkstyle.api.Check */ public int[] getDefaultTokens() @@ -49,6 +62,19 @@ public class RedundantImportCheck else if (fromPackage(imp.getText(), mPkgName)) { log(aAST.getLineNo(), aAST.getColumnNo(), "import.same"); } + // Check for a duplicate import + final Iterator it = mImports.iterator(); + while (it.hasNext()) { + final FullIdent full = (FullIdent) it.next(); + if (imp.getText().equals(full.getText())) { + log(aAST.getLineNo(), + aAST.getColumnNo(), + "import.duplicate", + new Integer(full.getLineNo())); + } + } + + mImports.add(imp); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index 7d64257dd..fdec343b3 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -720,16 +720,6 @@ public class CheckerTest final String filepath = getPath("InputImport.java"); assertNotNull(c); final String[] expected = { - filepath + ":7: Avoid using the '.*' form of import.", - filepath + ":7: Redundant import from the same package.", - filepath + ":8: Redundant import from the same package.", - filepath + ":9: Avoid using the '.*' form of import.", - filepath + ":10: Avoid using the '.*' form of import.", - filepath + ":10: Redundant import from the java.lang package.", - filepath + ":11: Redundant import from the java.lang package.", - filepath + ":13: Unused import - java.util.List.", - filepath + ":14: Duplicate import to line 13.", - filepath + ":14: Unused import - java.util.List.", filepath + ":15: Import from illegal package - sun.net.ftpclient.FtpClient.", }; verify(c, filepath, expected); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/RedundantImportCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/RedundantImportCheckTest.java index cb80325f4..fbc53f07e 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/RedundantImportCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/RedundantImportCheckTest.java @@ -22,6 +22,7 @@ public class RedundantImportCheckTest "8:38: Redundant import from the same package.", "10:1: Redundant import from the java.lang package.", "11:1: Redundant import from the java.lang package.", + "14:1: Duplicate import to line 13.", }; verify(c, fname, expected); }