001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 * <ul> 035 * <li> 036 * Property {@code tokens} - tokens to check 037 * Type is {@code java.lang.String[]}. 038 * Validation type is {@code tokenSet}. 039 * Default value is: 040 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PACKAGE_DEF"> 041 * PACKAGE_DEF</a>, 042 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPORT"> 043 * IMPORT</a>, 044 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_IMPORT"> 045 * STATIC_IMPORT</a>. 046 * </li> 047 * </ul> 048 * 049 * <p> 050 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 051 * </p> 052 * 053 * <p> 054 * Violation Message Keys: 055 * </p> 056 * <ul> 057 * <li> 058 * {@code no.line.wrap} 059 * </li> 060 * </ul> 061 * 062 * @since 5.8 063 */ 064@StatelessCheck 065public class NoLineWrapCheck extends AbstractCheck { 066 067 /** 068 * A key is pointing to the warning message text in "messages.properties" 069 * file. 070 */ 071 public static final String MSG_KEY = "no.line.wrap"; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT}; 076 } 077 078 @Override 079 public int[] getAcceptableTokens() { 080 return new int[] { 081 TokenTypes.IMPORT, 082 TokenTypes.STATIC_IMPORT, 083 TokenTypes.PACKAGE_DEF, 084 TokenTypes.CLASS_DEF, 085 TokenTypes.METHOD_DEF, 086 TokenTypes.CTOR_DEF, 087 TokenTypes.ENUM_DEF, 088 TokenTypes.INTERFACE_DEF, 089 TokenTypes.RECORD_DEF, 090 TokenTypes.COMPACT_CTOR_DEF, 091 }; 092 } 093 094 @Override 095 public int[] getRequiredTokens() { 096 return CommonUtil.EMPTY_INT_ARRAY; 097 } 098 099 @Override 100 public void visitToken(DetailAST ast) { 101 if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) { 102 log(ast, MSG_KEY, ast.getText()); 103 } 104 } 105 106}