1 19 20 package jode.bytecode; 21 import java.io.DataInputStream ; 22 import java.io.DataOutputStream ; 23 import java.io.IOException ; 24 import java.lang.reflect.Modifier ; 25 26 public class FieldInfo extends BinaryInfo { 27 ClassInfo clazzInfo; 28 29 int modifier; 30 String name; 31 String typeSig; 32 33 Object constant; 34 boolean syntheticFlag; 35 boolean deprecatedFlag; 36 37 public FieldInfo(ClassInfo ci) { 38 this.clazzInfo = ci; 39 } 40 41 public FieldInfo(ClassInfo ci, String name, String typeSig, int modifier) { 42 this.clazzInfo = ci; 43 this.name = name; 44 this.typeSig = typeSig; 45 this.modifier = modifier; 46 } 47 48 protected void readAttribute(String name, int length, 49 ConstantPool cp, 50 DataInputStream input, 51 int howMuch) throws IOException { 52 if ((howMuch & KNOWNATTRIBS) != 0 && name.equals("ConstantValue")) { 53 if (length != 2) 54 throw new ClassFormatException("ConstantValue attribute" 55 + " has wrong length"); 56 int index = input.readUnsignedShort(); 57 constant = cp.getConstant(index); 58 } else if (name.equals("Synthetic")) { 59 syntheticFlag = true; 60 if (length != 0) 61 throw new ClassFormatException 62 ("Synthetic attribute has wrong length"); 63 } else if (name.equals("Deprecated")) { 64 deprecatedFlag = true; 65 if (length != 0) 66 throw new ClassFormatException 67 ("Deprecated attribute has wrong length"); 68 } else 69 super.readAttribute(name, length, cp, input, howMuch); 70 } 71 72 public void read(ConstantPool constantPool, 73 DataInputStream input, int howMuch) throws IOException { 74 modifier = input.readUnsignedShort(); 75 name = constantPool.getUTF8(input.readUnsignedShort()); 76 typeSig = constantPool.getUTF8(input.readUnsignedShort()); 77 readAttributes(constantPool, input, howMuch); 78 } 79 80 public void reserveSmallConstants(GrowableConstantPool gcp) { 81 } 82 83 public void prepareWriting(GrowableConstantPool gcp) { 84 gcp.putUTF8(name); 85 gcp.putUTF8(typeSig); 86 if (constant != null) { 87 gcp.putUTF8("ConstantValue"); 88 if (typeSig.charAt(0) == 'J' || typeSig.charAt(0) == 'D') 89 gcp.putLongConstant(constant); 90 else 91 gcp.putConstant(constant); 92 } 93 if (syntheticFlag) 94 gcp.putUTF8("Synthetic"); 95 if (deprecatedFlag) 96 gcp.putUTF8("Deprecated"); 97 prepareAttributes(gcp); 98 } 99 100 protected int getKnownAttributeCount() { 101 int count = 0; 102 if (constant != null) 103 count++; 104 if (syntheticFlag) 105 count++; 106 if (deprecatedFlag) 107 count++; 108 return count; 109 } 110 111 public void writeKnownAttributes(GrowableConstantPool gcp, 112 DataOutputStream output) 113 throws IOException { 114 if (constant != null) { 115 output.writeShort(gcp.putUTF8("ConstantValue")); 116 output.writeInt(2); 117 int index; 118 if (typeSig.charAt(0) == 'J' 119 || typeSig.charAt(0) == 'D') 120 index = gcp.putLongConstant(constant); 121 else 122 index = gcp.putConstant(constant); 123 output.writeShort(index); 124 } 125 if (syntheticFlag) { 126 output.writeShort(gcp.putUTF8("Synthetic")); 127 output.writeInt(0); 128 } 129 if (deprecatedFlag) { 130 output.writeShort(gcp.putUTF8("Deprecated")); 131 output.writeInt(0); 132 } 133 } 134 135 public void write(GrowableConstantPool constantPool, 136 DataOutputStream output) throws IOException { 137 output.writeShort(modifier); 138 output.writeShort(constantPool.putUTF8(name)); 139 output.writeShort(constantPool.putUTF8(typeSig)); 140 writeAttributes(constantPool, output); 141 } 142 143 public void dropInfo(int howMuch) { 144 if ((howMuch & KNOWNATTRIBS) != 0) 145 constant = null; 146 super.dropInfo(howMuch); 147 } 148 149 public String getName() { 150 return name; 151 } 152 153 public String getType() { 154 return typeSig; 155 } 156 157 public int getModifiers() { 158 return modifier; 159 } 160 161 public boolean isSynthetic() { 162 return syntheticFlag; 163 } 164 165 public boolean isDeprecated() { 166 return deprecatedFlag; 167 } 168 169 public Object getConstant() { 170 clazzInfo.loadInfo(KNOWNATTRIBS); 171 return constant; 172 } 173 174 public void setName(String newName) { 175 name = newName; 176 } 177 178 public void setType(String newType) { 179 typeSig = newType; 180 } 181 182 public void setModifiers(int newModifier) { 183 modifier = newModifier; 184 } 185 186 public void setSynthetic(boolean flag) { 187 syntheticFlag = flag; 188 } 189 190 public void setDeprecated(boolean flag) { 191 deprecatedFlag = flag; 192 } 193 194 public void setConstant(Object newConstant) { 195 constant = newConstant; 196 } 197 198 public String toString() { 199 return "Field "+Modifier.toString(modifier)+" "+ 200 typeSig+" "+name; 201 } 202 } 203 204 | Popular Tags |