KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.Constants;
32 import org.apache.bcel.generic.*;
33 import org.jibx.runtime.JiBXException;
34
35 /**
36  * Builder for binding methods with a context and current object. Tracks the
37  * current object reference and the context object reference positions in the
38  * local variables table.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class ContextMethodBuilder extends ExceptionMethodBuilder
45 {
46     /** Variable slot for current object reference. */
47     private int m_objectSlot;
48     
49     /** Object type as accessed by method. */
50     private String JavaDoc m_objectType;
51     
52     /** Variable slot for context reference. */
53     private int m_contextSlot;
54     
55     /** Context type as accessed by method. */
56     private String JavaDoc m_contextType;
57     
58     /** Context type as accessed by method. */
59     private final boolean m_isStatic;
60
61     /**
62      * Constructor with types specified. This sets up for constructing a
63      * binding method that uses a current object and a marshalling or
64      * unmarshalling context.
65      *
66      * @param name method name to be built
67      * @param ret method return type
68      * @param args types of arguments
69      * @param cf owning class file information
70      * @param access flags for method access
71      * @param obj variable slot for current object (negative value if to be
72      * defined later)
73      * @param type current object type as defined in method
74      * @param ctx variable slot for marshalling/unmarshalling context
75      * @param ctype context type as defined in method
76      * @throws JiBXException on error in initializing method construction
77      */

78
79     public ContextMethodBuilder(String JavaDoc name, Type ret, Type[] args,
80         ClassFile cf, int access, int obj, String JavaDoc type, int ctx, String JavaDoc ctype)
81         throws JiBXException {
82         super(name, ret, args, cf, access);
83         m_objectSlot = obj;
84         m_objectType = type;
85         m_contextSlot = ctx;
86         m_contextType = ctype;
87         m_isStatic = (access & Constants.ACC_STATIC) != 0;
88         addException(FRAMEWORK_EXCEPTION_CLASS);
89     }
90
91     /**
92      * Constructor from signature.
93      *
94      * @param name method name to be built
95      * @param sig method signature
96      * @param cf owning class file information
97      * @param access flags for method access
98      * @param obj variable slot for current object (negative value if to be
99      * defined later)
100      * @param type current object type
101      * @param ctx variable slot for marshalling/unmarshalling context
102      * @param ctype context type as defined in method
103      * @throws JiBXException on error in initializing method construction
104      */

105
106     public ContextMethodBuilder(String JavaDoc name, String JavaDoc sig,
107         ClassFile cf, int access, int obj, String JavaDoc type, int ctx, String JavaDoc ctype)
108         throws JiBXException {
109         this(name, Type.getReturnType(sig), Type.getArgumentTypes(sig),
110             cf, access, obj, type, ctx, ctype);
111     }
112
113     /**
114      * Constructor from signature for public, final method.
115      *
116      * @param name method name to be built
117      * @param sig method signature
118      * @param cf owning class file information
119      * @param obj variable slot for current object (negative value if to be
120      * defined later)
121      * @param type current object type
122      * @param ctx variable slot for marshalling/unmarshalling context
123      * @param ctype context type as defined in method
124      * @throws JiBXException on error in initializing method construction
125      */

126
127     public ContextMethodBuilder(String JavaDoc name, String JavaDoc sig, ClassFile cf,
128         int obj, String JavaDoc type, int ctx, String JavaDoc ctype) throws JiBXException {
129         this(name, sig, cf, Constants.ACC_PUBLIC|Constants.ACC_FINAL,
130             obj, type, ctx, ctype);
131     }
132
133     /**
134      * Set current object slot. Sets the local variable position of the current
135      * object, as required when the object is actually created within the
136      * method.
137      *
138      * @param slot local variable slot for current object
139      */

140
141     public void setObjectSlot(int slot) {
142         m_objectSlot = slot;
143     }
144
145     /**
146      * Append instruction to load object to stack.
147      */

148
149     public void loadObject() {
150         appendLoadLocal(m_objectSlot);
151     }
152
153     /**
154      * Append instruction to store object from stack.
155      *
156      * @return handle of generated instruction
157      */

158
159     public void storeObject() {
160         if (m_objectSlot < 0) {
161             LocalVariableGen var = createLocal("obj",
162                 ClassItem.typeFromName(m_objectType));
163             m_objectSlot = var.getIndex();
164         } else {
165             appendStoreLocal(m_objectSlot);
166         }
167     }
168
169     /**
170      * Append instruction(s) to load object to stack as specified type.
171      *
172      * @param loaded type expected on stack
173      */

174
175     public void loadObject(String JavaDoc type) {
176         appendLoadLocal(m_objectSlot);
177         if (!m_objectType.equals(type)) {
178             appendCreateCast(m_objectType, type);
179         }
180     }
181
182     /**
183      * Append instruction to load context to stack.
184      */

185
186     public void loadContext() {
187         appendLoadLocal(m_contextSlot);
188     }
189
190     /**
191      * Append instruction(s) to load context to stack as specified type.
192      *
193      * @param loaded type expected on stack
194      */

195
196     public void loadContext(String JavaDoc type) {
197         appendLoadLocal(m_contextSlot);
198         if (!m_contextType.equals(type)) {
199             appendCreateCast(m_contextType, type);
200         }
201     }
202     
203     /**
204      * Check if method is static.
205      *
206      * @return
207      */

208     public boolean isStaticMethod() {
209         return m_isStatic;
210     }
211 }
Popular Tags