Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/api/Check.java


1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2003  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;
22  import java.util.Set;
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 mTokens = new HashSet();
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      /** current class loader */
51      private ClassLoader mLoader =
52          Thread.currentThread().getContextClassLoader();
53  
54      /**
55       * Returns the default token a check is interested in. Only used if the
56       * configuration for a check does not define the tokens.
57       * @return the default tokens
58       * @see TokenTypes
59       */
60      public abstract int[] getDefaultTokens();
61  
62      /**
63       * The configurable token set.
64       * Used to protect Checks against malicious users who specify an
65       * unacceptable token set in the configuration file.
66       * The default implementation returns the check's default tokens.
67       * @return the token set this check is designed for.
68       * @see TokenTypes
69       */
70      public int[] getAcceptableTokens()
71      {
72          final int[] defaultTokens = getDefaultTokens();
73          final int[] copy = new int[defaultTokens.length];
74          System.arraycopy(defaultTokens, 0, copy, 0, defaultTokens.length);
75          return copy;
76      }
77  
78      /**
79       * The tokens that this check must be registered for.
80       * @return the token set this must be registered for.
81       * @see TokenTypes
82       */
83      public int[] getRequiredTokens()
84      {
85          return new int[] {};
86      }
87  
88      /**
89       * Adds a set of tokens the check is interested in.
90       * @param aStrRep the string representation of the tokens interested in
91       */
92      public final void setTokens(String[] aStrRep)
93      {
94          for (int i = 0; i < aStrRep.length; i++) {
95              String s = aStrRep[i];
96              mTokens.add(s);
97          }
98      }
99  
100     /**
101      * Returns the tokens registered for the check.
102      * @return the set of token names
103      */
104     public final Set getTokenNames()
105     {
106         return mTokens;
107     }
108 
109     /**
110      * Set the global object used to collect messages.
111      * @param aMessages the messages to log with
112      */
113     public final void setMessages(LocalizedMessages aMessages)
114     {
115         mMessages = aMessages;
116     }
117 
118     /**
119      * Initialse the check. This is the time to verify that the check has
120      * everything required to perform it job.
121      */
122     public void init()
123     {
124     }
125 
126     /**
127      * Destroy the check. It is being retired from service.
128      */
129     public void destroy()
130     {
131     }
132 
133     /**
134      * Called before the starting to process a tree. Ideal place to initialise
135      * information that is to be collected whilst processing a tree.
136      * @param aRootAST the root of the tree
137      */
138     public void beginTree(DetailAST aRootAST)
139     {
140     }
141 
142     /**
143      * Called after finished processing a tree. Ideal place to report on
144      * information collected whilst processing a tree.
145      * @param aRootAST the root of the tree
146      */
147     public void finishTree(DetailAST aRootAST)
148     {
149     }
150 
151     /**
152      * Called to process a token.
153      * @param aAST the token to process
154      */
155     public void visitToken(DetailAST aAST)
156     {
157     }
158 
159     /**
160      * Called after all the child nodes have been process.
161      * @param aAST the token leaving
162      */
163     public void leaveToken(DetailAST aAST)
164     {
165     }
166 
167     /**
168      * Returns the lines associated with the tree.
169      * @return the file contents
170      */
171     public final String[] getLines()
172     {
173         return getFileContents().getLines();
174     }
175 
176     /**
177      * Set the file contents associated with the tree.
178      * @param aContents the manager
179      */
180     public final void setFileContents(FileContents aContents)
181     {
182         mFileContents = aContents;
183     }
184 
185     /**
186      * Returns the file contents associated with the tree.
187      * @return the file contents
188      */
189     public final FileContents getFileContents()
190     {
191         return mFileContents;
192     }
193 
194     /**
195      * Set the class loader associated with the tree.
196      * @param aLoader the class loader
197      */
198     public final void setClassLoader(ClassLoader aLoader)
199     {
200         mLoader = aLoader;
201     }
202 
203     /**
204      * Returns the class loader associated with the tree.
205      * @return the class loader
206      */
207     public final ClassLoader getClassLoader()
208     {
209         return mLoader;
210     }
211 
212     /** @return the tab width to report errors with */
213     protected final int getTabWidth()
214     {
215         return mTabWidth;
216     }
217 
218     /**
219      * Set the tab width to report errors with.
220      * @param aTabWidth an <code>int</code> value
221      */
222     public final void setTabWidth(int aTabWidth)
223     {
224         mTabWidth = aTabWidth;
225     }
226 
227     /**
228      * Log an error message.
229      *
230      * @param aLine the line number where the error was found
231      * @param aKey the message that describes the error
232      * @param aArgs the details of the message
233      *
234      * @see java.text.MessageFormat
235      */
236     protected final void log(int aLine, String aKey, Object aArgs[])
237     {
238         mMessages.add(
239             new LocalizedMessage(
240                 aLine,
241                 getMessageBundle(),
242                 aKey,
243                 aArgs,
244                 getSeverityLevel(),
245                 this.getClass()));
246     }
247 
248 
249     /**
250      * Helper method to log a LocalizedMessage.
251      *
252      * @param aLineNo line number to associate with the message
253      * @param aColNo column number to associate with the message
254      * @param aKey key to locale message format
255      * @param aArgs arguments for message
256      */
257     protected final void log(int aLineNo, int aColNo,
258                              String aKey, Object[] aArgs)
259     {
260         final int col = 1 + Utils.lengthExpandedTabs(
261             getLines()[aLineNo - 1], aColNo, getTabWidth());
262         mMessages.add(
263             new LocalizedMessage(
264                 aLineNo,
265                 col,
266                 getMessageBundle(),
267                 aKey,
268                 aArgs,
269                 getSeverityLevel(),
270                 this.getClass()));
271     }
272 }