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.List ; 22 import java.util.LinkedList ; 23 24 29 public final class FieldInfo { 30 ConstPool constPool; 31 int accessFlags; 32 int name; 33 int descriptor; 34 LinkedList attribute; 36 private FieldInfo(ConstPool cp) { 37 constPool = cp; 38 accessFlags = 0; 39 attribute = null; 40 } 41 42 51 public FieldInfo(ConstPool cp, String fieldName, String desc) { 52 this(cp); 53 name = cp.addUtf8Info(fieldName); 54 descriptor = cp.addUtf8Info(desc); 55 } 56 57 FieldInfo(ConstPool cp, DataInputStream in) throws IOException { 58 this(cp); 59 read(in); 60 } 61 62 70 void compact(ConstPool cp) { 71 name = cp.addUtf8Info(getName()); 72 descriptor = cp.addUtf8Info(getDescriptor()); 73 attribute = AttributeInfo.copyAll(attribute, cp); 74 constPool = cp; 75 } 76 77 void prune(ConstPool cp) { 78 int index = getConstantValue(); 79 if (index == 0) 80 attribute = null; 81 else { 82 index = constPool.copy(index, cp, null); 83 attribute = new LinkedList (); 84 attribute.add(new ConstantAttribute(cp, index)); 85 } 86 87 name = cp.addUtf8Info(getName()); 88 descriptor = cp.addUtf8Info(getDescriptor()); 89 constPool = cp; 90 } 91 92 96 public ConstPool getConstPool() { 97 return constPool; 98 } 99 100 103 public String getName() { 104 return constPool.getUtf8Info(name); 105 } 106 107 110 public void setName(String newName) { 111 name = constPool.addUtf8Info(newName); 112 } 113 114 119 public int getAccessFlags() { 120 return accessFlags; 121 } 122 123 128 public void setAccessFlags(int acc) { 129 accessFlags = acc; 130 } 131 132 137 public String getDescriptor() { 138 return constPool.getUtf8Info(descriptor); 139 } 140 141 146 public void setDescriptor(String desc) { 147 if (!desc.equals(getDescriptor())) 148 descriptor = constPool.addUtf8Info(desc); 149 } 150 151 157 public int getConstantValue() { 158 if ((accessFlags & AccessFlag.STATIC) == 0) 159 return 0; 160 161 ConstantAttribute attr 162 = (ConstantAttribute)getAttribute(ConstantAttribute.tag); 163 if (attr == null) 164 return 0; 165 else 166 return attr.getConstantValue(); 167 } 168 169 177 public List getAttributes() { 178 if (attribute == null) 179 attribute = new LinkedList (); 180 181 return attribute; 182 } 183 184 189 public AttributeInfo getAttribute(String name) { 190 return AttributeInfo.lookup(attribute, name); 191 } 192 193 197 public void addAttribute(AttributeInfo info) { 198 if (attribute == null) 199 attribute = new LinkedList (); 200 201 AttributeInfo.remove(attribute, info.getName()); 202 attribute.add(info); 203 } 204 205 private void read(DataInputStream in) throws IOException { 206 accessFlags = in.readUnsignedShort(); 207 name = in.readUnsignedShort(); 208 descriptor = in.readUnsignedShort(); 209 int n = in.readUnsignedShort(); 210 attribute = new LinkedList (); 211 for (int i = 0; i < n; ++i) 212 attribute.add(AttributeInfo.read(constPool, in)); 213 } 214 215 void write(DataOutputStream out) throws IOException { 216 out.writeShort(accessFlags); 217 out.writeShort(name); 218 out.writeShort(descriptor); 219 if (attribute == null) 220 out.writeShort(0); 221 else { 222 out.writeShort(attribute.size()); 223 AttributeInfo.writeAll(attribute, out); 224 } 225 } 226 } 227 | Popular Tags |