00001 package de.fub.bytecode.classfile;
00002
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 public abstract class Constant implements Cloneable {
00026
00027
00028
00029
00030
00031
00032
00033
00034 protected byte tag;
00035
00036 Constant(byte tag) { this.tag = tag; }
00037
00038
00039
00040
00041
00042
00043
00044 public abstract void accept(Visitor v);
00045 public Object clone() throws CloneNotSupportedException {
00046 return super.clone();
00047 }
00048
00049
00050
00051 public Constant copy() {
00052 try {
00053 return (Constant)super.clone();
00054 } catch(CloneNotSupportedException e) {}
00055
00056 return null;
00057 }
00058 public abstract void dump(DataOutputStream file) throws IOException;
00059
00060
00061
00062
00063 public final byte getTag() { return tag; }
00064
00065
00066
00067
00068
00069
00070 static final Constant readConstant(DataInputStream file)
00071 throws IOException, ClassFormatError
00072 {
00073 byte b = file.readByte();
00074
00075 switch(b) {
00076 case Constants.CONSTANT_Class: return new ConstantClass(file);
00077 case Constants.CONSTANT_Fieldref: return new ConstantFieldref(file);
00078 case Constants.CONSTANT_Methodref: return new ConstantMethodref(file);
00079 case Constants.CONSTANT_InterfaceMethodref: return new
00080 ConstantInterfaceMethodref(file);
00081 case Constants.CONSTANT_String: return new ConstantString(file);
00082 case Constants.CONSTANT_Integer: return new ConstantInteger(file);
00083 case Constants.CONSTANT_Float: return new ConstantFloat(file);
00084 case Constants.CONSTANT_Long: return new ConstantLong(file);
00085 case Constants.CONSTANT_Double: return new ConstantDouble(file);
00086 case Constants.CONSTANT_NameAndType: return new ConstantNameAndType(file);
00087 case Constants.CONSTANT_Utf8: return new ConstantUtf8(file);
00088 default:
00089 throw new ClassFormatError("Invalid byte tag in constant pool: " + b);
00090 }
00091 }
00092
00093
00094
00095 public String toString() {
00096 return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]";
00097 }
00098 }