1 /*
   2  * Copyright (c) 2000, 2011, 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 import sun.jvm.hotspot.runtime.BasicType;
  29 import sun.jvm.hotspot.utilities.*;
  30 
  31 public class FieldType {
  32 
  33   private Symbol signature;
  34   private char   first;
  35 
  36   public FieldType(Symbol signature) {
  37     this.signature = signature;
  38     this.first     = (char) signature.getByteAt(0);
  39     if (Assert.ASSERTS_ENABLED) {
  40        switch (this.first) {
  41        case 'B':
  42        case 'C':
  43        case 'D':
  44        case 'F':
  45        case 'I':
  46        case 'J':
  47        case 'S':
  48        case 'Z':
  49        case 'L':
  50        case '[':
  51            break;   // Ok. signature char known
  52        default:
  53          Assert.that(false, "Unknown char in field signature \"" + signature.asString() + "\": " + this.first);
  54        }
  55     }
  56   }
  57 
  58   public boolean isOop()     { return isObject() || isArray(); }
  59   public boolean isByte()    { return first == 'B'; }
  60   public boolean isChar()    { return first == 'C'; }
  61   public boolean isDouble()  { return first == 'D'; }
  62   public boolean isFloat()   { return first == 'F'; }
  63   public boolean isInt()     { return first == 'I'; }
  64   public boolean isLong()    { return first == 'J'; }
  65   public boolean isShort()   { return first == 'S'; }
  66   public boolean isBoolean() { return first == 'Z'; }
  67   public boolean isObject()  { return first == 'L'; }
  68   public boolean isArray()   { return first == '['; }
  69 
  70   public Symbol getSignature() { return signature; }
  71 
  72   public static class ArrayInfo {
  73     private int dimension;
  74     private int elementBasicType; // See BasicType.java
  75     // FIXME: consider adding name of element class
  76 
  77     public ArrayInfo(int dimension, int elementBasicType) {
  78       this.dimension = dimension;
  79       this.elementBasicType = elementBasicType;
  80     }
  81 
  82     public int dimension()        { return dimension; }
  83     /** See BasicType.java */
  84     public int elementBasicType() { return elementBasicType; }
  85   }
  86 
  87   /** Only valid for T_ARRAY; throws unspecified exception otherwise */
  88   public ArrayInfo getArrayInfo() {
  89     int index = 1;
  90     int dim   = 1;
  91     index = skipOptionalSize(signature, index);
  92     while (signature.getByteAt(index) == '[') {
  93       index++;
  94       dim++;
  95       skipOptionalSize(signature, index);
  96     }
  97     int elementType = BasicType.charToType((char) signature.getByteAt(index));
  98     return new ArrayInfo(dim, elementType);
  99   }
 100 
 101   private int skipOptionalSize(Symbol sig, int index) {
 102     byte c = sig.getByteAt(index);
 103     while (c >= '0' && c <= '9') {
 104       ++index;
 105       c = sig.getByteAt(index);
 106     }
 107     return index;
 108   }
 109 }