View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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      /**
62       * Creates a new {@code InterfaceIsTypeCheck} instance.
63       */
64      public InterfaceIsTypeCheck() {
65          // no code by default
66      }
67  
68      @Override
69      public int[] getDefaultTokens() {
70          return getRequiredTokens();
71      }
72  
73      @Override
74      public int[] getRequiredTokens() {
75          return new int[] {TokenTypes.INTERFACE_DEF};
76      }
77  
78      @Override
79      public int[] getAcceptableTokens() {
80          return getRequiredTokens();
81      }
82  
83      @Override
84      public void visitToken(DetailAST ast) {
85          final DetailAST objBlock =
86                  ast.findFirstToken(TokenTypes.OBJBLOCK);
87          final DetailAST methodDef =
88                  objBlock.findFirstToken(TokenTypes.METHOD_DEF);
89          final DetailAST variableDef =
90                  objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
91          final boolean methodRequired =
92                  !allowMarkerInterfaces || variableDef != null;
93  
94          if (methodDef == null && methodRequired) {
95              log(ast, MSG_KEY);
96          }
97      }
98  
99      /**
100      * Setter to control whether marker interfaces like Serializable are allowed.
101      *
102      * @param flag whether to allow marker interfaces or not
103      * @since 3.1
104      */
105     public void setAllowMarkerInterfaces(boolean flag) {
106         allowMarkerInterfaces = flag;
107     }
108 
109 }