1 package com.puppycrawl.tools.checkstyle.checks.imports; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 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 import org.apache.commons.beanutils.ConversionException; 27 28 39 public class ImportControlCheck extends Check 40 { 41 42 private PkgControl mRoot; 43 44 private String mInPkg; 45 46 50 private PkgControl mCurrentLeaf; 51 52 53 public int[] getDefaultTokens() 54 { 55 return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, 56 TokenTypes.STATIC_IMPORT, }; 57 } 58 59 60 public void beginTree(final DetailAST aRootAST) 61 { 62 mCurrentLeaf = null; 63 } 64 65 66 public void visitToken(final DetailAST aAST) 67 { 68 if (aAST.getType() == TokenTypes.PACKAGE_DEF) { 69 final DetailAST nameAST = aAST.getLastChild().getPreviousSibling(); 70 final FullIdent full = FullIdent.createFullIdent(nameAST); 71 if (mRoot == null) { 72 log(nameAST, "import.control.missing.file"); 73 } 74 else { 75 mInPkg = full.getText(); 76 mCurrentLeaf = mRoot.locateFinest(mInPkg); 77 if (mCurrentLeaf == null) { 78 log(nameAST, "import.control.unknown.pkg"); 79 } 80 } 81 } 82 else if (mCurrentLeaf != null) { 83 final FullIdent imp; 84 if (aAST.getType() == TokenTypes.IMPORT) { 85 imp = FullIdent.createFullIdentBelow(aAST); 86 } 87 else { 88 imp = FullIdent.createFullIdent((DetailAST) aAST 90 .getFirstChild().getNextSibling()); 91 } 92 final AccessResult access = mCurrentLeaf.checkAccess(imp.getText(), 93 mInPkg); 94 if (!AccessResult.ALLOWED.equals(access)) { 95 log(aAST, "import.control.disallowed", imp.getText()); 96 } 97 } 98 } 99 100 106 public void setFile(final String aName) 107 { 108 if ((aName == null) || (aName.trim().length() == 0)) { 110 return; 111 } 112 113 try { 114 mRoot = ImportControlLoader.load(aName); 115 } 116 catch (final CheckstyleException ex) { 117 throw new ConversionException("Unable to load " + aName, ex); 118 } 119 } 120 } 121 | Popular Tags |