001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal;
011:
012: import java.io.DataInputStream;
013: import java.io.DataOutputStream;
014: import java.io.IOException;
015:
016: import org.eclipse.jdi.internal.jdwp.JdwpFieldID;
017:
018: import com.sun.jdi.ClassNotLoadedException;
019: import com.sun.jdi.Field;
020: import com.sun.jdi.Type;
021:
022: /**
023: * this class implements the corresponding interfaces
024: * declared by the JDI specification. See the com.sun.jdi package
025: * for more information.
026: *
027: */
028: public class FieldImpl extends TypeComponentImpl implements Field {
029: /** ID that corresponds to this reference. */
030: private JdwpFieldID fFieldID;
031: private Type fType;
032: private String fTypeName;
033:
034: /**
035: * Creates new FieldImpl.
036: */
037: public FieldImpl(VirtualMachineImpl vmImpl,
038: ReferenceTypeImpl declaringType, JdwpFieldID ID,
039: String name, String signature, String genericSignature,
040: int modifierBits) {
041: super (
042: "Field", vmImpl, declaringType, name, signature, genericSignature, modifierBits); //$NON-NLS-1$
043: fFieldID = ID;
044: }
045:
046: /**
047: * Flushes all stored Jdwp results.
048: */
049: public void flushStoredJdwpResults() {
050: // Note that no results are cached.
051: }
052:
053: /**
054: * @return Returns fieldID of field.
055: */
056: public JdwpFieldID getFieldID() {
057: return fFieldID;
058: }
059:
060: /**
061: * @return Returns true if two mirrors refer to the same entity in the target VM.
062: * @see java.lang.Object#equals(Object)
063: */
064: public boolean equals(Object object) {
065: return object != null
066: && object.getClass().equals(this .getClass())
067: && fFieldID.equals(((FieldImpl) object).fFieldID)
068: && referenceTypeImpl().equals(
069: ((FieldImpl) object).referenceTypeImpl());
070: }
071:
072: /**
073: * @return Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
074: */
075: public int compareTo(Object object) {
076: if (object == null
077: || !object.getClass().equals(this .getClass()))
078: throw new ClassCastException(
079: JDIMessages.FieldImpl_Can__t_compare_field_to_given_object_1);
080:
081: // See if declaring types are the same, if not return comparison between declaring types.
082: Field type2 = (Field) object;
083: if (!declaringType().equals(type2.declaringType()))
084: return declaringType().compareTo(type2.declaringType());
085:
086: // Return comparison of position within declaring type.
087: int index1 = declaringType().fields().indexOf(this );
088: int index2 = type2.declaringType().fields().indexOf(type2);
089: if (index1 < index2)
090: return -1;
091: else if (index1 > index2)
092: return 1;
093: else
094: return 0;
095: }
096:
097: /**
098: * @return Returns the hash code value.
099: */
100: public int hashCode() {
101: return fFieldID.hashCode();
102: }
103:
104: /**
105: * @return Returns a text representation of the declared type.
106: */
107: public String typeName() {
108: if (fTypeName == null) {
109: fTypeName = TypeImpl.signatureToName(signature());
110: }
111: return fTypeName;
112: }
113:
114: /**
115: * @return Returns the type of the this Field.
116: */
117: public Type type() throws ClassNotLoadedException {
118: if (fType == null) {
119: fType = TypeImpl.create(virtualMachineImpl(), signature(),
120: declaringType().classLoader());
121: }
122: return fType;
123: }
124:
125: /**
126: * @return Returns true if object is transient.
127: */
128: public boolean isTransient() {
129: return (fModifierBits & MODIFIER_ACC_TRANSIENT) != 0;
130: }
131:
132: /**
133: * @return Returns true if object is volitile.
134: */
135: public boolean isVolatile() {
136: return (fModifierBits & MODIFIER_ACC_VOLITILE) != 0;
137: }
138:
139: /**
140: * Writes JDWP representation.
141: */
142: public void write(MirrorImpl target, DataOutputStream out)
143: throws IOException {
144: fFieldID.write(out);
145: if (target.fVerboseWriter != null)
146: target.fVerboseWriter.println("field", fFieldID.value()); //$NON-NLS-1$
147: }
148:
149: /**
150: * Writes JDWP representation, including ReferenceType.
151: */
152: public void writeWithReferenceType(MirrorImpl target,
153: DataOutputStream out) throws IOException {
154: // See EventRequest case FieldOnly
155: referenceTypeImpl().write(target, out);
156: write(target, out);
157: }
158:
159: /**
160: * @return Reads JDWP representation and returns new instance.
161: */
162: public static FieldImpl readWithReferenceTypeWithTag(
163: MirrorImpl target, DataInputStream in) throws IOException {
164: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
165: // See Events FIELD_ACCESS and FIELD_MODIFICATION (refTypeTag + typeID + fieldID).
166: ReferenceTypeImpl referenceType = ReferenceTypeImpl
167: .readWithTypeTag(target, in);
168: if (referenceType == null)
169: return null;
170:
171: JdwpFieldID ID = new JdwpFieldID(vmImpl);
172: ID.read(in);
173: if (target.fVerboseWriter != null)
174: target.fVerboseWriter.println("field", ID.value()); //$NON-NLS-1$
175:
176: if (ID.isNull())
177: return null;
178: FieldImpl field = referenceType.findField(ID);
179: if (field == null)
180: throw new InternalError(
181: JDIMessages.FieldImpl_Got_FieldID_of_ReferenceType_that_is_not_a_member_of_the_ReferenceType_2);
182: return field;
183: }
184:
185: /**
186: * @return Reads JDWP representation and returns new instance.
187: */
188: public static FieldImpl readWithNameSignatureModifiers(
189: ReferenceTypeImpl target, ReferenceTypeImpl referenceType,
190: boolean withGenericSignature, DataInputStream in)
191: throws IOException {
192: VirtualMachineImpl vmImpl = target.virtualMachineImpl();
193: JdwpFieldID ID = new JdwpFieldID(vmImpl);
194: ID.read(in);
195: if (target.fVerboseWriter != null)
196: target.fVerboseWriter.println("field", ID.value()); //$NON-NLS-1$
197:
198: if (ID.isNull())
199: return null;
200: String name = target.readString("name", in); //$NON-NLS-1$
201: String signature = target.readString("signature", in); //$NON-NLS-1$
202: String genericSignature = null;
203: if (withGenericSignature) {
204: genericSignature = target.readString(
205: "generic signature", in); //$NON-NLS-1$
206: if ("".equals(genericSignature)) { //$NON-NLS-1$
207: genericSignature = null;
208: }
209: }
210: int modifierBits = target.readInt(
211: "modifiers", AccessibleImpl.getModifierStrings(), in); //$NON-NLS-1$
212:
213: FieldImpl mirror = new FieldImpl(vmImpl, referenceType, ID,
214: name, signature, genericSignature, modifierBits);
215: return mirror;
216: }
217:
218: public boolean isEnumConstant() {
219: return (fModifierBits & MODIFIER_ACC_ENUM) != 0;
220: }
221: }
|