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.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 * <p>
029 * Implements Joshua Bloch, Effective Java, Item 17 -
030 * Use Interfaces only to define types.
031 * </p>
032 * <p>
033 * According to Bloch, an interface should describe a <em>type</em>. It is therefore
034 * inappropriate to define an interface that does not contain any methods
035 * but only constants. The Standard interface
036 * <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html">
037 * javax.swing.SwingConstants</a> is an example of an interface that would be flagged by this check.
038 * </p>
039 * <p>
040 * The check can be configured to also disallow marker interfaces like {@code java.io.Serializable},
041 * that do not contain methods or constants at all.
042 * </p>
043 * <ul>
044 * <li>
045 * Property {@code allowMarkerInterfaces} - Control whether marker interfaces
046 * like Serializable are allowed.
047 * Default value is {@code true}.
048 * </li>
049 * </ul>
050 * <p>
051 * To configure the check:
052 * </p>
053 * <pre>
054 * &lt;module name="InterfaceIsType"/&gt;
055 * </pre>
056 *
057 * @since 3.1
058 */
059@StatelessCheck
060public final class InterfaceIsTypeCheck
061        extends AbstractCheck {
062
063    /**
064     * A key is pointing to the warning message text in "messages.properties"
065     * file.
066     */
067    public static final String MSG_KEY = "interface.type";
068
069    /** Control whether marker interfaces like Serializable are allowed. */
070    private boolean allowMarkerInterfaces = true;
071
072    @Override
073    public int[] getDefaultTokens() {
074        return getRequiredTokens();
075    }
076
077    @Override
078    public int[] getRequiredTokens() {
079        return new int[] {TokenTypes.INTERFACE_DEF};
080    }
081
082    @Override
083    public int[] getAcceptableTokens() {
084        return getRequiredTokens();
085    }
086
087    @Override
088    public void visitToken(DetailAST ast) {
089        final DetailAST objBlock =
090                ast.findFirstToken(TokenTypes.OBJBLOCK);
091        final DetailAST methodDef =
092                objBlock.findFirstToken(TokenTypes.METHOD_DEF);
093        final DetailAST variableDef =
094                objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
095        final boolean methodRequired =
096                !allowMarkerInterfaces || variableDef != null;
097
098        if (methodDef == null && methodRequired) {
099            log(ast.getLineNo(), MSG_KEY);
100        }
101    }
102
103    /**
104     * Setter to control whether marker interfaces like Serializable are allowed.
105     * @param flag whether to allow marker interfaces or not
106     */
107    public void setAllowMarkerInterfaces(boolean flag) {
108        allowMarkerInterfaces = flag;
109    }
110
111}