KickJava   Java API By Example, From Geeks To Geeks.

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


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.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
25
26 import java.util.Stack JavaDoc;
27
28 /**
29  * <p>
30  * Checks that class which has only private ctors
31  * is declared as final.
32  * </p>
33  * <p>
34  * An example of how to configure the check is:
35  * </p>
36  * <pre>
37  * &lt;module name="FinalClass"/&gt;
38  * </pre>
39  * @author o_sukhodolsky
40  */

41 public class FinalClassCheck
42     extends Check
43 {
44     /** Keeps ClassDesc objects for stack of declared classes. */
45     private final Stack JavaDoc mClasses = new Stack JavaDoc();
46
47     /** {@inheritDoc} */
48     public int[] getDefaultTokens()
49     {
50         return new int[]{TokenTypes.CLASS_DEF, TokenTypes.CTOR_DEF};
51     }
52
53     /** {@inheritDoc} */
54     public void visitToken(DetailAST aAST)
55     {
56         final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS);
57
58         if (aAST.getType() == TokenTypes.CLASS_DEF) {
59             final boolean isFinal = (modifiers != null)
60                     && modifiers.branchContains(TokenTypes.FINAL);
61             final boolean isAbstract = (modifiers != null)
62                     && modifiers.branchContains(TokenTypes.ABSTRACT);
63             mClasses.push(new ClassDesc(isFinal, isAbstract));
64         }
65         else if (!ScopeUtils.inEnumBlock(aAST)) { //ctors in enums don't matter
66
final ClassDesc desc = (ClassDesc) mClasses.peek();
67             if ((modifiers != null)
68                 && modifiers.branchContains(TokenTypes.LITERAL_PRIVATE))
69             {
70                 desc.reportPrivateCtor();
71             }
72             else {
73                 desc.reportNonPrivateCtor();
74             }
75         }
76     }
77
78     /** {@inheritDoc} */
79     public void leaveToken(DetailAST aAST)
80     {
81         if (aAST.getType() != TokenTypes.CLASS_DEF) {
82             return;
83         }
84
85         final ClassDesc desc = (ClassDesc) mClasses.pop();
86         if (!desc.isDeclaredAsFinal()
87             && !desc.isDeclaredAsAbstract()
88             && desc.hasPrivateCtor()
89             && !desc.hasNonPrivateCtor())
90         {
91             final String JavaDoc className =
92                 aAST.findFirstToken(TokenTypes.IDENT).getText();
93             log(aAST.getLineNo(), "final.class", className);
94         }
95     }
96
97     /** maintains information about class' ctors */
98     private static final class ClassDesc
99     {
100         /** is class declared as final */
101         private final boolean mDeclaredAsFinal;
102
103         /** is class declared as abstract */
104         private final boolean mDeclaredAsAbstract;
105
106         /** does class have non-provate ctors */
107         private boolean mHasNonPrivateCtor;
108
109         /** does class have private ctors */
110         private boolean mHasPrivateCtor;
111
112         /**
113          * create a new ClassDesc instance.
114          * @param aDeclaredAsFinal indicates if the
115          * class declared as final
116          * @param aDeclaredAsAbstract indicates if the
117          * class declared as abstract
118          */

119         ClassDesc(boolean aDeclaredAsFinal, boolean aDeclaredAsAbstract)
120         {
121             mDeclaredAsFinal = aDeclaredAsFinal;
122             mDeclaredAsAbstract = aDeclaredAsAbstract;
123         }
124
125         /** adds private ctor. */
126         void reportPrivateCtor()
127         {
128             mHasPrivateCtor = true;
129         }
130
131         /** adds non-private ctor. */
132         void reportNonPrivateCtor()
133         {
134             mHasNonPrivateCtor = true;
135         }
136
137         /**
138          * does class have private ctors.
139          * @return true if class has private ctors
140          */

141         boolean hasPrivateCtor()
142         {
143             return mHasPrivateCtor;
144         }
145
146         /**
147          * does class have non-private ctors.
148          * @return true if class has non-private ctors
149          */

150         boolean hasNonPrivateCtor()
151         {
152             return mHasNonPrivateCtor;
153         }
154
155         /**
156          * is class declared as final.
157          * @return true if class is declared as final
158          */

159         boolean isDeclaredAsFinal()
160         {
161             return mDeclaredAsFinal;
162         }
163
164         /**
165          * is class declared as abstract.
166          * @return true if class is declared as final
167          */

168         boolean isDeclaredAsAbstract()
169         {
170             return mDeclaredAsAbstract;
171         }
172
173         /**
174          * Returns a string representation of the object.
175          * @return a string representation of the object
176          */

177         public String JavaDoc toString()
178         {
179             return this.getClass().getName()
180                 + "["
181                 + "final=" + mDeclaredAsFinal
182                 + " abstract=" + mDeclaredAsAbstract
183                 + " pctor=" + mHasPrivateCtor
184                 + " ctor=" + mHasNonPrivateCtor
185                 + "]";
186         }
187     }
188 }
189
Popular Tags