001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 after = ast.getColumnNo() + 1; 090 if (after < line.length()) { 091 if (option == PadOption.NOSPACE 092 && Character.isWhitespace(line.charAt(after))) { 093 log(ast, MSG_WS_FOLLOWED, OPEN_PARENTHESIS); 094 } 095 else if (option == PadOption.SPACE 096 && !Character.isWhitespace(line.charAt(after)) 097 && line.charAt(after) != CLOSE_PARENTHESIS) { 098 log(ast, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS); 099 } 100 } 101 } 102 103 /** 104 * Process a token representing a right parentheses. 105 * 106 * @param ast the token representing a right parentheses 107 */ 108 protected void processRight(DetailAST ast) { 109 final int before = ast.getColumnNo() - 1; 110 if (before >= 0) { 111 final String line = getLines()[ast.getLineNo() - 1]; 112 if (option == PadOption.NOSPACE 113 && Character.isWhitespace(line.charAt(before)) 114 && !CommonUtil.hasWhitespaceBefore(before, line)) { 115 log(ast, MSG_WS_PRECEDED, CLOSE_PARENTHESIS); 116 } 117 else if (option == PadOption.SPACE 118 && !Character.isWhitespace(line.charAt(before)) 119 && line.charAt(before) != OPEN_PARENTHESIS) { 120 log(ast, MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS); 121 } 122 } 123 } 124 125}