View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * Implements Bloch, Effective Java, Item 17 -
29   * Use Interfaces only to define types.
30   *
31   * <p>
32   * An interface should describe a <em>type</em>, it is therefore
33   * inappropriate to define an interface that does not contain any methods
34   * but only constants.
35   * </p>
36   *
37   * <p>
38   * The check can be configured to also disallow marker interfaces like
39   * {@code java.io.Serializable}, that do not contain methods or
40   * constants at all.
41   * </p>
42   *
43   * @author lkuehne
44   */
45  @StatelessCheck
46  public final class InterfaceIsTypeCheck
47          extends AbstractCheck {
48  
49      /**
50       * A key is pointing to the warning message text in "messages.properties"
51       * file.
52       */
53      public static final String MSG_KEY = "interface.type";
54  
55      /** Flag to control whether marker interfaces are allowed. */
56      private boolean allowMarkerInterfaces = true;
57  
58      @Override
59      public int[] getDefaultTokens() {
60          return new int[] {TokenTypes.INTERFACE_DEF};
61      }
62  
63      @Override
64      public int[] getRequiredTokens() {
65          return getDefaultTokens();
66      }
67  
68      @Override
69      public int[] getAcceptableTokens() {
70          return new int[] {TokenTypes.INTERFACE_DEF};
71      }
72  
73      @Override
74      public void visitToken(DetailAST ast) {
75          final DetailAST objBlock =
76                  ast.findFirstToken(TokenTypes.OBJBLOCK);
77          final DetailAST methodDef =
78                  objBlock.findFirstToken(TokenTypes.METHOD_DEF);
79          final DetailAST variableDef =
80                  objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
81          final boolean methodRequired =
82                  !allowMarkerInterfaces || variableDef != null;
83  
84          if (methodDef == null && methodRequired) {
85              log(ast.getLineNo(), MSG_KEY);
86          }
87  
88      }
89  
90      /**
91       * Controls whether marker interfaces like Serializable are allowed.
92       * @param flag whether to allow marker interfaces or not
93       */
94      public void setAllowMarkerInterfaces(boolean flag) {
95          allowMarkerInterfaces = flag;
96      }
97  }