1 23 package org.hammurapi; 24 25 import java.lang.reflect.Method ; 26 import java.util.Collection ; 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 46 public class SimpleReviewEngine implements VisitorStackSource { 47 private DispatchingVisitor visitor; 48 49 public SimpleReviewEngine(Collection inspectors, final HammurapiTask task) { 50 VisitorExceptionSink esink=new VisitorExceptionSink() { 51 52 public void consume(DispatchingVisitor dispatcher, Object visitor, Method method, Object visitee, Exception 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 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 type; 89 90 private void log(String message) { 91 task.log(message, Project.MSG_INFO); 92 } 93 94 boolean isListeningFor(Object o) { 95 if (isEnabled) { 96 if (type==null) { 97 try { 98 type=o.getClass().getClassLoader().loadClass(task.getDebugType()); 99 } catch (ClassNotFoundException 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 target, Method 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 target, Method method, Object 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 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 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 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 target) { 153 log("WARNING: No invocations for type: "+target.getClass()); 154 } 155 156 public void onFilterRegistration(Method filter, Method 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 |