1 26 package net.sf.javaguard.classfile; 27 28 import java.io.*; 29 30 31 37 public abstract class AttrInfo implements ClassConstants { 38 39 public static final int CONSTANT_FIELD_SIZE = 6; 40 41 42 private int attrNameIndex; 43 44 private int attrInfoLength; 45 46 private byte attrInfo[]; 47 48 protected ClassFile owner; 49 50 51 52 53 61 public static AttrInfo create(DataInput din, ClassFile cf) 62 throws IOException, IllegalStateException { 63 if (din == null) throw new IOException("No input stream was provided."); 64 65 AttrInfo ai = null; 67 int attrNameIndex = din.readUnsignedShort(); 68 int attrLength = din.readInt(); 69 CpInfo cpInfo = cf.getCpEntry(attrNameIndex); 70 if (cpInfo instanceof Utf8CpInfo) { 71 String attrName = ((Utf8CpInfo)cpInfo).getString(); 72 if (attrName.equals(ATTR_Code)) { 73 ai = new CodeAttrInfo(cf, attrNameIndex, attrLength); 74 } 75 else if (attrName.equals(ATTR_ConstantValue)) { 76 ai = new ConstantValueAttrInfo(cf, attrNameIndex, attrLength); 77 } 78 else if (attrName.equals(ATTR_Exceptions)) { 79 ai = new ExceptionsAttrInfo(cf, attrNameIndex, attrLength); 80 } 81 else if (attrName.equals(ATTR_LineNumberTable)) { 82 ai = new LineNumberTableAttrInfo(cf, attrNameIndex, attrLength); 83 } 84 else if (attrName.equals(ATTR_SourceFile)) { 85 ai = new SourceFileAttrInfo(cf, attrNameIndex, attrLength); 86 } 87 else if (attrName.equals(ATTR_LocalVariableTable)) { 88 ai = new LocalVariableTableAttrInfo(cf, attrNameIndex, attrLength); 89 } 90 else if (attrName.equals(ATTR_InnerClasses)) { 91 ai = new InnerClassesAttrInfo(cf, attrNameIndex, attrLength); 92 } 93 else if (attrName.equals(ATTR_Synthetic)) { 94 ai = new SyntheticAttrInfo(cf, attrNameIndex, attrLength); 95 } 96 else { 97 ai = new UnknownAttrInfo(cf, attrNameIndex, attrLength); 98 } 99 } 100 else { 101 throw new IllegalStateException ("Inconsistent reference to Constant Pool."); 102 } 103 ai.readInfo(din); 104 return ai; 105 } 106 107 108 109 110 115 protected AttrInfo(ClassFile cf, int attrNameIndex, int attrLength) { 116 owner = cf; 117 setAttrNameIndex(attrNameIndex); 118 setAttrInfoLength(attrLength); 119 } 120 121 122 123 124 128 protected void setAttrInfoLength(int len) { 129 attrInfoLength = len; 130 } 131 132 133 138 protected int getAttrInfoLength() { 139 return attrInfoLength; 140 } 141 142 143 144 145 148 abstract protected String getAttrName(); 149 150 151 152 153 156 protected void trimAttrsExcept(String [] keepAttrs) { 157 } 158 159 160 161 162 165 protected void markUtf8Refs(ConstantPool pool) { 166 pool.incRefCount(getAttrNameIndex()); 167 markUtf8RefsInInfo(pool); 168 } 169 170 171 172 173 177 protected void markUtf8RefsInInfo(ConstantPool pool) { 178 } 179 180 181 182 183 187 protected void setAttrNameIndex(int index) { 188 this.attrNameIndex = index; 189 } 190 191 192 196 protected int getAttrNameIndex() { 197 return attrNameIndex; 198 } 199 200 201 202 203 207 protected void setAttrInfo(byte[] info) { 208 attrInfo = info; 209 } 210 211 212 216 protected byte[] getAttrInfo() { 217 return attrInfo; 218 } 219 220 221 222 223 227 protected void readInfo(DataInput din) 228 throws IOException { 229 byte[] info = new byte[getAttrInfoLength()]; 230 din.readFully(info); 231 setAttrInfo(info); 232 } 233 234 235 239 public final void write(DataOutput dout) 240 throws IOException { 241 if (dout == null) throw new IOException("No output stream was provided."); 242 dout.writeShort(getAttrNameIndex()); 243 dout.writeInt(getAttrInfoLength()); 244 writeInfo(dout); 245 } 246 247 248 253 public void writeInfo(DataOutput dout) 254 throws IOException { 255 dout.write(getAttrInfo()); 256 } 257 258 259 260 261 265 abstract public void dump(PrintWriter pw, ClassFile cf); 266 } 267 | Popular Tags |