001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks; 020 021import com.puppycrawl.tools.checkstyle.api.Check; 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024 025/** 026 * Checks the style of array type definitions. 027 * Some like Java-style: <code>public static void main(String[] args)</code> 028 * and some like C-style: public static void main(String args[]) 029 * 030 * By default the Check enforces Java style. 031 * @author lkuehne 032 */ 033public class ArrayTypeStyleCheck extends Check 034{ 035 /** controls whether to use Java or C style */ 036 private boolean javaStyle = true; 037 038 @Override 039 public int[] getDefaultTokens() 040 { 041 return new int[] {TokenTypes.ARRAY_DECLARATOR}; 042 } 043 044 @Override 045 public void visitToken(DetailAST ast) 046 { 047 final DetailAST typeAST = ast.getParent(); 048 if (typeAST.getType() != TokenTypes.TYPE) { 049 return; 050 } 051 final DetailAST declAST = typeAST.getParent(); 052 if (declAST.getType() == TokenTypes.METHOD_DEF) { 053 // Do not check method's return type. 054 // We have no alternatives here. 055 return; 056 } 057 058 final DetailAST variableAST = typeAST.getNextSibling(); 059 if (variableAST != null) { 060 final boolean isJavaStyle = 061 (variableAST.getLineNo() > ast.getLineNo()) 062 || (variableAST.getColumnNo() > ast.getColumnNo()); 063 064 if (isJavaStyle != javaStyle) { 065 log(ast.getLineNo(), ast.getColumnNo(), "array.type.style"); 066 } 067 } 068 } 069 070 /** 071 * Controls whether to check for Java or C style. 072 * @param javaStyle true if Java style should be used. 073 */ 074 public void setJavaStyle(boolean javaStyle) 075 { 076 this.javaStyle = javaStyle; 077 } 078}