001: /*
002: Copyright (c) 2003-2004, 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.generic.InstructionHandle;
032: import org.apache.bcel.generic.ObjectType;
033: import org.apache.bcel.generic.Type;
034: import org.jibx.runtime.JiBXException;
035:
036: /**
037: * Builder for marshal and unmarshal methods. Adds exception accumulation with
038: * actual handling provided by the subclass.
039: *
040: * @author Dennis M. Sosnoski
041: * @version 1.0
042: */
043:
044: public abstract class MarshalUnmarshalBuilder extends
045: ContextMethodBuilder {
046: /**
047: * Constructor. This sets up for constructing the marshal or unmarshal
048: * method.
049: *
050: * @param name method name to be built
051: * @param ret method return type
052: * @param args types of arguments
053: * @param mf method generation class file information
054: * @param access flags for method access
055: * @param obj variable slot for current object
056: * @param type marshalled or unmarshalled class name
057: * @param ctx variable slot for marshalling/unmarshalling context
058: * @param ctype context type as defined in method
059: * @throws JiBXException on error in initializing method construction
060: */
061:
062: protected MarshalUnmarshalBuilder(String name, Type ret,
063: Type[] args, ClassFile mf, int access, int obj,
064: String type, int ctx, String ctype) throws JiBXException {
065: super (name, ret, args, mf, access, obj, type, ctx, ctype);
066: }
067:
068: /**
069: * Add exception handler code. This method must be implemented by each
070: * subclass to provide the appropriate handling code.
071: *
072: * @return handle for first instruction in handler
073: * @throws JiBXException on error in creating exception handler
074: */
075:
076: public abstract InstructionHandle genExceptionHandler()
077: throws JiBXException;
078:
079: /**
080: * Process accumulated exceptions. Sets up an exception handler framework
081: * and then calls the {@link #genExceptionHandler} method to build the
082: * handler body.
083: *
084: * @throws JiBXException on error in exception handling
085: */
086:
087: protected void handleExceptions() throws JiBXException {
088: int index = m_exceptions.indexOf(FRAMEWORK_EXCEPTION_CLASS);
089: if (index >= 0) {
090: m_generator.addException(FRAMEWORK_EXCEPTION_CLASS);
091: m_exceptions.remove(index);
092: }
093: if (m_exceptions.size() > 0) {
094: InstructionHandle begin = getFirstInstruction();
095: InstructionHandle end = getLastInstruction();
096: InstructionHandle handle = genExceptionHandler();
097: for (int i = 0; i < m_exceptions.size(); i++) {
098: m_generator.addExceptionHandler(begin, end, handle,
099: (ObjectType) ClassItem
100: .typeFromName((String) m_exceptions
101: .get(i)));
102: }
103: }
104: }
105: }
|