KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > InspectorContextImpl


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.util.Collection JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
42  * @author Pavel Vlasov
43  * @version $Revision: 1.7 $
44  */

45 public class InspectorContextImpl extends InspectorContextBase {
46
47     private Collection JavaDoc violationFilters;
48
49     /**
50      * @param descriptor
51      * @param logger
52      * @param visitorStackSource
53      */

54     public InspectorContextImpl(
55             InspectorDescriptor descriptor,
56             Logger logger,
57             VisitorStackSource visitorStackSource,
58             SessionImpl session,
59             Collection JavaDoc violationFilters) {
60         super(descriptor, logger, visitorStackSource, session);
61         this.violationFilters=violationFilters;
62     }
63
64     /**
65      *
66      * @param source
67      * @param message
68      */

69     public void reportViolation(final SourceMarker source, final String JavaDoc 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 JavaDoc 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 JavaDoc name, double value) {
101         ResultsFactory.getThreadResults().addMetric(detach(source), name, value);
102     }
103
104     /**
105      * Report warning
106      * @param source
107      * @param message
108      */

109     public void warn(SourceMarker source, String JavaDoc 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     /**
120      * Report warning
121      * @param source
122      */

123     public void warn(SourceMarker source, Throwable JavaDoc th) {
124         Violation violation=new SimpleViolation(detach(source), th.toString(), descriptor);
125         ResultsFactory.getThreadResults().addWarning(violation);
126         // TODO better warning handling here
127
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     /**
136      * Creates a waiver for inspector with a given key
137      * @param inspectorKey
138      */

139     public void waive(Signed signed, final String JavaDoc inspectorKey) {
140         final String JavaDoc 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 JavaDoc signature=signed==null ? null : signed.getSignature();
145             final Set JavaDoc signatures=new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc getExpirationDate() {
179                     return null;
180                 }
181     
182                 public String JavaDoc getReason() {
183                     return descriptor.getWaiveReason(inspectorKey);
184                 }
185     
186                 public boolean isActive() {
187                     return active;
188                 }
189     
190                 public Collection JavaDoc 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