1 package polyglot.types.reflect; 2 3 import polyglot.types.*; 4 import java.util.*; 5 import java.io.*; 6 7 19 class Field { 20 ClassFile clazz; 21 int modifiers; 22 int name; 23 int type; 24 Attribute[] attrs; 25 ConstantValue constantValue; 26 boolean synthetic; 27 28 33 boolean isString(Type t) { 34 return t.isClass() 35 && t.toClass().isTopLevel() 36 && t.toClass().fullName().equals("java.lang.String"); 37 } 38 39 FieldInstance fieldInstance(TypeSystem ts, ClassType ct) { 40 String name = (String ) clazz.constants[this.name].value(); 41 String type = (String ) clazz.constants[this.type].value(); 42 43 FieldInstance fi = ts.fieldInstance(ct.position(), ct, 44 ts.flagsForBits(modifiers), 45 clazz.typeForString(ts, type), name); 46 47 Constant c = constantValue(); 48 49 if (c != null) { 50 Object o = null; 51 52 try { 53 switch (c.tag()) { 54 case Constant.STRING: o = getString(); break; 55 case Constant.INTEGER: o = new Integer (getInt()); break; 56 case Constant.LONG: o = new Long (getLong()); break; 57 case Constant.FLOAT: o = new Float (getFloat()); break; 58 case Constant.DOUBLE: o = new Double (getDouble()); break; 59 } 60 } 61 catch (SemanticException e) { 62 throw new ClassFormatError ("Unexpected constant pool entry."); 63 } 64 65 if (o != null) { 66 return fi.constantValue(o); 67 } 68 } 69 70 return fi; 71 } 72 73 boolean isSynthetic() { 74 return synthetic; 75 } 76 77 Constant constantValue() { 78 if (this.constantValue != null) { 79 int index = this.constantValue.index; 80 return clazz.constants[index]; 81 } 82 83 return null; 84 } 85 86 int getInt() throws SemanticException { 87 Constant c = constantValue(); 88 89 if (c != null && c.tag() == Constant.INTEGER) { 90 Integer v = (Integer ) c.value(); 91 return v.intValue(); 92 } 93 94 throw new SemanticException("Could not find expected constant " + 95 "pool entry with tag INTEGER."); 96 } 97 98 float getFloat() throws SemanticException { 99 Constant c = constantValue(); 100 101 if (c != null && c.tag() == Constant.FLOAT) { 102 Float v = (Float ) c.value(); 103 return v.floatValue(); 104 } 105 106 throw new SemanticException("Could not find expected constant " + 107 "pool entry with tag FLOAT."); 108 } 109 110 double getDouble() throws SemanticException { 111 Constant c = constantValue(); 112 113 if (c != null && c.tag() == Constant.DOUBLE) { 114 Double v = (Double ) c.value(); 115 return v.doubleValue(); 116 } 117 118 throw new SemanticException("Could not find expected constant " + 119 "pool entry with tag DOUBLE."); 120 } 121 122 long getLong() throws SemanticException { 123 Constant c = constantValue(); 124 125 if (c != null && c.tag() == Constant.LONG) { 126 Long v = (Long ) c.value(); 127 return v.longValue(); 128 } 129 130 throw new SemanticException("Could not find expected constant " + 131 "pool entry with tag LONG."); 132 } 133 134 String getString() throws SemanticException { 135 Constant c = constantValue(); 136 137 if (c != null && c.tag() == Constant.STRING) { 138 Integer i = (Integer ) c.value(); 139 c = clazz.constants[i.intValue()]; 140 141 if (c != null && c.tag() == Constant.UTF8) { 142 String v = (String ) c.value(); 143 return v; 144 } 145 } 146 147 throw new SemanticException("Could not find expected constant " + 148 "pool entry with tag STRING or UTF8."); 149 } 150 151 String name() { 152 return (String ) clazz.constants[this.name].value(); 153 } 154 155 165 Field(DataInputStream in, ClassFile clazz) 166 throws IOException 167 { 168 this.clazz = clazz; 169 170 modifiers = in.readUnsignedShort(); 171 172 name = in.readUnsignedShort(); 173 type = in.readUnsignedShort(); 174 175 int numAttributes = in.readUnsignedShort(); 176 177 attrs = new Attribute[numAttributes]; 178 179 for (int i = 0; i < numAttributes; i++) { 180 int nameIndex = in.readUnsignedShort(); 181 int length = in.readInt(); 182 183 Constant name = clazz.constants[nameIndex]; 184 185 if (name != null) { 186 if ("ConstantValue".equals(name.value())) { 187 constantValue = new ConstantValue(in, nameIndex, length); 188 attrs[i] = constantValue; 189 } 190 if ("Synthetic".equals(name.value())) { 191 synthetic = true; 192 } 193 } 194 195 if (attrs[i] == null) { 196 long n = in.skip(length); 197 if (n != length) { 198 throw new EOFException(); 199 } 200 } 201 } 202 } 203 } 204 | Popular Tags |