1 cananian 1.1.2.6 // FieldPointer.java, created Sat Mar 27 17:05:07 1999 by duncan 2 cananian 1.1.2.5 // Copyright (C) 1998 Duncan Bryce <duncan@lcs.mit.edu> 3 cananian 1.1.2.5 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 duncan 1.1.2.1 package harpoon.Interpret.Tree; 5 duncan 1.1.2.1 6 duncan 1.1.2.2 import harpoon.ClassFile.HClass; 7 duncan 1.1.2.1 import harpoon.Util.Tuple; 8 duncan 1.1.2.1 9 duncan 1.1.2.1 /** 10 duncan 1.1.2.1 * The <code>FieldPointer</code> class represents a pointer to an 11 duncan 1.1.2.1 * <code>ObjectRef</code> plus some offset. This pointer can be dereferenced 12 duncan 1.1.2.1 * with <code>getValue()</code>, and the value at this location can be 13 duncan 1.1.2.1 * modified with <code>updateValue()</code>. 14 duncan 1.1.2.1 * 15 duncan 1.1.2.1 * @author Duncan Bryce <duncan@lcs.mit.edu> 16 cananian 1.2 * @version $Id: FieldPointer.java,v 1.2 2002/02/25 21:05:50 cananian Exp $ 17 duncan 1.1.2.1 */ 18 duncan 1.1.2.1 class FieldPointer extends Pointer { 19 duncan 1.1.2.1 private boolean isDerived; 20 duncan 1.1.2.1 21 duncan 1.1.2.1 // Private constructor used to add two FieldPointers. 22 duncan 1.1.2.1 private FieldPointer(FieldPointer base, long offset) { 23 duncan 1.1.2.1 this((ObjectRef)base.getBase(), base.getOffset() + offset, true); 24 duncan 1.1.2.1 } 25 duncan 1.1.2.1 26 duncan 1.1.2.1 // Private constructor used to clone a FieldPointer. 27 duncan 1.1.2.1 private FieldPointer(ObjectRef base, long offset, boolean isDerived) { 28 duncan 1.1.2.1 super(new Object[] { base, new Long(offset) }); 29 duncan 1.1.2.1 this.isDerived = isDerived; 30 duncan 1.1.2.1 } 31 duncan 1.1.2.1 32 duncan 1.1.2.1 /** Class constructor. */ 33 duncan 1.1.2.1 FieldPointer(ObjectRef base, long offset) { 34 duncan 1.1.2.1 this(base, offset, false); 35 duncan 1.1.2.1 } 36 duncan 1.1.2.1 37 duncan 1.1.2.1 /** Adds the specified parameter to this <code>ArrayPointer</code>'s 38 duncan 1.1.2.1 * offset. */ 39 duncan 1.1.2.1 public Pointer add(long offset) { 40 duncan 1.1.2.1 return new FieldPointer(this, offset); 41 duncan 1.1.2.1 } 42 duncan 1.1.2.1 43 duncan 1.1.2.1 /** Returns true if <code>obj</code> is a <code>FieldPointer</code> 44 duncan 1.1.2.1 * that points to the same location as this <code>FieldPointer</code>. 45 duncan 1.1.2.1 */ 46 duncan 1.1.2.1 public boolean equals(Object obj) { 47 cananian 1.1.2.3 FieldPointer ptr; 48 cananian 1.1.2.3 if (this==obj) return true; 49 cananian 1.1.2.3 if (null==obj) return false; 50 cananian 1.1.2.3 try { ptr = (FieldPointer)obj; } 51 cananian 1.1.2.3 catch (ClassCastException ignore) { return false; } 52 cananian 1.1.2.3 return (getBase()==ptr.getBase()) && 53 cananian 1.1.2.3 (getOffset()==ptr.getOffset()); 54 duncan 1.1.2.1 } 55 duncan 1.1.2.1 56 duncan 1.1.2.1 /** Returns an <code>ArrayRef</code> representing the base of this 57 duncan 1.1.2.1 * <code>ArrayPointer</code>. */ 58 duncan 1.1.2.1 public Object getBase() { return (ObjectRef)proj(0); } 59 duncan 1.1.2.1 60 duncan 1.1.2.1 /** Returns the offset of this <code>ArrayPointer</code> */ 61 duncan 1.1.2.1 public long getOffset() { return ((Long)proj(1)).longValue(); } 62 duncan 1.1.2.1 63 duncan 1.1.2.1 /** Returns the value obtained by dereferencing this 64 duncan 1.1.2.1 * <code>FieldPointer</code>. This value is in non-native format. 65 duncan 1.1.2.1 */ 66 duncan 1.1.2.1 Object getValue() { 67 duncan 1.1.2.2 //return Method.toNonNativeFormat(((ObjectRef)getBase()).get(this)); 68 duncan 1.1.2.2 return Method.toNonNativeFormat(ObjectRef.get(this)); 69 duncan 1.1.2.1 } 70 duncan 1.1.2.4 71 duncan 1.1.2.4 /** Returns an integer enumeration of the kind of this Pointer. The 72 duncan 1.1.2.4 enumerated values are public fields of the Pointer class. 73 duncan 1.1.2.4 */ 74 duncan 1.1.2.4 public int kind() { return Pointer.FIELD_PTR; } 75 duncan 1.1.2.4 76 duncan 1.1.2.1 /** Sets the value at the memory location specified by this 77 duncan 1.1.2.1 * <code>FieldPointer</code> to the specified parameter. 78 duncan 1.1.2.1 * This value should be in non-native format. 79 duncan 1.1.2.1 */ 80 duncan 1.1.2.1 void updateValue(Object value) { 81 duncan 1.1.2.1 ObjectRef objref = (ObjectRef)getBase(); 82 duncan 1.1.2.2 HClass type = objref.ss.getField(this).getType(); 83 cananian 1.1.2.7 if (Debug.DEBUG) 84 cananian 1.1.2.7 Debug.db("Calling FieldValue.update: " + value + ", " + type); 85 duncan 1.1.2.2 ObjectRef.update(this, Method.toNativeFormat(value, type)); 86 duncan 1.1.2.1 } 87 duncan 1.1.2.1 88 duncan 1.1.2.1 /** Always returns false. */ 89 duncan 1.1.2.1 public boolean isConst() { return false; } 90 duncan 1.1.2.1 91 duncan 1.1.2.1 /** Returns true if this <code>ArrayPointer</code> is derived. */ 92 duncan 1.1.2.1 public boolean isDerived() { return isDerived; } 93 duncan 1.1.2.1 94 duncan 1.1.2.1 /** Returns a human-readable representation of this 95 duncan 1.1.2.1 * <code>ArrayPointer</code> 96 duncan 1.1.2.1 */ 97 duncan 1.1.2.1 public String toString() { 98 duncan 1.1.2.2 StringBuffer sb = new StringBuffer("FieldPtr --> < "); 99 duncan 1.1.2.2 sb.append(((ObjectRef)getBase()).toString()); 100 duncan 1.1.2.1 sb.append(" , "); 101 duncan 1.1.2.1 sb.append(getOffset()); 102 duncan 1.1.2.1 sb.append(" >"); 103 duncan 1.1.2.1 return sb.toString(); 104 duncan 1.1.2.1 } 105 duncan 1.1.2.1 } 106 duncan 1.1.2.2 107 duncan 1.1.2.2 108 duncan 1.1.2.2 109 duncan 1.1.2.2 110 duncan 1.1.2.2 111 cananian 1.2