001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.Arrays; 023import java.util.Locale; 024 025import com.puppycrawl.tools.checkstyle.StatelessCheck; 026import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 027import com.puppycrawl.tools.checkstyle.api.DetailAST; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.utils.CodePointUtil; 030import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 031 032/** 033 * <div> 034 * Checks line wrapping with separators. 035 * </div> 036 * 037 * @since 5.8 038 */ 039@StatelessCheck 040public class SeparatorWrapCheck 041 extends AbstractCheck { 042 043 /** 044 * A key is pointing to the warning message text in "messages.properties" 045 * file. 046 */ 047 public static final String MSG_LINE_PREVIOUS = "line.previous"; 048 049 /** 050 * A key is pointing to the warning message text in "messages.properties" 051 * file. 052 */ 053 public static final String MSG_LINE_NEW = "line.new"; 054 055 /** Specify policy on how to wrap lines. */ 056 private WrapOption option = WrapOption.EOL; 057 058 /** 059 * Setter to specify policy on how to wrap lines. 060 * 061 * @param optionStr string to decode option from 062 * @throws IllegalArgumentException if unable to decode 063 * @since 5.8 064 */ 065 public void setOption(String optionStr) { 066 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 067 } 068 069 @Override 070 public int[] getDefaultTokens() { 071 return new int[] { 072 TokenTypes.DOT, 073 TokenTypes.COMMA, 074 }; 075 } 076 077 @Override 078 public int[] getAcceptableTokens() { 079 return new int[] { 080 TokenTypes.DOT, 081 TokenTypes.COMMA, 082 TokenTypes.SEMI, 083 TokenTypes.ELLIPSIS, 084 TokenTypes.AT, 085 TokenTypes.LPAREN, 086 TokenTypes.RPAREN, 087 TokenTypes.ARRAY_DECLARATOR, 088 TokenTypes.RBRACK, 089 TokenTypes.METHOD_REF, 090 }; 091 } 092 093 @Override 094 public int[] getRequiredTokens() { 095 return CommonUtil.EMPTY_INT_ARRAY; 096 } 097 098 @Override 099 public void visitToken(DetailAST ast) { 100 final String text = ast.getText(); 101 final int colNo = ast.getColumnNo(); 102 final int lineNo = ast.getLineNo(); 103 final int[] currentLine = getLineCodePoints(lineNo - 1); 104 final boolean isLineEmptyAfterToken = CodePointUtil.isBlank( 105 Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length) 106 ); 107 final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank( 108 Arrays.copyOfRange(currentLine, 0, colNo) 109 ); 110 111 if (option == WrapOption.NL 112 && isLineEmptyAfterToken) { 113 log(ast, MSG_LINE_NEW, text); 114 } 115 else if (option == WrapOption.EOL 116 && isLineEmptyBeforeToken) { 117 log(ast, MSG_LINE_PREVIOUS, text); 118 } 119 } 120 121}