001: /*
002: Copyright (c) 2003-2005, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.binding.classes;
030:
031: import org.apache.bcel.Constants;
032: import org.apache.bcel.generic.*;
033: import org.jibx.runtime.JiBXException;
034:
035: /**
036: * Builder for binding methods with a context and current object. Tracks the
037: * current object reference and the context object reference positions in the
038: * local variables table.
039: *
040: * @author Dennis M. Sosnoski
041: * @version 1.0
042: */
043:
044: public class ContextMethodBuilder extends ExceptionMethodBuilder {
045: /** Variable slot for current object reference. */
046: private int m_objectSlot;
047:
048: /** Object type as accessed by method. */
049: private String m_objectType;
050:
051: /** Variable slot for context reference. */
052: private int m_contextSlot;
053:
054: /** Context type as accessed by method. */
055: private String m_contextType;
056:
057: /** Context type as accessed by method. */
058: private final boolean m_isStatic;
059:
060: /**
061: * Constructor with types specified. This sets up for constructing a
062: * binding method that uses a current object and a marshalling or
063: * unmarshalling context.
064: *
065: * @param name method name to be built
066: * @param ret method return type
067: * @param args types of arguments
068: * @param cf owning class file information
069: * @param access flags for method access
070: * @param obj variable slot for current object (negative value if to be
071: * defined later)
072: * @param type current object type as defined in method
073: * @param ctx variable slot for marshalling/unmarshalling context
074: * @param ctype context type as defined in method
075: * @throws JiBXException on error in initializing method construction
076: */
077:
078: public ContextMethodBuilder(String name, Type ret, Type[] args,
079: ClassFile cf, int access, int obj, String type, int ctx,
080: String ctype) throws JiBXException {
081: super (name, ret, args, cf, access);
082: m_objectSlot = obj;
083: m_objectType = type;
084: m_contextSlot = ctx;
085: m_contextType = ctype;
086: m_isStatic = (access & Constants.ACC_STATIC) != 0;
087: addException(FRAMEWORK_EXCEPTION_CLASS);
088: }
089:
090: /**
091: * Constructor from signature.
092: *
093: * @param name method name to be built
094: * @param sig method signature
095: * @param cf owning class file information
096: * @param access flags for method access
097: * @param obj variable slot for current object (negative value if to be
098: * defined later)
099: * @param type current object type
100: * @param ctx variable slot for marshalling/unmarshalling context
101: * @param ctype context type as defined in method
102: * @throws JiBXException on error in initializing method construction
103: */
104:
105: public ContextMethodBuilder(String name, String sig, ClassFile cf,
106: int access, int obj, String type, int ctx, String ctype)
107: throws JiBXException {
108: this (name, Type.getReturnType(sig), Type.getArgumentTypes(sig),
109: cf, access, obj, type, ctx, ctype);
110: }
111:
112: /**
113: * Constructor from signature for public, final method.
114: *
115: * @param name method name to be built
116: * @param sig method signature
117: * @param cf owning class file information
118: * @param obj variable slot for current object (negative value if to be
119: * defined later)
120: * @param type current object type
121: * @param ctx variable slot for marshalling/unmarshalling context
122: * @param ctype context type as defined in method
123: * @throws JiBXException on error in initializing method construction
124: */
125:
126: public ContextMethodBuilder(String name, String sig, ClassFile cf,
127: int obj, String type, int ctx, String ctype)
128: 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:
157: public void storeObject() {
158: if (m_objectSlot < 0) {
159: LocalVariableGen var = createLocal("obj", ClassItem
160: .typeFromName(m_objectType));
161: m_objectSlot = var.getIndex();
162: } else {
163: appendCreateCast(m_objectType);
164: appendStoreLocal(m_objectSlot);
165: }
166: }
167:
168: /**
169: * Append instruction(s) to load object to stack as specified type.
170: *
171: * @param type loaded type expected on stack
172: */
173:
174: public void loadObject(String type) {
175: appendLoadLocal(m_objectSlot);
176: if (!m_objectType.equals(type)) {
177: appendCreateCast(m_objectType, type);
178: }
179: }
180:
181: /**
182: * Append instruction to load context to stack.
183: */
184:
185: public void loadContext() {
186: appendLoadLocal(m_contextSlot);
187: }
188:
189: /**
190: * Append instruction(s) to load context to stack as specified type.
191: *
192: * @param type loaded type expected on stack
193: */
194:
195: public void loadContext(String type) {
196: appendLoadLocal(m_contextSlot);
197: if (!m_contextType.equals(type)) {
198: appendCreateCast(m_contextType, type);
199: }
200: }
201:
202: /**
203: * Check if method is static.
204: *
205: * @return <code>true</code> if static, <code>false</code> if not
206: */
207: public boolean isStaticMethod() {
208: return m_isStatic;
209: }
210: }
|