Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/imports/AvoidStarImportCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2003  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  
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  /**
28   * <p>
29   * Check that finds import statements that use the * notation.
30   * </p>
31   * <p> Rationale: Importing all classes from a package leads to tight coupling
32   * between packages and might lead to problems when a new version of a library
33   * introduces name clashes.
34   * </p>
35   * <p>
36   * An example of how to configure the check is:
37   * </p>
38   * <pre>
39   * &lt;module name="AvoidStarImport"&gt;
40   *   &lt;property name="excludes" value="java.io,java.net"/&gt;
41   * &lt;/module&gt;
42   * </pre>
43   *
44   * The optional "excludes" property allows for certain packages like
45   * java.io or java.net to be exempted from the rule. Note that the excludes
46   * property is not recursive, subpackages of excluded packages are not
47   * automatically excluded.
48   *
49   * @author Oliver Burn
50   * @author <a href="bschneider@vecna.com">Bill Schneider</a>
51   * @version 1.0
52   */
53  public class AvoidStarImportCheck
54      extends Check
55  {
56      /** the packages to exempt from this check */
57      private String[] mExcludes = new String[0];
58  
59      /** @see com.puppycrawl.tools.checkstyle.api.Check */
60      public int[] getDefaultTokens()
61      {
62          return new int[] {TokenTypes.IMPORT};
63      }
64  
65      /**
66       * Sets the list of packages to exempt from the check.
67       * @param aExcludes a list of package names where star imports are ok
68       */
69      public void setExcludes(String[] aExcludes)
70      {
71          mExcludes = new String[aExcludes.length];
72          for (int i = 0; i < aExcludes.length; i++) {
73              mExcludes[i] = aExcludes[i];
74              if (!mExcludes[i].endsWith(".*")) {
75                  // force all package names to end with ".*" to disambiguate
76                  // "java.io"
77                  mExcludes[i] = mExcludes[i] + ".*";
78              }
79          }
80      }
81  
82      /** @see com.puppycrawl.tools.checkstyle.api.Check */
83      public void visitToken(DetailAST aAST)
84      {
85          final FullIdent name = FullIdent.createFullIdentBelow(aAST);
86          if ((name != null) && name.getText().endsWith(".*")) {
87              boolean exempt = false;
88              for (int i = 0; i < mExcludes.length && !exempt; i++) {
89                  if (name.getText().equals(mExcludes[i])) {
90                      exempt = true;
91                  }
92              }
93              if (!exempt) {
94                  log(aAST.getLineNo(), "import.avoidStar", name.getText());
95              }
96          }
97      }
98  }