1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import java.io.*; 59 60 81 public abstract class Attribute implements Cloneable , Node { 82 protected int name_index; protected int length; protected byte tag; protected ConstantPool constant_pool; 86 87 Attribute(byte tag, int name_index, int length, ConstantPool constant_pool) { 88 this.tag = tag; 89 this.name_index = name_index; 90 this.length = length; 91 this.constant_pool = constant_pool; 92 } 93 94 101 public abstract void accept(Visitor v); 102 103 109 public void dump(DataOutputStream file) throws IOException 110 { 111 file.writeShort(name_index); 112 file.writeInt(length); 113 } 114 115 128 static final Attribute readAttribute(DataInputStream file, 129 ConstantPool constant_pool) 130 throws IOException, ClassFormatError , InternalError 131 { 132 ConstantUtf8 c; 133 String name; 134 int name_index; 135 int length; 136 byte tag = Constants.ATTR_UNKNOWN; 138 name_index = (int)(file.readUnsignedShort()); 140 c = (ConstantUtf8)constant_pool.getConstant(name_index, 141 Constants.CONSTANT_Utf8); 142 name = c.getBytes(); 143 144 length = file.readInt(); 146 147 for(byte i=0; i < Constants.KNOWN_ATTRIBUTES; i++) { 149 if(name.equals(Constants.ATTRIBUTE_NAMES[i])) { 150 tag = i; break; 152 } 153 } 154 155 switch(tag) { 157 case Constants.ATTR_UNKNOWN: 158 return new Unknown(name_index, length, file, constant_pool); 159 160 case Constants.ATTR_CONSTANT_VALUE: 161 return new ConstantValue(name_index, length, file, constant_pool); 162 163 case Constants.ATTR_SOURCE_FILE: 164 return new SourceFile(name_index, length, file, constant_pool); 165 166 case Constants.ATTR_CODE: 167 return new Code(name_index, length, file, constant_pool); 168 169 case Constants.ATTR_EXCEPTIONS: 170 return new ExceptionTable(name_index, length, file, constant_pool); 171 172 case Constants.ATTR_LINE_NUMBER_TABLE: 173 return new LineNumberTable(name_index, length, file, constant_pool); 174 175 case Constants.ATTR_LOCAL_VARIABLE_TABLE: 176 return new LocalVariableTable(name_index, length, file, constant_pool); 177 178 case Constants.ATTR_INNER_CLASSES: 179 return new InnerClasses(name_index, length, file, constant_pool); 180 181 case Constants.ATTR_SYNTHETIC: 182 return new Synthetic(name_index, length, file, constant_pool); 183 184 case Constants.ATTR_DEPRECATED: 185 return new Deprecated (name_index, length, file, constant_pool); 186 187 case Constants.ATTR_PMG: 188 return new PMGClass(name_index, length, file, constant_pool); 189 190 case Constants.ATTR_SIGNATURE: 191 return new Signature(name_index, length, file, constant_pool); 192 193 case Constants.ATTR_STACK_MAP: 194 return new StackMap(name_index, length, file, constant_pool); 195 196 default: throw new InternalError ("Ooops! default case reached."); 198 } 199 } 200 201 204 public final int getLength() { return length; } 205 206 209 public final void setLength(int length) { 210 this.length = length; 211 } 212 213 216 public final void setNameIndex(int name_index) { 217 this.name_index = name_index; 218 } 219 220 223 public final int getNameIndex() { return name_index; } 224 225 229 public final byte getTag() { return tag; } 230 231 235 public final ConstantPool getConstantPool() { return constant_pool; } 236 237 241 public final void setConstantPool(ConstantPool constant_pool) { 242 this.constant_pool = constant_pool; 243 } 244 245 251 public Object clone() { 252 Object o = null; 253 254 try { 255 o = super.clone(); 256 } catch(CloneNotSupportedException e) { 257 e.printStackTrace(); } 259 260 return o; 261 } 262 263 266 public abstract Attribute copy(ConstantPool constant_pool); 267 268 271 public String toString() { 272 return Constants.ATTRIBUTE_NAMES[tag]; 273 } 274 } 275 | Popular Tags |