1 20 package com.puppycrawl.tools.checkstyle.checks.imports; 21 22 import com.puppycrawl.tools.checkstyle.api.Check; 23 import com.puppycrawl.tools.checkstyle.api.DetailAST; 24 import com.puppycrawl.tools.checkstyle.api.FullIdent; 25 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 26 27 61 public class ImportOrderCheck extends Check 62 { 63 64 private String [] mGroups = new String [0]; 65 66 67 private boolean mOrdered = true; 68 69 70 private boolean mSeparated; 71 72 private boolean mCaseSensitive = true; 73 74 75 private int mLastGroup; 76 77 private int mLastImportLine; 78 79 private String mLastImport; 80 81 private boolean mLastImportStatic; 82 83 private boolean mBeforeFirstImport; 84 85 88 public ImportOrderCheck() 89 { 90 } 91 92 98 public void setGroups(String [] aGroups) 99 { 100 mGroups = new String [ aGroups.length ]; 101 102 for (int i = 0; i < aGroups.length; i++) { 103 String pkg = aGroups[i]; 104 105 if (!pkg.endsWith(".")) { 106 pkg = pkg + "."; 107 } 108 109 mGroups[i] = pkg; 110 } 111 } 112 113 120 public void setOrdered(boolean aOrdered) 121 { 122 mOrdered = aOrdered; 123 } 124 125 131 public void setSeparated(boolean aSeparated) 132 { 133 mSeparated = aSeparated; 134 } 135 136 142 public void setCaseSensitive(boolean aCaseSensitive) 143 { 144 mCaseSensitive = aCaseSensitive; 145 } 146 147 148 public int[] getDefaultTokens() 149 { 150 return new int[]{TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT}; 151 } 152 153 154 public int[] getRequiredTokens() 155 { 156 return getDefaultTokens(); 157 } 158 159 163 private int getGroupNumber(String aName) 164 { 165 int i = 0; 166 167 for (; i < mGroups.length; i++) { 170 if (aName.startsWith(mGroups[i])) { 171 break; 172 } 173 } 174 175 return i; 176 } 177 178 179 public void beginTree(DetailAST aRootAST) 180 { 181 mLastGroup = Integer.MIN_VALUE; 182 mLastImportLine = Integer.MIN_VALUE; 183 mLastImport = ""; 184 mLastImportStatic = false; 185 mBeforeFirstImport = true; 186 } 187 188 189 public void visitToken(DetailAST aAST) 190 { 191 final FullIdent ident; 192 boolean isStatic; 193 if (aAST.getType() == TokenTypes.IMPORT) { 194 ident = FullIdent.createFullIdentBelow(aAST); 195 isStatic = false; 196 } 197 else { 198 ident = FullIdent.createFullIdent( 199 (DetailAST) aAST.getFirstChild().getNextSibling()); 200 isStatic = true; 201 } 202 203 if (ident != null) { 204 final String name = ident.getText(); 205 final int groupIdx = getGroupNumber(name); 206 final int line = ident.getLineNo(); 207 208 if (groupIdx > mLastGroup) { 209 if (!mBeforeFirstImport && mSeparated) { 210 if (line - mLastImportLine < 2) { 213 log(line, "import.separation", name); 214 } 215 } 216 } 217 else if (groupIdx == mLastGroup) { 218 if (mOrdered) { 219 boolean shouldFireError = false; 220 if (mCaseSensitive) { 221 shouldFireError = 222 (!(mLastImportStatic ^ isStatic) 225 && 226 (mLastImport.compareTo(name) >= 0)) 228 || 229 (mLastImportStatic && !isStatic); 231 } 232 else { 233 shouldFireError = 234 (!(mLastImportStatic ^ isStatic) 237 && 238 (mLastImport.compareToIgnoreCase(name) >= 0)) 240 || 241 (mLastImportStatic && !isStatic); 243 } 244 if (shouldFireError) { 245 log(line, "import.ordering", name); 246 } 247 } 248 } 249 else { 250 log(line, "import.ordering", name); 251 } 252 253 mLastGroup = groupIdx; 254 mLastImport = name; 255 mLastImportLine = aAST.findFirstToken(TokenTypes.SEMI).getLineNo(); 256 mLastImportStatic = isStatic; 257 mBeforeFirstImport = false; 258 } 259 } 260 } 261 | Popular Tags |