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.

InstructionCP.java 6.9KB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package org.aspectj.apache.bcel.generic;
  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 java.io.DataOutputStream;
  56. import java.io.IOException;
  57. import org.aspectj.apache.bcel.Constants;
  58. import org.aspectj.apache.bcel.classfile.Constant;
  59. import org.aspectj.apache.bcel.classfile.ConstantClass;
  60. import org.aspectj.apache.bcel.classfile.ConstantDouble;
  61. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  62. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  63. import org.aspectj.apache.bcel.classfile.ConstantLong;
  64. import org.aspectj.apache.bcel.classfile.ConstantPool;
  65. import org.aspectj.apache.bcel.classfile.ConstantString;
  66. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  67. /**
  68. * Class for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc.
  69. *
  70. * @version $Id: InstructionCP.java,v 1.6 2009/10/05 17:35:36 aclement Exp $
  71. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  72. */
  73. public class InstructionCP extends Instruction {
  74. protected int index;
  75. public InstructionCP(short opcode, int index) {
  76. super(opcode);
  77. this.index = index;
  78. }
  79. @Override
  80. public void dump(DataOutputStream out) throws IOException {
  81. if (opcode == LDC_W && index < 256) {
  82. out.writeByte(LDC);
  83. out.writeByte(index);
  84. } else {
  85. out.writeByte(opcode);
  86. if (Constants.iLen[opcode] == 2) {
  87. if (index > 255) {
  88. throw new IllegalStateException();
  89. }
  90. out.writeByte(index);
  91. } else {
  92. out.writeShort(index);
  93. }
  94. }
  95. }
  96. @Override
  97. public int getLength() {
  98. if (opcode == LDC_W && index < 256) {
  99. return 2;
  100. } else {
  101. return super.getLength();
  102. }
  103. }
  104. /**
  105. * Long output format:
  106. *
  107. * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool
  108. * index&gt;"&gt;"
  109. *
  110. * @param verbose long/short format switch
  111. * @return mnemonic for instruction
  112. */
  113. @Override
  114. public String toString(boolean verbose) {
  115. return super.toString(verbose) + " " + index;
  116. }
  117. /**
  118. * @return mnemonic for instruction with symbolic references resolved
  119. */
  120. public String toString(ConstantPool cp) {
  121. Constant c = cp.getConstant(index);
  122. String str = cp.constantToString(c);
  123. if (c instanceof ConstantClass) {
  124. str = str.replace('.', '/');
  125. }
  126. return org.aspectj.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + str;
  127. }
  128. /**
  129. * @return index in constant pool referred by this instruction.
  130. */
  131. @Override
  132. public final int getIndex() {
  133. return index;
  134. }
  135. @Override
  136. public void setIndex(int index) {
  137. this.index = index;
  138. if (this.index > 255 && opcode == LDC) {
  139. // promote it
  140. opcode = LDC_W;
  141. }
  142. }
  143. @Override
  144. public Type getType(ConstantPool cpg) {
  145. switch (cpg.getConstant(index).getTag()) {
  146. case CONSTANT_String:
  147. return Type.STRING;
  148. case CONSTANT_Float:
  149. return Type.FLOAT;
  150. case CONSTANT_Integer:
  151. return Type.INT;
  152. case CONSTANT_Long:
  153. return Type.LONG;
  154. case CONSTANT_Double:
  155. return Type.DOUBLE;
  156. case CONSTANT_Class:
  157. String name = cpg.getConstantString_CONSTANTClass(index);
  158. // ConstantPool cp = cpg.getConstantPool();
  159. // String name = cp.getConstantString(index, CONSTANT_Class);
  160. if (!name.startsWith("[")) {
  161. StringBuilder sb = new StringBuilder();
  162. sb.append("L").append(name).append(";");
  163. return Type.getType(sb.toString());
  164. } else {
  165. return Type.getType(name);
  166. }
  167. default:
  168. throw new RuntimeException("Unknown or invalid constant type at " + index);
  169. }
  170. }
  171. @Override
  172. public Object getValue(ConstantPool constantPool) {
  173. Constant constant = constantPool.getConstant(index);
  174. switch (constant.getTag()) {
  175. case Constants.CONSTANT_String:
  176. int i = ((ConstantString) constant).getStringIndex();
  177. constant = constantPool.getConstant(i);
  178. return ((ConstantUtf8) constant).getValue();
  179. case Constants.CONSTANT_Float:
  180. return ((ConstantFloat) constant).getValue();
  181. case Constants.CONSTANT_Integer:
  182. return ((ConstantInteger) constant).getValue();
  183. case Constants.CONSTANT_Long:
  184. return ((ConstantLong) constant).getValue();
  185. case Constants.CONSTANT_Double:
  186. return ((ConstantDouble) constant).getValue();
  187. default:
  188. throw new RuntimeException("Unknown or invalid constant type at " + index);
  189. }
  190. }
  191. public boolean equals(Object other) {
  192. if (!(other instanceof InstructionCP)) {
  193. return false;
  194. }
  195. InstructionCP o = (InstructionCP) other;
  196. return o.opcode == opcode && o.index == index;
  197. }
  198. public int hashCode() {
  199. return opcode * 37 + index;
  200. }
  201. }