View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.Arrays;
23  import java.util.Locale;
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.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  /**
33   * <div>
34   * Checks line wrapping with separators.
35   * </div>
36   * <ul>
37   * <li>
38   * Property {@code option} - Specify policy on how to wrap lines.
39   * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.WrapOption}.
40   * Default value is {@code eol}.
41   * </li>
42   * <li>
43   * Property {@code tokens} - tokens to check
44   * Type is {@code java.lang.String[]}.
45   * Validation type is {@code tokenSet}.
46   * Default value is:
47   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DOT">
48   * DOT</a>,
49   * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMMA">
50   * COMMA</a>.
51   * </li>
52   * </ul>
53   *
54   * <p>
55   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
56   * </p>
57   *
58   * <p>
59   * Violation Message Keys:
60   * </p>
61   * <ul>
62   * <li>
63   * {@code line.new}
64   * </li>
65   * <li>
66   * {@code line.previous}
67   * </li>
68   * </ul>
69   *
70   * @since 5.8
71   */
72  @StatelessCheck
73  public class SeparatorWrapCheck
74      extends AbstractCheck {
75  
76      /**
77       * A key is pointing to the warning message text in "messages.properties"
78       * file.
79       */
80      public static final String MSG_LINE_PREVIOUS = "line.previous";
81  
82      /**
83       * A key is pointing to the warning message text in "messages.properties"
84       * file.
85       */
86      public static final String MSG_LINE_NEW = "line.new";
87  
88      /** Specify policy on how to wrap lines. */
89      private WrapOption option = WrapOption.EOL;
90  
91      /**
92       * Setter to specify policy on how to wrap lines.
93       *
94       * @param optionStr string to decode option from
95       * @throws IllegalArgumentException if unable to decode
96       * @since 5.8
97       */
98      public void setOption(String optionStr) {
99          option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
100     }
101 
102     @Override
103     public int[] getDefaultTokens() {
104         return new int[] {
105             TokenTypes.DOT,
106             TokenTypes.COMMA,
107         };
108     }
109 
110     @Override
111     public int[] getAcceptableTokens() {
112         return new int[] {
113             TokenTypes.DOT,
114             TokenTypes.COMMA,
115             TokenTypes.SEMI,
116             TokenTypes.ELLIPSIS,
117             TokenTypes.AT,
118             TokenTypes.LPAREN,
119             TokenTypes.RPAREN,
120             TokenTypes.ARRAY_DECLARATOR,
121             TokenTypes.RBRACK,
122             TokenTypes.METHOD_REF,
123         };
124     }
125 
126     @Override
127     public int[] getRequiredTokens() {
128         return CommonUtil.EMPTY_INT_ARRAY;
129     }
130 
131     @Override
132     public void visitToken(DetailAST ast) {
133         final String text = ast.getText();
134         final int colNo = ast.getColumnNo();
135         final int lineNo = ast.getLineNo();
136         final int[] currentLine = getLineCodePoints(lineNo - 1);
137         final boolean isLineEmptyAfterToken = CodePointUtil.isBlank(
138                 Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length)
139         );
140         final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank(
141                 Arrays.copyOfRange(currentLine, 0, colNo)
142         );
143 
144         if (option == WrapOption.NL
145                  && isLineEmptyAfterToken) {
146             log(ast, MSG_LINE_NEW, text);
147         }
148         else if (option == WrapOption.EOL
149                 && isLineEmptyBeforeToken) {
150             log(ast, MSG_LINE_PREVIOUS, text);
151         }
152     }
153 
154 }