1 26 package com.yworks.yguard.obf.classfile; 27 28 import java.io.*; 29 import com.yworks.yguard.ParseException; 30 import com.yworks.yguard.Conversion; 31 32 38 public class AttrInfo implements ClassConstants 39 { 40 public static final int CONSTANT_FIELD_SIZE = 6; 42 43 44 private int u2attrNameIndex; 46 protected int u4attrLength; 47 private byte info[]; 48 49 protected ClassFile owner; 50 51 52 58 public static AttrInfo create(DataInput din, ClassFile cf) throws java.io.IOException 59 { 60 if (din == null) throw new NullPointerException ("No input stream was provided."); 61 62 AttrInfo ai = null; 64 int attrNameIndex = din.readUnsignedShort(); 65 int attrLength = din.readInt(); 66 69 CpInfo cpInfo = cf.getCpEntry(attrNameIndex); 70 if (cpInfo instanceof Utf8CpInfo) 71 { 72 String attrName = ((Utf8CpInfo)cpInfo).getString(); 73 if (attrName.equals(ATTR_Code)) 74 { 75 ai = new CodeAttrInfo(cf, attrNameIndex, attrLength); 76 } 77 else if (attrName.equals(ATTR_ConstantValue)) 78 { 79 ai = new ConstantValueAttrInfo(cf, attrNameIndex, attrLength); 80 } 81 else if (attrName.equals(ATTR_Exceptions)) 82 { 83 ai = new ExceptionsAttrInfo(cf, attrNameIndex, attrLength); 84 } 85 else if (attrName.equals(ATTR_StackMapTable)) 86 { 87 ai = new StackMapTableAttrInfo(cf, attrNameIndex, attrLength); 88 } 89 else if (attrName.equals(ATTR_LineNumberTable)) 90 { 91 ai = new LineNumberTableAttrInfo(cf, attrNameIndex, attrLength); 92 } 93 else if (attrName.equals(ATTR_SourceFile)) 94 { 95 ai = new SourceFileAttrInfo(cf, attrNameIndex, attrLength); 96 } 97 else if (attrName.equals(ATTR_LocalVariableTable)) 98 { 99 ai = new LocalVariableTableAttrInfo(cf, attrNameIndex, attrLength); 100 } 101 else if (attrName.equals(ATTR_InnerClasses)) 102 { 103 ai = new InnerClassesAttrInfo(cf, attrNameIndex, attrLength); 104 } 105 else if (attrName.equals(ATTR_Synthetic)) 106 { 107 ai = new SyntheticAttrInfo(cf, attrNameIndex, attrLength); 108 } 109 else if (attrName.equals(ATTR_Deprecated)) 110 { 111 ai = new DeprecatedAttrInfo(cf, attrNameIndex, attrLength); 112 } 113 else if (attrName.equals(ATTR_Signature)){ 114 ai = new SignatureAttrInfo(cf, attrNameIndex, attrLength); 115 } 116 else if (attrName.equals(ATTR_LocalVariableTypeTable)){ 117 ai = new LocalVariableTypeTableAttrInfo(cf, attrNameIndex, attrLength); 118 } 119 else if (attrName.equals(ATTR_EnclosingMethod)){ 120 ai = new EnclosingMethodAttrInfo(cf, attrNameIndex, attrLength); 121 } 122 else if (attrName.equals(ATTR_RuntimeVisibleAnnotations)){ 123 ai = new RuntimeVisibleAnnotationsAttrInfo(cf, attrNameIndex, attrLength); 124 } 125 else if (attrName.equals(ATTR_RuntimeInvisibleAnnotations)){ 126 ai = new RuntimeInvisibleAnnotationsAttrInfo(cf, attrNameIndex, attrLength); 127 } 128 else if (attrName.equals(ATTR_RuntimeVisibleParameterAnnotations)){ 129 ai = new RuntimeVisibleParameterAnnotationsAttrInfo(cf, attrNameIndex, attrLength); 130 } 131 else if (attrName.equals(ATTR_RuntimeInvisibleParameterAnnotations)){ 132 ai = new RuntimeInvisibleParameterAnnotationsAttrInfo(cf, attrNameIndex, attrLength); 133 } 134 else if (attrName.equals(ATTR_AnnotationDefault)){ 135 ai = new AnnotationDefaultAttrInfo(cf, attrNameIndex, attrLength); 136 } 137 else if (attrName.equals(ATTR_Bridge) && (attrLength==0)){ 138 ai = new AttrInfo(cf, attrNameIndex, attrLength); 139 } 140 else if (attrName.equals(ATTR_Enum) && (attrLength==0)){ 141 ai = new AttrInfo(cf, attrNameIndex, attrLength); 142 } 143 else if (attrName.equals(ATTR_Varargs) && (attrLength==0)){ 144 ai = new AttrInfo(cf, attrNameIndex, attrLength); 145 } 146 else { 147 if ( attrLength > 0 ) { 148 Logger.getInstance().warning( "Unrecognized attribute '" + attrName + "' in " + Conversion.toJavaClass( cf.getName() ) ); 149 } 150 ai = new AttrInfo( cf, attrNameIndex, attrLength ); 151 } 152 } 153 else 154 { 155 throw new ParseException("Inconsistent reference to Constant Pool."); 156 } 157 158 ai.readInfo(din); 160 return ai; 161 } 162 163 164 protected AttrInfo(ClassFile cf, int attrNameIndex, int attrLength) 166 { 167 owner = cf; 168 u2attrNameIndex = attrNameIndex; 169 u4attrLength = attrLength; 170 } 171 172 protected int getAttrNameIndex(){ 173 return u2attrNameIndex; 174 } 175 176 177 protected int getAttrInfoLength() 178 { 179 return u4attrLength; 180 } 181 182 183 protected String getAttrName() 184 { 185 return ATTR_Unknown; 186 } 187 188 191 protected void trimAttrsExcept(String [] keepAttrs) {} 192 193 194 protected void markUtf8Refs(ConstantPool pool) 195 { 196 pool.incRefCount(u2attrNameIndex); 197 markUtf8RefsInInfo(pool); 198 } 199 200 204 protected void markUtf8RefsInInfo(ConstantPool pool) {} 205 206 207 protected void readInfo(DataInput din) throws java.io.IOException 208 { 209 info = new byte[u4attrLength]; 210 din.readFully(info); 211 } 212 213 214 public final void write(DataOutput dout) throws java.io.IOException 215 { 216 if (dout == null) throw new IOException("No output stream was provided."); 217 dout.writeShort(u2attrNameIndex); 218 dout.writeInt(getAttrInfoLength()); 219 writeInfo(dout); 220 } 221 222 223 public void writeInfo(DataOutput dout) throws java.io.IOException 224 { 225 dout.write(info); 226 } 227 228 public String toString(){ 229 return getAttrName() + "[" + getAttrInfoLength() + "]"; 230 } 231 } 232 | Popular Tags |