KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > Field


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

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   /** The class that contains this field. */
17   ClassType owner;
18
19   /** If non-null, the interned source-file (unmangled) name of the field. */
20   String JavaDoc sourceName;
21
22   /** If non-null, a cached version of the Field for reflectivion. */
23   java.lang.reflect.Field JavaDoc rfield;
24
25   /** Add a new Field to a ClassType. */
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 JavaDoc
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 JavaDoc getReflectField()
82     throws java.lang.NoSuchFieldException JavaDoc
83   {
84     if (rfield == null)
85       rfield = owner.getReflectClass().getDeclaredField(getName());
86     return rfield;
87   }
88
89   public void setSourceName(String JavaDoc name)
90   {
91     sourceName = name;
92   }
93
94   public String JavaDoc getSourceName()
95   {
96     if (sourceName == null)
97       sourceName = getName().intern();
98     return sourceName;
99   }
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 JavaDoc 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   /** Set the ConstantValue attribute for this field.
121    * @param value the value to use for the ConstantValue attribute
122    * of this field
123    * @param ctype the class that contains this field
124    * This field's type is used to determine the kind of constant.
125    */

126   public final void setConstantValue (Object JavaDoc 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 JavaDoc)
140       {
141         entry = cpool.addInt(((Character JavaDoc) value).charValue());
142         break;
143       }
144     /// else fall through ...
145
case 'B':
146       case 'S':
147       case 'I':
148     entry = cpool.addInt(((Number JavaDoc) value).intValue()); break;
149       case 'J':
150     entry = cpool.addLong(((Number JavaDoc) value).longValue()); break;
151       case 'F':
152     entry = cpool.addFloat(((Number JavaDoc) value).floatValue()); break;
153       case 'D':
154     entry = cpool.addDouble(((Number JavaDoc) 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 JavaDoc toString()
163   {
164     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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