001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.design; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Implements Joshua Bloch, Effective Java, Item 17 - 030 * Use Interfaces only to define types. 031 * </p> 032 * <p> 033 * According to Bloch, an interface should describe a <em>type</em>. It is therefore 034 * inappropriate to define an interface that does not contain any methods 035 * but only constants. The Standard interface 036 * <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html"> 037 * javax.swing.SwingConstants</a> is an example of an interface that would be flagged by this check. 038 * </p> 039 * <p> 040 * The check can be configured to also disallow marker interfaces like {@code java.io.Serializable}, 041 * that do not contain methods or constants at all. 042 * </p> 043 * <ul> 044 * <li> 045 * Property {@code allowMarkerInterfaces} - Control whether marker interfaces 046 * like Serializable are allowed. 047 * Type is {@code boolean}. 048 * Default value is {@code true}. 049 * </li> 050 * </ul> 051 * <p> 052 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 053 * </p> 054 * <p> 055 * Violation Message Keys: 056 * </p> 057 * <ul> 058 * <li> 059 * {@code interface.type} 060 * </li> 061 * </ul> 062 * 063 * @since 3.1 064 */ 065@StatelessCheck 066public final class InterfaceIsTypeCheck 067 extends AbstractCheck { 068 069 /** 070 * A key is pointing to the warning message text in "messages.properties" 071 * file. 072 */ 073 public static final String MSG_KEY = "interface.type"; 074 075 /** Control whether marker interfaces like Serializable are allowed. */ 076 private boolean allowMarkerInterfaces = true; 077 078 @Override 079 public int[] getDefaultTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getRequiredTokens() { 085 return new int[] {TokenTypes.INTERFACE_DEF}; 086 } 087 088 @Override 089 public int[] getAcceptableTokens() { 090 return getRequiredTokens(); 091 } 092 093 @Override 094 public void visitToken(DetailAST ast) { 095 final DetailAST objBlock = 096 ast.findFirstToken(TokenTypes.OBJBLOCK); 097 final DetailAST methodDef = 098 objBlock.findFirstToken(TokenTypes.METHOD_DEF); 099 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}