001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.CommonUtil; 028 029/** 030 * <p>Abstract class for checking the padding of parentheses. That is whether a 031 * space is required after a left parenthesis and before a right parenthesis, 032 * or such spaces are forbidden. 033 * </p> 034 */ 035@StatelessCheck 036public abstract class AbstractParenPadCheck 037 extends AbstractCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" 041 * file. 042 */ 043 public static final String MSG_WS_FOLLOWED = "ws.followed"; 044 045 /** 046 * A key is pointing to the warning message text in "messages.properties" 047 * file. 048 */ 049 public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed"; 050 051 /** 052 * A key is pointing to the warning message text in "messages.properties" 053 * file. 054 */ 055 public static final String MSG_WS_PRECEDED = "ws.preceded"; 056 057 /** 058 * A key is pointing to the warning message text in "messages.properties" 059 * file. 060 */ 061 public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded"; 062 063 /** Open parenthesis literal. */ 064 private static final char OPEN_PARENTHESIS = '('; 065 066 /** Close parenthesis literal. */ 067 private static final char CLOSE_PARENTHESIS = ')'; 068 069 /** The policy to enforce. */ 070 private PadOption option = PadOption.NOSPACE; 071 072 /** 073 * Set the option to enforce. 074 * 075 * @param optionStr string to decode option from 076 * @throws IllegalArgumentException if unable to decode 077 */ 078 public void setOption(String optionStr) { 079 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 080 } 081 082 /** 083 * Process a token representing a left parentheses. 084 * 085 * @param ast the token representing a left parentheses 086 */ 087 protected void processLeft(DetailAST ast) { 088 final String line = getLines()[ast.getLineNo() - 1]; 089 final int[] codePoints = line.codePoints().toArray(); 090 final int after = ast.getColumnNo() + 1; 091 092 if (after < codePoints.length) { 093 final boolean hasWhitespaceAfter = 094 CommonUtil.isCodePointWhitespace(codePoints, after); 095 if (option == PadOption.NOSPACE && hasWhitespaceAfter) { 096 log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS); 097 } 098 else if (option == PadOption.SPACE && !hasWhitespaceAfter 099 && line.charAt(after) != CLOSE_PARENTHESIS) { 100 log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS); 101 } 102 } 103 } 104 105 /** 106 * Process a token representing a right parentheses. 107 * 108 * @param ast the token representing a right parentheses 109 */ 110 protected void processRight(DetailAST ast) { 111 final int before = ast.getColumnNo() - 1; 112 if (before >= 0) { 113 final String line = getLines()[ast.getLineNo() - 1]; 114 final int[] codePoints = line.codePoints().toArray(); 115 final boolean hasPrecedingWhitespace = 116 CommonUtil.isCodePointWhitespace(codePoints, before); 117 118 if (option == PadOption.NOSPACE && hasPrecedingWhitespace 119 && !CommonUtil.hasWhitespaceBefore(before, line)) { 120 log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS); 121 } 122 else if (option == PadOption.SPACE && !hasPrecedingWhitespace 123 && line.charAt(before) != OPEN_PARENTHESIS) { 124 log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS); 125 } 126 } 127 } 128 129}