KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > Check


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.api;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24
25 /**
26  * The base class for checks.
27  *
28  * @author Oliver Burn
29  * @version 1.0
30  * @see <a HREF="./{@docRoot}/../writingchecks.html" target="_top">Writing
31  * your own checks</a>
32  */

33 public abstract class Check extends AbstractViolationReporter
34 {
35     /** default tab width for column reporting */
36     private static final int DEFAULT_TAB_WIDTH = 8;
37
38     /** the current file contents */
39     private FileContents mFileContents;
40
41     /** the tokens the check is interested in */
42     private final Set JavaDoc mTokens = new HashSet JavaDoc();
43
44     /** the object for collecting messages. */
45     private LocalizedMessages mMessages;
46
47     /** the tab width for column reporting */
48     private int mTabWidth = DEFAULT_TAB_WIDTH; // meaningful default
49

50     /**
51      * The class loader to load external classes. Not initialised as this must
52      * be set by my creator.
53      */

54     private ClassLoader JavaDoc mLoader;
55
56     /**
57      * Returns the default token a check is interested in. Only used if the
58      * configuration for a check does not define the tokens.
59      * @return the default tokens
60      * @see TokenTypes
61      */

62     public abstract int[] getDefaultTokens();
63
64     /**
65      * The configurable token set.
66      * Used to protect Checks against malicious users who specify an
67      * unacceptable token set in the configuration file.
68      * The default implementation returns the check's default tokens.
69      * @return the token set this check is designed for.
70      * @see TokenTypes
71      */

72     public int[] getAcceptableTokens()
73     {
74         final int[] defaultTokens = getDefaultTokens();
75         final int[] copy = new int[defaultTokens.length];
76         System.arraycopy(defaultTokens, 0, copy, 0, defaultTokens.length);
77         return copy;
78     }
79
80     /**
81      * The tokens that this check must be registered for.
82      * @return the token set this must be registered for.
83      * @see TokenTypes
84      */

85     public int[] getRequiredTokens()
86     {
87         return new int[] {};
88     }
89
90     /**
91      * Adds a set of tokens the check is interested in.
92      * @param aStrRep the string representation of the tokens interested in
93      */

94     public final void setTokens(String JavaDoc[] aStrRep)
95     {
96         for (int i = 0; i < aStrRep.length; i++) {
97             final String JavaDoc s = aStrRep[i];
98             mTokens.add(s);
99         }
100     }
101
102     /**
103      * Returns the tokens registered for the check.
104      * @return the set of token names
105      */

106     public final Set JavaDoc getTokenNames()
107     {
108         return mTokens;
109     }
110
111     /**
112      * Set the global object used to collect messages.
113      * @param aMessages the messages to log with
114      */

115     public final void setMessages(LocalizedMessages aMessages)
116     {
117         mMessages = aMessages;
118     }
119
120     /**
121      * Initialse the check. This is the time to verify that the check has
122      * everything required to perform it job.
123      */

124     public void init()
125     {
126     }
127
128     /**
129      * Destroy the check. It is being retired from service.
130      */

131     public void destroy()
132     {
133     }
134
135     /**
136      * Called before the starting to process a tree. Ideal place to initialise
137      * information that is to be collected whilst processing a tree.
138      * @param aRootAST the root of the tree
139      */

140     public void beginTree(DetailAST aRootAST)
141     {
142     }
143
144     /**
145      * Called after finished processing a tree. Ideal place to report on
146      * information collected whilst processing a tree.
147      * @param aRootAST the root of the tree
148      */

149     public void finishTree(DetailAST aRootAST)
150     {
151     }
152
153     /**
154      * Called to process a token.
155      * @param aAST the token to process
156      */

157     public void visitToken(DetailAST aAST)
158     {
159     }
160
161     /**
162      * Called after all the child nodes have been process.
163      * @param aAST the token leaving
164      */

165     public void leaveToken(DetailAST aAST)
166     {
167     }
168
169     /**
170      * Returns the lines associated with the tree.
171      * @return the file contents
172      */

173     public final String JavaDoc[] getLines()
174     {
175         return getFileContents().getLines();
176     }
177
178     /**
179      * Set the file contents associated with the tree.
180      * @param aContents the manager
181      */

182     public final void setFileContents(FileContents aContents)
183     {
184         mFileContents = aContents;
185     }
186
187     /**
188      * Returns the file contents associated with the tree.
189      * @return the file contents
190      */

191     public final FileContents getFileContents()
192     {
193         return mFileContents;
194     }
195
196     /**
197      * Set the class loader associated with the tree.
198      * @param aLoader the class loader
199      */

200     public final void setClassLoader(ClassLoader JavaDoc aLoader)
201     {
202         mLoader = aLoader;
203     }
204
205     /**
206      * Returns the class loader associated with the tree.
207      * @return the class loader
208      */

209     public final ClassLoader JavaDoc getClassLoader()
210     {
211         return mLoader;
212     }
213
214     /** @return the tab width to report errors with */
215     protected final int getTabWidth()
216     {
217         return mTabWidth;
218     }
219
220     /**
221      * Set the tab width to report errors with.
222      * @param aTabWidth an <code>int</code> value
223      */

224     public final void setTabWidth(int aTabWidth)
225     {
226         mTabWidth = aTabWidth;
227     }
228
229     /**
230      * Log an error message.
231      *
232      * @param aLine the line number where the error was found
233      * @param aKey the message that describes the error
234      * @param aArgs the details of the message
235      *
236      * @see java.text.MessageFormat
237      */

238     protected final void log(int aLine, String JavaDoc aKey, Object JavaDoc aArgs[])
239     {
240         mMessages.add(
241             new LocalizedMessage(
242                 aLine,
243                 getMessageBundle(),
244                 aKey,
245                 aArgs,
246                 getSeverityLevel(),
247                 getId(),
248                 this.getClass()));
249     }
250
251
252     /**
253      * Helper method to log a LocalizedMessage.
254      *
255      * @param aLineNo line number to associate with the message
256      * @param aColNo column number to associate with the message
257      * @param aKey key to locale message format
258      * @param aArgs arguments for message
259      */

260     protected final void log(int aLineNo, int aColNo,
261                              String JavaDoc aKey, Object JavaDoc[] aArgs)
262     {
263         final int col = 1 + Utils.lengthExpandedTabs(
264             getLines()[aLineNo - 1], aColNo, getTabWidth());
265         mMessages.add(
266             new LocalizedMessage(
267                 aLineNo,
268                 col,
269                 getMessageBundle(),
270                 aKey,
271                 aArgs,
272                 getSeverityLevel(),
273                 getId(),
274                 this.getClass()));
275     }
276 }
277
Popular Tags