| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package com.puppycrawl.tools.checkstyle.checks.imports; |
| 21 | |
|
| 22 | |
import com.google.common.collect.Sets; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 25 | |
import com.puppycrawl.tools.checkstyle.api.FullIdent; |
| 26 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 27 | |
import java.util.Set; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 1 | public class RedundantImportCheck |
| 55 | |
extends Check |
| 56 | |
{ |
| 57 | |
|
| 58 | |
private String mPkgName; |
| 59 | |
|
| 60 | 1 | private final Set<FullIdent> mImports = Sets.newHashSet(); |
| 61 | |
|
| 62 | 1 | private final Set<FullIdent> mStaticImports = Sets.newHashSet(); |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public void beginTree(DetailAST aRootAST) |
| 66 | |
{ |
| 67 | 1 | mPkgName = null; |
| 68 | 1 | mImports.clear(); |
| 69 | 1 | mStaticImports.clear(); |
| 70 | 1 | } |
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public int[] getDefaultTokens() |
| 74 | |
{ |
| 75 | 1 | return new int[] |
| 76 | |
{TokenTypes.IMPORT, |
| 77 | |
TokenTypes.STATIC_IMPORT, |
| 78 | |
TokenTypes.PACKAGE_DEF, }; |
| 79 | |
} |
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public void visitToken(DetailAST aAST) |
| 83 | |
{ |
| 84 | 29 | if (aAST.getType() == TokenTypes.PACKAGE_DEF) { |
| 85 | 1 | mPkgName = FullIdent.createFullIdent( |
| 86 | |
aAST.getLastChild().getPreviousSibling()).getText(); |
| 87 | |
} |
| 88 | 28 | else if (aAST.getType() == TokenTypes.IMPORT) { |
| 89 | 23 | final FullIdent imp = FullIdent.createFullIdentBelow(aAST); |
| 90 | 23 | if (fromPackage(imp.getText(), "java.lang")) { |
| 91 | 2 | log(aAST.getLineNo(), aAST.getColumnNo(), "import.lang", |
| 92 | |
imp.getText()); |
| 93 | |
} |
| 94 | 21 | else if (fromPackage(imp.getText(), mPkgName)) { |
| 95 | 2 | log(aAST.getLineNo(), aAST.getColumnNo(), "import.same", |
| 96 | |
imp.getText()); |
| 97 | |
} |
| 98 | |
|
| 99 | 23 | for (FullIdent full : mImports) { |
| 100 | 253 | if (imp.getText().equals(full.getText())) { |
| 101 | 1 | log(aAST.getLineNo(), aAST.getColumnNo(), |
| 102 | |
"import.duplicate", full.getLineNo(), |
| 103 | |
imp.getText()); |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|
| 107 | 23 | mImports.add(imp); |
| 108 | 23 | } |
| 109 | |
else { |
| 110 | |
|
| 111 | 5 | final FullIdent imp = |
| 112 | |
FullIdent.createFullIdent( |
| 113 | |
aAST.getLastChild().getPreviousSibling()); |
| 114 | 5 | for (FullIdent full : mStaticImports) { |
| 115 | 10 | if (imp.getText().equals(full.getText())) { |
| 116 | 1 | log(aAST.getLineNo(), aAST.getColumnNo(), |
| 117 | |
"import.duplicate", full.getLineNo(), imp.getText()); |
| 118 | |
} |
| 119 | |
} |
| 120 | |
|
| 121 | 5 | mStaticImports.add(imp); |
| 122 | |
} |
| 123 | 29 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
private static boolean fromPackage(String aImport, String aPkg) |
| 132 | |
{ |
| 133 | 44 | boolean retVal = false; |
| 134 | 44 | if (aPkg == null) { |
| 135 | |
|
| 136 | 0 | retVal = (aImport.indexOf('.') == -1); |
| 137 | |
} |
| 138 | |
else { |
| 139 | 44 | final int index = aImport.lastIndexOf('.'); |
| 140 | 44 | if (index != -1) { |
| 141 | 44 | final String front = aImport.substring(0, index); |
| 142 | 44 | retVal = front.equals(aPkg); |
| 143 | |
} |
| 144 | |
} |
| 145 | 44 | return retVal; |
| 146 | |
} |
| 147 | |
} |