1 16 17 package org.cojen.classfile; 18 19 import java.io.DataInput ; 20 import java.io.DataOutput ; 21 import java.io.IOException ; 22 import org.cojen.classfile.attribute.CodeAttr; 23 import org.cojen.classfile.attribute.ConstantValueAttr; 24 import org.cojen.classfile.attribute.DeprecatedAttr; 25 import org.cojen.classfile.attribute.EnclosingMethodAttr; 26 import org.cojen.classfile.attribute.ExceptionsAttr; 27 import org.cojen.classfile.attribute.InnerClassesAttr; 28 import org.cojen.classfile.attribute.LineNumberTableAttr; 29 import org.cojen.classfile.attribute.LocalVariableTableAttr; 30 import org.cojen.classfile.attribute.RuntimeInvisibleAnnotationsAttr; 31 import org.cojen.classfile.attribute.RuntimeInvisibleParameterAnnotationsAttr; 32 import org.cojen.classfile.attribute.RuntimeVisibleAnnotationsAttr; 33 import org.cojen.classfile.attribute.RuntimeVisibleParameterAnnotationsAttr; 34 import org.cojen.classfile.attribute.SignatureAttr; 35 import org.cojen.classfile.attribute.SourceFileAttr; 36 import org.cojen.classfile.attribute.SyntheticAttr; 37 import org.cojen.classfile.attribute.UnknownAttr; 38 import org.cojen.classfile.constant.ConstantUTFInfo; 39 40 47 public abstract class Attribute { 48 final static Attribute[] NO_ATTRIBUTES = new Attribute[0]; 49 50 public static final String CODE = "Code"; 51 public static final String CONSTANT_VALUE = "ConstantValue"; 52 public static final String DEPRECATED = "Deprecated"; 53 public static final String EXCEPTIONS = "Exceptions"; 54 public static final String INNER_CLASSES = "InnerClasses"; 55 public static final String LINE_NUMBER_TABLE = "LineNumberTable"; 56 public static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable"; 57 public static final String SOURCE_FILE = "SourceFile"; 58 public static final String SYNTHETIC = "Synthetic"; 59 public static final String SIGNATURE = "Signature"; 60 public static final String ENCLOSING_METHOD = "EnclosingMethod"; 61 public static final String RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"; 62 public static final String RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"; 63 public static final String RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = 64 "RuntimeVisibleParamaterAnnotations"; 65 public static final String RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = 66 "RuntimeInvisibleParamaterAnnotations"; 67 68 69 private final ConstantPool mCp; 70 71 private String mName; 72 private ConstantUTFInfo mNameConstant; 73 74 protected Attribute(ConstantPool cp, String name) { 75 mCp = cp; 76 mName = name; 77 mNameConstant = cp.addConstantUTF(name); 78 } 79 80 83 public ConstantPool getConstantPool() { 84 return mCp; 85 } 86 87 90 public String getName() { 91 return mName; 92 } 93 94 public ConstantUTFInfo getNameConstant() { 95 return mNameConstant; 96 } 97 98 102 public Attribute[] getAttributes() { 103 return NO_ATTRIBUTES; 104 } 105 106 109 public abstract int getLength(); 110 111 115 public final void writeTo(DataOutput dout) throws IOException { 116 dout.writeShort(mNameConstant.getIndex()); 117 dout.writeInt(getLength()); 118 writeDataTo(dout); 119 } 120 121 125 public void writeDataTo(DataOutput dout) throws IOException { 126 } 127 128 131 public static Attribute readFrom(ConstantPool cp, 132 DataInput din, 133 AttributeFactory attrFactory) 134 throws IOException 135 { 136 int index = din.readUnsignedShort(); 137 String name = ((ConstantUTFInfo)cp.getConstant(index)).getValue(); 138 int length = din.readInt(); 139 140 attrFactory = new Factory(attrFactory); 141 return attrFactory.createAttribute(cp, name, length, din); 142 } 143 144 private static class Factory implements AttributeFactory { 145 private final AttributeFactory mAttrFactory; 146 147 public Factory(AttributeFactory attrFactory) { 148 mAttrFactory = attrFactory; 149 } 150 151 public Attribute createAttribute(ConstantPool cp, 152 String name, 153 int length, 154 DataInput din) throws IOException { 155 if (name.length() > 0) { 156 switch (name.charAt(0)) { 157 case 'C': 158 if (name.equals(CODE)) { 159 return new CodeAttr(cp, name, length, din, mAttrFactory); 160 } else if (name.equals(CONSTANT_VALUE)) { 161 return new ConstantValueAttr(cp, name, length, din); 162 } 163 break; 164 case 'D': 165 if (name.equals(DEPRECATED)) { 166 return new DeprecatedAttr(cp, name, length, din); 167 } 168 break; 169 case 'E': 170 if (name.equals(EXCEPTIONS)) { 171 return new ExceptionsAttr(cp, name, length, din); 172 } else if (name.equals(ENCLOSING_METHOD)) { 173 return new EnclosingMethodAttr(cp, name, length, din); 174 } 175 break; 176 case 'I': 177 if (name.equals(INNER_CLASSES)) { 178 return new InnerClassesAttr(cp, name, length, din); 179 } 180 break; 181 case 'L': 182 if (name.equals(LINE_NUMBER_TABLE)) { 183 return new LineNumberTableAttr(cp, name, length, din); 184 } else if (name.equals(LOCAL_VARIABLE_TABLE)) { 185 return new LocalVariableTableAttr(cp, name, length, din); 186 } 187 break; 188 case 'R': 189 if (name.equals(RUNTIME_VISIBLE_ANNOTATIONS)) { 190 return new RuntimeVisibleAnnotationsAttr(cp, name, length, din); 191 } else if (name.equals(RUNTIME_INVISIBLE_ANNOTATIONS)) { 192 return new RuntimeInvisibleAnnotationsAttr(cp, name, length, din); 193 } else if (name.equals(RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS)) { 194 return new RuntimeVisibleParameterAnnotationsAttr(cp, name, length, din); 195 } else if (name.equals(RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS)) { 196 return new RuntimeInvisibleParameterAnnotationsAttr(cp, name, length, din); 197 } 198 break; 199 case 'S': 200 if (name.equals(SOURCE_FILE)) { 201 return new SourceFileAttr(cp, name, length, din); 202 } else if (name.equals(SYNTHETIC)) { 203 return new SyntheticAttr(cp, name, length, din); 204 } else if (name.equals(SIGNATURE)) { 205 return new SignatureAttr(cp, name, length, din); 206 } 207 break; 208 } 209 } 210 211 if (mAttrFactory != null) { 212 Attribute attr = mAttrFactory.createAttribute(cp, name, length, din); 213 if (attr != null) { 214 return attr; 215 } 216 } 217 218 return new UnknownAttr(cp, name, length, din); 221 } 222 } 223 } 224 | Popular Tags |