KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > SimpleReviewEngine


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

46 public class SimpleReviewEngine implements VisitorStackSource {
47     private DispatchingVisitor visitor;
48
49     public SimpleReviewEngine(Collection JavaDoc inspectors, final HammurapiTask task) {
50         VisitorExceptionSink esink=new VisitorExceptionSink() {
51
52             public void consume(DispatchingVisitor dispatcher, Object JavaDoc visitor, Method JavaDoc method, Object JavaDoc visitee, Exception JavaDoc e) {
53                 task.log("WARN: Exception in "+visitee, Project.MSG_WARN);
54                 e.printStackTrace();
55                 
56                 AggregatedResults results=ResultsFactory.getThreadResults();
57                 if (task.failOnFirstException) {
58                     throw new BuildException("Cause: "+e, e);
59                 } else if (results==null || e instanceof HammurapiNonConsumableException) {
60                     task.setHadExceptions();
61                 } else {
62                     results.addWarning(new SimpleViolation(visitee instanceof SourceMarker ? (SourceMarker) visitee : null, "Exception "+e, null));
63                 }
64                 
65                 if (task.evictBadInspectors) {
66                     dispatcher.remove(visitor);
67                     if (visitor instanceof Inspector) {
68                         String JavaDoc name=((Inspector) visitor).getContext().getDescriptor().getName();
69                         results.addWarning(new SimpleViolation(visitee instanceof SourceMarker ? (SourceMarker) visitee : null, "Inspector "+name+" threw "+e+" and has been disabled", null));
70                     }
71                 }
72             }
73         };
74         visitor = new DispatchingVisitor(inspectors, esink, getListener(task));
75     }
76     
77     public void review(Repository repository) {
78         repository.accept(visitor);
79     }
80     
81     protected static DispatchingVisitor.Listener getListener(final HammurapiTask task) {
82         if (task.getDebugType()==null) {
83             return null;
84         }
85         
86         return new DispatchingVisitor.Listener() {
87             private boolean isEnabled=true;
88             private Class JavaDoc type;
89             
90             private void log(String JavaDoc message) {
91                 task.log(message, Project.MSG_INFO);
92             }
93             
94             boolean isListeningFor(Object JavaDoc o) {
95                 if (isEnabled) {
96                     if (type==null) {
97                         try {
98                             type=o.getClass().getClassLoader().loadClass(task.getDebugType());
99                         } catch (ClassNotFoundException JavaDoc e) {
100                             task.log(e.toString(), Project.MSG_WARN);
101                             isEnabled=false;
102                             return false;
103                         }
104                     }
105                     return type.isAssignableFrom(o.getClass());
106                     
107                 }
108                 
109                 return false;
110             }
111             
112             public void onInvocationRegistration(Object JavaDoc target, Method JavaDoc method) {
113                 log("\tDispatch invocaton registration");
114                 log("\t\tTarget type: "+target.getClass());
115                 log("\t\tTarget method: "+method);
116             }
117             
118             public void onInvocation(Object JavaDoc target, Method JavaDoc method, Object JavaDoc visitable) {
119                 if (isListeningFor(visitable)) {
120                     log("Dispatch invocation");
121                     log("\tTarget type: "+target.getClass());
122                     log("\tTarget method: "+method);
123                     log("\tVisitable type: "+visitable.getClass());
124                     log("\tVisitable: "+visitable);
125                 }
126             }
127             
128             public void onVisit(Object JavaDoc target) {
129                 if (isListeningFor(target)) {
130                     log("Dispatch visit");
131                     log("\tVisitable type: "+target.getClass());
132                     log("\tVisitable: "+target);
133                 }
134             }
135             
136             public void onLeave(Object JavaDoc target) {
137                 if (isListeningFor(target)) {
138                     log("Dispatch leave");
139                     log("\tVisitable type: "+target.getClass());
140                     log("\tVisitable: "+target);
141                 }
142             }
143             
144             public void onTargetRegistration(Object JavaDoc target) {
145                 log("Dispatch type registration");
146                 log("\tTarget type: "+target.getClass());
147                 if (target instanceof OrderedTarget) {
148                     log("\tOrder: "+((OrderedTarget) target).getOrder());
149                 }
150             }
151
152             public void noInvocationsWarning(Object JavaDoc target) {
153                 log("WARNING: No invocations for type: "+target.getClass());
154             }
155
156             public void onFilterRegistration(Method JavaDoc filter, Method JavaDoc target) {
157                 log("\tFilter registration");
158                 log("\t\tFilter method: "+filter);
159                 log("\t\tTarget method: "+target);
160             }
161    };
162     }
163
164     public VisitorStack getVisitorStack() {
165         return visitor.getVisitorStack();
166     }
167
168     public DispatchingVisitor getVisitor() {
169         return visitor;
170     }
171 }
172
Popular Tags