1 52 53 package com.go.trove.classfile; 54 55 import java.io.*; 56 57 66 public abstract class Attribute { 67 final static Attribute[] NO_ATTRIBUTES = new Attribute[0]; 68 69 final static String CODE = "Code"; 70 final static String CONSTANT_VALUE = "ConstantValue"; 71 final static String DEPRECATED = "Deprecated"; 72 final static String EXCEPTIONS = "Exceptions"; 73 final static String INNER_CLASSES = "InnerClasses"; 74 final static String LINE_NUMBER_TABLE = "LineNumberTable"; 75 final static String LOCAL_VARIABLE_TABLE = "LocalVariableTable"; 76 final static String SOURCE_FILE = "SourceFile"; 77 final static String SYNTHETIC = "Synthetic"; 78 79 80 protected final ConstantPool mCp; 81 82 private String mName; 83 private ConstantUTFInfo mNameConstant; 84 85 protected Attribute(ConstantPool cp, String name) { 86 mCp = cp; 87 mName = name; 88 mNameConstant = ConstantUTFInfo.make(cp, name); 89 } 90 91 94 public ConstantPool getConstantPool() { 95 return mCp; 96 } 97 98 101 public String getName() { 102 return mName; 103 } 104 105 public ConstantUTFInfo getNameConstant() { 106 return mNameConstant; 107 } 108 109 113 public Attribute[] getAttributes() { 114 return NO_ATTRIBUTES; 115 } 116 117 120 public abstract int getLength(); 121 122 126 public final void writeTo(DataOutput dout) throws IOException { 127 dout.writeShort(mNameConstant.getIndex()); 128 dout.writeInt(getLength()); 129 writeDataTo(dout); 130 } 131 132 136 public void writeDataTo(DataOutput dout) throws IOException { 137 } 138 139 142 public static Attribute readFrom(ConstantPool cp, 143 DataInput din, 144 AttributeFactory attrFactory) 145 throws IOException 146 { 147 int index = din.readUnsignedShort(); 148 String name = ((ConstantUTFInfo)cp.getConstant(index)).getValue(); 149 int length = din.readInt(); 150 151 attrFactory = new Factory(attrFactory); 152 return attrFactory.createAttribute(cp, name, length, din); 153 } 154 155 private static class Factory implements AttributeFactory { 156 private final AttributeFactory mAttrFactory; 157 158 public Factory(AttributeFactory attrFactory) { 159 mAttrFactory = attrFactory; 160 } 161 162 public Attribute createAttribute(ConstantPool cp, 163 String name, 164 final int length, 165 DataInput din) throws IOException { 166 if (name.equals(CODE)) { 167 return CodeAttr.define(cp, name, length, din, mAttrFactory); 168 } 169 else if (name.equals(CONSTANT_VALUE)) { 170 return ConstantValueAttr.define(cp, name, length, din); 171 } 172 else if (name.equals(DEPRECATED)) { 173 return DeprecatedAttr.define(cp, name, length, din); 174 } 175 else if (name.equals(EXCEPTIONS)) { 176 return ExceptionsAttr.define(cp, name, length, din); 177 } 178 else if (name.equals(INNER_CLASSES)) { 179 return InnerClassesAttr.define(cp, name, length, din); 180 } 181 else if (name.equals(LINE_NUMBER_TABLE)) { 182 return LineNumberTableAttr.define(cp, name, length, din); 183 } 184 else if (name.equals(LOCAL_VARIABLE_TABLE)) { 185 return LocalVariableTableAttr.define 186 (cp, name, length, din); 187 } 188 else if (name.equals(SOURCE_FILE)) { 189 return SourceFileAttr.define(cp, name, length, din); 190 } 191 else if (name.equals(SYNTHETIC)) { 192 return SyntheticAttr.define(cp, name, length, din); 193 } 194 195 if (mAttrFactory != null) { 196 Attribute attr = 197 mAttrFactory.createAttribute(cp, name, length, din); 198 if (attr != null) { 199 return attr; 200 } 201 } 202 203 206 final byte[] data = new byte[length]; 207 din.readFully(data); 208 209 return new Attribute(cp, name) { 210 public int getLength() { 211 return length; 212 } 213 214 public void writeDataTo(DataOutput dout) throws IOException { 215 dout.write(data); 216 } 217 }; 218 } 219 } 220 } 221 | Popular Tags |