View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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   *
46   * @since 3.1
47   */
48  @StatelessCheck
49  public final class InterfaceIsTypeCheck
50          extends AbstractCheck {
51  
52      /**
53       * A key is pointing to the warning message text in "messages.properties"
54       * file.
55       */
56      public static final String MSG_KEY = "interface.type";
57  
58      /** Control whether marker interfaces like Serializable are allowed. */
59      private boolean allowMarkerInterfaces = true;
60  
61      @Override
62      public int[] getDefaultTokens() {
63          return getRequiredTokens();
64      }
65  
66      @Override
67      public int[] getRequiredTokens() {
68          return new int[] {TokenTypes.INTERFACE_DEF};
69      }
70  
71      @Override
72      public int[] getAcceptableTokens() {
73          return getRequiredTokens();
74      }
75  
76      @Override
77      public void visitToken(DetailAST ast) {
78          final DetailAST objBlock =
79                  ast.findFirstToken(TokenTypes.OBJBLOCK);
80          final DetailAST methodDef =
81                  objBlock.findFirstToken(TokenTypes.METHOD_DEF);
82          final DetailAST variableDef =
83                  objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
84          final boolean methodRequired =
85                  !allowMarkerInterfaces || variableDef != null;
86  
87          if (methodDef == null && methodRequired) {
88              log(ast, MSG_KEY);
89          }
90      }
91  
92      /**
93       * Setter to control whether marker interfaces like Serializable are allowed.
94       *
95       * @param flag whether to allow marker interfaces or not
96       * @since 3.1
97       */
98      public void setAllowMarkerInterfaces(boolean flag) {
99          allowMarkerInterfaces = flag;
100     }
101 
102 }