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 IllegalImportCheck 62 extends Check 63 { 64 65 private String [] mIllegalPkgs; 66 67 70 public IllegalImportCheck() 71 { 72 setIllegalPkgs(new String [] {"sun"}); 73 } 74 75 79 public void setIllegalPkgs(String [] aFrom) 80 { 81 mIllegalPkgs = aFrom; 82 } 83 84 85 public int[] getDefaultTokens() 86 { 87 return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT}; 88 } 89 90 91 public void visitToken(DetailAST aAST) 92 { 93 final FullIdent imp; 94 if (aAST.getType() == TokenTypes.IMPORT) { 95 imp = FullIdent.createFullIdentBelow(aAST); 96 } 97 else { 98 imp = FullIdent.createFullIdent( 99 (DetailAST) aAST.getFirstChild().getNextSibling()); 100 } 101 if (isIllegalImport(imp.getText())) { 102 log(aAST.getLineNo(), 103 aAST.getColumnNo(), 104 "import.illegal", 105 imp.getText()); 106 } 107 } 108 109 114 private boolean isIllegalImport(String aImportText) 115 { 116 for (int i = 0; i < mIllegalPkgs.length; i++) { 117 if (aImportText.startsWith(mIllegalPkgs[i] + ".")) { 118 return true; 119 } 120 } 121 return false; 122 } 123 } 124 | Popular Tags |