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