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 java.util.HashMap ; 24 import java.util.Map ; 25 import org.apache.bcel.Constants; 26 27 49 public abstract class Attribute implements Cloneable , Node, Serializable { 50 51 protected int name_index; protected int length; protected byte tag; protected ConstantPool constant_pool; 55 56 57 protected Attribute(byte tag, int name_index, int length, ConstantPool constant_pool) { 58 this.tag = tag; 59 this.name_index = name_index; 60 this.length = length; 61 this.constant_pool = constant_pool; 62 } 63 64 65 72 public abstract void accept( Visitor v ); 73 74 75 81 public void dump( DataOutputStream file ) throws IOException { 82 file.writeShort(name_index); 83 file.writeInt(length); 84 } 85 86 private static Map readers = new HashMap (); 87 88 89 96 public static void addAttributeReader( String name, AttributeReader r ) { 97 readers.put(name, r); 98 } 99 100 101 105 public static void removeAttributeReader( String name ) { 106 readers.remove(name); 107 } 108 109 110 122 public static final Attribute readAttribute( DataInputStream file, ConstantPool constant_pool ) 123 throws IOException , ClassFormatException { 124 ConstantUtf8 c; 125 String name; 126 int name_index; 127 int length; 128 byte tag = Constants.ATTR_UNKNOWN; name_index = file.readUnsignedShort(); 131 c = (ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8); 132 name = c.getBytes(); 133 length = file.readInt(); 135 for (byte i = 0; i < Constants.KNOWN_ATTRIBUTES; i++) { 137 if (name.equals(Constants.ATTRIBUTE_NAMES[i])) { 138 tag = i; break; 140 } 141 } 142 switch (tag) { 144 case Constants.ATTR_UNKNOWN: 145 AttributeReader r = (AttributeReader) readers.get(name); 146 if (r != null) { 147 return r.createAttribute(name_index, length, file, constant_pool); 148 } 149 return new Unknown(name_index, length, file, constant_pool); 150 case Constants.ATTR_CONSTANT_VALUE: 151 return new ConstantValue(name_index, length, file, constant_pool); 152 case Constants.ATTR_SOURCE_FILE: 153 return new SourceFile(name_index, length, file, constant_pool); 154 case Constants.ATTR_CODE: 155 return new Code(name_index, length, file, constant_pool); 156 case Constants.ATTR_EXCEPTIONS: 157 return new ExceptionTable(name_index, length, file, constant_pool); 158 case Constants.ATTR_LINE_NUMBER_TABLE: 159 return new LineNumberTable(name_index, length, file, constant_pool); 160 case Constants.ATTR_LOCAL_VARIABLE_TABLE: 161 return new LocalVariableTable(name_index, length, file, constant_pool); 162 case Constants.ATTR_INNER_CLASSES: 163 return new InnerClasses(name_index, length, file, constant_pool); 164 case Constants.ATTR_SYNTHETIC: 165 return new Synthetic(name_index, length, file, constant_pool); 166 case Constants.ATTR_DEPRECATED: 167 return new Deprecated (name_index, length, file, constant_pool); 168 case Constants.ATTR_PMG: 169 return new PMGClass(name_index, length, file, constant_pool); 170 case Constants.ATTR_SIGNATURE: 171 return new Signature(name_index, length, file, constant_pool); 172 case Constants.ATTR_STACK_MAP: 173 return new StackMap(name_index, length, file, constant_pool); 174 default: throw new IllegalStateException ("Ooops! default case reached."); 186 } 187 } 188 189 190 193 public final int getLength() { 194 return length; 195 } 196 197 198 201 public final void setLength( int length ) { 202 this.length = length; 203 } 204 205 206 209 public final void setNameIndex( int name_index ) { 210 this.name_index = name_index; 211 } 212 213 214 217 public final int getNameIndex() { 218 return name_index; 219 } 220 221 222 226 public final byte getTag() { 227 return tag; 228 } 229 230 231 235 public final ConstantPool getConstantPool() { 236 return constant_pool; 237 } 238 239 240 244 public final void setConstantPool( ConstantPool constant_pool ) { 245 this.constant_pool = constant_pool; 246 } 247 248 249 255 public Object clone() { 256 Object o = null; 257 try { 258 o = super.clone(); 259 } catch (CloneNotSupportedException e) { 260 e.printStackTrace(); } 262 return o; 263 } 264 265 266 269 public abstract Attribute copy( ConstantPool _constant_pool ); 270 271 272 275 public String toString() { 276 return Constants.ATTRIBUTE_NAMES[tag]; 277 } 278 } 279 | Popular Tags |