Reviewer.java


Package: org.hammurapi

Results
Date2007/07/27
Codebase4901
Reviews8507
DPMO881
Sigma4.627

Metrics
NameNumberMinAvgMaxTotal
Class complexity224.0040.5057.0081.00
Code length350.008.6279.00302.00
File length1432.00432.00432.00432.00
Operation complexity351.002.2816.0080.00
Work order13.363.363.363.36

Violations
#LineColumnNameSeverityDescription
1231ER-0233Packages should begin with []
274135ER-0363Line is too long
38382ER-0363Line is too long
484101ER-0363Line is too long
59287ER-0363Line is too long
6102101ER-0363Line is too long
7107142ER-0363Line is too long
8116107ER-0363Line is too long
9122137ER-0363Line is too long
1013291ER-0363Line is too long
11134175ER-0363Line is too long
1213699ER-0363Line is too long
13143151ER-0363Line is too long
1415397ER-0363Line is too long
15154104ER-0363Line is too long
16157178ER-0363Line is too long
1715985ER-0363Line is too long
1817295ER-0363Line is too long
1919698ER-0363Line is too long
20205127ER-0363Line is too long
21206126ER-0363Line is too long
22212110ER-0363Line is too long
2321496ER-0363Line is too long
24217107ER-0363Line is too long
25225100ER-0363Line is too long
26240124ER-0363Line is too long
27250160ER-0363Line is too long
28252102ER-0363Line is too long
29257188ER-0363Line is too long
3027898ER-0363Line is too long
31287127ER-0363Line is too long
32288126ER-0363Line is too long
3329488ER-0363Line is too long
3429593ER-0363Line is too long
3529796ER-0363Line is too long
36300107ER-0363Line is too long
37313124ER-0363Line is too long
38323160ER-0363Line is too long
39325102ER-0363Line is too long
40330188ER-0363Line is too long
4134984ER-0363Line is too long
42352178ER-0363Line is too long
4338383ER-0363Line is too long
44388101ER-0363Line is too long
45390142ER-0363Line is too long
4639389ER-0363Line is too long
47405130ER-0363Line is too long
4840985ER-0363Line is too long
49621ER-0492Unify logging strategy - define individual logger for class
507029ER-0293Avoid hardwired numeric literals
51729ER-0492Unify logging strategy - define individual logger for class
527417ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
538817ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
549217ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
559273ER-0481Use BigDecimal instead of Float or Double for monetary values
5610548ER-0303Avoid hardwired string literals
57107133ER-1163Use StringBuffer for excessive String concatenation
58107127ER-1163Use StringBuffer for excessive String concatenation
59107108ER-1163Use StringBuffer for excessive String concatenation
60107104ER-1163Use StringBuffer for excessive String concatenation
6110787ER-1163Use StringBuffer for excessive String concatenation
6210783ER-1163Use StringBuffer for excessive String concatenation
6310748ER-0303Avoid hardwired string literals
6410784ER-0303Avoid hardwired string literals
65107105ER-0303Avoid hardwired string literals
66107128ER-0303Avoid hardwired string literals
6712048ER-0303Avoid hardwired string literals
68122133ER-1163Use StringBuffer for excessive String concatenation
69122127ER-1163Use StringBuffer for excessive String concatenation
70122108ER-1163Use StringBuffer for excessive String concatenation
71122104ER-1163Use StringBuffer for excessive String concatenation
7212287ER-1163Use StringBuffer for excessive String concatenation
7312283ER-1163Use StringBuffer for excessive String concatenation
7412248ER-0303Avoid hardwired string literals
7512284ER-0303Avoid hardwired string literals
76122105ER-0303Avoid hardwired string literals
77122128ER-0303Avoid hardwired string literals
7813117ER-0111Cyclomatic complexity (16) exceeds 12
79134159ER-1163Use StringBuffer for excessive String concatenation
80134146ER-1163Use StringBuffer for excessive String concatenation
81134122ER-0303Avoid hardwired string literals
82134160ER-0303Avoid hardwired string literals
83143140ER-1163Use StringBuffer for excessive String concatenation
84143133ER-1163Use StringBuffer for excessive String concatenation
85143127ER-1163Use StringBuffer for excessive String concatenation
86143112ER-1163Use StringBuffer for excessive String concatenation
8714374ER-0303Avoid hardwired string literals
88143113ER-0303Avoid hardwired string literals
89143134ER-0303Avoid hardwired string literals
9014941ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
9115341ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
9216741ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
9317141ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
9417541ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
9517941ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
961889ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
971889ER-1093It is good practice to call in any case super() in a constructor. (see also: UnnecessaryConstructorRule )
9819417ER-1042Use a Collection instead of arrays Object[]
9919825ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
100205106ER-0823Avoid using method parameter names that conflict with class member names
10120533ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
10221293ER-0303Avoid hardwired string literals
10321462ER-0303Avoid hardwired string literals
10421791ER-0303Avoid hardwired string literals
10522635ER-1032Catch-blocks should log the exeption with Log4J.error("Context String" , exception )
10624036ER-0303Avoid hardwired string literals
107240104ER-0303Avoid hardwired string literals
10824136ER-0303Avoid hardwired string literals
109250100ER-0823Avoid using method parameter names that conflict with class member names
11025049ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
11125286ER-0303Avoid hardwired string literals
112257165ER-0303Avoid hardwired string literals
11326319ER-1032Catch-blocks should log the exeption with Log4J.error("Context String" , exception )
1142709ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
1152709ER-1093It is good practice to call in any case super() in a constructor. (see also: UnnecessaryConstructorRule )
11627617ER-1042Use a Collection instead of arrays Object[]
11728025ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
118287106ER-0823Avoid using method parameter names that conflict with class member names
11928733ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
12029587ER-0303Avoid hardwired string literals
12129762ER-0303Avoid hardwired string literals
12230091ER-0303Avoid hardwired string literals
12331336ER-0303Avoid hardwired string literals
124313104ER-0303Avoid hardwired string literals
12531436ER-0303Avoid hardwired string literals
126323100ER-0823Avoid using method parameter names that conflict with class member names
12732349ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
12832586ER-0303Avoid hardwired string literals
129330165ER-0303Avoid hardwired string literals
13033619ER-1032Catch-blocks should log the exeption with Log4J.error("Context String" , exception )
1313499ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
13235419ER-1032Catch-blocks should log the exeption with Log4J.error("Context String" , exception )
1333799ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
13438219ER-1032Catch-blocks should log the exeption with Log4J.error("Context String" , exception )
13538354ER-0303Avoid hardwired string literals
13638754ER-0462Method declares too general exception types (Exception, Throwable) in throws clause
1373879ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
13838844ER-0303Avoid hardwired string literals
13938935ER-1111LOG4J is mandatory for all logging in ERC. System.out.println will be lost in a system console of our web/app server.
14039017ER-1132Unused private/local variables
14139081ER-0303Avoid hardwired string literals
14239625ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
14340025ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
14440140ER-0303Avoid hardwired string literals
14540425ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
14640551ER-1111LOG4J is mandatory for all logging in ERC. System.out.println will be lost in a system console of our web/app server.
147405106ER-1163Use StringBuffer for excessive String concatenation
148405101ER-1163Use StringBuffer for excessive String concatenation
14940552ER-0303Avoid hardwired string literals
150405102ER-0303Avoid hardwired string literals
15140825ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
15240951ER-1111LOG4J is mandatory for all logging in ERC. System.out.println will be lost in a system console of our web/app server.
15340952ER-0303Avoid hardwired string literals
15441225ER-1053Document all Interfaces and public methods. Use a Class header. Provide Javadoc
15541349ER-0303Avoid hardwired string literals
15642832ER-0823Avoid using method parameter names that conflict with class member names

JavaDoc
Reviewer
Reviewer.ReviewerInspectorContext

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 */

23package org.hammurapi;
24
25import java.io.File;
26import java.io.FileReader;
27import java.io.InputStream;
28import java.io.Reader;
29import java.lang.reflect.Method;
30import java.net.URL;
31import java.net.URLClassLoader;
32import java.sql.SQLException;
33import java.util.Collection;
34import java.util.Date;
35import java.util.HashSet;
36import java.util.Iterator;
37import java.util.Set;
38
39import org.hammurapi.results.Annotation;
40
41import com.pavelvlasov.config.ConfigurationException;
42import com.pavelvlasov.jsel.CompilationUnit;
43import com.pavelvlasov.jsel.JselException;
44import com.pavelvlasov.jsel.impl.CompilationUnitImpl;
45import com.pavelvlasov.logging.Logger;
46import com.pavelvlasov.persistence.CompositeStorage;
47import com.pavelvlasov.persistence.FileStorage;
48import com.pavelvlasov.persistence.MemoryStorage;
49import com.pavelvlasov.review.Signed;
50import com.pavelvlasov.review.SourceMarker;
51import com.pavelvlasov.util.ClassResourceLoader;
52import com.pavelvlasov.util.DispatchingVisitor;
53import com.pavelvlasov.util.VisitorExceptionSink;
54import com.pavelvlasov.util.VisitorStack;
55import com.pavelvlasov.util.VisitorStackSource;
56
57/**
58 * This class is to be used by IDE plugins
59 * @author Pavel Vlasov
60 * @version $Revision: 1.8 $
61 */

62public class Reviewer {
63
64 private Logger logger;
65 private InspectorSet inspectorSet;
66 private WaiverSet waiverSet=new WaiverSet();
67 private CompositeStorage storage=new CompositeStorage();
68 private DispatchingVisitor visitor;
69 private SessionImpl session=new SessionImpl();
70 private int tabSize=8;
71
72 private class ReviewerInspectorContext extends InspectorContextBase {
73
74 public ReviewerInspectorContext(InspectorDescriptor descriptor, Logger logger, VisitorStackSource visitorStackSource) {
75 super(descriptor, logger, visitorStackSource, session);
76 }
77
78 /**
79 *
80 * @param source
81 * @param message
82 */

83 public void reportViolation(SourceMarker source, String message) {
84 Violation violation=new SimpleViolation(detach(source), message, descriptor);
85 addViolation(violation);
86 }
87
88 public void annotate(Annotation annotation) {
89 addAnnotation(annotation);
90 }
91
92 public void addMetric(SourceMarker source, String name, double value) {
93 // Ignore
94
}
95
96 /**
97 * Report warning
98 * @param source
99 * @param message
100 */

101 public void warn(SourceMarker source, String message) {
102 Violation violation=new SimpleViolation(detach(source), message, descriptor);
103 addWarning(violation);
104 if (source==null) {
105 System.err.println("WARNING: "+message);
106 } else {
107 System.err.println("WARNING at "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" : "+message);
108 }
109 }
110
111 /**
112 * Report warning
113 * @param source
114 */

115 public void warn(SourceMarker source, Throwable th) {
116 Violation violation=new SimpleViolation(detach(source), th.toString(), descriptor);
117 addWarning(violation);
118 // TODO better warning handling here
119
if (source==null) {
120 System.err.println("WARNING: "+th);
121 } else {
122 System.err.println("WARNING at "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" : "+th);
123 }
124 th.printStackTrace();
125 }
126
127 /**
128 * Creates a waiver for inspector with a given key
129 * @param inspectorKey
130 */

131 public void waive(Signed signed, final String inspectorKey) {
132 final String iName=descriptor.getWaivedInspectorName(inspectorKey);
133 if (iName==null) {
134 warn(signed instanceof SourceMarker ? (SourceMarker) signed : null, descriptor.getName()+": Inspector with key '"+inspectorKey+"' not found.");
135 } else {
136 final String signature=signed==null ? null : signed.getSignature();
137 final Set signatures=new HashSet();
138 if (signature!=null) {
139 signatures.add(signature);
140 }
141
142 if (Reviewer.this.logger!=null) {
143 Reviewer.this.logger.debug(this, "Inspector "+getDescriptor().getName()+" autowaives "+iName+" at "+signature);
144 }
145
146 Waiver waiver=new Waiver() {
147 boolean active=true;
148
149 public String getInspectorName() {
150 return iName;
151 }
152
153 public boolean waive(Violation violation, boolean peek) {
154 if (iName.equals(violation.getDescriptor().getName())) {
155 if (signature==null) {
156 return true;
157 } else if (violation.getSource() instanceof Signed && signature.equals(((Signed) violation.getSource()).getSignature())) {
158 if (!peek) {
159 active=false;
160 }
161 return true;
162 }
163 }
164 return false;
165 }
166
167 public Date getExpirationDate() {
168 return null;
169 }
170
171 public String getReason() {
172 return descriptor.getWaiveReason(inspectorKey);
173 }
174
175 public boolean isActive() {
176 return active;
177 }
178
179 public Collection getSignatures() {
180 return signatures;
181 }
182 };
183 waiverSet.addWaiver(waiver, date);
184 }
185 }
186 }
187
188 public Reviewer(
189 final Logger logger,
190 boolean embeddedInspectors,
191 Collection inspectors,
192 Collection waivers) throws HammurapiException {
193 this.logger=logger;
194 final VisitorStack[] visitorStack={null};
195
196 final VisitorStackSource delegatingVisitorStackSource = new VisitorStackSource() {
197
198 public VisitorStack getVisitorStack() {
199 return visitorStack[0];
200 }
201 };
202
203 inspectorSet=new InspectorSet(
204 new InspectorContextFactory() {
205 public InspectorContext newContext(final InspectorDescriptor descriptor, final Logger logger) {
206 return new ReviewerInspectorContext(descriptor, logger, delegatingVisitorStackSource);
207 }
208 },
209 logger);
210
211 if (embeddedInspectors) {
212 InputStream inspectorStream=HammurapiTask.class.getResourceAsStream("inspectors.xml");
213 if (inspectorStream==null) {
214 throw new HammurapiException("Cannot load embedded inspectors");
215 }
216
217 DomInspectorSource source=new DomInspectorSource(inspectorStream, "Hammurapi.jar");
218 source.loadInspectors(inspectorSet);
219 }
220
221 if (inspectors!=null) {
222 Iterator it=inspectors.iterator();
223 while (it.hasNext()) {
224 try {
225 inspectorSet.addDescriptor((InspectorDescriptor) it.next());
226 } catch (ConfigurationException e) {
227 throw new HammurapiException(e);
228 }
229 }
230 }
231
232 if (waivers!=null) {
233 Date now=new Date();
234 Iterator it=waivers.iterator();
235 while (it.hasNext()) {
236 waiverSet.addWaiver((Waiver) it.next(), now);
237 }
238 }
239
240 storage.addStorage("file", new FileStorage(new File(System.getProperties().getProperty("java.io.tmpdir"))));
241 storage.addStorage("memory", new MemoryStorage());
242 session.setStorage(storage);
243
244 try {
245 Collection ic = inspectorSet.getInspectors();
246
247 visitor = new DispatchingVisitor(
248 ic,
249 new VisitorExceptionSink() {
250 public void consume(DispatchingVisitor dispatcher, Object visitor, Method method, Object visitee, Exception e) {
251 if (logger!=null) {
252 logger.warn(visitee, "Exception: "+e);
253 }
254
255 e.printStackTrace();
256
257 getRequest().onWarning(new SimpleViolation(visitee instanceof SourceMarker ? (SourceMarker) visitee : null, "Exception: "+e, null));
258 }
259 },
260 null);
261 session.setInspectors(inspectorSet);
262 visitorStack[0]=visitor.getVisitorStack();
263 } catch (ConfigurationException e) {
264 throw new HammurapiException(e);
265 }
266
267 session.setVisitor(visitor);
268 }
269
270 public Reviewer(
271 final Logger logger,
272 boolean embeddedInspectors,
273 InspectorSource inspectorSource,
274 WaiverSource waiverSource) throws HammurapiException {
275 this.logger=logger;
276 final VisitorStack[] visitorStack={null};
277
278 final VisitorStackSource delegatingVisitorStackSource = new VisitorStackSource() {
279
280 public VisitorStack getVisitorStack() {
281 return visitorStack[0];
282 }
283 };
284
285 inspectorSet=new InspectorSet(
286 new InspectorContextFactory() {
287 public InspectorContext newContext(final InspectorDescriptor descriptor, final Logger logger) {
288 return new ReviewerInspectorContext(descriptor, logger, delegatingVisitorStackSource);
289 }
290 },
291 logger);
292
293 if (embeddedInspectors) {
294 ClassResourceLoader crl=new ClassResourceLoader(TaskBase.class);
295 InputStream inspectorStream=crl.getResourceAsStream(null,null,"xml");
296 if (inspectorStream==null) {
297 throw new HammurapiException("Cannot load embedded inspectors");
298 }
299
300 DomInspectorSource source=new DomInspectorSource(inspectorStream, "Hammurapi.jar");
301 source.loadInspectors(inspectorSet);
302 }
303
304 if (inspectorSource!=null) {
305 inspectorSource.loadInspectors(inspectorSet);
306 }
307
308 if (waiverSource!=null) {
309 Date now=new Date();
310 waiverSource.loadWaivers(waiverSet, now);
311 }
312
313 storage.addStorage("file", new FileStorage(new File(System.getProperties().getProperty("java.io.tmpdir"))));
314 storage.addStorage("memory", new MemoryStorage());
315 session.setStorage(storage);
316
317 try {
318 Collection ic = inspectorSet.getInspectors();
319
320 visitor = new DispatchingVisitor(
321 ic,
322 new VisitorExceptionSink() {
323 public void consume(DispatchingVisitor dispatcher, Object visitor, Method method, Object visitee, Exception e) {
324 if (logger!=null) {
325 logger.warn(visitee, "Exception: "+e);
326 }
327
328 e.printStackTrace();
329
330 getRequest().onWarning(new SimpleViolation(visitee instanceof SourceMarker ? (SourceMarker) visitee : null, "Exception: "+e, null));
331 }
332 },
333 null);
334 session.setInspectors(inspectorSet);
335 visitorStack[0]=visitor.getVisitorStack();
336 } catch (ConfigurationException e) {
337 throw new HammurapiException(e);
338 }
339
340 session.setVisitor(visitor);
341 }
342
343 private ThreadLocal requestTL=new ThreadLocal();
344
345 private ReviewRequest getRequest() {
346 return (ReviewRequest) requestTL.get();
347 }
348
349 public void process(final ReviewRequest request) throws HammurapiException {
350 requestTL.set(request);
351 try {
352 CompilationUnit cu=new CompilationUnitImpl(request.getSource(), null, request.getRootDir(), request.getName(), tabSize, request.getClassLoader(), logger);
353 cu.accept(visitor);
354 } catch (JselException e) {
355 throw new HammurapiException(e);
356 } finally {
357 requestTL.set(null);
358 }
359 }
360
361 private void addViolation(Violation violation) {
362 getRequest().onViolation(violation);
363 }
364
365 /**
366 * @param annotation
367 */

368 private void addAnnotation(Annotation annotation) {
369 // Annotations are ignored
370
}
371
372 /**
373 * @param violation
374 */

375 private void addWarning(Violation violation) {
376 getRequest().onWarning(violation);
377 }
378
379 public void shutdown() throws HammurapiException {
380 try {
381 session.shutdown();
382 } catch (SQLException e) {
383 throw new HammurapiException("Cannot shutdown session", e);
384 }
385 }
386
387 public static void main(String[] arg) throws Exception {
388 final File source=new File("..\\UmlApi\\src\\com\\pavelvlasov\\uml\\Operation.java");
389 System.out.println(source.getAbsolutePath());
390 ClassLoader classLoader=new URLClassLoader(new URL[] { new File("..\\UmlApi\\bin").toURL()}, Reviewer.class.getClassLoader());
391
392 final Reader sr=new FileReader(source);
393 Reviewer reviewer=new Reviewer(null, true, (InspectorSource) null, null);
394 reviewer.process(new ReviewRequestBase(classLoader) {
395
396 public Reader getSource() {
397 return sr;
398 }
399
400 public String getName() {
401 return "com/pavelvlasov/uml/Operation.java";
402 }
403
404 public void onViolation(Violation violation) {
405 System.out.println("VIOLATION: "+violation.getDescriptor().getName()+": "+violation.getMessage());
406 }
407
408 public void onWarning(Violation warning) {
409 System.out.println("WARNING: "+warning.getMessage());
410 }
411
412 public File getRootDir() {
413 return new File("..\\UmlApi\\src");
414 }
415 });
416 }
417
418 /**
419 * @return Returns the tabSize.
420 */

421 public int getTabSize() {
422 return tabSize;
423 }
424
425 /**
426 * @param tabSize The tabSize to set.
427 */

428 public void setTabSize(int tabSize) {
429 this.tabSize = tabSize;
430 }
431}
432
433

Hammurapi 3 Copyright © 2004 Hammurapi Group. All Rights Reserved.