1 17 package org.apache.bcel.generic; 18 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import org.apache.bcel.classfile.Constant; 22 import org.apache.bcel.classfile.ConstantClass; 23 import org.apache.bcel.classfile.ConstantPool; 24 import org.apache.bcel.util.ByteSequence; 25 26 37 public abstract class CPInstruction extends Instruction implements TypedInstruction, 38 IndexedInstruction { 39 40 protected int index; 42 43 47 CPInstruction() { 48 } 49 50 51 54 protected CPInstruction(short opcode, int index) { 55 super(opcode, (short) 3); 56 setIndex(index); 57 } 58 59 60 64 public void dump( DataOutputStream out ) throws IOException { 65 out.writeByte(opcode); 66 out.writeShort(index); 67 } 68 69 70 79 public String toString( boolean verbose ) { 80 return super.toString(verbose) + " " + index; 81 } 82 83 84 87 public String toString( ConstantPool cp ) { 88 Constant c = cp.getConstant(index); 89 String str = cp.constantToString(c); 90 if (c instanceof ConstantClass) { 91 str = str.replace('.', '/'); 92 } 93 return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + str; 94 } 95 96 97 102 protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException { 103 setIndex(bytes.readUnsignedShort()); 104 length = 3; 105 } 106 107 108 111 public final int getIndex() { 112 return index; 113 } 114 115 116 120 public void setIndex( int index ) { 121 if (index < 0) { 122 throw new ClassGenException("Negative index value: " + index); 123 } 124 this.index = index; 125 } 126 127 128 130 public Type getType( ConstantPoolGen cpg ) { 131 ConstantPool cp = cpg.getConstantPool(); 132 String name = cp.getConstantString(index, org.apache.bcel.Constants.CONSTANT_Class); 133 if (!name.startsWith("[")) { 134 name = "L" + name + ";"; 135 } 136 return Type.getType(name); 137 } 138 } 139 | Popular Tags |