View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
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 java.util.ArrayList;
23  import java.util.List;
24  
25  import com.puppycrawl.tools.checkstyle.StatelessCheck;
26  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27  import com.puppycrawl.tools.checkstyle.api.DetailAST;
28  import com.puppycrawl.tools.checkstyle.api.FullIdent;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  /**
32   * <p>
33   * Check that finds import statements that use the * notation.
34   * </p>
35   * <p>
36   * Rationale: Importing all classes from a package or static
37   * members from a class leads to tight coupling between packages
38   * or classes and might lead to problems when a new version of a
39   * library introduces name clashes.
40   * </p>
41   * <p>
42   * An example of how to configure the check is:
43   * </p>
44   * <pre>
45   * &lt;module name="AvoidStarImport"&gt;
46   *   &lt;property name="excludes" value="java.io,java.net,java.lang.Math"/&gt;
47   *   &lt;property name="allowClassImports" value="false"/&gt;
48   *   &lt;property name="allowStaticMemberImports" value="false"/&gt;
49   * &lt;/module&gt;
50   * </pre>
51   * The optional "excludes" property allows for certain packages like
52   * java.io or java.net to be exempted from the rule. It also is used to
53   * allow certain classes like java.lang.Math or java.io.File to be
54   * excluded in order to support static member imports.
55   *
56   * <p>The optional "allowClassImports" when set to true, will allow starred
57   * class imports but will not affect static member imports.
58   *
59   * <p>The optional "allowStaticMemberImports" when set to true will allow
60   * starred static member imports but will not affect class imports.
61   *
62   * @author Oliver Burn
63   * @author <a href="bschneider@vecna.com">Bill Schneider</a>
64   * @author Travis Schneeberger
65   */
66  @StatelessCheck
67  public class AvoidStarImportCheck
68      extends AbstractCheck {
69  
70      /**
71       * A key is pointing to the warning message text in "messages.properties"
72       * file.
73       */
74      public static final String MSG_KEY = "import.avoidStar";
75  
76      /** Suffix for the star import. */
77      private static final String STAR_IMPORT_SUFFIX = ".*";
78  
79      /** The packages/classes to exempt from this check. */
80      private final List<String> excludes = new ArrayList<>();
81  
82      /** Whether to allow all class imports. */
83      private boolean allowClassImports;
84  
85      /** Whether to allow all static member imports. */
86      private boolean allowStaticMemberImports;
87  
88      @Override
89      public int[] getDefaultTokens() {
90          return getRequiredTokens();
91      }
92  
93      @Override
94      public int[] getAcceptableTokens() {
95          return getRequiredTokens();
96      }
97  
98      @Override
99      public int[] getRequiredTokens() {
100         // original implementation checks both IMPORT and STATIC_IMPORT tokens to avoid ".*" imports
101         // however user can allow using "import" or "import static"
102         // by configuring allowClassImports and allowStaticMemberImports
103         // To avoid potential confusion when user specifies conflicting options on configuration
104         // (see example below) we are adding both tokens to Required list
105         //   <module name="AvoidStarImport">
106         //      <property name="tokens" value="IMPORT"/>
107         //      <property name="allowStaticMemberImports" value="false"/>
108         //   </module>
109         return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
110     }
111 
112     /**
113      * Sets the list of packages or classes to be exempt from the check.
114      * The excludes can contain a .* or not.
115      * @param excludesParam a list of package names/fully-qualifies class names
116      *     where star imports are ok.
117      */
118     public void setExcludes(String... excludesParam) {
119         for (final String exclude : excludesParam) {
120             if (exclude.endsWith(STAR_IMPORT_SUFFIX)) {
121                 excludes.add(exclude);
122             }
123             else {
124                 excludes.add(exclude + STAR_IMPORT_SUFFIX);
125             }
126         }
127     }
128 
129     /**
130      * Sets whether or not to allow all non-static class imports.
131      * @param allow true to allow false to disallow
132      */
133     public void setAllowClassImports(boolean allow) {
134         allowClassImports = allow;
135     }
136 
137     /**
138      * Sets whether or not to allow all static member imports.
139      * @param allow true to allow false to disallow
140      */
141     public void setAllowStaticMemberImports(boolean allow) {
142         allowStaticMemberImports = allow;
143     }
144 
145     @Override
146     public void visitToken(final DetailAST ast) {
147         if (!allowClassImports && ast.getType() == TokenTypes.IMPORT) {
148             final DetailAST startingDot = ast.getFirstChild();
149             logsStarredImportViolation(startingDot);
150         }
151         else if (!allowStaticMemberImports
152             && ast.getType() == TokenTypes.STATIC_IMPORT) {
153             // must navigate past the static keyword
154             final DetailAST startingDot = ast.getFirstChild().getNextSibling();
155             logsStarredImportViolation(startingDot);
156         }
157     }
158 
159     /**
160      * Gets the full import identifier.  If the import is a starred import and
161      * it's not excluded then a violation is logged.
162      * @param startingDot the starting dot for the import statement
163      */
164     private void logsStarredImportViolation(DetailAST startingDot) {
165         final FullIdent name = FullIdent.createFullIdent(startingDot);
166         final String importText = name.getText();
167         if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) {
168             log(startingDot.getLineNo(), MSG_KEY, importText);
169         }
170     }
171 
172 }