View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.whitespace;
21  
22  import java.util.Locale;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28  import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  /**
32   * <div>
33   * Checks the padding of an empty for initializer; that is whether a white
34   * space is required at an empty for initializer, or such white space is
35   * forbidden. No check occurs if there is a line wrap at the initializer, as in
36   * </div>
37   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
38   * for (
39   *     ; i &lt; j; i++, j--)
40   *  </code></pre></div>
41   *
42   * @since 3.4
43   */
44  @StatelessCheck
45  public class EmptyForInitializerPadCheck
46      extends AbstractCheck {
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties"
50       * file.
51       */
52      public static final String MSG_PRECEDED = "ws.preceded";
53  
54      /**
55       * A key is pointing to the warning message text in "messages.properties"
56       * file.
57       */
58      public static final String MSG_NOT_PRECEDED = "ws.notPreceded";
59  
60      /** Semicolon literal. */
61      private static final String SEMICOLON = ";";
62  
63      /** Specify policy on how to pad an empty for iterator. */
64      private PadOption option = PadOption.NOSPACE;
65  
66      /**
67       * Creates a new {@code EmptyForInitializerPadCheck} instance.
68       */
69      public EmptyForInitializerPadCheck() {
70          // no code by default
71      }
72  
73      /**
74       * Setter to specify policy on how to pad an empty for iterator.
75       *
76       * @param optionStr string to decode option from
77       * @throws IllegalArgumentException if unable to decode
78       * @since 3.4
79       */
80      public void setOption(String optionStr) {
81          option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
82      }
83  
84      @Override
85      public int[] getDefaultTokens() {
86          return getRequiredTokens();
87      }
88  
89      @Override
90      public int[] getAcceptableTokens() {
91          return getRequiredTokens();
92      }
93  
94      @Override
95      public int[] getRequiredTokens() {
96          return new int[] {TokenTypes.FOR_INIT};
97      }
98  
99      @Override
100     public void visitToken(DetailAST ast) {
101         if (!ast.hasChildren()) {
102             final int lineIdx = ast.getLineNo() - 1;
103             final int[] line = getLineCodePoints(lineIdx);
104             final int before = ast.getColumnNo() - 1;
105             // don't check if semi at beginning of line
106             if (ast.getColumnNo() > 0 && !CodePointUtil.hasWhitespaceBefore(before, line)) {
107                 if (option == PadOption.NOSPACE
108                     && CommonUtil.isCodePointWhitespace(line, before)) {
109                     log(ast, MSG_PRECEDED, SEMICOLON);
110                 }
111                 else if (option == PadOption.SPACE
112                          && !CommonUtil.isCodePointWhitespace(line, before)) {
113                     log(ast, MSG_NOT_PRECEDED, SEMICOLON);
114                 }
115             }
116         }
117     }
118 
119 }