1 23 package org.hammurapi; 24 25 import java.util.Collection ; 26 import java.util.Date ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import org.hammurapi.results.Annotation; 32 import org.hammurapi.results.DetailedResults; 33 import org.hammurapi.results.ResultsFactory; 34 35 import com.pavelvlasov.logging.Logger; 36 import com.pavelvlasov.review.Signed; 37 import com.pavelvlasov.review.SimpleSourceMarker; 38 import com.pavelvlasov.review.SourceMarker; 39 import com.pavelvlasov.util.VisitorStackSource; 40 41 45 public class InspectorContextImpl extends InspectorContextBase { 46 47 private Collection violationFilters; 48 49 54 public InspectorContextImpl( 55 InspectorDescriptor descriptor, 56 Logger logger, 57 VisitorStackSource visitorStackSource, 58 SessionImpl session, 59 Collection violationFilters) { 60 super(descriptor, logger, visitorStackSource, session); 61 this.violationFilters=violationFilters; 62 } 63 64 69 public void reportViolation(final SourceMarker source, final String message) { 70 try { 71 final DetailedResults threadResults = ResultsFactory.getThreadResults(); 72 final SourceMarker detachedSource = detach(source); 73 74 ResultsFactory.getInstance().execute( 75 new ResultsFactory.Task() { 76 77 public void execute() throws HammurapiException { 78 SimpleViolation violation = new SimpleViolation(detachedSource, message, descriptor); 79 80 Iterator filterIterator = violationFilters==null ? null : violationFilters.iterator(); 81 while (filterIterator!=null && filterIterator.hasNext()) { 82 if (!((ViolationFilter) filterIterator.next()).accept(violation)) { 83 return; 84 } 85 } 86 87 threadResults.addViolation(violation); 88 } 89 90 }); 91 } catch (HammurapiException e) { 92 warn(source, e); 93 } 94 } 95 96 public void annotate(Annotation annotation) { 97 ResultsFactory.getThreadResults().addAnnotation(annotation); 98 } 99 100 public void addMetric(SourceMarker source, String name, double value) { 101 ResultsFactory.getThreadResults().addMetric(detach(source), name, value); 102 } 103 104 109 public void warn(SourceMarker source, String message) { 110 Violation violation=new SimpleViolation(source==null ? null : new SimpleSourceMarker(source), message, descriptor); 111 ResultsFactory.getThreadResults().addWarning(violation); 112 if (source==null) { 113 System.err.println("WARNING: "+message); 114 } else { 115 System.err.println("WARNING at "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" : "+message); 116 } 117 } 118 119 123 public void warn(SourceMarker source, Throwable th) { 124 Violation violation=new SimpleViolation(detach(source), th.toString(), descriptor); 125 ResultsFactory.getThreadResults().addWarning(violation); 126 if (source==null) { 128 System.err.println("WARNING: "+th); 129 } else { 130 System.err.println("WARNING at "+source.getSourceURL()+" "+source.getLine()+":"+source.getColumn()+" : "+th); 131 } 132 th.printStackTrace(); 133 } 134 135 139 public void waive(Signed signed, final String inspectorKey) { 140 final String iName=descriptor.getWaivedInspectorName(inspectorKey); 141 if (iName==null) { 142 warn(signed instanceof SourceMarker ? (SourceMarker) signed : null, descriptor.getName()+": Inspector with key '"+inspectorKey+"' not found."); 143 } else { 144 final String signature=signed==null ? null : signed.getSignature(); 145 final Set signatures=new HashSet (); 146 if (signature!=null) { 147 signatures.add(signature); 148 } 149 150 if (logger!=null) { 151 logger.debug(this, "Inspector "+getDescriptor().getName()+" autowaives "+iName+" at "+signature); 152 } 153 154 155 final Waiver waiver=new Waiver() { 156 boolean active=true; 157 158 public String getInspectorName() { 159 return iName; 160 } 161 162 public boolean waive(Violation violation, boolean peek) { 163 if (iName.equals(violation.getDescriptor().getName())) { 164 if (signature==null) { 165 return true; 166 } 167 168 if (violation.getSource() instanceof Signed && signature.equals(((Signed) violation.getSource()).getSignature())) { 169 if (!peek) { 170 active=false; 171 } 172 return true; 173 } 174 } 175 return false; 176 } 177 178 public Date getExpirationDate() { 179 return null; 180 } 181 182 public String getReason() { 183 return descriptor.getWaiveReason(inspectorKey); 184 } 185 186 public boolean isActive() { 187 return active; 188 } 189 190 public Collection getSignatures() { 191 return signatures; 192 } 193 }; 194 195 final DetailedResults threadResults = ResultsFactory.getThreadResults(); 196 try { 197 ResultsFactory.getInstance().execute( 198 new ResultsFactory.Task() { 199 200 public void execute() { 201 threadResults.getWaiverSet().addWaiver(waiver, date); 202 } 203 204 }); 205 } catch (HammurapiException e) { 206 throw new HammurapiRuntimeException(e); 207 } 208 } 209 } 210 } 211 | Popular Tags |