View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.naming;
21  
22  import java.util.regex.Pattern;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  import com.puppycrawl.tools.checkstyle.utils.ScopeUtils;
27  
28  /**
29   * <p>
30   * Checks that local, non-final variable names conform to a format specified
31   * by the format property. A catch parameter is considered to be
32   * a local variable. The format is a
33   * {@link Pattern regular expression}
34   * and defaults to
35   * <strong>^[a-z][a-zA-Z0-9]*$</strong>.
36   * </p>
37   * <p>
38   * An example of how to configure the check is:
39   * </p>
40   * <pre>
41   * &lt;module name="LocalVariableName"/&gt;
42   * </pre>
43   * <p>
44   * An example of how to configure the check for names that begin with a lower
45   * case letter, followed by letters, digits, and underscores is:
46   * </p>
47   * <pre>
48   * &lt;module name="LocalVariableName"&gt;
49   *    &lt;property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
50   * &lt;/module&gt;
51   * </pre>
52   * <p>
53   * An example of one character variable name in
54   * initialization expression(like "i") in FOR loop:
55   * </p>
56   * <pre>
57   * for(int i = 1; i &lt; 10; i++) {}
58   * </pre>
59   * <p>
60   * An example of how to configure the check to allow one char variable name in
61   * <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html">
62   * initialization expressions</a> in FOR loop:
63   * </p>
64   * <pre>
65   * &lt;module name="LocalVariableName"&gt;
66   *    &lt;property name="allowOneCharVarInForLoop" value="true"/&gt;
67   * &lt;/module&gt;
68   * </pre>
69   *
70   * @author Rick Giles
71   * @author maxvetrenko
72   */
73  public class LocalVariableNameCheck
74      extends AbstractNameCheck {
75  
76      /** Regexp for one-char loop variables. */
77      private static final Pattern SINGLE_CHAR = Pattern.compile("^[a-z]$");
78  
79      /**
80       * Allow one character name for initialization expression in FOR loop.
81       */
82      private boolean allowOneCharVarInForLoop;
83  
84      /** Creates a new {@code LocalVariableNameCheck} instance. */
85      public LocalVariableNameCheck() {
86          super("^[a-z][a-zA-Z0-9]*$");
87      }
88  
89      /**
90       * Sets whether to allow one character name in FOR loop or not.
91       *
92       * @param allow Flag for allowing or not one character name in FOR loop.
93       */
94      public final void setAllowOneCharVarInForLoop(boolean allow) {
95          allowOneCharVarInForLoop = allow;
96      }
97  
98      @Override
99      public int[] getDefaultTokens() {
100         return getRequiredTokens();
101     }
102 
103     @Override
104     public int[] getAcceptableTokens() {
105         return getRequiredTokens();
106     }
107 
108     @Override
109     public int[] getRequiredTokens() {
110         return new int[] {
111             TokenTypes.VARIABLE_DEF,
112         };
113     }
114 
115     @Override
116     protected final boolean mustCheckName(DetailAST ast) {
117         final boolean result;
118         if (allowOneCharVarInForLoop && isForLoopVariable(ast)) {
119             final String variableName = ast.findFirstToken(TokenTypes.IDENT).getText();
120             result = !SINGLE_CHAR.matcher(variableName).find();
121         }
122         else {
123             final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS);
124             final boolean isFinal = modifiersAST.findFirstToken(TokenTypes.FINAL) != null;
125             result = !isFinal && ScopeUtils.isLocalVariableDef(ast);
126         }
127         return result;
128     }
129 
130     /**
131      * Checks if a variable is the loop's one.
132      * @param variableDef variable definition.
133      * @return true if a variable is the loop's one.
134      */
135     private static boolean isForLoopVariable(DetailAST variableDef) {
136         final int parentType = variableDef.getParent().getType();
137         return parentType == TokenTypes.FOR_INIT
138                 || parentType == TokenTypes.FOR_EACH_CLAUSE;
139     }
140 
141 }