Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
InterfaceIsTypeCheck |
|
| 1.75;1.75 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 Oliver Burn | |
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 | package com.puppycrawl.tools.checkstyle.checks.design; | |
20 | ||
21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
22 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
23 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
24 | ||
25 | /** | |
26 | * Implements Bloch, Effective Java, Item 17 - | |
27 | * Use Interfaces only to define types. | |
28 | * | |
29 | * <p> | |
30 | * An interface should describe a <em>type</em>, it is therefore | |
31 | * inappropriate to define an interface that does not contain any methods | |
32 | * but only constants. | |
33 | * </p> | |
34 | * | |
35 | * <p> | |
36 | * The check can be configured to also disallow marker interfaces like | |
37 | * <code>java.io.Serializable</code>, that do not contain methods or | |
38 | * constants at all. | |
39 | * </p> | |
40 | * | |
41 | * @author lkuehne | |
42 | */ | |
43 | 2 | public final class InterfaceIsTypeCheck |
44 | extends Check | |
45 | { | |
46 | /** flag to control whether marker interfaces are allowed. */ | |
47 | 2 | private boolean mAllowMarkerInterfaces = true; |
48 | ||
49 | @Override | |
50 | public int[] getDefaultTokens() | |
51 | { | |
52 | 2 | return new int[] {TokenTypes.INTERFACE_DEF}; |
53 | } | |
54 | ||
55 | @Override | |
56 | public int[] getRequiredTokens() | |
57 | { | |
58 | 0 | return getDefaultTokens(); |
59 | } | |
60 | ||
61 | @Override | |
62 | public void visitToken(DetailAST aAST) | |
63 | { | |
64 | 6 | final DetailAST objBlock = |
65 | aAST.findFirstToken(TokenTypes.OBJBLOCK); | |
66 | 6 | final DetailAST methodDef = |
67 | objBlock.findFirstToken(TokenTypes.METHOD_DEF); | |
68 | 6 | final DetailAST variableDef = |
69 | objBlock.findFirstToken(TokenTypes.VARIABLE_DEF); | |
70 | 6 | final boolean methodRequired = |
71 | !mAllowMarkerInterfaces || (variableDef != null); | |
72 | ||
73 | 6 | if ((methodDef == null) && methodRequired) { |
74 | 3 | log(aAST.getLineNo(), "interface.type"); |
75 | } | |
76 | ||
77 | 6 | } |
78 | ||
79 | /** | |
80 | * Controls whether marker interfaces like Serializable are allowed. | |
81 | * @param aFlag whether to allow marker interfaces or not | |
82 | */ | |
83 | public void setAllowMarkerInterfaces(boolean aFlag) | |
84 | { | |
85 | 1 | mAllowMarkerInterfaces = aFlag; |
86 | 1 | } |
87 | } |