KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 Copyright (c) 2003-2004, 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 org.apache.bcel.generic.InstructionHandle;
32 import org.apache.bcel.generic.ObjectType;
33 import org.apache.bcel.generic.Type;
34 import org.jibx.runtime.JiBXException;
35
36 /**
37  * Builder for marshal and unmarshal methods. Adds exception accumulation with
38  * actual handling provided by the subclass.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public abstract class MarshalUnmarshalBuilder extends ContextMethodBuilder
45 {
46     /**
47      * Constructor. This sets up for constructing the marshal or unmarshal
48      * method.
49      *
50      * @param name method name to be built
51      * @param ret method return type
52      * @param args types of arguments
53      * @param mf method generation class file information
54      * @param access flags for method access
55      * @param obj variable slot for current object
56      * @param type marshalled or unmarshalled class name
57      * @param ctx variable slot for marshalling/unmarshalling context
58      * @param ctype context type as defined in method
59      * @throws JiBXException on error in initializing method construction
60      */

61
62     protected MarshalUnmarshalBuilder(String JavaDoc name, Type ret, Type[] args,
63         ClassFile mf, int access, int obj, String JavaDoc type, int ctx, String JavaDoc ctype)
64         throws JiBXException {
65         super(name, ret, args, mf, access, obj, type, ctx, ctype);
66     }
67
68     /**
69      * Add exception handler code. This method must be implemented by each
70      * subclass to provide the appropriate handling code.
71      *
72      * @return handle for first instruction in handler
73      * @throws JiBXException on error in creating exception handler
74      */

75
76     public abstract InstructionHandle genExceptionHandler()
77         throws JiBXException;
78
79     /**
80      * Process accumulated exceptions. Sets up an exception handler framework
81      * and then calls the {@link #genExceptionHandler} method to build the
82      * handler body.
83      *
84      * @throws JiBXException on error in exception handling
85      */

86
87     protected void handleExceptions() throws JiBXException {
88         int index = m_exceptions.indexOf(FRAMEWORK_EXCEPTION_CLASS);
89         if (index >= 0) {
90             m_generator.addException(FRAMEWORK_EXCEPTION_CLASS);
91             m_exceptions.remove(index);
92         }
93         if (m_exceptions.size() > 0) {
94             InstructionHandle begin = getFirstInstruction();
95             InstructionHandle end = getLastInstruction();
96             InstructionHandle handle = genExceptionHandler();
97             for (int i = 0; i < m_exceptions.size(); i++) {
98                 m_generator.addExceptionHandler(begin, end, handle,
99                     (ObjectType)ClassItem.
100                     typeFromName((String JavaDoc)m_exceptions.get(i)));
101             }
102         }
103     }
104 }
Popular Tags