001: // Copyright (c) 1997 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.bytecode;
005:
006: import java.io.*;
007:
008: public class Field extends Location implements AttrContainer, Member {
009: int flags;
010: Field next;
011:
012: Attribute attributes;
013:
014: public final Attribute getAttributes() {
015: return attributes;
016: }
017:
018: public final void setAttributes(Attribute attributes) {
019: this .attributes = attributes;
020: }
021:
022: /** The class that contains this field. */
023: ClassType owner;
024:
025: /** If non-null, the interned source-file (unmangled) name of the field. */
026: String sourceName;
027:
028: /** If non-null, a cached version of the Field for reflectivion. */
029: java.lang.reflect.Field rfield;
030:
031: /** Add a new Field to a ClassType. */
032: public Field(ClassType ctype) {
033: if (ctype.last_field == null)
034: ctype.fields = this ;
035: else
036: ctype.last_field.next = this ;
037: ctype.last_field = this ;
038: ctype.fields_count++;
039: owner = ctype;
040: }
041:
042: public final ClassType getDeclaringClass() {
043: return owner;
044: }
045:
046: public final void setStaticFlag(boolean is_static) {
047: if (is_static)
048: flags |= Access.STATIC;
049: else
050: flags ^= ~Access.STATIC;
051: }
052:
053: public final boolean getStaticFlag() {
054: return (flags & Access.STATIC) != 0;
055: }
056:
057: public final int getFlags() {
058: return flags;
059: }
060:
061: public final int getModifiers() {
062: return flags;
063: }
064:
065: void write(DataOutputStream dstr, ClassType classfile)
066: throws java.io.IOException {
067: dstr.writeShort(flags);
068: dstr.writeShort(name_index);
069: dstr.writeShort(signature_index);
070:
071: Attribute.writeAll(this , dstr);
072: }
073:
074: void assign_constants(ClassType classfile) {
075: ConstantPool constants = classfile.constants;
076: if (name_index == 0 && name != null)
077: name_index = constants.addUtf8(name).index;
078: if (signature_index == 0 && type != null)
079: signature_index = constants.addUtf8(type.signature).index;
080: Attribute.assignConstants(this , classfile);
081: }
082:
083: public java.lang.reflect.Field getReflectField()
084: throws java.lang.NoSuchFieldException {
085: if (rfield == null)
086: rfield = owner.getReflectClass()
087: .getDeclaredField(getName());
088: return rfield;
089: }
090:
091: public void setSourceName(String name) {
092: sourceName = name;
093: }
094:
095: public String getSourceName() {
096: if (sourceName == null)
097: sourceName = getName().intern();
098: return sourceName;
099: }
100:
101: /** Find a field with the given name.
102: * @param fields list of fields to search
103: * @param name (interned source) name of field to look for
104: */
105: public static Field searchField(Field fields, String name) {
106: for (; fields != null; fields = fields.next) {
107: if (fields.getSourceName() == name)
108: return fields;
109: }
110: return null;
111: }
112:
113: public final Field getNext() {
114: return next;
115: }
116:
117: /** Set the ConstantValue attribute for this field.
118: * @param value the value to use for the ConstantValue attribute
119: * of this field
120: * @param ctype the class that contains this field
121: * This field's type is used to determine the kind of constant.
122: */
123: public final void setConstantValue(Object value, ClassType ctype) {
124: ConstantPool cpool = ctype.constants;
125: if (cpool == null)
126: ctype.constants = cpool = new ConstantPool();
127: char sig1 = getType().getSignature().charAt(0);
128: CpoolEntry entry;
129: switch (sig1) {
130: case 'Z':
131: entry = cpool.addInt(PrimType.booleanValue(value) ? 1 : 0);
132: break;
133: case 'C':
134: if (value instanceof Character) {
135: entry = cpool.addInt(((Character) value).charValue());
136: break;
137: }
138: /// else fall through ...
139: case 'B':
140: case 'S':
141: case 'I':
142: entry = cpool.addInt(((Number) value).intValue());
143: break;
144: case 'J':
145: entry = cpool.addLong(((Number) value).longValue());
146: break;
147: case 'F':
148: entry = cpool.addFloat(((Number) value).floatValue());
149: break;
150: case 'D':
151: entry = cpool.addDouble(((Number) value).doubleValue());
152: break;
153: default:
154: entry = cpool.addString(value.toString());
155: break;
156: }
157: ConstantValueAttr attr = new ConstantValueAttr(entry.getIndex());
158: attr.addToFrontOf(this );
159: }
160:
161: public String toString() {
162: StringBuffer sbuf = new StringBuffer(100);
163: sbuf.append("Field:");
164: sbuf.append(getDeclaringClass().getName());
165: sbuf.append('.');
166: sbuf.append(name);
167: return sbuf.toString();
168: }
169: }
|