001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.CommonUtils; 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 * @author Oliver Burn 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 * Set the option to enforce. 075 * @param optionStr string to decode option from 076 * @throws IllegalArgumentException if unable to decode 077 */ 078 public void setOption(String optionStr) { 079 try { 080 option = PadOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 081 } 082 catch (IllegalArgumentException iae) { 083 throw new IllegalArgumentException("unable to parse " + optionStr, iae); 084 } 085 } 086 087 /** 088 * Process a token representing a left parentheses. 089 * @param ast the token representing a left parentheses 090 */ 091 protected void processLeft(DetailAST ast) { 092 final String line = getLines()[ast.getLineNo() - 1]; 093 final int after = ast.getColumnNo() + 1; 094 if (after < line.length()) { 095 if (option == PadOption.NOSPACE 096 && Character.isWhitespace(line.charAt(after))) { 097 log(ast.getLineNo(), after, MSG_WS_FOLLOWED, OPEN_PARENTHESIS); 098 } 099 else if (option == PadOption.SPACE 100 && !Character.isWhitespace(line.charAt(after)) 101 && line.charAt(after) != CLOSE_PARENTHESIS) { 102 log(ast.getLineNo(), after, MSG_WS_NOT_FOLLOWED, OPEN_PARENTHESIS); 103 } 104 } 105 } 106 107 /** 108 * Process a token representing a right parentheses. 109 * @param ast the token representing a right parentheses 110 */ 111 protected void processRight(DetailAST ast) { 112 final int before = ast.getColumnNo() - 1; 113 if (before >= 0) { 114 final String line = getLines()[ast.getLineNo() - 1]; 115 if (option == PadOption.NOSPACE 116 && Character.isWhitespace(line.charAt(before)) 117 && !CommonUtils.hasWhitespaceBefore(before, line)) { 118 log(ast.getLineNo(), before, MSG_WS_PRECEDED, CLOSE_PARENTHESIS); 119 } 120 else if (option == PadOption.SPACE 121 && !Character.isWhitespace(line.charAt(before)) 122 && line.charAt(before) != OPEN_PARENTHESIS) { 123 log(ast.getLineNo(), ast.getColumnNo(), 124 MSG_WS_NOT_PRECEDED, CLOSE_PARENTHESIS); 125 } 126 } 127 } 128 129}