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" 051 * file. 052 */ 053 public static final String MSG_KEY = "array.type.style"; 054 055 /** Control whether to enforce Java style (true) or C style (false). */ 056 private boolean javaStyle = true; 057 058 /** 059 * Creates a new {@code ArrayTypeStyleCheck} instance. 060 */ 061 public ArrayTypeStyleCheck() { 062 // no code by default 063 } 064 065 @Override 066 public int[] getDefaultTokens() { 067 return getRequiredTokens(); 068 } 069 070 @Override 071 public int[] getAcceptableTokens() { 072 return getRequiredTokens(); 073 } 074 075 @Override 076 public int[] getRequiredTokens() { 077 return new int[] {TokenTypes.ARRAY_DECLARATOR}; 078 } 079 080 @Override 081 public void visitToken(DetailAST ast) { 082 final DetailAST typeAST = ast.getParent(); 083 final DetailAST identAst = typeAST.getNextSibling(); 084 // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]' 085 if (identAst != null) { 086 final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF; 087 final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo() 088 || identAst.getColumnNo() - ast.getColumnNo() > -1; 089 090 // force all methods to be Java style (see note in top Javadoc) 091 final boolean isMethodViolation = isMethod && !isJavaStyle; 092 final boolean isVariableViolation = !isMethod 093 && isJavaStyle != javaStyle; 094 095 if (isMethodViolation || isVariableViolation) { 096 log(ast, MSG_KEY); 097 } 098 } 099 } 100 101 /** 102 * Setter to control whether to enforce Java style (true) or C style (false). 103 * 104 * @param javaStyle true if Java style should be used. 105 * @since 3.1 106 */ 107 public void setJavaStyle(boolean javaStyle) { 108 this.javaStyle = javaStyle; 109 } 110 111}