KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > FieldImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.DataOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 import org.eclipse.jdi.internal.jdwp.JdwpFieldID;
19
20 import com.sun.jdi.ClassNotLoadedException;
21 import com.sun.jdi.Field;
22 import com.sun.jdi.Type;
23
24 /**
25  * this class implements the corresponding interfaces
26  * declared by the JDI specification. See the com.sun.jdi package
27  * for more information.
28  *
29  */

30 public class FieldImpl extends TypeComponentImpl implements Field {
31     /** ID that corresponds to this reference. */
32     private JdwpFieldID fFieldID;
33     private Type fType;
34     private String JavaDoc fTypeName;
35
36     /**
37      * Creates new FieldImpl.
38      */

39     public FieldImpl(VirtualMachineImpl vmImpl, ReferenceTypeImpl declaringType, JdwpFieldID ID, String JavaDoc name, String JavaDoc signature, String JavaDoc genericSignature, int modifierBits) {
40         super("Field", vmImpl, declaringType, name, signature, genericSignature, modifierBits); //$NON-NLS-1$
41
fFieldID = ID;
42     }
43     
44     /**
45      * Flushes all stored Jdwp results.
46      */

47     public void flushStoredJdwpResults() {
48         // Note that no results are cached.
49
}
50     
51     /**
52      * @return Returns fieldID of field.
53      */

54     public JdwpFieldID getFieldID() {
55         return fFieldID;
56     }
57
58     /**
59      * @return Returns true if two mirrors refer to the same entity in the target VM.
60      * @see java.lang.Object#equals(Object)
61      */

62     public boolean equals(Object JavaDoc object) {
63         return object != null
64             && object.getClass().equals(this.getClass())
65             && fFieldID.equals(((FieldImpl)object).fFieldID)
66             && referenceTypeImpl().equals(((FieldImpl)object).referenceTypeImpl());
67     }
68
69     /**
70      * @return Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
71      */

72     public int compareTo(Object JavaDoc object) {
73         if (object == null || !object.getClass().equals(this.getClass()))
74             throw new ClassCastException JavaDoc(JDIMessages.FieldImpl_Can__t_compare_field_to_given_object_1);
75         
76         // See if declaring types are the same, if not return comparison between declaring types.
77
Field type2 = (Field)object;
78         if (!declaringType().equals(type2.declaringType()))
79             return declaringType().compareTo(type2.declaringType());
80         
81         // Return comparison of position within declaring type.
82
int index1 = declaringType().fields().indexOf(this);
83         int index2 = type2.declaringType().fields().indexOf(type2);
84         if (index1 < index2)
85             return -1;
86         else if (index1 > index2)
87             return 1;
88         else return 0;
89     }
90     
91     /**
92      * @return Returns the hash code value.
93      */

94     public int hashCode() {
95         return fFieldID.hashCode();
96     }
97     
98     /**
99      * @return Returns a text representation of the declared type.
100      */

101     public String JavaDoc typeName() {
102         if (fTypeName == null) {
103             fTypeName = TypeImpl.signatureToName(signature());
104         }
105         return fTypeName;
106     }
107     
108     /**
109      * @return Returns the type of the this Field.
110      */

111     public Type type() throws ClassNotLoadedException {
112         if (fType == null) {
113             fType = TypeImpl.create(virtualMachineImpl(), signature(), declaringType().classLoader());
114         }
115         return fType;
116     }
117
118     /**
119      * @return Returns true if object is transient.
120      */

121     public boolean isTransient() {
122         return (fModifierBits & MODIFIER_ACC_TRANSIENT) != 0;
123     }
124     
125     /**
126      * @return Returns true if object is volitile.
127      */

128     public boolean isVolatile() {
129         return (fModifierBits & MODIFIER_ACC_VOLITILE) != 0;
130     }
131     
132     /**
133      * Writes JDWP representation.
134      */

135     public void write(MirrorImpl target, DataOutputStream JavaDoc out) throws IOException JavaDoc {
136         fFieldID.write(out);
137         if (target.fVerboseWriter != null)
138             target.fVerboseWriter.println("field", fFieldID.value()); //$NON-NLS-1$
139
}
140
141     /**
142      * Writes JDWP representation, including ReferenceType.
143      */

144     public void writeWithReferenceType(MirrorImpl target, DataOutputStream JavaDoc out) throws IOException JavaDoc {
145         // See EventRequest case FieldOnly
146
referenceTypeImpl().write(target, out);
147         write(target, out);
148     }
149
150     /**
151      * @return Reads JDWP representation and returns new instance.
152      */

153     public static FieldImpl readWithReferenceTypeWithTag(MirrorImpl target, DataInputStream JavaDoc in) throws IOException JavaDoc {
154         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
155         // See Events FIELD_ACCESS and FIELD_MODIFICATION (refTypeTag + typeID + fieldID).
156
ReferenceTypeImpl referenceType = ReferenceTypeImpl.readWithTypeTag(target, in);
157         if (referenceType == null)
158             return null;
159
160         JdwpFieldID ID = new JdwpFieldID(vmImpl);
161         ID.read(in);
162         if (target.fVerboseWriter != null)
163             target.fVerboseWriter.println("field", ID.value()); //$NON-NLS-1$
164

165         if (ID.isNull())
166             return null;
167         FieldImpl field = referenceType.findField(ID);
168         if (field == null)
169             throw new InternalError JavaDoc(JDIMessages.FieldImpl_Got_FieldID_of_ReferenceType_that_is_not_a_member_of_the_ReferenceType_2);
170         return field;
171     }
172     
173     /**
174      * @return Reads JDWP representation and returns new instance.
175      */

176     public static FieldImpl readWithNameSignatureModifiers(ReferenceTypeImpl target, ReferenceTypeImpl referenceType, boolean withGenericSignature, DataInputStream JavaDoc in) throws IOException JavaDoc {
177         VirtualMachineImpl vmImpl = target.virtualMachineImpl();
178         JdwpFieldID ID = new JdwpFieldID(vmImpl);
179         ID.read(in);
180         if (target.fVerboseWriter != null)
181             target.fVerboseWriter.println("field", ID.value()); //$NON-NLS-1$
182

183         if (ID.isNull())
184             return null;
185         String JavaDoc name = target.readString("name", in); //$NON-NLS-1$
186
String JavaDoc signature = target.readString("signature", in); //$NON-NLS-1$
187
String JavaDoc genericSignature= null;
188         if (withGenericSignature) {
189             genericSignature = target.readString("generic signature", in); //$NON-NLS-1$
190
if ("".equals(genericSignature)) { //$NON-NLS-1$
191
genericSignature= null;
192             }
193         }
194         int modifierBits = target.readInt("modifiers", AccessibleImpl.getModifierStrings(), in); //$NON-NLS-1$
195

196         FieldImpl mirror = new FieldImpl(vmImpl, referenceType, ID, name, signature, genericSignature, modifierBits);
197         return mirror;
198     }
199     
200     public boolean isEnumConstant() {
201         return (fModifierBits & MODIFIER_ACC_ENUM) != 0;
202     }
203 }
204
Popular Tags