KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > QuickReviewEngine


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.Method JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31 import org.hammurapi.results.AggregatedResults;
32 import org.hammurapi.results.ResultsFactory;
33
34 import com.pavelvlasov.jsel.CompilationUnit;
35 import com.pavelvlasov.review.SourceMarker;
36 import com.pavelvlasov.util.DispatchingVisitor;
37 import com.pavelvlasov.util.OrderedTarget;
38 import com.pavelvlasov.util.VisitorExceptionSink;
39 import com.pavelvlasov.util.VisitorStack;
40 import com.pavelvlasov.util.VisitorStackSource;
41
42 /**
43  * Pool of review threads. Delegates review work to threads.
44  * @author Pavel Vlasov
45  * @version $Revision: 1.3 $
46  */

47 public class QuickReviewEngine implements VisitorStackSource {
48     private DispatchingVisitor visitor;
49     private QuickResultsCollector collector;
50     private QuickHammurapiTask task;
51
52     /**
53      * @param task
54      * @param rules
55      * @param poolSize If > 1 then multithreaded processing will be used.
56      * @throws SQLException
57      */

58     public QuickReviewEngine(Collection JavaDoc inspectors, final QuickHammurapiTask task, QuickResultsCollector collector) throws SQLException JavaDoc {
59         super();
60         VisitorExceptionSink esink=new VisitorExceptionSink() {
61
62             public void consume(
63                     final DispatchingVisitor dispatcher,
64                     final Object JavaDoc visitor,
65                     final Method JavaDoc method,
66                     final Object JavaDoc visitee,
67                     final Exception JavaDoc e) {
68                 task.log("WARN: Exception in "+visitee, Project.MSG_WARN);
69                 e.printStackTrace();
70                 
71                 AggregatedResults results=ResultsFactory.getThreadResults();
72                 if (task.failOnFirstException) {
73                     throw new BuildException("Cause: "+e, e);
74                 } else if (results==null || e instanceof HammurapiNonConsumableException) {
75                     task.setHadExceptions();
76                 } else {
77                     results.addWarning(new SimpleViolation(visitee instanceof SourceMarker ? (SourceMarker) visitee : null, "Exception "+e, null));
78                 }
79                 
80                 if (task.evictBadInspectors) {
81                     dispatcher.remove(visitor);
82                 }
83             }
84         };
85         visitor = new DispatchingVisitor(inspectors, esink, getListener(task));
86         this.collector=collector;
87         collector.getEngine().init();
88         this.task=task;
89     }
90     
91     public void review(CompilationUnit compilationUnit) throws SQLException JavaDoc {
92         if (!task.force) {
93             if (task.forceOnWarnings) {
94                 Integer JavaDoc id = collector.getEngine().getResultByNameSizeChecksumNoWarnings(
95                         compilationUnit.getPackage().getName(),
96                         compilationUnit.getName(),
97                         compilationUnit.getSize(),
98                         compilationUnit.getCheckSum());
99                 
100                 if (id!=null) {
101                     collector.getEngine().setUntouched(id.intValue());
102                     return;
103                 }
104             } else {
105                 Integer JavaDoc id = collector.getEngine().getResultByNameSizeChecksum(
106                         compilationUnit.getPackage().getName(),
107                         compilationUnit.getName(),
108                         compilationUnit.getSize(),
109                         compilationUnit.getCheckSum());
110                 
111                 if (id!=null) {
112                     collector.getEngine().setUntouched(id.intValue());
113                     return;
114                 }
115             }
116         }
117         
118         compilationUnit.accept(visitor);
119     }
120     
121     protected static DispatchingVisitor.Listener getListener(final QuickHammurapiTask task) {
122         if (task.getDebugType()==null) {
123             return null;
124         } else {
125             return new DispatchingVisitor.Listener() {
126                 private boolean isEnabled=true;
127                 private Class JavaDoc type;
128                 
129                 private void log(String JavaDoc message) {
130                     task.log(message, Project.MSG_INFO);
131                 }
132                 
133                 boolean isListeningFor(Object JavaDoc o) {
134                     if (isEnabled) {
135                         if (type==null) {
136                             try {
137                                 type=o.getClass().getClassLoader().loadClass(task.getDebugType());
138                             } catch (ClassNotFoundException JavaDoc e) {
139                                 task.log(e.toString(), Project.MSG_WARN);
140                                 isEnabled=false;
141                                 return false;
142                             }
143                         }
144                         return type.isAssignableFrom(o.getClass());
145                         
146                     } else {
147                         return false;
148                     }
149                 }
150                 
151                 public void onInvocationRegistration(Object JavaDoc target, Method JavaDoc method) {
152                     log("\tDispatch invocaton registration");
153                     log("\t\tTarget type: "+target.getClass());
154                     log("\t\tTarget method: "+method);
155                 }
156                 
157                 public void onInvocation(Object JavaDoc target, Method JavaDoc method, Object JavaDoc visitable) {
158                     if (isListeningFor(visitable)) {
159                         log("Dispatch invocation");
160                         log("\tTarget type: "+target.getClass());
161                         log("\tTarget method: "+method);
162                         log("\tVisitable type: "+visitable.getClass());
163                         log("\tVisitable: "+visitable);
164                     }
165                 }
166                 
167                 public void onVisit(Object JavaDoc target) {
168                     if (isListeningFor(target)) {
169                         log("Dispatch visit");
170                         log("\tVisitable type: "+target.getClass());
171                         log("\tVisitable: "+target);
172                     }
173                 }
174                 
175                 public void onLeave(Object JavaDoc target) {
176                     if (isListeningFor(target)) {
177                         log("Dispatch leave");
178                         log("\tVisitable type: "+target.getClass());
179                         log("\tVisitable: "+target);
180                     }
181                 }
182                 
183                 public void onTargetRegistration(Object JavaDoc target) {
184                     log("Dispatch type registration");
185                     log("\tTarget type: "+target.getClass());
186                     if (target instanceof OrderedTarget) {
187                         log("\tOrder: "+((OrderedTarget) target).getOrder());
188                     }
189                 }
190
191                 public void noInvocationsWarning(Object JavaDoc target) {
192                     log("WARNING: No invocations for type: "+target.getClass());
193                 }
194
195                 public void onFilterRegistration(Method JavaDoc filter, Method JavaDoc target) {
196                     log("\tFilter registration");
197                     log("\t\tFilter method: "+filter);
198                     log("\t\tTarget method: "+target);
199                 }
200            };
201         }
202     }
203
204     public VisitorStack getVisitorStack() {
205         return visitor.getVisitorStack();
206     }
207
208     public DispatchingVisitor getVisitor() {
209         return visitor;
210     }
211     
212 }
213
Popular Tags