001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.design;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024
025/**
026 * Implements Bloch, Effective Java, Item 17 -
027 * Use Interfaces only to define types.
028 *
029 * <p>
030 * An interface should describe a <em>type</em>, it is therefore
031 * inappropriate to define an interface that does not contain any methods
032 * but only constants.
033 * </p>
034 *
035 * <p>
036 * The check can be configured to also disallow marker interfaces like
037 * <code>java.io.Serializable</code>, that do not contain methods or
038 * constants at all.
039 * </p>
040 *
041 * @author lkuehne
042 */
043public final class InterfaceIsTypeCheck
044        extends Check
045{
046    /** flag to control whether marker interfaces are allowed. */
047    private boolean allowMarkerInterfaces = true;
048
049    @Override
050    public int[] getDefaultTokens()
051    {
052        return new int[] {TokenTypes.INTERFACE_DEF};
053    }
054
055    @Override
056    public int[] getRequiredTokens()
057    {
058        return getDefaultTokens();
059    }
060
061    @Override
062    public void visitToken(DetailAST ast)
063    {
064        final DetailAST objBlock =
065                ast.findFirstToken(TokenTypes.OBJBLOCK);
066        final DetailAST methodDef =
067                objBlock.findFirstToken(TokenTypes.METHOD_DEF);
068        final DetailAST variableDef =
069                objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
070        final boolean methodRequired =
071                !allowMarkerInterfaces || (variableDef != null);
072
073        if ((methodDef == null) && methodRequired) {
074            log(ast.getLineNo(), "interface.type");
075        }
076
077    }
078
079    /**
080     * Controls whether marker interfaces like Serializable are allowed.
081     * @param flag whether to allow marker interfaces or not
082     */
083    public void setAllowMarkerInterfaces(boolean flag)
084    {
085        allowMarkerInterfaces = flag;
086    }
087}