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/AvoidStarImportCheck.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2002  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;
21  
22  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23  import com.puppycrawl.tools.checkstyle.api.DetailAST;
24  import com.puppycrawl.tools.checkstyle.api.FullIdent;
25  
26  /**
27   * <p>
28   * Check that finds import statements that use the * notation.
29   * </p>
30   * <p> Rationale: Importing all classes from a package leads to tight coupling
31   * between packages and might lead to problems when a new version of a library
32   * introduces name clashes.
33   * </p>
34   * <p>
35   * An example of how to configure the check is:
36   * </p>
37   * <pre>
38   * &lt;module name="AvoidStarImport"/&gt;
39   * </pre>
40   * @author Oliver Burn
41   * @version 1.0
42   */
43  public class AvoidStarImportCheck
44      extends AbstractImportCheck
45  {
46      /** @see com.puppycrawl.tools.checkstyle.api.Check */
47      public int[] getDefaultTokens()
48      {
49          return new int[] {TokenTypes.IMPORT};
50      }
51  
52      /** @see com.puppycrawl.tools.checkstyle.api.Check */
53      public void visitToken(DetailAST aAST)
54      {
55          final FullIdent name = getImportText(aAST);
56          if ((name != null) && name.getText().endsWith(".*")) {
57              log(aAST.getLineNo(), "import.avoidStar", name.getText());
58          }
59      }
60  }