View Javadoc
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   * <p>
29   * Implements Joshua Bloch, Effective Java, Item 17 -
30   * Use Interfaces only to define types.
31   * </p>
32   * <p>
33   * According to Bloch, an interface should describe a <em>type</em>. It is therefore
34   * inappropriate to define an interface that does not contain any methods
35   * but only constants. The Standard interface
36   * <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html">
37   * javax.swing.SwingConstants</a> is an example of an interface that would be flagged by this check.
38   * </p>
39   * <p>
40   * The check can be configured to also disallow marker interfaces like {@code java.io.Serializable},
41   * that do not contain methods or constants at all.
42   * </p>
43   * <ul>
44   * <li>
45   * Property {@code allowMarkerInterfaces} - Control whether marker interfaces
46   * like Serializable are allowed.
47   * Type is {@code boolean}.
48   * Default value is {@code true}.
49   * </li>
50   * </ul>
51   * <p>
52   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
53   * </p>
54   * <p>
55   * Violation Message Keys:
56   * </p>
57   * <ul>
58   * <li>
59   * {@code interface.type}
60   * </li>
61   * </ul>
62   *
63   * @since 3.1
64   */
65  @StatelessCheck
66  public final class InterfaceIsTypeCheck
67          extends AbstractCheck {
68  
69      /**
70       * A key is pointing to the warning message text in "messages.properties"
71       * file.
72       */
73      public static final String MSG_KEY = "interface.type";
74  
75      /** Control whether marker interfaces like Serializable are allowed. */
76      private boolean allowMarkerInterfaces = true;
77  
78      @Override
79      public int[] getDefaultTokens() {
80          return getRequiredTokens();
81      }
82  
83      @Override
84      public int[] getRequiredTokens() {
85          return new int[] {TokenTypes.INTERFACE_DEF};
86      }
87  
88      @Override
89      public int[] getAcceptableTokens() {
90          return getRequiredTokens();
91      }
92  
93      @Override
94      public void visitToken(DetailAST ast) {
95          final DetailAST objBlock =
96                  ast.findFirstToken(TokenTypes.OBJBLOCK);
97          final DetailAST methodDef =
98                  objBlock.findFirstToken(TokenTypes.METHOD_DEF);
99          final DetailAST variableDef =
100                 objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
101         final boolean methodRequired =
102                 !allowMarkerInterfaces || variableDef != null;
103 
104         if (methodDef == null && methodRequired) {
105             log(ast, MSG_KEY);
106         }
107     }
108 
109     /**
110      * Setter to control whether marker interfaces like Serializable are allowed.
111      *
112      * @param flag whether to allow marker interfaces or not
113      * @since 3.1
114      */
115     public void setAllowMarkerInterfaces(boolean flag) {
116         allowMarkerInterfaces = flag;
117     }
118 
119 }