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.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 * Creates a new {@code SeparatorWrapCheck} instance. 060 */ 061 public SeparatorWrapCheck() { 062 // no code by default 063 } 064 065 /** 066 * Setter to specify policy on how to wrap lines. 067 * 068 * @param optionStr string to decode option from 069 * @throws IllegalArgumentException if unable to decode 070 * @since 5.8 071 */ 072 public void setOption(String optionStr) { 073 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH)); 074 } 075 076 @Override 077 public int[] getDefaultTokens() { 078 return new int[] { 079 TokenTypes.DOT, 080 TokenTypes.COMMA, 081 }; 082 } 083 084 @Override 085 public int[] getAcceptableTokens() { 086 return new int[] { 087 TokenTypes.DOT, 088 TokenTypes.COMMA, 089 TokenTypes.SEMI, 090 TokenTypes.ELLIPSIS, 091 TokenTypes.AT, 092 TokenTypes.LPAREN, 093 TokenTypes.RPAREN, 094 TokenTypes.ARRAY_DECLARATOR, 095 TokenTypes.RBRACK, 096 TokenTypes.METHOD_REF, 097 }; 098 } 099 100 @Override 101 public int[] getRequiredTokens() { 102 return CommonUtil.EMPTY_INT_ARRAY; 103 } 104 105 @Override 106 public void visitToken(DetailAST ast) { 107 final String text = ast.getText(); 108 final int colNo = ast.getColumnNo(); 109 final int lineNo = ast.getLineNo(); 110 final int[] currentLine = getLineCodePoints(lineNo - 1); 111 final boolean isLineEmptyAfterToken = CodePointUtil.isBlank( 112 Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length) 113 ); 114 final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank( 115 Arrays.copyOfRange(currentLine, 0, colNo) 116 ); 117 118 if (option == WrapOption.NL 119 && isLineEmptyAfterToken) { 120 log(ast, MSG_LINE_NEW, text); 121 } 122 else if (option == WrapOption.EOL 123 && isLineEmptyBeforeToken) { 124 log(ast, MSG_LINE_PREVIOUS, text); 125 } 126 } 127 128}