KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > props > WarningPropertySet


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package edu.umd.cs.findbugs.props;
20
21
22 import edu.umd.cs.findbugs.*;
23 import java.util.*;
24
25 /**
26  * A Set of WarningProperty objects, each with an optional attribute Object.
27  * A WarningPropertySet is useful for collecting heuristics to use in
28  * the determination of whether or not a warning is a false positive,
29  * or what the warning's priority should be.
30  *
31  * @author David Hovemeyer
32  */

33 public class WarningPropertySet implements Cloneable JavaDoc {
34     private Map<WarningProperty, Object JavaDoc> map;
35
36     @Override JavaDoc
37          public String JavaDoc toString() {
38         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{ ");
39         for (Map.Entry<WarningProperty, Object JavaDoc> entry : map.entrySet()) {
40             WarningProperty prop = entry.getKey();
41             Object JavaDoc attribute = entry.getValue();
42             buf.append(" ");
43             buf.append(prop.getPriorityAdjustment());
44             buf.append("\t");
45             buf.append(prop.getName());
46             buf.append("\t");
47             buf.append(attribute);
48             buf.append("\n");
49         }
50         buf.append("}\n");
51         return buf.toString();
52         }
53     
54     /**
55      * Constructor
56      * Creates empty object.
57      */

58     public WarningPropertySet() {
59         this.map = new HashMap<WarningProperty, Object JavaDoc>();
60     }
61
62     //@Override
63
@Override JavaDoc
64          public Object JavaDoc clone() {
65         try {
66             return super.clone();
67         } catch (CloneNotSupportedException JavaDoc e) {
68             throw new AssertionError JavaDoc(e);
69         }
70     }
71     
72     /**
73      * Add a warning property to the set.
74      * The warning implicitly has the boolean value "true"
75      * as its attribute.
76      *
77      * @param prop the WarningProperty
78      * @return this object
79      */

80     public WarningPropertySet addProperty(WarningProperty prop) {
81         map.put(prop, Boolean.TRUE);
82         return this;
83     }
84
85     /**
86      * Add a warning property and its attribute value.
87      *
88      * @param prop the WarningProperty
89      * @param value the attribute value
90      * @return this object
91      */

92     public WarningPropertySet setProperty(WarningProperty prop, String JavaDoc value) {
93         map.put(prop, value);
94         return this;
95     }
96     
97     /**
98      * Add a warning property and its attribute value.
99      *
100      * @param prop the WarningProperty
101      * @param value the attribute value
102      */

103     public void setProperty(WarningProperty prop, Boolean JavaDoc value) {
104         map.put(prop, value);
105     }
106     
107     /**
108      * Return whether or not the set contains the given WarningProperty.
109      *
110      * @param prop the WarningProperty
111      * @return true if the set contains the WarningProperty, false if not
112      */

113     public boolean containsProperty(WarningProperty prop) {
114         return map.keySet().contains(prop);
115     }
116     
117     /**
118      * Check whether or not the given WarningProperty has the given
119      * attribute value.
120      *
121      * @param prop the WarningProperty
122      * @param value the attribute value
123      * @return true if the set contains the WarningProperty and has
124      * an attribute equal to the one given, false otherwise
125      */

126     public boolean checkProperty(WarningProperty prop, Object JavaDoc value) {
127         Object JavaDoc attribute = getProperty(prop);
128         return (attribute != null && attribute.equals(value));
129     }
130     
131     /**
132      * Get the value of the attribute for the given WarningProperty.
133      * Returns null if the set does not contain the WarningProperty.
134      *
135      * @param prop the WarningProperty
136      * @return the WarningProperty's attribute value, or null if
137      * the set does not contain the WarningProperty
138      */

139     public Object JavaDoc getProperty(WarningProperty prop) {
140         return map.get(prop);
141     }
142     
143     /**
144      * Use the PriorityAdjustments specified by the set's WarningProperty
145      * elements to compute a warning priority from the given
146      * base priority.
147      *
148      * @param basePriority the base priority
149      * @return the computed warning priority
150      */

151     public int computePriority(int basePriority) {
152         boolean relaxedReporting = FindBugsAnalysisFeatures.isRelaxedMode();
153     
154         boolean atLeastMedium = false;
155         boolean falsePositive = false;
156         boolean atMostLow = false;
157         int priority = basePriority;
158         if (!relaxedReporting) {
159             for (WarningProperty warningProperty : map.keySet()) {
160                 PriorityAdjustment adj = warningProperty.getPriorityAdjustment();
161                 if (adj == PriorityAdjustment.FALSE_POSITIVE) {
162                     falsePositive = true;
163                 }else if (adj == PriorityAdjustment.RAISE_PRIORITY)
164                     
165                     --priority;
166                 else if (adj == PriorityAdjustment.RAISE_PRIORITY_TO_AT_LEAST_NORMAL) {
167                     
168                     --priority;
169                     atLeastMedium = true;
170                 } else if (adj == PriorityAdjustment.RAISE_PRIORITY_TO_HIGH) {
171                     
172                     return Detector.HIGH_PRIORITY;
173                 }else if (adj == PriorityAdjustment.LOWER_PRIORITY) {
174                     ++priority;
175                 }else if (adj == PriorityAdjustment.AT_MOST_LOW) {
176                     priority++;
177                     atMostLow = true;
178                 }else if (adj == PriorityAdjustment.NO_ADJUSTMENT) {
179                     assert true; // do nothing
180
} else throw new IllegalStateException JavaDoc("Unknown priority " + adj);
181                  
182             }
183             if (atMostLow) return Math.min(Math.max(Detector.LOW_PRIORITY, priority), Detector.EXP_PRIORITY);
184             if (atLeastMedium && priority > Detector.NORMAL_PRIORITY) priority = Detector.NORMAL_PRIORITY;
185             else if (falsePositive && !atLeastMedium) return Detector.EXP_PRIORITY+1;
186             
187                 
188             if (priority < Detector.HIGH_PRIORITY)
189                 priority = Detector.HIGH_PRIORITY;
190             else if (priority > Detector.EXP_PRIORITY)
191                 priority = Detector.EXP_PRIORITY;
192         }
193         
194         return priority;
195     }
196
197     /**
198      * Determine whether or not a warning with given priority
199      * is expected to be a false positive.
200      *
201      * @param priority the priority
202      * @return true if the warning is expected to be a false positive, false if not
203      */

204     public boolean isFalsePositive(int priority) {
205         return priority > Detector.EXP_PRIORITY;
206     }
207
208     /**
209      * Decorate given BugInstance with properties.
210      *
211      * @param bugInstance the BugInstance
212      */

213     public void decorateBugInstance(BugInstance bugInstance) {
214         for (Map.Entry<WarningProperty, Object JavaDoc> entry : map.entrySet()) {
215             WarningProperty prop = entry.getKey();
216             Object JavaDoc attribute = entry.getValue();
217             if (attribute == null)
218                 attribute = "";
219             bugInstance.setProperty(prop.getName(), attribute.toString());
220         }
221     }
222 }
223
Popular Tags