001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * <p>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 * </p> 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 * <p> 049 * To configure the check to force no line-wrapping 050 * in package and import statements (default values): 051 * </p> 052 * <pre> 053 * <module name="NoLineWrap"/> 054 * </pre> 055 * <p>Examples of line-wrapped statements (bad case): 056 * </p> 057 * <pre> 058 * package com.puppycrawl. // violation 059 * tools.checkstyle.checks; 060 * 061 * import com.puppycrawl.tools. // violation 062 * checkstyle.api.AbstractCheck; 063 * 064 * import static java.math. // violation 065 * BigInteger.ZERO; 066 * </pre> 067 * 068 * <p> 069 * Examples: 070 * </p> 071 * <pre> 072 * package com.puppycrawl.tools.checkstyle. // violation 073 * checks.whitespace; 074 * 075 * import java.lang.Object; // OK 076 * import java.lang. // violation 077 * Integer; 078 * 079 * import static java.math. // violation 080 * BigInteger.TEN; 081 * </pre> 082 * <pre> 083 * package com.puppycrawl.tools.checkstyle.checks.coding; // OK 084 * 085 * import java.lang. // violation 086 * Boolean; 087 * 088 * import static java.math.BigInteger.ONE; // OK 089 * </pre> 090 * 091 * <p> 092 * To configure the check to force no line-wrapping only 093 * in import statements: 094 * </p> 095 * <pre> 096 * <module name="NoLineWrap"> 097 * <property name="tokens" value="IMPORT"/> 098 * </module> 099 * </pre> 100 * <p> 101 * Example: 102 * </p> 103 * <pre> 104 * package com.puppycrawl. // OK 105 * tools.checkstyle.checks; 106 * 107 * import java.io.*; // OK 108 * import java.lang. // violation 109 * Boolean; 110 * 111 * import static java.math. // OK 112 * BigInteger.ZERO; 113 * </pre> 114 * <p> 115 * To configure the check to force no line-wrapping only 116 * in class, method and constructor definitions: 117 * </p> 118 * <pre> 119 * <module name="NoLineWrap"> 120 * <property name="tokens" value="CLASS_DEF, METHOD_DEF, CTOR_DEF"/> 121 * </module> 122 * </pre> 123 * <p> 124 * Example: 125 * </p> 126 * <pre> 127 * public class // violation, class definition not wrapped in a single line 128 * Foo { 129 * 130 * public Foo() { // OK 131 * } 132 * 133 * public static void // violation, method definition not wrapped in a single line 134 * doSomething() { 135 * } 136 * } 137 * 138 * public class Bar { // OK 139 * 140 * public // violation, constructor definition not wrapped in a single line 141 * Bar() { 142 * } 143 * 144 * public int fun() { // OK 145 * } 146 * } 147 * </pre> 148 * 149 * <p>Examples of not line-wrapped statements (good case): 150 * </p> 151 * <pre> 152 * import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 153 * import static java.math.BigInteger.ZERO; 154 * </pre> 155 * <p> 156 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 157 * </p> 158 * <p> 159 * Violation Message Keys: 160 * </p> 161 * <ul> 162 * <li> 163 * {@code no.line.wrap} 164 * </li> 165 * </ul> 166 * 167 * @since 5.8 168 */ 169@StatelessCheck 170public class NoLineWrapCheck extends AbstractCheck { 171 172 /** 173 * A key is pointing to the warning message text in "messages.properties" 174 * file. 175 */ 176 public static final String MSG_KEY = "no.line.wrap"; 177 178 @Override 179 public int[] getDefaultTokens() { 180 return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT}; 181 } 182 183 @Override 184 public int[] getAcceptableTokens() { 185 return new int[] { 186 TokenTypes.IMPORT, 187 TokenTypes.STATIC_IMPORT, 188 TokenTypes.PACKAGE_DEF, 189 TokenTypes.CLASS_DEF, 190 TokenTypes.METHOD_DEF, 191 TokenTypes.CTOR_DEF, 192 TokenTypes.ENUM_DEF, 193 TokenTypes.INTERFACE_DEF, 194 TokenTypes.RECORD_DEF, 195 TokenTypes.COMPACT_CTOR_DEF, 196 }; 197 } 198 199 @Override 200 public int[] getRequiredTokens() { 201 return CommonUtil.EMPTY_INT_ARRAY; 202 } 203 204 @Override 205 public void visitToken(DetailAST ast) { 206 if (!TokenUtil.areOnSameLine(ast, ast.getLastChild())) { 207 log(ast, MSG_KEY, ast.getText()); 208 } 209 } 210 211}