KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > plan > AnalysisPass


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, 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
20 package edu.umd.cs.findbugs.plan;
21
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import edu.umd.cs.findbugs.BugReporter;
29 import edu.umd.cs.findbugs.Detector;
30 import edu.umd.cs.findbugs.Detector2;
31 import edu.umd.cs.findbugs.DetectorFactory;
32 import edu.umd.cs.findbugs.FindBugs2;
33
34 /**
35  * An analysis pass in the overall ExecutionPlan.
36  * This is a list of Detectors to be applied to analyzed classes.
37  *
38  * @see ExecutionPlan
39  * @author David Hovemeyer
40  */

41 public class AnalysisPass {
42     private LinkedList JavaDoc<DetectorFactory> orderedFactoryList;
43     private HashSet JavaDoc<DetectorFactory> memberSet;
44 // private Detector2[] detectorList;
45

46     /**
47      * Constructor.
48      *
49      * Creates an empty analysis pass.
50      */

51     public AnalysisPass() {
52         this.orderedFactoryList = new LinkedList JavaDoc<DetectorFactory>();
53         this.memberSet = new HashSet JavaDoc<DetectorFactory>();
54     }
55
56     /**
57      * Make given DetectorFactory a member of this pass.
58      * Does not position the factory within the overall list of detectors.
59      *
60      * @param factory a DetectorFactory
61      */

62     public void addToPass(DetectorFactory factory) {
63         this.memberSet.add(factory);
64     }
65
66     /**
67      * Append the given DetectorFactory to the end of the ordered detector list.
68      * The factory must be a member of the pass.
69      *
70      * @param factory a DetectorFactory
71      */

72     public void append(DetectorFactory factory) {
73         if (!memberSet.contains(factory))
74             throw new IllegalArgumentException JavaDoc("Detector " + factory.getFullName() + " appended to pass it doesn't belong to");
75         this.orderedFactoryList.addLast(factory);
76     }
77     
78     /**
79      * Get the members of this pass.
80      *
81      * @return members of this pass
82      */

83     public Collection JavaDoc<DetectorFactory> getMembers() {
84         return memberSet;
85     }
86     
87     /**
88      * Get Set of pass members which haven't been assigned a position in the pass.
89      */

90     public Set JavaDoc<DetectorFactory> getUnpositionedMembers() {
91         HashSet JavaDoc<DetectorFactory> result = new HashSet JavaDoc<DetectorFactory>(memberSet);
92         result.removeAll(orderedFactoryList);
93         return result;
94     }
95
96     /**
97      * Get an Iterator over the DetectorFactory objects in the pass,
98      * in their assigned order.
99      */

100     public Iterator JavaDoc<DetectorFactory> iterator() {
101         return orderedFactoryList.iterator();
102     }
103     
104     /**
105      * Return whether or not this pass contains the given DetectorFactory.
106      *
107      * @param factory the DetectorFactory
108      * @return true if this pass contains the DetectorFactory, false if not
109      */

110     public boolean contains(DetectorFactory factory) {
111         return memberSet.contains(factory);
112     }
113
114     /**
115      * Instantiate all of the Detector2s in this pass and return
116      * them in a (correctly-ordered) array.
117      *
118      * @param bugReporter the BugReporter
119      * @return array of Detector2s
120      */

121     public Detector2[] instantiateDetector2sInPass(BugReporter bugReporter) {
122         Detector2[] detectorList = new Detector2[orderedFactoryList.size()];
123         int count = 0;
124         for (Iterator JavaDoc<DetectorFactory> j = iterator(); j.hasNext();) {
125             detectorList[count++] = j.next().createDetector2(bugReporter);
126         }
127         return detectorList;
128     }
129     
130     /**
131      * Instantiate all of the detectors in this pass as objects implementing
132      * the BCEL-only Detector interface. Detectors that do not support this
133      * interface will not be created. Therefore, new code should use
134      * the instantiateDetector2sInPass() method, which can support all detectors.
135      *
136      * @param bugReporter the BugReporter
137      * @return array of Detectors
138      * @deprecated call instantiateDetector2sInPass() instead
139      */

140     public Detector[] instantiateDetectorsInPass(BugReporter bugReporter) {
141         int count;
142         
143         count = 0;
144         for (DetectorFactory factory : orderedFactoryList) {
145             if (factory.isDetectorClassSubtypeOf(Detector.class)) {
146                 count++;
147             }
148         }
149
150         Detector[] detectorList = new Detector[count];
151         
152         count = 0;
153         for (DetectorFactory factory : orderedFactoryList) {
154             if (factory.isDetectorClassSubtypeOf(Detector.class)) {
155                 detectorList[count++] = factory.create(bugReporter);
156             }
157         }
158         
159         return detectorList;
160     }
161 }
162
163 // vim:ts=4
164
Popular Tags