KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > obl > ObligationFactory


1 /*
2  * Bytecode Analysis Framework
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.ba.obl;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.bcel.generic.ObjectType;
27
28 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
29 import edu.umd.cs.findbugs.ba.Hierarchy;
30
31 /**
32  * Factory for Obligation and ObligationSet objects to be
33  * used in an instance of ObligationAnalysis.
34  */

35 public class ObligationFactory {
36     private Map JavaDoc<String JavaDoc, Obligation> classNameToObligationMap;
37     
38 // // XXX: this is just for debugging.
39
// static ObligationFactory lastInstance;
40

41     @SuppressWarnings JavaDoc("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
42     public ObligationFactory() {
43         this.classNameToObligationMap = new HashMap JavaDoc<String JavaDoc, Obligation>();
44 // lastInstance = this;
45
}
46
47     public int getMaxObligationTypes() {
48         return classNameToObligationMap.size();
49     }
50     
51     public Iterator JavaDoc<Obligation> obligationIterator() {
52         return classNameToObligationMap.values().iterator();
53     }
54     
55     /**
56      * Look up an Obligation by type.
57      * This returns the first Obligation that is a supertype
58      * of the type given (meaning that the given type could
59      * be an instance of the returned Obligation).
60      *
61      * @param type a type
62      * @return an Obligation that is a supertype of the given type,
63      * or null if there is no such Obligation
64      * @throws ClassNotFoundException
65      */

66     public Obligation getObligationByType(ObjectType type)
67             throws ClassNotFoundException JavaDoc {
68         for (Iterator JavaDoc<Obligation> i = obligationIterator(); i.hasNext(); ) {
69             Obligation obligation = i.next();
70             if (Hierarchy.isSubtype(type, obligation.getType()))
71                 return obligation;
72         }
73         return null;
74     }
75
76     public Obligation addObligation(String JavaDoc className) {
77         int nextId = classNameToObligationMap.size();
78         Obligation obligation = new Obligation(className, nextId);
79         if (classNameToObligationMap.put(className, obligation) != null) {
80             throw new IllegalStateException JavaDoc("Obligation " + className +
81                 " added multiple times");
82         }
83         return obligation;
84     }
85     
86     public Obligation getObligationById(int id) {
87         for (Obligation obligation : classNameToObligationMap.values()) {
88             if (obligation.getId() == id)
89                 return obligation;
90         }
91         return null;
92     }
93
94 // public Obligation getObligation(String className) {
95
// Obligation obligation = classNameToObligationMap.get(className);
96
// if (obligation == null)
97
// throw new IllegalArgumentException("Unknown obligation class " + className);
98
// return obligation;
99
// }
100

101     public ObligationSet createObligationSet() {
102         return new ObligationSet(getMaxObligationTypes(), this);
103     }
104 }
105
106 // vim:ts=4
107
Popular Tags