001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.whitespace; 021 022import java.util.Locale; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.utils.CodePointUtil; 028import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 029 030/** 031 * <div>Abstract class for checking the padding of parentheses. That is whether a 032 * space is required after a left parenthesis and before a right parenthesis, 033 * or such spaces are forbidden. 034 * </div> 035 */ 036@StatelessCheck 037public abstract class AbstractParenPadCheck 038 extends AbstractCheck { 039 040 /** 041 * A key is pointing to the warning message text in "messages.properties" 042 * file. 043 */ 044 public static final String MSG_WS_FOLLOWED = "ws.followed"; 045 046 /** 047 * A key is pointing to the warning message text in "messages.properties" 048 * file. 049 */ 050 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 051 052 /** 053 * A key is pointing to the warning message text in "messages.properties" 054 * file. 055 */ 056 public static final String MSG_WS_PRECEDED = "ws.preceded"; 057 058 /** 059 * A key is pointing to the warning message text in "messages.properties" 060 * file. 061 */ 062 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 063 064 /** Open parenthesis literal. */ 065 private static final char OPEN_PARENTHESIS = '('; 066 067 /** Close parenthesis literal. */ 068 private static final char CLOSE_PARENTHESIS = ')'; 069 070 /** The policy to enforce. */ 071 private PadOption option = PadOption.NOSPACE; 072 073 /** 074 * Creates a new {@code AbstractParenPadCheck} instance. 075 */ 076 protected AbstractParenPadCheck() { 077 // no code by default 078 } 079 080 /** 081 * Specify policy on how to pad parentheses. 082 * 083 * @param optionStr string to decode option from 084 * @throws IllegalArgumentException if unable to decode 085 */ 086 public void setOption(String optionStr) { 087 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 088 } 089 090 /** 091 * Process a token representing a left parentheses. 092 * 093 * @param ast the token representing a left parentheses 094 */ 095 protected void processLeft(DetailAST ast) { 096 final int[] line = getLineCodePoints(ast.getLineNo() - 1); 097 final int after = ast.getColumnNo() + 1; 098 099 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}