1 /*
   2  * Copyright (c) 2001, 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,
  20  * CA 94065 USA or visit www.oracle.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.debugger.cdbg.basic;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 import sun.jvm.hotspot.debugger.cdbg.*;
  29 
  30 public class BasicField implements Field {
  31   private String  name;
  32   private Type    type;
  33   private int     accessControl;
  34   private boolean isStatic;
  35   private long    offset;
  36   private Address address;
  37 
  38   /** See {@link sun.jvm.hotspot.debugger.cdbg.AccessControl} for
  39       valid access control values */
  40   public BasicField(String name, Type type, int accessControl, boolean isStatic) {
  41     this.name = name;
  42     this.type = type;
  43     this.accessControl = accessControl;
  44   }
  45 
  46   public int getAccessControl() { return accessControl; }
  47 
  48   public String getName() { return name; }
  49 
  50   public Type getType() { return type; }
  51 
  52   public boolean isStatic() { return isStatic; }
  53 
  54   /** Nonstatic fields only: set offset of field */
  55   public void setOffset(long offset) {
  56     if (isStatic) throw new RuntimeException("Nonstatic fields only");
  57     this.offset = offset;
  58   }
  59 
  60   /** Nonstatic fields only: get offset of field */
  61   public long getOffset() {
  62     if (isStatic) throw new RuntimeException("Nonstatic fields only");
  63     return offset;
  64   }
  65 
  66   /** Static fields only: set address of field. The resolution
  67       mechanism will automatically attempt to find the address of the
  68       field based on a "class name"::"field name" global symbol lookup
  69       in the database if the address has not been set. */
  70   public void setAddress(Address address) {
  71     if (!isStatic) throw new RuntimeException("Static fields only");
  72     this.address = address;
  73   }
  74 
  75   /** Static fields only: get address of field */
  76   public Address getAddress() {
  77     if (!isStatic) throw new RuntimeException("Static fields only");
  78     return address;
  79   }
  80 
  81   public void resolveTypes(Type containingType, BasicCDebugInfoDataBase db, ResolveListener listener) {
  82     type = db.resolveType(containingType, type, listener, "resolving field type");
  83     if (isStatic) {
  84       if (address == null) {
  85         String fieldSymName = getType().getName() + "::" + getName();
  86         GlobalSym sym = db.lookupSym(fieldSymName);
  87         if (sym == null) {
  88           listener.resolveFailed(getType(), getName());
  89         } else {
  90           address = sym.getAddress();
  91         }
  92       }
  93     }
  94   }
  95 }