001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2023 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.coding; 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 * <p> 029 * Detects inline conditionals. Here is one example of an inline conditional: 030 * </p> 031 * <pre> 032 * String a = getParameter("a"); 033 * String b = (a==null || a.length()<1) ? null : a.substring(1); 034 * </pre> 035 * <p> 036 * Rationale: Some developers find inline conditionals hard to read, so 037 * their employer's coding standards forbid them. 038 * </p> 039 * <p> 040 * To configure the check: 041 * </p> 042 * <pre> 043 * <module name="AvoidInlineConditionals"/> 044 * </pre> 045 * <p>Example:</p> 046 * <pre> 047 * int x = 5; 048 * boolean foobar = (x == 5); // OK 049 * 050 * String text; 051 * text = (text == null) ? "" : text; // violation 052 * 053 * String b; 054 * if (a != null && a.length() >= 1) { // OK 055 * b = a.substring(1); 056 * } else { 057 * b = null; 058 * } 059 * 060 * b = (a != null && a.length() >= 1) ? a.substring(1) : null; // violation 061 * </pre> 062 * <p> 063 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 064 * </p> 065 * <p> 066 * Violation Message Keys: 067 * </p> 068 * <ul> 069 * <li> 070 * {@code inline.conditional.avoid} 071 * </li> 072 * </ul> 073 * 074 * @since 3.1 075 */ 076@StatelessCheck 077public class AvoidInlineConditionalsCheck extends AbstractCheck { 078 079 /** 080 * A key is pointing to the warning message text in "messages.properties" 081 * file. 082 */ 083 public static final String MSG_KEY = "inline.conditional.avoid"; 084 085 @Override 086 public int[] getDefaultTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getRequiredTokens() { 092 return new int[] {TokenTypes.QUESTION}; 093 } 094 095 @Override 096 public int[] getAcceptableTokens() { 097 return getRequiredTokens(); 098 } 099 100 @Override 101 public void visitToken(DetailAST ast) { 102 // the only place a QUESTION token can occur is in inline conditionals 103 // so no need to do any further tricks here - pretty trivial Check! 104 105 log(ast, MSG_KEY); 106 } 107 108}