1 17 package org.apache.bcel.generic; 18 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import org.apache.bcel.util.ByteSequence; 22 23 31 public class LDC extends CPInstruction implements PushInstruction, ExceptionThrower, 32 TypedInstruction { 33 34 38 LDC() { 39 } 40 41 42 public LDC(int index) { 43 super(org.apache.bcel.Constants.LDC_W, index); 44 setSize(); 45 } 46 47 48 protected final void setSize() { 50 if (index <= org.apache.bcel.Constants.MAX_BYTE) { opcode = org.apache.bcel.Constants.LDC; 52 length = 2; 53 } else { 54 opcode = org.apache.bcel.Constants.LDC_W; 55 length = 3; 56 } 57 } 58 59 60 64 public void dump( DataOutputStream out ) throws IOException { 65 out.writeByte(opcode); 66 if (length == 2) { 67 out.writeByte(index); 68 } else { 69 out.writeShort(index); 70 } 71 } 72 73 74 77 public final void setIndex( int index ) { 78 super.setIndex(index); 79 setSize(); 80 } 81 82 83 86 protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException { 87 length = 2; 88 index = bytes.readUnsignedByte(); 89 } 90 91 92 public Object getValue( ConstantPoolGen cpg ) { 93 org.apache.bcel.classfile.Constant c = cpg.getConstantPool().getConstant(index); 94 switch (c.getTag()) { 95 case org.apache.bcel.Constants.CONSTANT_String: 96 int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex(); 97 c = cpg.getConstantPool().getConstant(i); 98 return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes(); 99 case org.apache.bcel.Constants.CONSTANT_Float: 100 return new Float (((org.apache.bcel.classfile.ConstantFloat) c).getBytes()); 101 case org.apache.bcel.Constants.CONSTANT_Integer: 102 return new Integer (((org.apache.bcel.classfile.ConstantInteger) c).getBytes()); 103 case org.apache.bcel.Constants.CONSTANT_Class: 104 return c; 105 default: throw new RuntimeException ("Unknown or invalid constant type at " + index); 107 } 108 } 109 110 111 public Type getType( ConstantPoolGen cpg ) { 112 switch (cpg.getConstantPool().getConstant(index).getTag()) { 113 case org.apache.bcel.Constants.CONSTANT_String: 114 return Type.STRING; 115 case org.apache.bcel.Constants.CONSTANT_Float: 116 return Type.FLOAT; 117 case org.apache.bcel.Constants.CONSTANT_Integer: 118 return Type.INT; 119 case org.apache.bcel.Constants.CONSTANT_Class: 120 return Type.CLASS; 121 default: throw new RuntimeException ("Unknown or invalid constant type at " + index); 123 } 124 } 125 126 127 public Class [] getExceptions() { 128 return org.apache.bcel.ExceptionConstants.EXCS_STRING_RESOLUTION; 129 } 130 131 132 140 public void accept( Visitor v ) { 141 v.visitStackProducer(this); 142 v.visitPushInstruction(this); 143 v.visitExceptionThrower(this); 144 v.visitTypedInstruction(this); 145 v.visitCPInstruction(this); 146 v.visitLDC(this); 147 } 148 } 149 | Popular Tags |