KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > design > InterfaceIsTypeCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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  * @version $Revision: 1.7 $
43  */

44 public final class InterfaceIsTypeCheck
45         extends Check
46 {
47     /** flag to control whether marker interfaces are allowed. */
48     private boolean mAllowMarkerInterfaces = true;
49
50     /** {@inheritDoc} */
51     public int[] getDefaultTokens()
52     {
53         return new int[] {TokenTypes.INTERFACE_DEF};
54     }
55
56     /** {@inheritDoc} */
57     public int[] getRequiredTokens()
58     {
59         return getDefaultTokens();
60     }
61
62     /** {@inheritDoc} */
63     public void visitToken(DetailAST aAST)
64     {
65         final DetailAST objBlock =
66                 aAST.findFirstToken(TokenTypes.OBJBLOCK);
67         final DetailAST methodDef =
68                 objBlock.findFirstToken(TokenTypes.METHOD_DEF);
69         final DetailAST variableDef =
70                 objBlock.findFirstToken(TokenTypes.VARIABLE_DEF);
71         final boolean methodRequired =
72                 !mAllowMarkerInterfaces || (variableDef != null);
73
74         if ((methodDef == null) && methodRequired) {
75             log(aAST.getLineNo(), "interface.type");
76         }
77
78     }
79
80     /**
81      * Controls whether marker interfaces like Serializable are allowed.
82      * @param aFlag whether to allow marker interfaces or not
83      */

84     public void setAllowMarkerInterfaces(boolean aFlag)
85     {
86         mAllowMarkerInterfaces = aFlag;
87     }
88 }
89
Popular Tags