1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 import java.io.*; 57 import com.sun.org.apache.bcel.internal.util.ByteSequence; 58 59 67 public class LDC extends CPInstruction 68 implements PushInstruction, ExceptionThrower, TypedInstruction { 69 73 LDC() {} 74 75 public LDC(int index) { 76 super(com.sun.org.apache.bcel.internal.Constants.LDC_W, index); 77 setSize(); 78 } 79 80 protected final void setSize() { 82 if(index <= com.sun.org.apache.bcel.internal.Constants.MAX_BYTE) { opcode = com.sun.org.apache.bcel.internal.Constants.LDC; 84 length = 2; 85 } else { 86 opcode = com.sun.org.apache.bcel.internal.Constants.LDC_W; 87 length = 3; 88 } 89 } 90 91 95 public void dump(DataOutputStream out) throws IOException { 96 out.writeByte(opcode); 97 98 if(length == 2) 99 out.writeByte(index); 100 else out.writeShort(index); 102 } 103 104 107 public final void setIndex(int index) { 108 super.setIndex(index); 109 setSize(); 110 } 111 112 115 protected void initFromFile(ByteSequence bytes, boolean wide) 116 throws IOException 117 { 118 length = 2; 119 index = bytes.readUnsignedByte(); 120 } 121 122 public Object getValue(ConstantPoolGen cpg) { 123 com.sun.org.apache.bcel.internal.classfile.Constant c = cpg.getConstantPool().getConstant(index); 124 125 switch(c.getTag()) { 126 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_String: 127 int i = ((com.sun.org.apache.bcel.internal.classfile.ConstantString)c).getStringIndex(); 128 c = cpg.getConstantPool().getConstant(i); 129 return ((com.sun.org.apache.bcel.internal.classfile.ConstantUtf8)c).getBytes(); 130 131 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Float: 132 return new Float (((com.sun.org.apache.bcel.internal.classfile.ConstantFloat)c).getBytes()); 133 134 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Integer: 135 return new Integer (((com.sun.org.apache.bcel.internal.classfile.ConstantInteger)c).getBytes()); 136 137 default: throw new RuntimeException ("Unknown or invalid constant type at " + index); 139 } 140 } 141 142 public Type getType(ConstantPoolGen cpg) { 143 switch(cpg.getConstantPool().getConstant(index).getTag()) { 144 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_String: return Type.STRING; 145 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Float: return Type.FLOAT; 146 case com.sun.org.apache.bcel.internal.Constants.CONSTANT_Integer: return Type.INT; 147 default: throw new RuntimeException ("Unknown or invalid constant type at " + index); 149 } 150 } 151 152 public Class [] getExceptions() { 153 return com.sun.org.apache.bcel.internal.ExceptionConstants.EXCS_STRING_RESOLUTION; 154 } 155 156 164 public void accept(Visitor v) { 165 v.visitStackProducer(this); 166 v.visitPushInstruction(this); 167 v.visitExceptionThrower(this); 168 v.visitTypedInstruction(this); 169 v.visitCPInstruction(this); 170 v.visitLDC(this); 171 } 172 } 173 | Popular Tags |