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.coding; 020 021import com.puppycrawl.tools.checkstyle.api.Check; 022import com.puppycrawl.tools.checkstyle.api.TokenTypes; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024 025/** 026 * Detects inline conditionals. 027 * 028 * An example inline conditional is this: 029 * <pre> 030 * String a = getParameter("a"); 031 * String b = (a==null || a.length<1) ? null : a.substring(1); 032 * </pre> 033 * 034 * Rationale: Some developers find inline conditionals hard to read, 035 * so their company's coding standards forbids them. 036 * 037 * @author lkuehne 038 */ 039public class AvoidInlineConditionalsCheck extends Check 040{ 041 @Override 042 public int[] getDefaultTokens() 043 { 044 return new int[]{TokenTypes.QUESTION}; 045 } 046 047 @Override 048 public int[] getRequiredTokens() 049 { 050 return getDefaultTokens(); 051 } 052 053 @Override 054 public void visitToken(DetailAST ast) 055 { 056 // the only place a QUESTION token can occur is in inline conditionals 057 // so no need to do any further tricks here - pretty trivial Check! 058 059 log(ast.getLineNo(), ast.getColumnNo(), "inline.conditional.avoid"); 060 } 061}