1 23 package org.hammurapi; 24 25 import java.lang.reflect.Method ; 26 import java.sql.SQLException ; 27 import java.util.Collection ; 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 47 public class QuickReviewEngine implements VisitorStackSource { 48 private DispatchingVisitor visitor; 49 private QuickResultsCollector collector; 50 private QuickHammurapiTask task; 51 52 58 public QuickReviewEngine(Collection inspectors, final QuickHammurapiTask task, QuickResultsCollector collector) throws SQLException { 59 super(); 60 VisitorExceptionSink esink=new VisitorExceptionSink() { 61 62 public void consume( 63 final DispatchingVisitor dispatcher, 64 final Object visitor, 65 final Method method, 66 final Object visitee, 67 final Exception 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 { 92 if (!task.force) { 93 if (task.forceOnWarnings) { 94 Integer 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 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 type; 128 129 private void log(String message) { 130 task.log(message, Project.MSG_INFO); 131 } 132 133 boolean isListeningFor(Object o) { 134 if (isEnabled) { 135 if (type==null) { 136 try { 137 type=o.getClass().getClassLoader().loadClass(task.getDebugType()); 138 } catch (ClassNotFoundException 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 target, Method 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 target, Method method, Object 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 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 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 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 target) { 192 log("WARNING: No invocations for type: "+target.getClass()); 193 } 194 195 public void onFilterRegistration(Method filter, Method 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 |