KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > ImportControlCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
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 /**
29  * Check that controls what packages can be imported in each package. Useful
30  * for ensuring that application layering is not violated. Ideas on how the
31  * check can be improved include support for:
32  * <ul>
33  * <li>Change the default policy that if a package being checked does not
34  * match any guards, then it is allowed. Currently defaults to disallowed.
35  *
36  * </ul>
37  * @author Oliver Burn
38  */

39 public class ImportControlCheck extends Check
40 {
41     /** The root package controller. */
42     private PkgControl mRoot;
43     /** The package doing the import. */
44     private String JavaDoc mInPkg;
45
46     /**
47      * The package controller for the current file. Used for performance
48      * optimisation.
49      */

50     private PkgControl mCurrentLeaf;
51
52     /** {@inheritDoc} */
53     public int[] getDefaultTokens()
54     {
55         return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT,
56                           TokenTypes.STATIC_IMPORT, };
57     }
58
59     /** {@inheritDoc} */
60     public void beginTree(final DetailAST aRootAST)
61     {
62         mCurrentLeaf = null;
63     }
64
65     /** {@inheritDoc} */
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                 // know it is a static import
89
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     /**
101      * Set the parameter for the file containing the import control
102      * configuration. It will cause the file to be loaded.
103      * @param aName the name of the file to load.
104      * @throws ConversionException on error loading the file.
105      */

106     public void setFile(final String JavaDoc aName)
107     {
108         // Handle empty param
109
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