KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > ExceptionHandlerMap


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,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;
21
22 import java.util.IdentityHashMap JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.bcel.generic.CodeExceptionGen;
27 import org.apache.bcel.generic.InstructionHandle;
28 import org.apache.bcel.generic.MethodGen;
29
30 /**
31  * This class provides a convenient way of determining the exception handlers
32  * for instructions in a method. Essentially, it's a
33  * a map of InstructionHandles to lists of CodeExceptionGen objects.
34  * This class also maps instructions which are the start of exception handlers
35  * to the CodeExceptionGen object representing the handler.
36  *
37  * @author David Hovemeyer
38  */

39 public class ExceptionHandlerMap {
40     private IdentityHashMap JavaDoc<InstructionHandle, List JavaDoc<CodeExceptionGen>> codeToHandlerMap;
41     private IdentityHashMap JavaDoc<InstructionHandle, CodeExceptionGen> startInstructionToHandlerMap;
42
43     /**
44      * Constructor.
45      *
46      * @param methodGen the method to build the map for
47      */

48     public ExceptionHandlerMap(MethodGen methodGen) {
49         codeToHandlerMap = new IdentityHashMap JavaDoc<InstructionHandle, List JavaDoc<CodeExceptionGen>>();
50         startInstructionToHandlerMap = new IdentityHashMap JavaDoc<InstructionHandle, CodeExceptionGen>();
51         build(methodGen);
52     }
53
54     /**
55      * Get the list of exception handlers (CodeExceptionGen objects)
56      * which are specified to handle exceptions for the instruction whose
57      * handle is given. Note that the handlers in the returned list
58      * are <b>in order of priority</b>, as defined in the method's exception handler
59      * table.
60      *
61      * @param handle the handle of the instruction we want the exception handlers for
62      * @return the list of exception handlers, or null if there are no handlers
63      * registered for the instruction
64      */

65     public List JavaDoc<CodeExceptionGen> getHandlerList(InstructionHandle handle) {
66         return codeToHandlerMap.get(handle);
67     }
68
69     /**
70      * If the given instruction is the start of an exception handler,
71      * get the CodeExceptionGen object representing the handler.
72      *
73      * @param start the instruction
74      * @return the CodeExceptionGen object, or null if the instruction is not the
75      * start of an exception handler
76      */

77     public CodeExceptionGen getHandlerForStartInstruction(InstructionHandle start) {
78         return startInstructionToHandlerMap.get(start);
79     }
80
81     private void build(MethodGen methodGen) {
82         CodeExceptionGen[] handlerList = methodGen.getExceptionHandlers();
83
84         // Map handler start instructions to the actual exception handlers
85
for (CodeExceptionGen exceptionHandler : handlerList) {
86             startInstructionToHandlerMap.put(exceptionHandler.getHandlerPC(), exceptionHandler);
87         }
88
89         // For each instruction, determine which handlers it can reach
90
InstructionHandle handle = methodGen.getInstructionList().getStart();
91         while (handle != null) {
92             int offset = handle.getPosition();
93
94             handlerLoop:
95             for (CodeExceptionGen exceptionHandler : handlerList) {
96                 int startOfRange = exceptionHandler.getStartPC().getPosition();
97                 int endOfRange = exceptionHandler.getEndPC().getPosition();
98
99                 if (offset >= startOfRange && offset <= endOfRange) {
100                     // This handler is reachable from the instruction
101
addHandler(handle, exceptionHandler);
102
103                     // If this handler handles all exception types
104
// i.e., an ANY handler, or catch(Throwable...),
105
// then no further (lower-priority)
106
// handlers are reachable from the instruction.
107
if (Hierarchy.isUniversalExceptionHandler(exceptionHandler.getCatchType()))
108                         break handlerLoop;
109                 }
110             }
111
112             handle = handle.getNext();
113         }
114     }
115
116     private void addHandler(InstructionHandle handle, CodeExceptionGen exceptionHandler) {
117         List JavaDoc<CodeExceptionGen> handlerList = codeToHandlerMap.get(handle);
118         if (handlerList == null) {
119             handlerList = new LinkedList JavaDoc<CodeExceptionGen>();
120             codeToHandlerMap.put(handle, handlerList);
121         }
122         handlerList.add(exceptionHandler);
123     }
124 }
125
126 // vim:ts=4
127
Popular Tags