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 * Checks if unnecessary semicolon is in enum definitions.
030 * Semicolon is not needed if enum body contains only enum constants.
031 * </p>
032 * <p>
033 * To configure the check:
034 * </p>
035 * <pre>
036 * &lt;module name=&quot;UnnecessarySemicolonInEnumeration&quot;/&gt;
037 * </pre>
038 * <p>
039 * Example of violations
040 * </p>
041 * <pre>
042 * enum One {
043 *     A,B; // violation
044 * }
045 * enum Two {
046 *     A,B,; // violation
047 * }
048 * enum Three {
049 *     A,B(); // violation
050 * }
051 * enum Four {
052 *     A,B{}; // violation
053 * }
054 * enum Five {
055 *     A,
056 *     B
057 *     ; // violation
058 * }
059 * </pre>
060 * <p>
061 * Example of good cases
062 * </p>
063 * <pre>
064 * enum Normal {
065 *     A,
066 *     B,
067 *     ; // required ";", no violation
068 *     Normal(){}
069 * }
070 * enum NoSemicolon {
071 *     A, B // only enum constants, no semicolon required
072 * }
073 * </pre>
074 * <p>
075 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
076 * </p>
077 * <p>
078 * Violation Message Keys:
079 * </p>
080 * <ul>
081 * <li>
082 * {@code unnecessary.semicolon}
083 * </li>
084 * </ul>
085 *
086 * @since 8.22
087 */
088@StatelessCheck
089public final class UnnecessarySemicolonInEnumerationCheck extends AbstractCheck {
090
091    /**
092     * A key is pointing to the warning message text in "messages.properties"
093     * file.
094     */
095    public static final String MSG_SEMI = "unnecessary.semicolon";
096
097    @Override
098    public int[] getDefaultTokens() {
099        return getRequiredTokens();
100    }
101
102    @Override
103    public int[] getAcceptableTokens() {
104        return getRequiredTokens();
105    }
106
107    @Override
108    public int[] getRequiredTokens() {
109        return new int[] {
110            TokenTypes.ENUM_DEF,
111        };
112    }
113
114    @Override
115    public void visitToken(DetailAST ast) {
116        final DetailAST enumBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
117        final DetailAST semicolon = enumBlock.findFirstToken(TokenTypes.SEMI);
118        if (semicolon != null && isEndOfEnumerationAfter(semicolon)) {
119            log(semicolon, MSG_SEMI);
120        }
121    }
122
123    /**
124     * Checks if enum body has no code elements after enum constants semicolon.
125     *
126     * @param ast semicolon in enum constants definition end
127     * @return true if there is no code elements, false otherwise.
128     */
129    private static boolean isEndOfEnumerationAfter(DetailAST ast) {
130        return ast.getNextSibling().getType() == TokenTypes.RCURLY;
131    }
132}