KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > classes > ExceptionMethodBuilder


1 /*
2 Copyright (c) 2003-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.classes;
30
31 import java.util.HashMap JavaDoc;
32
33 import org.apache.bcel.Constants;
34 import org.apache.bcel.generic.*;
35 import org.jibx.runtime.JiBXException;
36
37 /**
38  * Builder for simple methods that may just pass checked exceptions on to
39  * caller.
40  *
41  * @author Dennis M. Sosnoski
42  * @version 1.0
43  */

44
45 public class ExceptionMethodBuilder extends MethodBuilder
46 {
47     /** Map for object to variable assignments. */
48     private HashMap JavaDoc m_slotMap;
49     
50     /**
51      * Constructor with types specified.
52      *
53      * @param name method name to be built
54      * @param ret method return type
55      * @param args types of arguments
56      * @param cf owning class file information
57      * @param access flags for method access
58      * @throws JiBXException on error in initializing method construction
59      */

60
61     public ExceptionMethodBuilder(String JavaDoc name, Type ret, Type[] args,
62         ClassFile cf, int access) throws JiBXException {
63         super(name, ret, args, cf, access);
64     }
65
66     /**
67      * Constructor from signature.
68      *
69      * @param name method name to be built
70      * @param sig method signature
71      * @param cf owning class file information
72      * @param access flags for method access
73      * @throws JiBXException on error in initializing method construction
74      */

75
76     public ExceptionMethodBuilder(String JavaDoc name, String JavaDoc sig,
77         ClassFile cf, int access) throws JiBXException {
78         super(name, Type.getReturnType(sig), Type.getArgumentTypes(sig),
79             cf, access);
80     }
81
82     /**
83      * Constructor from signature for public, final method.
84      *
85      * @param name method name to be built
86      * @param sig method signature
87      * @param cf owning class file information
88      * @throws JiBXException on error in initializing method construction
89      */

90
91     public ExceptionMethodBuilder(String JavaDoc name, String JavaDoc sig, ClassFile cf)
92         throws JiBXException {
93         super(name, Type.getReturnType(sig), Type.getArgumentTypes(sig),
94             cf, Constants.ACC_PUBLIC | Constants.ACC_FINAL);
95     }
96
97     /**
98      * Define local variable slot for object. The current code in the method
99      * must have the initial value for the variable on the stack
100      *
101      * @param obj owning object of slot
102      */

103
104     public void defineSlot(Object JavaDoc obj, Type type) {
105         if (m_slotMap == null) {
106             m_slotMap = new HashMap JavaDoc();
107         }
108         LocalVariableGen var = createLocal("var" + m_slotMap.size(), type);
109         m_slotMap.put(obj, var);
110     }
111
112     /**
113      * Check if local variable slot defined for object.
114      *
115      * @param obj owning object of slot
116      * @return local variable slot assigned to object, or <code>-1</code> if
117      * none
118      */

119
120     public int getSlot(Object JavaDoc obj) {
121         if (m_slotMap != null) {
122             LocalVariableGen var = (LocalVariableGen)m_slotMap.get(obj);
123             if (var != null) {
124                 return var.getIndex();
125             }
126         }
127         return -1;
128     }
129
130     /**
131      * Free local variable slot for object. This clears the usage of the slot
132      * (if one has been defined for the object) so it can be reused for other
133      * purposes.
134      *
135      * @param obj owning object of slot
136      */

137
138     public void freeSlot(Object JavaDoc obj) {
139         if (m_slotMap != null) {
140             LocalVariableGen var = (LocalVariableGen)m_slotMap.get(obj);
141             if (var != null) {
142                 var.setEnd(getLastInstruction());
143                 m_slotMap.remove(obj);
144             }
145         }
146     }
147
148     /**
149      * Process accumulated exceptions. Just adds the checked exceptions that
150      * may be thrown within the body to the list for this method, passing them
151      * on to the caller for handling.
152      *
153      * @throws JiBXException on error in exception handling
154      */

155
156     protected void handleExceptions() throws JiBXException {
157         for (int i = 0; i < m_exceptions.size(); i++) {
158             m_generator.addException((String JavaDoc)m_exceptions.get(i));
159         }
160     }
161 }
162
Popular Tags