1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 import java.io.*; 58 import com.sun.org.apache.bcel.internal.util.ByteSequence; 59 import com.sun.org.apache.bcel.internal.Constants; 60 import com.sun.org.apache.bcel.internal.classfile.*; 61 62 73 public abstract class CPInstruction extends Instruction 74 implements TypedInstruction, IndexedInstruction 75 { 76 protected int index; 78 82 CPInstruction() {} 83 84 87 protected CPInstruction(short opcode, int index) { 88 super(opcode, (short)3); 89 setIndex(index); 90 } 91 92 96 public void dump(DataOutputStream out) throws IOException { 97 out.writeByte(opcode); 98 out.writeShort(index); 99 } 100 101 110 public String toString(boolean verbose) { 111 return super.toString(verbose) + " " + index; 112 } 113 114 117 public String toString(ConstantPool cp) { 118 Constant c = cp.getConstant(index); 119 String str = cp.constantToString(c); 120 121 if(c instanceof ConstantClass) 122 str = str.replace('.', '/'); 123 124 return com.sun.org.apache.bcel.internal.Constants.OPCODE_NAMES[opcode] + " " + str; 125 } 126 127 132 protected void initFromFile(ByteSequence bytes, boolean wide) 133 throws IOException 134 { 135 setIndex(bytes.readUnsignedShort()); 136 length = 3; 137 } 138 139 142 public final int getIndex() { return index; } 143 144 148 public void setIndex(int index) { 149 if(index < 0) 150 throw new ClassGenException("Negative index value: " + index); 151 152 this.index = index; 153 } 154 155 157 public Type getType(ConstantPoolGen cpg) { 158 ConstantPool cp = cpg.getConstantPool(); 159 String name = cp.getConstantString(index, com.sun.org.apache.bcel.internal.Constants.CONSTANT_Class); 160 161 if(!name.startsWith("[")) 162 name = "L" + name + ";"; 163 164 return Type.getType(name); 165 } 166 } 167 | Popular Tags |