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.utils.CodePointUtil;
28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29
30 /**
31 * <div>Abstract class for checking the padding of parentheses. That is whether a
32 * space is required after a left parenthesis and before a right parenthesis,
33 * or such spaces are forbidden.
34 * </div>
35 */
36 @StatelessCheck
37 public abstract class AbstractParenPadCheck
38 extends AbstractCheck {
39
40 /**
41 * A key is pointing to the warning message text in "messages.properties"
42 * file.
43 */
44 public static final String MSG_WS_FOLLOWED = "ws.followed";
45
46 /**
47 * A key is pointing to the warning message text in "messages.properties"
48 * file.
49 */
50 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
51
52 /**
53 * A key is pointing to the warning message text in "messages.properties"
54 * file.
55 */
56 public static final String MSG_WS_PRECEDED = "ws.preceded";
57
58 /**
59 * A key is pointing to the warning message text in "messages.properties"
60 * file.
61 */
62 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
63
64 /** Open parenthesis literal. */
65 private static final char OPEN_PARENTHESIS = '(';
66
67 /** Close parenthesis literal. */
68 private static final char CLOSE_PARENTHESIS = ')';
69
70 /** The policy to enforce. */
71 private PadOption option = PadOption.NOSPACE;
72
73 /**
74 * Creates a new {@code AbstractParenPadCheck} instance.
75 */
76 protected AbstractParenPadCheck() {
77 // no code by default
78 }
79
80 /**
81 * Specify policy on how to pad parentheses.
82 *
83 * @param optionStr string to decode option from
84 * @throws IllegalArgumentException if unable to decode
85 */
86 public void setOption(String optionStr) {
87 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
88 }
89
90 /**
91 * Process a token representing a left parentheses.
92 *
93 * @param ast the token representing a left parentheses
94 */
95 protected void processLeft(DetailAST ast) {
96 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
97 final int after = ast.getColumnNo() + 1;
98
99 if (after < line.length) {
100 final boolean hasWhitespaceAfter =
101 CommonUtil.isCodePointWhitespace(line, after);
102 if (option == PadOption.NOSPACE && hasWhitespaceAfter) {
103 log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS);
104 }
105 else if (option == PadOption.SPACE && !hasWhitespaceAfter
106 && line[after] != CLOSE_PARENTHESIS) {
107 log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS);
108 }
109 }
110 }
111
112 /**
113 * Process a token representing a right parentheses.
114 *
115 * @param ast the token representing a right parentheses
116 */
117 protected void processRight(DetailAST ast) {
118 final int before = ast.getColumnNo() - 1;
119 if (before >= 0) {
120 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
121 final boolean hasPrecedingWhitespace =
122 CommonUtil.isCodePointWhitespace(line, before);
123
124 if (option == PadOption.NOSPACE && hasPrecedingWhitespace
125 && !CodePointUtil.hasWhitespaceBefore(before, line)) {
126 log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS);
127 }
128 else if (option == PadOption.SPACE && !hasPrecedingWhitespace
129 && line[before] != OPEN_PARENTHESIS) {
130 log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS);
131 }
132 }
133 }
134
135 }