00001 package de.fub.bytecode.generic;
00002
00003 import java.io.*;
00004 import de.fub.bytecode.util.ByteSequence;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 public class LDC extends CPInstruction
00015 implements PushInstruction, ExceptionThrower, TypedInstruction {
00016
00017
00018
00019
00020 LDC() {}
00021 public LDC(int index) {
00022 super(de.fub.bytecode.Constants.LDC_W, index);
00023 setSize();
00024 }
00025
00026
00027
00028
00029
00030
00031
00032
00033 public void accept(Visitor v) {
00034 v.visitStackProducer(this);
00035 v.visitPushInstruction(this);
00036 v.visitExceptionThrower(this);
00037 v.visitTypedInstruction(this);
00038 v.visitCPInstruction(this);
00039 v.visitLDC(this);
00040 }
00041
00042
00043
00044
00045 public void dump(DataOutputStream out) throws IOException {
00046 out.writeByte(tag);
00047
00048 if(length == 2)
00049 out.writeByte(index);
00050 else
00051 out.writeShort(index);
00052 }
00053 public Class[] getExceptions() {
00054 return de.fub.bytecode.ExceptionConstants.EXCS_STRING_RESOLUTION;
00055 }
00056 public Type getType(ConstantPoolGen cpg) {
00057 switch(cpg.getConstantPool().getConstant(index).getTag()) {
00058 case de.fub.bytecode.Constants.CONSTANT_String: return Type.STRING;
00059 case de.fub.bytecode.Constants.CONSTANT_Float: return Type.FLOAT;
00060 case de.fub.bytecode.Constants.CONSTANT_Integer: return Type.INT;
00061 default:
00062 throw new RuntimeException("Unknown or invalid constant type at " + index);
00063 }
00064 }
00065 public Object getValue(ConstantPoolGen cpg) {
00066 de.fub.bytecode.classfile.Constant c = cpg.getConstantPool().getConstant(index);
00067
00068 switch(c.getTag()) {
00069 case de.fub.bytecode.Constants.CONSTANT_String:
00070 int i = ((de.fub.bytecode.classfile.ConstantString)c).getStringIndex();
00071 c = cpg.getConstantPool().getConstant(i);
00072 return ((de.fub.bytecode.classfile.ConstantUtf8)c).getBytes();
00073
00074 case de.fub.bytecode.Constants.CONSTANT_Float:
00075 return new Float(((de.fub.bytecode.classfile.ConstantFloat)c).getBytes());
00076
00077 case de.fub.bytecode.Constants.CONSTANT_Integer:
00078 return new Integer(((de.fub.bytecode.classfile.ConstantInteger)c).getBytes());
00079
00080 default:
00081 throw new RuntimeException("Unknown or invalid constant type at " + index);
00082 }
00083 }
00084
00085
00086
00087 protected void initFromFile(ByteSequence bytes, boolean wide)
00088 throws IOException
00089 {
00090 length = 2;
00091 index = bytes.readUnsignedByte();
00092 }
00093
00094
00095
00096 public final void setIndex(int index) {
00097 super.setIndex(index);
00098 setSize();
00099 }
00100
00101 protected final void setSize() {
00102 if(index <= de.fub.bytecode.Constants.MAX_BYTE) {
00103 tag = de.fub.bytecode.Constants.LDC;
00104 length = 2;
00105 } else {
00106 tag = de.fub.bytecode.Constants.LDC_W;
00107 length = 3;
00108 }
00109 }
00110 }