1 4 package gnu.bytecode; 5 import java.io.*; 6 7 public class Field extends Location implements AttrContainer, Member { 8 int flags; 9 Field next; 10 11 Attribute attributes; 12 public final Attribute getAttributes () { return attributes; } 13 public final void setAttributes (Attribute attributes) 14 { this.attributes = attributes; } 15 16 17 ClassType owner; 18 19 20 String sourceName; 21 22 23 java.lang.reflect.Field rfield; 24 25 26 public Field (ClassType ctype) 27 { 28 if (ctype.last_field == null) 29 ctype.fields = this; 30 else 31 ctype.last_field.next = this; 32 ctype.last_field = this; 33 ctype.fields_count++; 34 owner = ctype; 35 } 36 37 public final ClassType getDeclaringClass() 38 { 39 return owner; 40 } 41 42 public final void setStaticFlag (boolean is_static) { 43 if (is_static) 44 flags |= Access.STATIC; 45 else 46 flags ^= ~Access.STATIC; 47 } 48 49 public final boolean getStaticFlag () { 50 return (flags & Access.STATIC) != 0; 51 } 52 53 public final int getFlags() { 54 return flags; 55 } 56 57 public final int getModifiers() { 58 return flags; 59 } 60 61 void write (DataOutputStream dstr, ClassType classfile) 62 throws java.io.IOException 63 { 64 dstr.writeShort (flags); 65 dstr.writeShort (name_index); 66 dstr.writeShort (signature_index); 67 68 Attribute.writeAll(this, dstr); 69 } 70 71 void assign_constants (ClassType classfile) 72 { 73 ConstantPool constants = classfile.constants; 74 if (name_index == 0 && name != null) 75 name_index = constants.addUtf8(name).index; 76 if (signature_index == 0 && type != null) 77 signature_index = constants.addUtf8(type.signature).index; 78 Attribute.assignConstants(this, classfile); 79 } 80 81 public java.lang.reflect.Field getReflectField() 82 throws java.lang.NoSuchFieldException 83 { 84 if (rfield == null) 85 rfield = owner.getReflectClass().getDeclaredField(getName()); 86 return rfield; 87 } 88 89 public void setSourceName(String name) 90 { 91 sourceName = name; 92 } 93 94 public String getSourceName() 95 { 96 if (sourceName == null) 97 sourceName = getName().intern(); 98 return sourceName; 99 } 100 101 105 public static Field searchField(Field fields, String name) 106 { 107 for (; fields != null; fields = fields.next) 108 { 109 if (fields.getSourceName() == name) 110 return fields; 111 } 112 return null; 113 } 114 115 public final Field getNext() 116 { 117 return next; 118 } 119 120 126 public final void setConstantValue (Object value, ClassType ctype) 127 { 128 ConstantPool cpool = ctype.constants; 129 if (cpool == null) 130 ctype.constants = cpool = new ConstantPool(); 131 char sig1 = getType().getSignature().charAt(0); 132 CpoolEntry entry; 133 switch (sig1) 134 { 135 case 'Z': 136 entry = cpool.addInt(PrimType.booleanValue(value)? 1 : 0); 137 break; 138 case 'C': 139 if (value instanceof Character ) 140 { 141 entry = cpool.addInt(((Character ) value).charValue()); 142 break; 143 } 144 case 'B': 146 case 'S': 147 case 'I': 148 entry = cpool.addInt(((Number ) value).intValue()); break; 149 case 'J': 150 entry = cpool.addLong(((Number ) value).longValue()); break; 151 case 'F': 152 entry = cpool.addFloat(((Number ) value).floatValue()); break; 153 case 'D': 154 entry = cpool.addDouble(((Number ) value).doubleValue()); break; 155 default: 156 entry = cpool.addString(value.toString()); break; 157 } 158 ConstantValueAttr attr = new ConstantValueAttr(entry.getIndex()); 159 attr.addToFrontOf(this); 160 } 161 162 public String toString() 163 { 164 StringBuffer sbuf = new StringBuffer (100); 165 sbuf.append("Field:"); 166 sbuf.append(getDeclaringClass().getName()); 167 sbuf.append('.'); 168 sbuf.append(name); 169 return sbuf.toString(); 170 } 171 } 172 | Popular Tags |