1 /*
   2  * Copyright 2000 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *  
  23  */
  24 
  25 package sun.jvm.hotspot.types.basic;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 import sun.jvm.hotspot.types.*;
  29 
  30 /** A basic implementation of Field which should be suitable for
  31     cross-platform use. */
  32 
  33 public class BasicField implements Field {
  34   protected BasicTypeDataBase db;
  35   protected Type type;
  36 
  37   private Type containingType;
  38   private String name;
  39   private long size;
  40   private boolean isStatic;
  41   /** Used for nonstatic fields only */
  42   private long offset;
  43   /** Used for static fields only */
  44   private Address staticFieldAddress;
  45 
  46   /** offsetInBytes is ignored if the field is static;
  47       staticFieldAddress is used only if the field is static. */
  48   public BasicField(BasicTypeDataBase db, Type containingType, String name, Type type,
  49                     boolean isStatic, long offsetInBytes, Address staticFieldAddress) {
  50     this.db = db;
  51     this.containingType = containingType;
  52     this.name = name;
  53     this.type = type;
  54     this.size = type.getSize();
  55     this.isStatic = isStatic;
  56     this.offset = offsetInBytes;
  57     this.staticFieldAddress = staticFieldAddress;
  58   }
  59   
  60   public String getName() {
  61     return name;
  62   }
  63 
  64   public Type getType() {
  65     return type;
  66   }
  67 
  68   public long getSize() {
  69     return size;
  70   }
  71 
  72   public boolean isStatic() {
  73     return isStatic;
  74   }
  75 
  76   public long getOffset() throws WrongTypeException {
  77     if (isStatic) {
  78       throw new WrongTypeException("field \"" + name + "\" in class " +
  79                                    containingType.getName() + " is static");
  80     }
  81     return offset;
  82   }
  83   
  84   public Address getStaticFieldAddress() throws WrongTypeException {
  85     if (!isStatic) {
  86       throw new WrongTypeException("field \"" + name + "\" in class " +
  87                                    containingType.getName() + " is not static");
  88     }
  89     return staticFieldAddress;
  90   }
  91   
  92   //--------------------------------------------------------------------------------
  93   // Dereferencing operations for non-static fields
  94   //
  95 
  96   public boolean   getJBoolean (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
  97     if (isStatic) {
  98       throw new WrongTypeException();
  99     }
 100     return addr.getJBooleanAt(offset);
 101   }
 102   public byte      getJByte    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 103     if (isStatic) {
 104       throw new WrongTypeException();
 105     }
 106     return addr.getJByteAt(offset);
 107   }
 108   public char      getJChar    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 109     if (isStatic) {
 110       throw new WrongTypeException();
 111     }
 112     return addr.getJCharAt(offset);
 113   }
 114   public double    getJDouble  (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 115     if (isStatic) {
 116       throw new WrongTypeException();
 117     }
 118     return addr.getJDoubleAt(offset);
 119   }
 120   public float     getJFloat   (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 121     if (isStatic) {
 122       throw new WrongTypeException();
 123     }
 124     return addr.getJFloatAt(offset);
 125   }
 126   public int       getJInt     (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 127     if (isStatic) {
 128       throw new WrongTypeException();
 129     }
 130     return addr.getJIntAt(offset);
 131   }
 132   public long      getJLong    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 133     if (isStatic) {
 134       throw new WrongTypeException();
 135     }
 136     return addr.getJLongAt(offset);
 137   }
 138   public short     getJShort   (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 139     if (isStatic) {
 140       throw new WrongTypeException();
 141     }
 142     return addr.getJShortAt(offset);
 143   }
 144   public long      getCInteger (Address addr, CIntegerType type)
 145     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 146     if (isStatic) {
 147       throw new WrongTypeException();
 148     }
 149     return addr.getCIntegerAt(offset, type.getSize(), type.isUnsigned());
 150   }
 151   public Address   getAddress  (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 152     if (isStatic) {
 153       throw new WrongTypeException();
 154     }
 155     return addr.getAddressAt(offset);
 156   }
 157   public OopHandle getOopHandle(Address addr)
 158     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
 159     if (isStatic) {
 160       throw new WrongTypeException();
 161     }
 162     return addr.getOopHandleAt(offset);
 163   }
 164 
 165   //--------------------------------------------------------------------------------
 166   // Dereferencing operations for static fields
 167   //
 168 
 169   public boolean   getJBoolean () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 170     if (!isStatic) {
 171       throw new WrongTypeException();
 172     }
 173     return staticFieldAddress.getJBooleanAt(0);
 174   }
 175   public byte      getJByte    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 176     if (!isStatic) {
 177       throw new WrongTypeException();
 178     }
 179     return staticFieldAddress.getJByteAt(0);
 180   }
 181   public char      getJChar    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 182     if (!isStatic) {
 183       throw new WrongTypeException();
 184     }
 185     return staticFieldAddress.getJCharAt(0);
 186   }
 187   public double    getJDouble  () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 188     if (!isStatic) {
 189       throw new WrongTypeException();
 190     }
 191     return staticFieldAddress.getJDoubleAt(0);
 192   }
 193   public float     getJFloat   () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 194     if (!isStatic) {
 195       throw new WrongTypeException();
 196     }
 197     return staticFieldAddress.getJFloatAt(0);
 198   }
 199   public int       getJInt     () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 200     if (!isStatic) {
 201       throw new WrongTypeException();
 202     }
 203     return staticFieldAddress.getJIntAt(0);
 204   }
 205   public long      getJLong    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 206     if (!isStatic) {
 207       throw new WrongTypeException();
 208     }
 209     return staticFieldAddress.getJLongAt(0);
 210   }
 211   public short     getJShort   () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 212     if (!isStatic) {
 213       throw new WrongTypeException();
 214     }
 215     return staticFieldAddress.getJShortAt(0);
 216   }
 217   public long      getCInteger (CIntegerType type)
 218     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 219     if (!isStatic) {
 220       throw new WrongTypeException();
 221     }
 222     return staticFieldAddress.getCIntegerAt(0, type.getSize(), type.isUnsigned());
 223   }
 224   public Address   getAddress  () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
 225     if (!isStatic) {
 226       throw new WrongTypeException();
 227     }
 228     return staticFieldAddress.getAddressAt(0);
 229   }
 230   public OopHandle getOopHandle()
 231     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
 232     if (!isStatic) {
 233       throw new WrongTypeException();
 234     }
 235     return staticFieldAddress.getOopHandleAt(0);
 236   }
 237 }