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 extends AbstractCheck { 038 039 /** Open parenthesis literal. */ 040 private static final char OPEN_PARENTHESIS = '('; 041 042 /** Close parenthesis literal. */ 043 private static final char CLOSE_PARENTHESIS = ')'; 044 045 /** Message key for followed whitespace. */ 046 private final String msgWsFollowed; 047 048 /** Message key for not followed whitespace. */ 049 private final String msgWsNotFollowed; 050 051 /** Message key for preceded whitespace. */ 052 private final String msgWsPreceded; 053 054 /** Message key for not preceded whitespace. */ 055 private final String msgWsNotPreceded; 056 057 /** The policy to enforce. */ 058 private PadOption option = PadOption.NOSPACE; 059 060 /** 061 * Creates a new {@code AbstractParenPadCheck} instance. 062 * 063 * @param msgWsFollowed message key for followed whitespace 064 * @param msgWsNotFollowed message key for not followed whitespace 065 * @param msgWsPreceded message key for preceded whitespace 066 * @param msgWsNotPreceded message key for not preceded whitespace 067 */ 068 protected AbstractParenPadCheck(String msgWsFollowed, String msgWsNotFollowed, 069 String msgWsPreceded, String msgWsNotPreceded) { 070 this.msgWsFollowed = msgWsFollowed; 071 this.msgWsNotFollowed = msgWsNotFollowed; 072 this.msgWsPreceded = msgWsPreceded; 073 this.msgWsNotPreceded = msgWsNotPreceded; 074 } 075 076 /** 077 * Specify policy on how to pad parentheses. 078 * 079 * @param optionStr string to decode option from 080 * @throws IllegalArgumentException if unable to decode 081 */ 082 public void setOption(String optionStr) { 083 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 084 } 085 086 /** 087 * Process a token representing a left parentheses. 088 * 089 * @param ast the token representing a left parentheses 090 */ 091 protected void processLeft(DetailAST ast) { 092 final int[] line = getLineCodePoints(ast.getLineNo() - 1); 093 final int after = ast.getColumnNo() + 1; 094 095 if (after < line.length) { 096 final boolean hasWhitespaceAfter = 097 CommonUtil.isCodePointWhitespace(line, after); 098 if (option == PadOption.NOSPACE && hasWhitespaceAfter) { 099 log(ast, msgWsFollowed, OPEN_PARENTHESIS); 100 } 101 else if (option == PadOption.SPACE && !hasWhitespaceAfter 102 && line[after] != CLOSE_PARENTHESIS) { 103 log(ast, msgWsNotFollowed, OPEN_PARENTHESIS); 104 } 105 } 106 } 107 108 /** 109 * Process a token representing a right parentheses. 110 * 111 * @param ast the token representing a right parentheses 112 */ 113 protected void processRight(DetailAST ast) { 114 final int before = ast.getColumnNo() - 1; 115 if (before >= 0) { 116 final int[] line = getLineCodePoints(ast.getLineNo() - 1); 117 final boolean hasPrecedingWhitespace = 118 CommonUtil.isCodePointWhitespace(line, before); 119 120 if (option == PadOption.NOSPACE && hasPrecedingWhitespace 121 && !CodePointUtil.hasWhitespaceBefore(before, line)) { 122 log(ast, msgWsPreceded, CLOSE_PARENTHESIS); 123 } 124 else if (option == PadOption.SPACE && !hasPrecedingWhitespace 125 && line[before] != OPEN_PARENTHESIS) { 126 log(ast, msgWsNotPreceded, CLOSE_PARENTHESIS); 127 } 128 } 129 } 130 131}