1 26 package net.sf.javaguard.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 32 38 abstract public class CpInfo implements ClassConstants { 39 42 private int tag; 43 44 private int refCount = 0; 45 46 47 48 49 54 public static CpInfo create(DataInput din) 55 throws IOException { 56 if (din == null) throw new IOException("No input stream was provided."); 57 58 CpInfo ci = null; 60 switch (din.readUnsignedByte()) { 61 case CONSTANT_Utf8: 62 ci = new Utf8CpInfo(); 63 break; 64 65 case CONSTANT_Integer: 66 ci = new IntegerCpInfo(); 67 break; 68 case CONSTANT_Float: 69 ci = new FloatCpInfo(); 70 break; 71 case CONSTANT_Long: 72 ci = new LongCpInfo(); 73 break; 74 75 case CONSTANT_Double: 76 ci = new DoubleCpInfo(); 77 break; 78 79 case CONSTANT_Class: 80 ci = new ClassCpInfo(); 81 break; 82 83 case CONSTANT_String: 84 ci = new StringCpInfo(); 85 break; 86 87 case CONSTANT_Fieldref: 88 ci = new FieldrefCpInfo(); 89 break; 90 91 case CONSTANT_Methodref: 92 ci = new MethodrefCpInfo(); 93 break; 94 95 case CONSTANT_InterfaceMethodref: 96 ci = new InterfaceMethodrefCpInfo(); 97 break; 98 99 case CONSTANT_NameAndType: 100 ci = new NameAndTypeCpInfo(); 101 break; 102 103 default: 104 throw new IOException("Unknown tag type in constant pool."); 105 } 106 ci.readInfo(din); 107 return ci; 108 } 109 110 111 112 113 116 protected CpInfo(int tag) { 117 setTag(tag); 118 } 119 120 121 125 private void setTag(int tag) { 126 this.tag = tag; 127 } 128 129 130 134 private int getTag() { 135 return tag; 136 } 137 138 139 144 abstract protected void readInfo(DataInput din) 145 throws IOException; 146 147 148 152 protected void markUtf8Refs(ConstantPool pool) { 153 } 154 155 156 160 protected void markNTRefs(ConstantPool pool) { 161 } 162 163 164 168 public void write(DataOutput dout) 169 throws IOException { 170 if (dout == null) throw new IOException("No output stream was provided."); 171 dout.writeByte(getTag()); 172 writeInfo(dout); 173 } 174 175 176 181 abstract protected void writeInfo(DataOutput dout) 182 throws IOException; 183 184 185 186 187 190 public int getRefCount() { 191 return refCount; 192 } 193 194 195 197 public void incRefCount() { 198 refCount++; 199 } 200 201 202 205 public void decRefCount() 206 throws IllegalStateException { 207 if (refCount == 0) throw new IllegalStateException ("Illegal to decrement ref count that is already zero."); 208 refCount--; 209 } 210 211 212 214 public void resetRefCount() { 215 refCount = 0; 216 } 217 218 219 220 221 226 abstract public void dump(PrintWriter pw, ClassFile cf, int index); 227 } 228 | Popular Tags |