1 /*
2  * Copyright (c) 2000, 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * 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   // Copy constructor to create NarrowOopField from OopField.
BasicField(Field fld)47   public BasicField(Field fld) {
48     BasicField field = (BasicField)fld;
49 
50     this.db = field.db;
51     this.containingType = field.containingType;
52     this.name = field.name;
53     this.type = field.type;
54     this.size = field.size;
55     this.isStatic = field.isStatic;
56     this.offset = field.offset;
57     this.staticFieldAddress = field.staticFieldAddress;
58   }
59   /** offsetInBytes is ignored if the field is static;
60       staticFieldAddress is used only if the field is static. */
BasicField(BasicTypeDataBase db, Type containingType, String name, Type type, boolean isStatic, long offsetInBytes, Address staticFieldAddress)61   public BasicField(BasicTypeDataBase db, Type containingType, String name, Type type,
62                     boolean isStatic, long offsetInBytes, Address staticFieldAddress) {
63     this.db = db;
64     this.containingType = containingType;
65     this.name = name;
66     this.type = type;
67     this.size = type.getSize();
68     this.isStatic = isStatic;
69     this.offset = offsetInBytes;
70     this.staticFieldAddress = staticFieldAddress;
71   }
72 
getName()73   public String getName() {
74     return name;
75   }
76 
getType()77   public Type getType() {
78     return type;
79   }
80 
getSize()81   public long getSize() {
82     return size;
83   }
84 
isStatic()85   public boolean isStatic() {
86     return isStatic;
87   }
88 
getOffset()89   public long getOffset() throws WrongTypeException {
90     if (isStatic) {
91       throw new WrongTypeException("field \"" + name + "\" in class " +
92                                    containingType.getName() + " is static");
93     }
94     return offset;
95   }
96 
getStaticFieldAddress()97   public Address getStaticFieldAddress() throws WrongTypeException {
98     if (!isStatic) {
99       throw new WrongTypeException("field \"" + name + "\" in class " +
100                                    containingType.getName() + " is not static");
101     }
102     return staticFieldAddress;
103   }
104 
105   //--------------------------------------------------------------------------------
106   // Dereferencing operations for non-static fields
107   //
108 
getJBoolean(Address addr)109   public boolean   getJBoolean (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
110     if (isStatic) {
111       throw new WrongTypeException();
112     }
113     return addr.getJBooleanAt(offset);
114   }
getJByte(Address addr)115   public byte      getJByte    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
116     if (isStatic) {
117       throw new WrongTypeException();
118     }
119     return addr.getJByteAt(offset);
120   }
getJChar(Address addr)121   public char      getJChar    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
122     if (isStatic) {
123       throw new WrongTypeException();
124     }
125     return addr.getJCharAt(offset);
126   }
getJDouble(Address addr)127   public double    getJDouble  (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
128     if (isStatic) {
129       throw new WrongTypeException();
130     }
131     return addr.getJDoubleAt(offset);
132   }
getJFloat(Address addr)133   public float     getJFloat   (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
134     if (isStatic) {
135       throw new WrongTypeException();
136     }
137     return addr.getJFloatAt(offset);
138   }
getJInt(Address addr)139   public int       getJInt     (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
140     if (isStatic) {
141       throw new WrongTypeException();
142     }
143     return addr.getJIntAt(offset);
144   }
getJLong(Address addr)145   public long      getJLong    (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
146     if (isStatic) {
147       throw new WrongTypeException();
148     }
149     return addr.getJLongAt(offset);
150   }
getJShort(Address addr)151   public short     getJShort   (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
152     if (isStatic) {
153       throw new WrongTypeException();
154     }
155     return addr.getJShortAt(offset);
156   }
getCInteger(Address addr, CIntegerType type)157   public long      getCInteger (Address addr, CIntegerType type)
158     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
159     if (isStatic) {
160       throw new WrongTypeException();
161     }
162     return addr.getCIntegerAt(offset, type.getSize(), type.isUnsigned());
163   }
getAddress(Address addr)164   public Address   getAddress  (Address addr) throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
165     if (isStatic) {
166       throw new WrongTypeException();
167     }
168     return addr.getAddressAt(offset);
169   }
getOopHandle(Address addr)170   public OopHandle getOopHandle(Address addr)
171     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
172     if (isStatic) {
173       throw new WrongTypeException();
174     }
175     return addr.getOopHandleAt(offset);
176   }
getNarrowOopHandle(Address addr)177   public OopHandle getNarrowOopHandle(Address addr)
178     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
179     if (isStatic) {
180       throw new WrongTypeException();
181     }
182     return addr.getCompOopHandleAt(offset);
183   }
184 
185   //--------------------------------------------------------------------------------
186   // Dereferencing operations for static fields
187   //
188 
getJBoolean()189   public boolean   getJBoolean () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
190     if (!isStatic) {
191       throw new WrongTypeException();
192     }
193     return staticFieldAddress.getJBooleanAt(0);
194   }
getJByte()195   public byte      getJByte    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
196     if (!isStatic) {
197       throw new WrongTypeException();
198     }
199     return staticFieldAddress.getJByteAt(0);
200   }
getJChar()201   public char      getJChar    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
202     if (!isStatic) {
203       throw new WrongTypeException();
204     }
205     return staticFieldAddress.getJCharAt(0);
206   }
getJDouble()207   public double    getJDouble  () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
208     if (!isStatic) {
209       throw new WrongTypeException();
210     }
211     return staticFieldAddress.getJDoubleAt(0);
212   }
getJFloat()213   public float     getJFloat   () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
214     if (!isStatic) {
215       throw new WrongTypeException();
216     }
217     return staticFieldAddress.getJFloatAt(0);
218   }
getJInt()219   public int       getJInt     () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
220     if (!isStatic) {
221       throw new WrongTypeException();
222     }
223     return staticFieldAddress.getJIntAt(0);
224   }
getJLong()225   public long      getJLong    () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
226     if (!isStatic) {
227       throw new WrongTypeException();
228     }
229     return staticFieldAddress.getJLongAt(0);
230   }
getJShort()231   public short     getJShort   () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
232     if (!isStatic) {
233       throw new WrongTypeException();
234     }
235     return staticFieldAddress.getJShortAt(0);
236   }
getCInteger(CIntegerType type)237   public long      getCInteger (CIntegerType type)
238     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
239     if (!isStatic) {
240       throw new WrongTypeException();
241     }
242     return staticFieldAddress.getCIntegerAt(0, type.getSize(), type.isUnsigned());
243   }
getAddress()244   public Address   getAddress  () throws UnmappedAddressException, UnalignedAddressException, WrongTypeException {
245     if (!isStatic) {
246       throw new WrongTypeException();
247     }
248     return staticFieldAddress.getAddressAt(0);
249   }
getOopHandle()250   public OopHandle getOopHandle()
251     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
252     if (!isStatic) {
253       throw new WrongTypeException();
254     }
255     return staticFieldAddress.getOopHandleAt(0);
256   }
getNarrowOopHandle()257   public OopHandle getNarrowOopHandle()
258     throws UnmappedAddressException, UnalignedAddressException, WrongTypeException, NotInHeapException {
259     if (!isStatic) {
260       throw new WrongTypeException();
261     }
262     return staticFieldAddress.getCompOopHandleAt(0);
263   }
264 }
265