KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
32 import org.apache.bcel.generic.*;
33 import org.jibx.runtime.JiBXException;
34
35 /**
36  * Marshalling method builder. Tracks the creation of a marshalling method,
37  * including special handling of exceptions that may be generated by object
38  * accesses during the marshalling process.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class MarshalBuilder extends MarshalUnmarshalBuilder
45 {
46     //
47
// Constants for code generation.
48

49     private static final String JavaDoc MARSHALCONTEXT_CLASS =
50         "org.jibx.runtime.impl.MarshallingContext";
51     protected static final String JavaDoc MARSHAL_EXCEPTION_TEXT =
52         "Error while marshalling";
53     protected static final Type[] MARSHAL_METHOD_ARGS =
54     {
55         new ObjectType("org.jibx.runtime.impl.MarshallingContext")
56     };
57
58     /**
59      * Constructor. This sets up for constructing a marshalling method with
60      * public access and wrapped exception handling. If the method is being
61      * generated directly to the class being marshalled it's built as a virtual
62      * method; otherwise, it's done as a static method.
63      *
64      * @param name method name to be built
65      * @param cf owning class file information
66      * @param mf method generation class file information
67      * @throws JiBXException on error in initializing method construction
68      */

69
70     public MarshalBuilder(String JavaDoc name, ClassFile cf, ClassFile mf)
71         throws JiBXException {
72         super(name, Type.VOID,
73             (mf == cf) ? MARSHAL_METHOD_ARGS :
74             new Type[] {ClassItem.typeFromName(cf.getName()),
75             MARSHAL_METHOD_ARGS[0]},
76             mf, (mf == cf) ? Constants.ACC_PUBLIC | Constants.ACC_FINAL :
77             Constants.ACC_PUBLIC | Constants.ACC_STATIC, 0, cf.getName(),
78             1, MARSHALCONTEXT_CLASS);
79     }
80
81     /**
82      * Add exception handler code. The implementation of this abstract base
83      * class method provides handling specific to a marshalling method.
84      *
85      * @return handle for first instruction in handler
86      * @throws JiBXException on error in creating exception handler
87      */

88
89     public InstructionHandle genExceptionHandler() throws JiBXException {
90         
91         // instruction sequence is create new exception object, duplicate two
92
// down (below caught exception), swap (so order is new, new, caught),
93
// load message, swap again, invoke constructor, and throw exception
94
initStackState(new String JavaDoc[] {"java.lang.Exception"});
95         InstructionHandle start =
96             internalAppendCreateNew(FRAMEWORK_EXCEPTION_CLASS);
97         appendDUP_X1();
98         appendSWAP();
99         appendLoadConstant(MARSHAL_EXCEPTION_TEXT);
100         appendSWAP();
101         appendCallInit(FRAMEWORK_EXCEPTION_CLASS,
102             EXCEPTION_CONSTRUCTOR_SIGNATURE2);
103         appendThrow();
104         return start;
105     }
106 }
Popular Tags