KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > bcel > AbstractCheckVisitor


1 //Tested with BCEL-5.1
2
//http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3

4 package com.puppycrawl.tools.checkstyle.bcel;
5
6 import java.util.Set JavaDoc;
7
8 import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter;
9 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
10 import com.puppycrawl.tools.checkstyle.api.LocalizedMessages;
11 import java.util.HashMap JavaDoc;
12 import org.apache.bcel.classfile.JavaClass;
13
14 /**
15  * Abstract class for checks with visitors.
16  * @author Rick Giles
17  */

18 //TODO: Refactor with class Check
19
public abstract class AbstractCheckVisitor
20     extends AbstractViolationReporter
21     implements IObjectSetVisitor,
22     IDeepVisitor
23 {
24     /** the object for collecting messages. */
25     private HashMap JavaDoc mMessageMap; // <String fileName, LocalizedMessages msg>
26
/** Filename for when no file can be found */
27     private String JavaDoc NO_FILE = "File not available";
28
29     /** @see com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor */
30     public org.apache.bcel.classfile.Visitor getClassFileVisitor()
31     {
32         return new EmptyClassFileVisitor();
33     }
34
35     /**
36      * @see com.puppycrawl.tools.checkstyle.bcel.IDeepVisitor
37      */

38     public org.apache.bcel.generic.Visitor getGenericVisitor()
39     {
40         return new EmptyGenericVisitor();
41     }
42
43      /**
44      * Initialse the check. This is the time to verify that the check has
45      * everything required to perform it job.
46      */

47     public void init()
48     {
49     }
50
51     /**
52      * Destroy the check. It is being retired from service.
53      */

54     public void destroy()
55     {
56     }
57
58     /**
59      * Set the global object used to collect messages.
60      * @param aMessages the messages to log with
61      */

62     public final void setMessageMap(HashMap JavaDoc aMessageMap)
63     {
64         mMessageMap = aMessageMap;
65     }
66
67     /**
68      * Log an error message.
69      *
70      * @param aLine the line number where the error was found
71      * @param aKey the message that describes the error
72      * @param aArgs the details of the message
73      *
74      * @see java.text.MessageFormat
75      */

76     protected final void log(JavaClass javaClass, int aLine, String JavaDoc aKey, Object JavaDoc aArgs[])
77     {
78         // Should this be on the .java file instead of the .class file?
79
final String JavaDoc file = javaClass.getFileName();
80         log(file, aLine, aKey, aArgs);
81     }
82
83     /**
84      * Log an error message.
85      *
86      * @param aLine the line number where the error was found
87      * @param aKey the message that describes the error
88      * @param aArgs the details of the message
89      *
90      * @see java.text.MessageFormat
91      */

92     private final void log(String JavaDoc fileName, int aLine, String JavaDoc aKey, Object JavaDoc aArgs[])
93     {
94         LocalizedMessages localizedMessages = (LocalizedMessages) mMessageMap.get(fileName);
95         if (localizedMessages == null) {
96             localizedMessages = new LocalizedMessages();
97             mMessageMap.put(fileName, localizedMessages);
98         }
99         localizedMessages.add(
100             new LocalizedMessage(
101                 aLine,
102                 getMessageBundle(),
103                 aKey,
104                 aArgs,
105                 getSeverityLevel(),
106                 this.getClass()));
107     }
108
109     /**
110      * Log an error message.
111      *
112      * @param aLine the line number where the error was found
113      * @param aKey the message that describes the error
114      * @param aArgs the details of the message
115      *
116      * @see java.text.MessageFormat
117      */

118     protected final void log(int aLine, String JavaDoc aKey, Object JavaDoc aArgs[])
119     {
120         log(NO_FILE, aLine, aKey, aArgs);
121     }
122
123     /**
124      * @see com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
125      */

126     protected void log(int aLine, int aCol, String JavaDoc aKey, Object JavaDoc[] aArgs)
127     {
128           // Ignore the column, it is not relevant for .class files
129
log(aLine, aKey, aArgs);
130     }
131
132     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
133     public void visitObject(Object JavaDoc aObject)
134     {
135     }
136
137     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
138     public void leaveObject(Object JavaDoc aObject)
139     {
140     }
141
142     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
143     public void visitSet(Set JavaDoc aJavaClassDefs)
144     {
145     }
146
147     /** @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor */
148     public void leaveSet(Set JavaDoc aJavaClassDefs)
149     {
150     }
151
152     /**
153      * Gets the deep BCEL visitor.
154      * @return the deep BCEL visitor for this check.
155      */

156     public abstract IDeepVisitor getVisitor();
157 }
158
Popular Tags