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 com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 027import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 028 029/** 030 * <div>Checks that chosen statements are not line-wrapped. 031 * By default, this Check restricts wrapping import and package statements, 032 * but it's possible to check any statement. 033 * </div> 034 * 035 * @since 5.8 036 */ 037@StatelessCheck 038public class NoLineWrapCheck 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_KEY = "no.line.wrap"; 045 046 @Override 047 public int[] getDefaultTokens() { 048 return new int[] { 049 TokenTypes.PACKAGE_DEF, 050 TokenTypes.IMPORT, 051 TokenTypes.STATIC_IMPORT, 052 TokenTypes.MODULE_IMPORT, 053 }; 054 } 055 056 @Override 057 public int[] getAcceptableTokens() { 058 return new int[] { 059 TokenTypes.IMPORT, 060 TokenTypes.STATIC_IMPORT, 061 TokenTypes.MODULE_IMPORT, 062 TokenTypes.PACKAGE_DEF, 063 TokenTypes.CLASS_DEF, 064 TokenTypes.METHOD_DEF, 065 TokenTypes.CTOR_DEF, 066 TokenTypes.ENUM_DEF, 067 TokenTypes.INTERFACE_DEF, 068 TokenTypes.RECORD_DEF, 069 TokenTypes.COMPACT_CTOR_DEF, 070 }; 071 } 072 073 @Override 074 public int[] getRequiredTokens() { 075 return CommonUtil.EMPTY_INT_ARRAY; 076 } 077 078 @Override 079 public void visitToken(DetailAST ast) { 080 if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) { 081 log(ast, MSG_KEY, ast.getText()); 082 } 083 } 084 085}