1 15 16 package javassist.bytecode; 17 18 import java.io.DataInputStream ; 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import java.util.Map ; 22 import java.util.LinkedList ; 23 import java.util.ListIterator ; 24 25 28 31 public class AttributeInfo { 32 protected ConstPool constPool; 33 int name; 34 byte[] info; 35 36 protected AttributeInfo(ConstPool cp, int attrname, byte[] attrinfo) { 37 constPool = cp; 38 name = attrname; 39 info = attrinfo; 40 } 41 42 protected AttributeInfo(ConstPool cp, String attrname) { 43 this(cp, attrname, (byte[])null); 44 } 45 46 54 public AttributeInfo(ConstPool cp, String attrname, byte[] attrinfo) { 55 this(cp, cp.addUtf8Info(attrname), attrinfo); 56 } 57 58 protected AttributeInfo(ConstPool cp, int n, DataInputStream in) 59 throws IOException 60 { 61 constPool = cp; 62 name = n; 63 int len = in.readInt(); 64 info = new byte[len]; 65 if (len > 0) 66 in.readFully(info); 67 } 68 69 static AttributeInfo read(ConstPool cp, DataInputStream in) 70 throws IOException 71 { 72 int name = in.readUnsignedShort(); 73 String nameStr = cp.getUtf8Info(name); 74 if (nameStr.charAt(0) < 'L') { 75 if (nameStr.equals(CodeAttribute.tag)) 76 return new CodeAttribute(cp, name, in); 77 else if (nameStr.equals(ConstantAttribute.tag)) 78 return new ConstantAttribute(cp, name, in); 79 else if (nameStr.equals(DeprecatedAttribute.tag)) 80 return new DeprecatedAttribute(cp, name, in); 81 else if (nameStr.equals(EnclosingMethodAttribute.tag)) 82 return new EnclosingMethodAttribute(cp, name, in); 83 else if (nameStr.equals(ExceptionsAttribute.tag)) 84 return new ExceptionsAttribute(cp, name, in); 85 else if (nameStr.equals(InnerClassesAttribute.tag)) 86 return new InnerClassesAttribute(cp, name, in); 87 } 88 else { 89 91 if (nameStr.equals(LineNumberAttribute.tag)) 92 return new LineNumberAttribute(cp, name, in); 93 else if (nameStr.equals(LocalVariableAttribute.tag) 94 || nameStr.equals(LocalVariableAttribute.typeTag)) 95 return new LocalVariableAttribute(cp, name, in); 96 else if (nameStr.equals(AnnotationsAttribute.visibleTag) 97 || nameStr.equals(AnnotationsAttribute.invisibleTag)) 98 return new AnnotationsAttribute(cp, name, in); 99 else if (nameStr.equals(ParameterAnnotationsAttribute.visibleTag) 100 || nameStr.equals(ParameterAnnotationsAttribute.invisibleTag)) 101 return new ParameterAnnotationsAttribute(cp, name, in); 102 else if (nameStr.equals(SignatureAttribute.tag)) 103 return new SignatureAttribute(cp, name, in); 104 else if (nameStr.equals(SourceFileAttribute.tag)) 105 return new SourceFileAttribute(cp, name, in); 106 else if (nameStr.equals(SyntheticAttribute.tag)) 107 return new SyntheticAttribute(cp, name, in); 108 } 109 110 return new AttributeInfo(cp, name, in); 111 } 112 113 116 public String getName() { 117 return constPool.getUtf8Info(name); 118 } 119 120 123 public ConstPool getConstPool() { return constPool; } 124 125 130 public int length() { 131 return info.length + 6; 132 } 133 134 141 public byte[] get() { return info; } 142 143 150 public void set(byte[] newinfo) { info = newinfo; } 151 152 160 public AttributeInfo copy(ConstPool newCp, Map classnames) { 161 int s = info.length; 162 byte[] newInfo = new byte[s]; 163 for (int i = 0; i < s; ++i) 164 newInfo[i] = info[i]; 165 166 return new AttributeInfo(newCp, getName(), newInfo); 167 } 168 169 void write(DataOutputStream out) throws IOException { 170 out.writeShort(name); 171 out.writeInt(info.length); 172 if (info.length > 0) 173 out.write(info); 174 } 175 176 static int getLength(LinkedList list) { 177 int size = 0; 178 int n = list.size(); 179 for (int i = 0; i < n; ++i) { 180 AttributeInfo attr = (AttributeInfo)list.get(i); 181 size += attr.length(); 182 } 183 184 return size; 185 } 186 187 static AttributeInfo lookup(LinkedList list, String name) { 188 if (list == null) 189 return null; 190 191 ListIterator iterator = list.listIterator(); 192 while (iterator.hasNext()) { 193 AttributeInfo ai = (AttributeInfo)iterator.next(); 194 if (ai.getName().equals(name)) 195 return ai; 196 } 197 198 return null; } 200 201 static synchronized void remove(LinkedList list, String name) { 202 if (list == null) 203 return; 204 205 ListIterator iterator = list.listIterator(); 206 while (iterator.hasNext()) { 207 AttributeInfo ai = (AttributeInfo)iterator.next(); 208 if (ai.getName().equals(name)) 209 iterator.remove(); 210 } 211 } 212 213 static void writeAll(LinkedList list, DataOutputStream out) 214 throws IOException 215 { 216 if (list == null) 217 return; 218 219 int n = list.size(); 220 for (int i = 0; i < n; ++i) { 221 AttributeInfo attr = (AttributeInfo)list.get(i); 222 attr.write(out); 223 } 224 } 225 226 static LinkedList copyAll(LinkedList list, ConstPool cp) { 227 if (list == null) 228 return null; 229 230 LinkedList newList = new LinkedList (); 231 int n = list.size(); 232 for (int i = 0; i < n; ++i) { 233 AttributeInfo attr = (AttributeInfo)list.get(i); 234 newList.add(attr.copy(cp, null)); 235 } 236 237 return newList; 238 } 239 } 240 | Popular Tags |