KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > AbstractUsageCheck


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.usage;
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.Utils;
25 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.ASTManager;
26 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.ClassManager;
27 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.Definition;
28 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.SymTabAST;
29 import com.puppycrawl.tools.checkstyle.checks.usage.transmogrify.
30
    SymbolTableException;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34 import java.util.regex.PatternSyntaxException JavaDoc;
35
36 import org.apache.commons.beanutils.ConversionException;
37
38 /**
39  * Performs a usage check for fields, methods, parameters, variables.
40  * @author Rick Giles
41  */

42 public abstract class AbstractUsageCheck
43     extends Check
44 {
45     /** the regexp to match against */
46     private Pattern JavaDoc mRegexp;
47     /** the format string of the regexp */
48     private String JavaDoc mIgnoreFormat;
49
50     /**
51      * Constructs an <code>AbstractUsageCheck</code>.
52      */

53     public AbstractUsageCheck()
54     {
55         setIgnoreFormat("^$");
56     }
57
58     /**
59      * Set the ignore format to the specified regular expression.
60      * @param aFormat a <code>String</code> value
61      * @throws ConversionException unable to parse aFormat
62      */

63     public void setIgnoreFormat(String JavaDoc aFormat)
64         throws ConversionException
65     {
66         try {
67             mRegexp = Utils.getPattern(aFormat);
68             mIgnoreFormat = aFormat;
69         }
70         catch (PatternSyntaxException JavaDoc e) {
71             throw new ConversionException("unable to parse " + aFormat, e);
72         }
73     }
74
75     /** @return the regexp to match against */
76     public Pattern JavaDoc getRegexp()
77     {
78         return mRegexp;
79     }
80
81     /** @return the regexp format */
82     public String JavaDoc getIgnoreFormat()
83     {
84         return mIgnoreFormat;
85     }
86
87     /** @see com.puppycrawl.tools.checkstyle.api.Check */
88     public void beginTree(DetailAST aRootAST)
89     {
90         // use my class loader
91
ClassManager.setClassLoader(getClassLoader());
92
93         final String JavaDoc fileName = getFileContents().getFilename();
94         getASTManager().addTree(fileName, aRootAST);
95     }
96
97     /** @see com.puppycrawl.tools.checkstyle.api.Check */
98     public void visitToken(DetailAST aAST)
99     {
100         if (mustCheckReferenceCount(aAST)) {
101             final DetailAST nameAST = aAST.findFirstToken(TokenTypes.IDENT);
102             Pattern JavaDoc regexp = getRegexp();
103             if ((regexp == null)
104                 || !regexp.matcher(nameAST.getText()).find())
105             {
106                 getASTManager().registerCheckNode(this, nameAST);
107             }
108         }
109     }
110
111     /** @see com.puppycrawl.tools.checkstyle.api.Check */
112     public void finishTree(DetailAST aAST)
113     {
114         if (aAST == null) {
115             // we have nothing to check
116
return;
117         }
118         try {
119             final Set JavaDoc nodes = getASTManager().getCheckNodes(this);
120             if (nodes != null) {
121                 applyTo(nodes);
122             }
123         }
124         catch (SymbolTableException ste) {
125             logError(ste);
126         }
127         getASTManager().removeCheck(this);
128     }
129
130     /**
131      * Logs an exception.
132      * @param aException the exception to log.
133      */

134     public void logError(Exception JavaDoc aException)
135     {
136         log(0, "general.exception", new String JavaDoc[] {aException.getMessage()});
137         Utils.getExceptionLogger().debug("An exception occured.", aException);
138     }
139
140     /**
141      * Determines the reference count for a DetailAST.
142      * @param aAST the DetailAST to count.
143      * @return the number of references to aAST.
144      */

145     private int getReferenceCount(DetailAST aAST)
146     {
147         final SymTabAST ident = getASTManager().get(aAST);
148         if (ident == null) {
149             return 0;
150         }
151         final Definition definition =
152             (Definition) ident.getDefinition();
153         if (definition != null) {
154             return definition.getNumReferences();
155         }
156
157         return 0;
158     }
159
160     /**
161      * Returns the key for the Checkstyle error message.
162      * @return the key for the Checkstyle error message.
163      */

164     public abstract String JavaDoc getErrorKey();
165
166     /**
167      * Determines whether the reference count of an aAST is required.
168      * @param aAST the node to check.
169      * @return true if the reference count of aAST is required.
170      */

171     public abstract boolean mustCheckReferenceCount(DetailAST aAST);
172
173     /**
174      * Applies this check to a set of nodes.
175      * @param aNodes the nodes to check.
176      */

177     public void applyTo(Set JavaDoc aNodes)
178     {
179         final Iterator JavaDoc it = aNodes.iterator();
180         while (it.hasNext()) {
181             final DetailAST nameAST = (DetailAST) it.next();
182             if (getReferenceCount(nameAST) == 1) {
183                 log(
184                     nameAST.getLineNo(),
185                     nameAST.getColumnNo(),
186                     getErrorKey(),
187                     nameAST.getText());
188             }
189         }
190     }
191
192     /**
193      * Gets the manager for AST nodes.
194      * @return the AST manager.
195      */

196     protected ASTManager getASTManager()
197     {
198         return ASTManager.getInstance();
199     }
200 }
201
Popular Tags