1. package com.sun.org.apache.bcel.internal.classfile;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import com.sun.org.apache.bcel.internal.Constants;
  56. import java.io.*;
  57. /**
  58. * This class represents a stack map entry recording the types of
  59. * local variables and the the of stack items at a given byte code offset.
  60. * See CLDC specification ÷5.3.1.2
  61. *
  62. * @version $Id: StackMapEntry.java,v 1.1.1.1 2001/10/29 20:00:03 jvanzyl Exp $
  63. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  64. * @see StackMap
  65. * @see StackMapType
  66. */
  67. public final class StackMapEntry implements Cloneable {
  68. private int byte_code_offset;
  69. private int number_of_locals;
  70. private StackMapType[] types_of_locals;
  71. private int number_of_stack_items;
  72. private StackMapType[] types_of_stack_items;
  73. private ConstantPool constant_pool;
  74. /**
  75. * Construct object from file stream.
  76. * @param file Input stream
  77. * @throw IOException
  78. */
  79. StackMapEntry(DataInputStream file, ConstantPool constant_pool) throws IOException
  80. {
  81. this(file.readShort(), file.readShort(), null, -1, null, constant_pool);
  82. types_of_locals = new StackMapType[number_of_locals];
  83. for(int i=0; i < number_of_locals; i++)
  84. types_of_locals[i] = new StackMapType(file, constant_pool);
  85. number_of_stack_items = file.readShort();
  86. types_of_stack_items = new StackMapType[number_of_stack_items];
  87. for(int i=0; i < number_of_stack_items; i++)
  88. types_of_stack_items[i] = new StackMapType(file, constant_pool);
  89. }
  90. public StackMapEntry(int byte_code_offset, int number_of_locals,
  91. StackMapType[] types_of_locals,
  92. int number_of_stack_items,
  93. StackMapType[] types_of_stack_items,
  94. ConstantPool constant_pool) {
  95. this.byte_code_offset = byte_code_offset;
  96. this.number_of_locals = number_of_locals;
  97. this.types_of_locals = types_of_locals;
  98. this.number_of_stack_items = number_of_stack_items;
  99. this.types_of_stack_items = types_of_stack_items;
  100. this.constant_pool = constant_pool;
  101. }
  102. /**
  103. * Dump stack map entry
  104. *
  105. * @param file Output file stream
  106. * @throw IOException
  107. */
  108. public final void dump(DataOutputStream file) throws IOException
  109. {
  110. file.writeShort(byte_code_offset);
  111. file.writeShort(number_of_locals);
  112. for(int i=0; i < number_of_locals; i++)
  113. types_of_locals[i].dump(file);
  114. file.writeShort(number_of_stack_items);
  115. for(int i=0; i < number_of_stack_items; i++)
  116. types_of_stack_items[i].dump(file);
  117. }
  118. /**
  119. * @return String representation.
  120. */
  121. public final String toString() {
  122. StringBuffer buf = new StringBuffer("(offset=" + byte_code_offset);
  123. if(number_of_locals > 0) {
  124. buf.append(", locals={");
  125. for(int i=0; i < number_of_locals; i++) {
  126. buf.append(types_of_locals[i]);
  127. if(i < number_of_locals - 1)
  128. buf.append(", ");
  129. }
  130. buf.append("}");
  131. }
  132. if(number_of_stack_items > 0) {
  133. buf.append(", stack items={");
  134. for(int i=0; i < number_of_stack_items; i++) {
  135. buf.append(types_of_stack_items[i]);
  136. if(i < number_of_stack_items - 1)
  137. buf.append(", ");
  138. }
  139. buf.append("}");
  140. }
  141. buf.append(")");
  142. return buf.toString();
  143. }
  144. public void setByteCodeOffset(int b) { byte_code_offset = b; }
  145. public int getByteCodeOffset() { return byte_code_offset; }
  146. public void setNumberOfLocals(int n) { number_of_locals = n; }
  147. public int getNumberOfLocals() { return number_of_locals; }
  148. public void setTypesOfLocals(StackMapType[] t) { types_of_locals = t; }
  149. public StackMapType[] getTypesOfLocals() { return types_of_locals; }
  150. public void setNumberOfStackItems(int n) { number_of_stack_items = n; }
  151. public int getNumberOfStackItems() { return number_of_stack_items; }
  152. public void setTypesOfStackItems(StackMapType[] t) { types_of_stack_items = t; }
  153. public StackMapType[] getTypesOfStackItems() { return types_of_stack_items; }
  154. /**
  155. * @return deep copy of this object
  156. */
  157. public StackMapEntry copy() {
  158. try {
  159. return (StackMapEntry)clone();
  160. } catch(CloneNotSupportedException e) {}
  161. return null;
  162. }
  163. /**
  164. * Called by objects that are traversing the nodes of the tree implicitely
  165. * defined by the contents of a Java class. I.e., the hierarchy of methods,
  166. * fields, attributes, etc. spawns a tree of objects.
  167. *
  168. * @param v Visitor object
  169. */
  170. public void accept(Visitor v) {
  171. v.visitStackMapEntry(this);
  172. }
  173. /**
  174. * @return Constant pool used by this object.
  175. */
  176. public final ConstantPool getConstantPool() { return constant_pool; }
  177. /**
  178. * @param constant_pool Constant pool to be used for this object.
  179. */
  180. public final void setConstantPool(ConstantPool constant_pool) {
  181. this.constant_pool = constant_pool;
  182. }
  183. }