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 java.util.HashMap;
032:
033: import org.apache.bcel.Constants;
034: import org.apache.bcel.generic.*;
035: import org.jibx.runtime.JiBXException;
036:
037: /**
038: * Builder for simple methods that may just pass checked exceptions on to
039: * caller.
040: *
041: * @author Dennis M. Sosnoski
042: * @version 1.0
043: */
044:
045: public class ExceptionMethodBuilder extends MethodBuilder {
046: /** Map for object to variable assignments. */
047: private HashMap m_slotMap;
048:
049: /**
050: * Constructor with types specified.
051: *
052: * @param name method name to be built
053: * @param ret method return type
054: * @param args types of arguments
055: * @param cf owning class file information
056: * @param access flags for method access
057: * @throws JiBXException on error in initializing method construction
058: */
059:
060: public ExceptionMethodBuilder(String name, Type ret, Type[] args,
061: ClassFile cf, int access) throws JiBXException {
062: super (name, ret, args, cf, access);
063: }
064:
065: /**
066: * Constructor from signature.
067: *
068: * @param name method name to be built
069: * @param sig method signature
070: * @param cf owning class file information
071: * @param access flags for method access
072: * @throws JiBXException on error in initializing method construction
073: */
074:
075: public ExceptionMethodBuilder(String name, String sig,
076: ClassFile cf, int access) throws JiBXException {
077: super (name, Type.getReturnType(sig),
078: Type.getArgumentTypes(sig), cf, access);
079: }
080:
081: /**
082: * Constructor from signature for public, final method.
083: *
084: * @param name method name to be built
085: * @param sig method signature
086: * @param cf owning class file information
087: * @throws JiBXException on error in initializing method construction
088: */
089:
090: public ExceptionMethodBuilder(String name, String sig, ClassFile cf)
091: throws JiBXException {
092: super (name, Type.getReturnType(sig),
093: Type.getArgumentTypes(sig), cf, Constants.ACC_PUBLIC
094: | Constants.ACC_FINAL);
095: }
096:
097: /**
098: * Define local variable slot for object. The current code in the method
099: * must have the initial value for the variable on the stack
100: *
101: * @param obj owning object of slot
102: */
103:
104: public void defineSlot(Object obj, Type type) {
105: if (m_slotMap == null) {
106: m_slotMap = new HashMap();
107: }
108: LocalVariableGen var = createLocal("var" + m_slotMap.size(),
109: type);
110: m_slotMap.put(obj, var);
111: }
112:
113: /**
114: * Check if local variable slot defined for object.
115: *
116: * @param obj owning object of slot
117: * @return local variable slot assigned to object, or <code>-1</code> if
118: * none
119: */
120:
121: public int getSlot(Object obj) {
122: if (m_slotMap != null) {
123: LocalVariableGen var = (LocalVariableGen) m_slotMap
124: .get(obj);
125: if (var != null) {
126: return var.getIndex();
127: }
128: }
129: return -1;
130: }
131:
132: /**
133: * Free local variable slot for object. This clears the usage of the slot
134: * (if one has been defined for the object) so it can be reused for other
135: * purposes.
136: *
137: * @param obj owning object of slot
138: */
139:
140: public void freeSlot(Object obj) {
141: if (m_slotMap != null) {
142: LocalVariableGen var = (LocalVariableGen) m_slotMap
143: .get(obj);
144: if (var != null) {
145: var.setEnd(getLastInstruction());
146: m_slotMap.remove(obj);
147: }
148: }
149: }
150:
151: /**
152: * Process accumulated exceptions. Just adds the checked exceptions that
153: * may be thrown within the body to the list for this method, passing them
154: * on to the caller for handling.
155: *
156: * @throws JiBXException on error in exception handling
157: */
158:
159: protected void handleExceptions() throws JiBXException {
160: for (int i = 0; i < m_exceptions.size(); i++) {
161: m_generator.addException((String) m_exceptions.get(i));
162: }
163: }
164: }
|