001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.design;
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 * Implements Bloch, Effective Java, Item 17 -
029 * Use Interfaces only to define types.
030 *
031 * <p>
032 * An interface should describe a <em>type</em>, it is therefore
033 * inappropriate to define an interface that does not contain any methods
034 * but only constants.
035 * </p>
036 *
037 * <p>
038 * The check can be configured to also disallow marker interfaces like
039 * {@code java.io.Serializable}, that do not contain methods or
040 * constants at all.
041 * </p>
042 *
043 * @author lkuehne
044 */
045@StatelessCheck
046public final class InterfaceIsTypeCheck
047        extends AbstractCheck {
048
049    /**
050     * A key is pointing to the warning message text in "messages.properties"
051     * file.
052     */
053    public static final String MSG_KEY = "interface.type";
054
055    /** Flag to control whether marker interfaces are allowed. */
056    private boolean allowMarkerInterfaces = true;
057
058    @Override
059    public int[] getDefaultTokens() {
060        return new int[] {TokenTypes.INTERFACE_DEF};
061    }
062
063    @Override
064    public int[] getRequiredTokens() {
065        return getDefaultTokens();
066    }
067
068    @Override
069    public int[] getAcceptableTokens() {
070        return new int[] {TokenTypes.INTERFACE_DEF};
071    }
072
073    @Override
074    public void visitToken(DetailAST ast) {
075        final DetailAST objBlock =
076                ast.findFirstToken(TokenTypes.OBJBLOCK);
077        final DetailAST methodDef =
078                objBlock.findFirstToken(TokenTypes.METHOD_DEF);
079        final DetailAST variableDef =
080                objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
081        final boolean methodRequired =
082                !allowMarkerInterfaces || variableDef != null;
083
084        if (methodDef == null && methodRequired) {
085            log(ast.getLineNo(), MSG_KEY);
086        }
087
088    }
089
090    /**
091     * Controls whether marker interfaces like Serializable are allowed.
092     * @param flag whether to allow marker interfaces or not
093     */
094    public void setAllowMarkerInterfaces(boolean flag) {
095        allowMarkerInterfaces = flag;
096    }
097}