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 /** Open parenthesis literal. */
41 private static final char OPEN_PARENTHESIS = '(';
42
43 /** Close parenthesis literal. */
44 private static final char CLOSE_PARENTHESIS = ')';
45
46 /** Message key for followed whitespace. */
47 private final String msgWsFollowed;
48
49 /** Message key for not followed whitespace. */
50 private final String msgWsNotFollowed;
51
52 /** Message key for preceded whitespace. */
53 private final String msgWsPreceded;
54
55 /** Message key for not preceded whitespace. */
56 private final String msgWsNotPreceded;
57
58 /** The policy to enforce. */
59 private PadOption option = PadOption.NOSPACE;
60
61 /**
62 * Creates a new {@code AbstractParenPadCheck} instance.
63 *
64 * @param msgWsFollowed message key for followed whitespace
65 * @param msgWsNotFollowed message key for not followed whitespace
66 * @param msgWsPreceded message key for preceded whitespace
67 * @param msgWsNotPreceded message key for not preceded whitespace
68 */
69 protected AbstractParenPadCheck(String msgWsFollowed, String msgWsNotFollowed,
70 String msgWsPreceded, String msgWsNotPreceded) {
71 this.msgWsFollowed = msgWsFollowed;
72 this.msgWsNotFollowed = msgWsNotFollowed;
73 this.msgWsPreceded = msgWsPreceded;
74 this.msgWsNotPreceded = msgWsNotPreceded;
75 }
76
77 /**
78 * Specify policy on how to pad parentheses.
79 *
80 * @param optionStr string to decode option from
81 * @throws IllegalArgumentException if unable to decode
82 */
83 public void setOption(String optionStr) {
84 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
85 }
86
87 /**
88 * Process a token representing a left parentheses.
89 *
90 * @param ast the token representing a left parentheses
91 */
92 protected void processLeft(DetailAST ast) {
93 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
94 final int after = ast.getColumnNo() + 1;
95
96 if (after < line.length) {
97 final boolean hasWhitespaceAfter =
98 CommonUtil.isCodePointWhitespace(line, after);
99 if (option == PadOption.NOSPACE && hasWhitespaceAfter) {
100 log(ast, msgWsFollowed, OPEN_PARENTHESIS);
101 }
102 else if (option == PadOption.SPACE && !hasWhitespaceAfter
103 && line[after] != CLOSE_PARENTHESIS) {
104 log(ast, msgWsNotFollowed, OPEN_PARENTHESIS);
105 }
106 }
107 }
108
109 /**
110 * Process a token representing a right parentheses.
111 *
112 * @param ast the token representing a right parentheses
113 */
114 protected void processRight(DetailAST ast) {
115 final int before = ast.getColumnNo() - 1;
116 if (before >= 0) {
117 final int[] line = getLineCodePoints(ast.getLineNo() - 1);
118 final boolean hasPrecedingWhitespace =
119 CommonUtil.isCodePointWhitespace(line, before);
120
121 if (option == PadOption.NOSPACE && hasPrecedingWhitespace
122 && !CodePointUtil.hasWhitespaceBefore(before, line)) {
123 log(ast, msgWsPreceded, CLOSE_PARENTHESIS);
124 }
125 else if (option == PadOption.SPACE && !hasPrecedingWhitespace
126 && line[before] != OPEN_PARENTHESIS) {
127 log(ast, msgWsNotPreceded, CLOSE_PARENTHESIS);
128 }
129 }
130 }
131
132 }