1 /*
   2  * Copyright (c) 2000, 2003, 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.oops;
  26 
  27 import java.io.*;
  28 
  29 import sun.jvm.hotspot.runtime.*;
  30 
  31 // Super class for all fields in an object
  32 public class Field {
  33 
  34   Field(FieldIdentifier id, long offset, boolean isVMField) {
  35     this.offset    = offset;
  36     this.id        = id;
  37     this.isVMField = isVMField;
  38   }
  39 
  40   /** Constructor for fields that are named in an InstanceKlass's
  41       fields array (i.e., named, non-VM fields) */
  42   Field(InstanceKlass holder, int fieldArrayIndex) {
  43     this.holder = holder;
  44     this.fieldArrayIndex = fieldArrayIndex;
  45 
  46     ConstantPool cp      = holder.getConstants();
  47     TypeArray fields     = holder.getFields();
  48     short access         = fields.getShortAt(fieldArrayIndex + InstanceKlass.ACCESS_FLAGS_OFFSET);
  49     short nameIndex      = fields.getShortAt(fieldArrayIndex + InstanceKlass.NAME_INDEX_OFFSET);
  50     short signatureIndex = fields.getShortAt(fieldArrayIndex + InstanceKlass.SIGNATURE_INDEX_OFFSET);
  51     offset               = VM.getVM().buildIntFromShorts(fields.getShortAt(fieldArrayIndex + InstanceKlass.LOW_OFFSET),
  52                                                          fields.getShortAt(fieldArrayIndex + InstanceKlass.HIGH_OFFSET));
  53     short genericSignatureIndex = fields.getShortAt(fieldArrayIndex + InstanceKlass.GENERIC_SIGNATURE_INDEX_OFFSET);
  54     Symbol name = cp.getSymbolAt(nameIndex);
  55     id          = new NamedFieldIdentifier(name.asString());
  56     signature   = cp.getSymbolAt(signatureIndex);
  57     if (genericSignatureIndex != 0)  {
  58        genericSignature = cp.getSymbolAt(genericSignatureIndex);
  59     } else {
  60        genericSignature = null;
  61     }
  62 
  63     fieldType   = new FieldType(signature);
  64     accessFlags = new AccessFlags(access);
  65   }
  66 
  67   private long            offset;
  68   private FieldIdentifier id;
  69   private boolean         isVMField;
  70   // Java fields only
  71   private InstanceKlass   holder;
  72   private FieldType       fieldType;
  73   private Symbol          signature;
  74   private Symbol          genericSignature;
  75   private AccessFlags     accessFlags;
  76   private int             fieldArrayIndex;
  77 
  78   /** Returns the byte offset of the field within the object or klass */
  79   public long getOffset() { return offset; }
  80 
  81   /** Returns the identifier of the field */
  82   public FieldIdentifier getID() { return id; }
  83 
  84   /** Indicates whether this is a VM field */
  85   public boolean isVMField() { return isVMField; }
  86 
  87   /** Indicates whether this is a named field */
  88   public boolean isNamedField() { return (id instanceof NamedFieldIdentifier); }
  89 
  90   public void printOn(PrintStream tty) {
  91     getID().printOn(tty);
  92     tty.print(" {" + getOffset() + "} :");
  93   }
  94 
  95   /** (Named, non-VM fields only) Returns the InstanceKlass containing
  96       this (static or non-static) field. */
  97   public InstanceKlass getFieldHolder() {
  98     return holder;
  99   }
 100 
 101   /** (Named, non-VM fields only) Returns the index in the fields
 102       TypeArray for this field. Equivalent to the "index" in the VM's
 103       fieldDescriptors. */
 104   public int getFieldArrayIndex() {
 105     return fieldArrayIndex;
 106   }
 107 
 108   /** (Named, non-VM fields only) Retrieves the access flags. */
 109   public long getAccessFlags() { return accessFlags.getValue(); }
 110   public AccessFlags getAccessFlagsObj() { return accessFlags; }
 111 
 112   /** (Named, non-VM fields only) Returns the type of this field. */
 113   public FieldType getFieldType() { return fieldType; }
 114 
 115   /** (Named, non-VM fields only) Returns the signature of this
 116       field. */
 117   public Symbol getSignature() { return signature; }
 118   public Symbol getGenericSignature() { return genericSignature; }
 119 
 120   //
 121   // Following acccessors are for named, non-VM fields only
 122   //
 123   public boolean isPublic()                  { return accessFlags.isPublic(); }
 124   public boolean isPrivate()                 { return accessFlags.isPrivate(); }
 125   public boolean isProtected()               { return accessFlags.isProtected(); }
 126   public boolean isPackagePrivate()          { return !isPublic() && !isPrivate() && !isProtected(); }
 127 
 128   public boolean isStatic()                  { return accessFlags.isStatic(); }
 129   public boolean isFinal()                   { return accessFlags.isFinal(); }
 130   public boolean isVolatile()                { return accessFlags.isVolatile(); }
 131   public boolean isTransient()               { return accessFlags.isTransient(); }
 132 
 133   public boolean isSynthetic()               { return accessFlags.isSynthetic(); }
 134   public boolean isEnumConstant()            { return accessFlags.isEnum();      }
 135 
 136   public boolean equals(Object obj) {
 137      if (obj == null) {
 138         return false;
 139      }
 140 
 141      if (! (obj instanceof Field)) {
 142         return false;
 143      }
 144 
 145      Field other = (Field) obj;
 146      return this.getFieldHolder().equals(other.getFieldHolder()) &&
 147             this.getID().equals(other.getID());
 148   }
 149 
 150   public int hashCode() {
 151      return getFieldHolder().hashCode() ^ getID().hashCode();
 152   }
 153 }