KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Bytecode Analysis Framework
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
20 package edu.umd.cs.findbugs.ba.obl;
21
22 import java.util.LinkedList JavaDoc;
23
24 import edu.umd.cs.findbugs.ba.Hierarchy;
25
26 /**
27  * Policy database which defines which methods create and remove
28  * obligations.
29  *
30  * <p>See Weimer and Necula,
31  * <a HREF="http://doi.acm.org/10.1145/1028976.1029011"
32  * >Finding and preventing run-time error handling mistakes</a>,
33  * OOPSLA 2004.</p>
34  *
35  * @author David Hovemeyer
36  */

37 public class PolicyDatabase {
38     /** Action constant for methods which create an obligation. */
39     public static final int ADD = 0;
40
41     public static final int DEL = 1;
42
43     private static class Entry {
44         private final String JavaDoc className;
45         private final String JavaDoc methodName;
46         private final String JavaDoc signature;
47         private final boolean isStatic;
48         private final int action;
49         private final Obligation obligation;
50
51         public Entry(String JavaDoc className, String JavaDoc methodName, String JavaDoc signature,
52                     boolean isStatic,
53                     int action, Obligation obligation) {
54             this.className = className;
55             this.methodName = methodName;
56             this.signature = signature;
57             this.isStatic = isStatic;
58             this.action = action;
59             this.obligation = obligation;
60         }
61
62         public String JavaDoc getClassName() {
63             return className;
64         }
65
66         public String JavaDoc getMethodName() {
67             return methodName;
68         }
69
70         public String JavaDoc getSignature() {
71             return signature;
72         }
73
74         public boolean isStatic() {
75             return isStatic;
76         }
77         
78         public int getAction() {
79             return action;
80         }
81         
82         public Obligation getObligation() {
83             return obligation;
84         }
85     }
86
87     // FIXME: may want to figure out a way to do lookups more efficiently
88
private LinkedList JavaDoc<Entry> entryList;
89
90     public PolicyDatabase() {
91         this.entryList = new LinkedList JavaDoc<Entry>();
92     }
93
94     public void addEntry(
95             String JavaDoc className, String JavaDoc methodName, String JavaDoc signature, boolean isStatic,
96             int action, Obligation obligation) {
97         entryList.add(new Entry(className, methodName, signature, isStatic, action, obligation));
98     }
99     
100     public Obligation lookup(
101             String JavaDoc className, String JavaDoc methodName, String JavaDoc signature, boolean isStatic,
102             int action) throws ClassNotFoundException JavaDoc {
103         for (Entry entry : entryList) {
104             if (isStatic == entry.isStatic()
105                     && action == entry.getAction()
106                     && methodName.equals(entry.getMethodName())
107                     && signature.equals(entry.getSignature())
108                     && Hierarchy.isSubtype(className, entry.getClassName())) {
109                 return entry.getObligation();
110             }
111         }
112         
113         return null;
114     }
115 }
116
117 // vim:ts=4
118
Popular Tags