You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StackMapType.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package org.aspectj.apache.bcel.classfile;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. /* ====================================================================
  6. * The Apache Software License, Version 1.1
  7. *
  8. * Copyright (c) 2001 The Apache Software Foundation. All rights
  9. * reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * 3. The end-user documentation included with the redistribution,
  24. * if any, must include the following acknowledgment:
  25. * "This product includes software developed by the
  26. * Apache Software Foundation (http://www.apache.org/)."
  27. * Alternately, this acknowledgment may appear in the software itself,
  28. * if and wherever such third-party acknowledgments normally appear.
  29. *
  30. * 4. The names "Apache" and "Apache Software Foundation" and
  31. * "Apache BCEL" must not be used to endorse or promote products
  32. * derived from this software without prior written permission. For
  33. * written permission, please contact apache@apache.org.
  34. *
  35. * 5. Products derived from this software may not be called "Apache",
  36. * "Apache BCEL", nor may "Apache" appear in their name, without
  37. * prior written permission of the Apache Software Foundation.
  38. *
  39. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  40. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  41. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  45. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  46. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  47. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  48. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  49. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This software consists of voluntary contributions made by many
  54. * individuals on behalf of the Apache Software Foundation. For more
  55. * information on the Apache Software Foundation, please see
  56. * <http://www.apache.org/>.
  57. */
  58. import org.aspectj.apache.bcel.Constants;
  59. /**
  60. * This class represents the type of a local variable or item on stack
  61. * used in the StackMap entries.
  62. *
  63. * @version $Id: StackMapType.java,v 1.3 2008/05/28 23:53:02 aclement Exp $
  64. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  65. * @see StackMapEntry
  66. * @see StackMap
  67. * @see Constants
  68. */
  69. public final class StackMapType implements Cloneable {
  70. private byte type;
  71. private int index = -1; // Index to CONSTANT_Class or offset
  72. private ConstantPool constant_pool;
  73. /**
  74. * Construct object from file stream.
  75. * @param file Input stream
  76. * @throws IOException
  77. */
  78. StackMapType(DataInputStream file, ConstantPool constant_pool) throws IOException
  79. {
  80. this(file.readByte(), -1, constant_pool);
  81. if(hasIndex())
  82. setIndex(file.readShort());
  83. setConstantPool(constant_pool);
  84. }
  85. /**
  86. * @param type type tag as defined in the Constants interface
  87. * @param index index to constant pool, or byte code offset
  88. */
  89. public StackMapType(byte type, int index, ConstantPool constant_pool) {
  90. setType(type);
  91. setIndex(index);
  92. setConstantPool(constant_pool);
  93. }
  94. public void setType(byte t) {
  95. if((t < Constants.ITEM_Bogus) || (t > Constants.ITEM_NewObject))
  96. throw new RuntimeException("Illegal type for StackMapType: " + t);
  97. type = t;
  98. }
  99. public byte getType() { return type; }
  100. public void setIndex(int t) { index = t; }
  101. /** @return index to constant pool if type == ITEM_Object, or offset
  102. * in byte code, if type == ITEM_NewObject, and -1 otherwise
  103. */
  104. public int getIndex() { return index; }
  105. /**
  106. * Dump type entries to file.
  107. *
  108. * @param file Output file stream
  109. * @throws IOException
  110. */
  111. public final void dump(DataOutputStream file) throws IOException
  112. {
  113. file.writeByte(type);
  114. if(hasIndex())
  115. file.writeShort(getIndex());
  116. }
  117. /** @return true, if type is either ITEM_Object or ITEM_NewObject
  118. */
  119. public final boolean hasIndex() {
  120. return ((type == Constants.ITEM_Object) ||
  121. (type == Constants.ITEM_NewObject));
  122. }
  123. private String printIndex() {
  124. if(type == Constants.ITEM_Object)
  125. return ", class=" + constant_pool.constantToString(index, Constants.CONSTANT_Class);
  126. else if(type == Constants.ITEM_NewObject)
  127. return ", offset=" + index;
  128. else
  129. return "";
  130. }
  131. /**
  132. * @return String representation
  133. */
  134. public final String toString() {
  135. return "(type=" + Constants.ITEM_NAMES[type] + printIndex() + ")";
  136. }
  137. /**
  138. * @return deep copy of this object
  139. */
  140. public StackMapType copy() {
  141. try {
  142. return (StackMapType)clone();
  143. } catch(CloneNotSupportedException e) {}
  144. return null;
  145. }
  146. /**
  147. * @return Constant pool used by this object.
  148. */
  149. public final ConstantPool getConstantPool() { return constant_pool; }
  150. /**
  151. * @param constant_pool Constant pool to be used for this object.
  152. */
  153. public final void setConstantPool(ConstantPool constant_pool) {
  154. this.constant_pool = constant_pool;
  155. }
  156. }