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; 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; 026 027/** 028 * <div> 029 * Checks the style of array type definitions. 030 * Some like Java style: {@code public static void main(String[] args)} 031 * and some like C style: {@code public static void main(String args[])}. 032 * </div> 033 * 034 * <p> 035 * By default, the Check enforces Java style. 036 * </p> 037 * 038 * <p> 039 * This check strictly enforces only Java style for method return types regardless 040 * of the value for 'javaStyle'. For example, {@code byte[] getData()}. 041 * This is because C doesn't compile methods with array declarations on the name. 042 * </p> 043 * 044 * @since 3.1 045 */ 046@StatelessCheck 047public class ArrayTypeStyleCheck extends AbstractCheck { 048 049 /** 050 * A key is pointing to the warning message text in "messages.properties" file. 051 */ 052 public static final String MSG_KEY = "array.type.style"; 053 054 /** Control whether to enforce Java style (true) or C style (false). */ 055 private boolean javaStyle = true; 056 057 /** 058 * Creates a new {@code ArrayTypeStyleCheck} instance. 059 */ 060 public ArrayTypeStyleCheck() { 061 // no code by default 062 } 063 064 @Override 065 public int[] getDefaultTokens() { 066 return getRequiredTokens(); 067 } 068 069 @Override 070 public int[] getAcceptableTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getRequiredTokens() { 076 return new int[] {TokenTypes.ARRAY_DECLARATOR}; 077 } 078 079 @Override 080 public void visitToken(DetailAST ast) { 081 final DetailAST typeAST = ast.getParent(); 082 final DetailAST identAst = typeAST.getNextSibling(); 083 // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]' 084 if (identAst != null) { 085 final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF; 086 final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo() 087 || identAst.getColumnNo() - ast.getColumnNo() > -1; 088 089 // force all methods to be Java style (see note in top Javadoc) 090 final boolean isMethodViolation = isMethod && !isJavaStyle; 091 final boolean isVariableViolation = !isMethod 092 && isJavaStyle != javaStyle; 093 094 if (isMethodViolation || isVariableViolation) { 095 log(ast, MSG_KEY); 096 } 097 } 098 } 099 100 /** 101 * Setter to control whether to enforce Java style (true) or C style (false). 102 * 103 * @param javaStyle true if Java style should be used. 104 * @since 3.1 105 */ 106 public void setJavaStyle(boolean javaStyle) { 107 this.javaStyle = javaStyle; 108 } 109 110}