1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.design;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <div>
29 * Implements Joshua Bloch, Effective Java, Item 17 -
30 * Use Interfaces only to define types.
31 * </div>
32 *
33 * <p>
34 * According to Bloch, an interface should describe a <em>type</em>. It is therefore
35 * inappropriate to define an interface that does not contain any methods
36 * but only constants. The Standard interface
37 * <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html">
38 * javax.swing.SwingConstants</a> is an example of an interface that would be flagged by this check.
39 * </p>
40 *
41 * <p>
42 * The check can be configured to also disallow marker interfaces like {@code java.io.Serializable},
43 * that do not contain methods or constants at all.
44 * </p>
45 * <ul>
46 * <li>
47 * Property {@code allowMarkerInterfaces} - Control whether marker interfaces
48 * like Serializable are allowed.
49 * Type is {@code boolean}.
50 * Default value is {@code true}.
51 * </li>
52 * </ul>
53 *
54 * <p>
55 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
56 * </p>
57 *
58 * <p>
59 * Violation Message Keys:
60 * </p>
61 * <ul>
62 * <li>
63 * {@code interface.type}
64 * </li>
65 * </ul>
66 *
67 * @since 3.1
68 */
69 @StatelessCheck
70 public final class InterfaceIsTypeCheck
71 extends AbstractCheck {
72
73 /**
74 * A key is pointing to the warning message text in "messages.properties"
75 * file.
76 */
77 public static final String MSG_KEY = "interface.type";
78
79 /** Control whether marker interfaces like Serializable are allowed. */
80 private boolean allowMarkerInterfaces = true;
81
82 @Override
83 public int[] getDefaultTokens() {
84 return getRequiredTokens();
85 }
86
87 @Override
88 public int[] getRequiredTokens() {
89 return new int[] {TokenTypes.INTERFACE_DEF};
90 }
91
92 @Override
93 public int[] getAcceptableTokens() {
94 return getRequiredTokens();
95 }
96
97 @Override
98 public void visitToken(DetailAST ast) {
99 final DetailAST objBlock =
100 ast.findFirstToken(TokenTypes.OBJBLOCK);
101 final DetailAST methodDef =
102 objBlock.findFirstToken(TokenTypes.METHOD_DEF);
103 final DetailAST variableDef =
104 objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
105 final boolean methodRequired =
106 !allowMarkerInterfaces || variableDef != null;
107
108 if (methodDef == null && methodRequired) {
109 log(ast, MSG_KEY);
110 }
111 }
112
113 /**
114 * Setter to control whether marker interfaces like Serializable are allowed.
115 *
116 * @param flag whether to allow marker interfaces or not
117 * @since 3.1
118 */
119 public void setAllowMarkerInterfaces(boolean flag) {
120 allowMarkerInterfaces = flag;
121 }
122
123 }