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/IllegalImportCheck.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.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.FullIdent;
24  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
25  
26  /**
27   * <p>
28   * Checks for imports from a set of illegal packages.
29   * By default, the check rejects all <code>sun.*</code> packages
30   * since programs that contain direct calls to the <code>sun.*</code> packages
31   * are <a href="http://java.sun.com/products/jdk/faq/faq-sun-packages.html">
32   * not 100% Pure Java</a>.
33   * </p>
34   * To reject other packages, set property illegalPkgs to a comma-separated
35   * list of the illegal packages.  
36   * </p>
37   * <p>
38   * An example of how to configure the check is:
39   * </p>
40   * <pre>
41   * &lt;module name="IllegalImport"/&gt;
42   * </pre>
43   * <p>
44   * An example of how to configure the check so that it rejects packages
45   * <code>java.io.*</code> and <code>java.sql.*</code> is
46   * </p>
47   * <pre>
48   * &lt;module name="IllegalImport"&gt;
49   *    &lt;property name="illegalPkgs" value="java.io, java.sql"/&gt;
50   * &lt;/module&gt;
51   * </pre>
52   * @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
53   * @author Lars Kühne
54   * @version 1.0
55   */
56  public class IllegalImportCheck
57      extends AbstractImportCheck
58  {
59      /** list of illegal packages */
60      private String[] mIllegalPkgs;
61  
62      /**
63       * Creates a new <code>IllegalImportCheck</code> instance.
64       */
65      public IllegalImportCheck()
66      {
67          setIllegalPkgs(new String[] {"sun"});
68      }
69  
70      /**
71       * Set the list of illegal packages.
72       * @param aFrom array of illegal packages
73       */
74      public void setIllegalPkgs(String[] aFrom)
75      {
76          mIllegalPkgs = aFrom;
77      }
78  
79      /** @see com.puppycrawl.tools.checkstyle.api.Check */
80      public int[] getDefaultTokens()
81      {
82          return new int[] {TokenTypes.IMPORT};
83      }
84  
85      /** @see com.puppycrawl.tools.checkstyle.api.Check */
86      public void visitToken(DetailAST aAST)
87      {
88          final FullIdent imp = getImportText(aAST);
89          if (isIllegalImport(imp.getText())) {
90              log(aAST.getLineNo(),
91                  aAST.getColumnNo(),
92                  "import.illegal",
93                  imp.getText());
94          }
95      }
96  
97      /**
98       * Checks if an import is from a package that must not be used.
99       * @param aImportText the argument of the import keyword
100      * @return if <code>aImportText</code> contains an illegal package prefix
101      */
102     private boolean isIllegalImport(String aImportText)
103     {
104         for (int i = 0; i < mIllegalPkgs.length; i++) {
105             if (aImportText.startsWith(mIllegalPkgs[i] + ".")) {
106                 return true;
107             }
108         }
109         return false;
110     }
111 }