KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > InspectorContextBase


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import com.pavelvlasov.jsel.CompilationUnit;
35 import com.pavelvlasov.jsel.Package;
36 import com.pavelvlasov.logging.Logger;
37 import com.pavelvlasov.review.SourceMarker;
38 import com.pavelvlasov.util.VisitorStack;
39 import com.pavelvlasov.util.VisitorStackSource;
40 import com.pavelvlasov.wrap.WrapperHandler;
41
42 /**
43  * @author Pavel Vlasov
44  * @version $Revision: 1.7 $
45  */

46 public abstract class InspectorContextBase implements InspectorContext {
47     protected InspectorDescriptor descriptor;
48     public final static String JavaDoc GENERIC_MESSAGE="SimpleViolation. Message not found";
49     protected Logger logger;
50     private VisitorStackSource visitorStackSource;
51     private SessionImpl session;
52     
53     public InspectorDescriptor getDescriptor() {
54         return descriptor;
55     }
56     
57     /**
58      * Reports violation with a message from descriptor
59      * @param source
60      */

61     public void reportViolation(SourceMarker source) {
62         reportViolation(source, descriptor.getMessage());
63     }
64     
65     /**
66      * Reports violation with a message from descriptor
67      * @param source
68      */

69     public void reportViolationEx(SourceMarker source, String JavaDoc messageKey) {
70         reportViolation(source, descriptor.getMessage(messageKey));
71     }
72     
73     /**
74      * @param source
75      */

76     public SourceMarker detach(final SourceMarker source) {
77         if (source==null) {
78             return null;
79         }
80         
81         Class JavaDoc sourceClass=source.getClass();
82         // TODO - real detach through findBySignature
83
final String JavaDoc[] sourceUrl = {source.getSourceURL()};
84         List JavaDoc stack = getVisitorStack().getStack(CompilationUnit.class);
85         if (!stack.isEmpty()) {
86             CompilationUnit cu=(CompilationUnit) stack.get(0);
87             Package JavaDoc pkg=cu.getPackage();
88             if (pkg.getName().length()==0) {
89                 sourceUrl[0]=cu.getName();
90             } else {
91                 sourceUrl[0]=pkg.getName().replace('.', '/')+'/'+cu.getName();
92             }
93         }
94         
95         return (SourceMarker) Proxy.newProxyInstance(
96                 sourceClass.getClassLoader(),
97                 WrapperHandler.getClassInterfaces(sourceClass),
98                 new InvocationHandler JavaDoc() {
99
100                     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
101                         if (SourceMarker.class.getMethod("getSourceURL", null).equals(method)) {
102                             return sourceUrl[0];
103                         }
104                         
105                         return method.invoke(source, args);
106                     }
107                     
108                 });
109     }
110     
111     /**
112      * Formats message taken from InspectorDescriptor with parameters.
113      * @param source
114      * @param params
115      */

116     public void reportViolation(SourceMarker source, Object JavaDoc[] params) {
117         String JavaDoc message = descriptor.getMessage();
118         if (message==null) {
119             warn(source, "Message not found for inspector "+getDescriptor().getName());
120             message=GENERIC_MESSAGE;
121         }
122         reportViolation(source, MessageFormat.format(message, params));
123     }
124     
125     /**
126      * Formats message taken from InspectorDescriptor with parameters.
127      * @param source
128      * @param params
129      */

130     public void reportViolationEx(SourceMarker source, Object JavaDoc[] params, String JavaDoc messageKey) {
131         String JavaDoc message = descriptor.getMessage(messageKey);
132         if (message==null) {
133             warn(source, "Message with key '"+messageKey+"' not found for inspector "+getDescriptor().getName());
134             message=GENERIC_MESSAGE;
135         }
136         reportViolation(source, MessageFormat.format(message+" for key '"+messageKey+"'", params));
137     }
138     
139     /**
140      * @param descriptor
141      * @param session
142      */

143     public InspectorContextBase(
144             InspectorDescriptor descriptor,
145             Logger logger,
146             VisitorStackSource visitorStackSource, SessionImpl session) {
147         super();
148         this.descriptor = descriptor;
149         this.logger=logger;
150         this.visitorStackSource=visitorStackSource;
151         this.session=session;
152         if (session!=null) {
153             session.addContext(descriptor.getName(), this);
154         }
155     }
156     
157     /**
158      * Outputs a message to the log
159      * @param source
160      * @param message
161      */

162     public void info(SourceMarker source, String JavaDoc message) {
163         if (logger!=null) {
164             logger.info(this, loggerMessage(source, message));
165         }
166     }
167     
168     /**
169      * Outputs a message to the log
170      * @param source
171      * @param message
172      */

173     public void debug(SourceMarker source, String JavaDoc message) {
174         if (logger!=null) {
175             logger.debug(this, loggerMessage(source, message));
176         }
177     }
178     
179     /**
180      * Outputs a message to the log
181      * @param source
182      * @param message
183      */

184     public void verbose(SourceMarker source, String JavaDoc message) {
185         if (logger!=null) {
186             logger.verbose(this, loggerMessage(source, message));
187         }
188     }
189     
190     /**
191      * @param source
192      * @param message
193      * @return formatted message
194      */

195     private String JavaDoc loggerMessage(SourceMarker source, String JavaDoc message) {
196         return descriptor.getName()+" "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" - "+message;
197     }
198
199     protected static final Date JavaDoc date = new Date JavaDoc();
200     
201     public VisitorStack getVisitorStack() {
202         return visitorStackSource==null ? null : visitorStackSource.getVisitorStack();
203     }
204     
205     private Map JavaDoc attributes=new HashMap JavaDoc();
206     
207     public void setAttribute(Object JavaDoc key, Object JavaDoc value) {
208         attributes.put(key, value);
209     }
210     
211     public Object JavaDoc getAttribute(Object JavaDoc key) {
212         return attributes.get(key);
213     }
214     
215     public Object JavaDoc removeAttribute(Object JavaDoc key) {
216         return attributes.remove(key);
217     }
218     
219     public Session getSession() {
220         return session;
221     }
222 }
223
Popular Tags