001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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 *
075 * @since 8.22
076 */
077@StatelessCheck
078public final class UnnecessarySemicolonInEnumerationCheck extends AbstractCheck {
079
080    /**
081     * A key is pointing to the warning message text in "messages.properties"
082     * file.
083     */
084    public static final String MSG_SEMI = "unnecessary.semicolon";
085
086    @Override
087    public int[] getDefaultTokens() {
088        return getRequiredTokens();
089    }
090
091    @Override
092    public int[] getAcceptableTokens() {
093        return getRequiredTokens();
094    }
095
096    @Override
097    public int[] getRequiredTokens() {
098        return new int[] {
099            TokenTypes.ENUM_DEF,
100        };
101    }
102
103    @Override
104    public void visitToken(DetailAST ast) {
105        final DetailAST enumBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
106        final DetailAST semicolon = enumBlock.findFirstToken(TokenTypes.SEMI);
107        if (semicolon != null && isEndOfEnumerationAfter(semicolon)) {
108            log(semicolon, MSG_SEMI);
109        }
110    }
111
112    /**
113     * Checks if enum body has no code elements after enum constants semicolon.
114     * @param ast semicolon in enum constants definition end
115     * @return true if there is no code elements, false otherwise.
116     */
117    private static boolean isEndOfEnumerationAfter(DetailAST ast) {
118        return ast.getNextSibling().getType() == TokenTypes.RCURLY;
119    }
120}