KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Unmarshalling method builder. Tracks the creation of an unmarshalling method,
37  * including special handling of exceptions that may be generated by object
38  * accesses during the unmarshalling process.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

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

49     private static final String JavaDoc UNMARSHALCONTEXT_CLASS =
50         "org.jibx.runtime.impl.UnmarshallingContext";
51     protected static final String JavaDoc UNMARSHAL_EXCEPTION_TEXT =
52         "Error while unmarshalling ";
53     protected static final String JavaDoc UNMARSHALLING_POSITION_METHOD =
54         "org.jibx.runtime.impl.UnmarshallingContext.buildPositionString";
55     protected static final String JavaDoc UNMARSHALLING_POSITION_SIGNATURE =
56         "()Ljava/lang/String;";
57     protected static final Type[] UNMARSHAL_METHOD_ARGS =
58     {
59         new ObjectType("org.jibx.runtime.impl.UnmarshallingContext")
60     };
61     protected static final Type[] SINGLE_STRING_ARGS =
62     {
63         Type.STRING
64     };
65
66     /**
67      * Constructor. This sets up for constructing a virtual unmarshalling method
68      * with public access and wrapped exception handling. If the method is being
69      * generated directly to the class being unmarshalled it's built as a
70      * virtual method; otherwise, it's done as a static method.
71      *
72      * @param name method name to be built
73      * @param cf unmarshal class file information
74      * @param mf method generation class file information
75      * @throws JiBXException on error in initializing method construction
76      */

77
78     public UnmarshalBuilder(String JavaDoc name, ClassFile cf, ClassFile mf)
79         throws JiBXException {
80         super(name, Type.VOID,
81             (mf == cf) ? UNMARSHAL_METHOD_ARGS :
82             new Type[] {ClassItem.typeFromName(cf.getName()),
83             UNMARSHAL_METHOD_ARGS[0]},
84             mf, (mf == cf) ? Constants.ACC_PUBLIC | Constants.ACC_FINAL :
85             Constants.ACC_PUBLIC | Constants.ACC_STATIC, 0, cf.getName(),
86             1, UNMARSHALCONTEXT_CLASS);
87     }
88
89     /**
90      * Add exception handler code. The implementation of this abstract base
91      * class method provides handling specific to an unmarshalling method.
92      *
93      * @return handle for first instruction in handler
94      * @throws JiBXException on error in creating exception handler
95      */

96
97     public InstructionHandle genExceptionHandler() throws JiBXException {
98         
99         // first part of instruction sequence is create new exception object,
100
// duplicate two down (below caught exception), swap (so order is new,
101
// new, caught)
102
initStackState(new String JavaDoc[] {"java.lang.Exception"});
103         InstructionHandle start =
104             internalAppendCreateNew(FRAMEWORK_EXCEPTION_CLASS);
105         appendDUP_X1();
106         appendSWAP();
107         
108         // second part of sequence is build StringBuffer, duplicate, load lead
109
// String, call constructor with String argument, load unmarshalling
110
// context, call position String method, call StringBuffer append,
111
// call StringBuffer toString
112
appendCreateNew("java.lang.StringBuffer");
113         appendDUP();
114         appendLoadConstant(UNMARSHAL_EXCEPTION_TEXT);
115         appendCallInit("java.lang.StringBuffer", "(Ljava/lang/String;)V");
116         loadContext();
117         appendCallVirtual(UNMARSHALLING_POSITION_METHOD,
118             UNMARSHALLING_POSITION_SIGNATURE);
119         appendCallVirtual("java.lang.StringBuffer.append",
120             "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
121         appendCallVirtual("java.lang.StringBuffer.toString",
122             "()Ljava/lang/String;");
123         
124         // final part of sequence is swap to get arguments in correct order,
125
// invoke exception constructor, throw exception
126
appendSWAP();
127         appendCallInit(FRAMEWORK_EXCEPTION_CLASS,
128             EXCEPTION_CONSTRUCTOR_SIGNATURE2);
129         appendThrow();
130         return start;
131     }
132 }
Popular Tags