1 17 package org.apache.bcel.classfile; 18 19 import java.io.DataInputStream ; 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 import org.apache.bcel.Constants; 24 import org.apache.bcel.util.BCELComparator; 25 26 34 public abstract class Constant implements Cloneable , Node, Serializable { 35 36 private static BCELComparator _cmp = new BCELComparator() { 37 38 public boolean equals( Object o1, Object o2 ) { 39 Constant THIS = (Constant) o1; 40 Constant THAT = (Constant) o2; 41 return THIS.toString().equals(THAT.toString()); 42 } 43 44 45 public int hashCode( Object o ) { 46 Constant THIS = (Constant) o; 47 return THIS.toString().hashCode(); 48 } 49 }; 50 58 protected byte tag; 59 60 61 Constant(byte tag) { 62 this.tag = tag; 63 } 64 65 66 73 public abstract void accept( Visitor v ); 74 75 76 public abstract void dump( DataOutputStream file ) throws IOException ; 77 78 79 83 public final byte getTag() { 84 return tag; 85 } 86 87 88 91 public String toString() { 92 return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]"; 93 } 94 95 96 99 public Constant copy() { 100 try { 101 return (Constant) super.clone(); 102 } catch (CloneNotSupportedException e) { 103 } 104 return null; 105 } 106 107 108 public Object clone() throws CloneNotSupportedException { 109 return super.clone(); 110 } 111 112 113 119 static final Constant readConstant( DataInputStream file ) throws IOException , 120 ClassFormatException { 121 byte b = file.readByte(); switch (b) { 123 case Constants.CONSTANT_Class: 124 return new ConstantClass(file); 125 case Constants.CONSTANT_Fieldref: 126 return new ConstantFieldref(file); 127 case Constants.CONSTANT_Methodref: 128 return new ConstantMethodref(file); 129 case Constants.CONSTANT_InterfaceMethodref: 130 return new ConstantInterfaceMethodref(file); 131 case Constants.CONSTANT_String: 132 return new ConstantString(file); 133 case Constants.CONSTANT_Integer: 134 return new ConstantInteger(file); 135 case Constants.CONSTANT_Float: 136 return new ConstantFloat(file); 137 case Constants.CONSTANT_Long: 138 return new ConstantLong(file); 139 case Constants.CONSTANT_Double: 140 return new ConstantDouble(file); 141 case Constants.CONSTANT_NameAndType: 142 return new ConstantNameAndType(file); 143 case Constants.CONSTANT_Utf8: 144 return new ConstantUtf8(file); 145 default: 146 throw new ClassFormatException("Invalid byte tag in constant pool: " + b); 147 } 148 } 149 150 151 154 public static BCELComparator getComparator() { 155 return _cmp; 156 } 157 158 159 162 public static void setComparator( BCELComparator comparator ) { 163 _cmp = comparator; 164 } 165 166 167 174 public boolean equals( Object obj ) { 175 return _cmp.equals(this, obj); 176 } 177 178 179 185 public int hashCode() { 186 return _cmp.hashCode(this); 187 } 188 } 189 | Popular Tags |