001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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()&lt;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 * &lt;module name=&quot;AvoidInlineConditionals&quot;/&gt;
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 &amp;&amp; a.length() &gt;= 1) { // OK
055 *   b = a.substring(1);
056 * } else {
057 *   b = null;
058 * }
059 *
060 * b = (a != null &amp;&amp; a.length() &gt;= 1) ? a.substring(1) : null; // violation
061 * </pre>
062 *
063 * @since 3.1
064 */
065@StatelessCheck
066public class AvoidInlineConditionalsCheck extends AbstractCheck {
067
068    /**
069     * A key is pointing to the warning message text in "messages.properties"
070     * file.
071     */
072    public static final String MSG_KEY = "inline.conditional.avoid";
073
074    @Override
075    public int[] getDefaultTokens() {
076        return getRequiredTokens();
077    }
078
079    @Override
080    public int[] getRequiredTokens() {
081        return new int[] {TokenTypes.QUESTION};
082    }
083
084    @Override
085    public int[] getAcceptableTokens() {
086        return getRequiredTokens();
087    }
088
089    @Override
090    public void visitToken(DetailAST ast) {
091        // the only place a QUESTION token can occur is in inline conditionals
092        // so no need to do any further tricks here - pretty trivial Check!
093
094        log(ast, MSG_KEY);
095    }
096
097}