javap/src/main/java/sun/tools/javap/FieldData.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Nov 2012 13:23:52 +0100
branchjavap
changeset 151 40f95fe90cdc
parent 144 b06660b614db
permissions -rw-r--r--
Almost rewritten to javap: @JavaScriptBody and double/long may not yet be supported
     1 /*
     2  * Copyright (c) 2002, 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 
    27 package sun.tools.javap;
    28 
    29 import java.util.*;
    30 import java.io.*;
    31 
    32 /**
    33  * Strores field data informastion.
    34  *
    35  * @author  Sucheta Dambalkar (Adopted code from jdis)
    36  */
    37 
    38 public class FieldData implements RuntimeConstants  {
    39 
    40     ClassData cls;
    41     int access;
    42     int name_index;
    43     int descriptor_index;
    44     int attributes_count;
    45     int value_cpx=0;
    46     boolean isSynthetic=false;
    47     boolean isDeprecated=false;
    48     Vector attrs;
    49 
    50     public FieldData(ClassData cls){
    51         this.cls=cls;
    52     }
    53 
    54     /**
    55      * Read and store field info.
    56      */
    57     public void read(DataInputStream in) throws IOException {
    58         access = in.readUnsignedShort();
    59         name_index = in.readUnsignedShort();
    60         descriptor_index = in.readUnsignedShort();
    61         // Read the attributes
    62         int attributes_count = in.readUnsignedShort();
    63         attrs=new Vector(attributes_count);
    64         for (int i = 0; i < attributes_count; i++) {
    65             int attr_name_index=in.readUnsignedShort();
    66             if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
    67             String attr_name=cls.getString(attr_name_index);
    68             if (attr_name.equals("ConstantValue")){
    69                 if (in.readInt()!=2)
    70                     throw new ClassFormatError("invalid ConstantValue attr length");
    71                 value_cpx=in.readUnsignedShort();
    72                 AttrData attr=new AttrData(cls);
    73                 attr.read(attr_name_index);
    74                 attrs.addElement(attr);
    75             } else if (attr_name.equals("Synthetic")){
    76                 if (in.readInt()!=0)
    77                     throw new ClassFormatError("invalid Synthetic attr length");
    78                 isSynthetic=true;
    79                 AttrData attr=new AttrData(cls);
    80                 attr.read(attr_name_index);
    81                 attrs.addElement(attr);
    82             } else if (attr_name.equals("Deprecated")){
    83                 if (in.readInt()!=0)
    84                     throw new ClassFormatError("invalid Synthetic attr length");
    85                 isDeprecated = true;
    86                 AttrData attr=new AttrData(cls);
    87                 attr.read(attr_name_index);
    88                 attrs.addElement(attr);
    89             } else {
    90                 AttrData attr=new AttrData(cls);
    91                 attr.read(attr_name_index, in);
    92                 attrs.addElement(attr);
    93             }
    94         }
    95 
    96     }  // end read
    97 
    98     public boolean isStatic() {
    99         return (access & ACC_STATIC) != 0;
   100     }
   101     
   102     /**
   103      * Returns access of a field.
   104      */
   105     public String[] getAccess(){
   106         Vector v = new Vector();
   107         if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
   108         if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
   109         if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
   110         if ((access & ACC_STATIC)   !=0) v.addElement("static");
   111         if ((access & ACC_FINAL)    !=0) v.addElement("final");
   112         if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
   113         if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
   114         String[] accflags = new String[v.size()];
   115         v.copyInto(accflags);
   116         return accflags;
   117     }
   118 
   119     /**
   120      * Returns name of a field.
   121      */
   122     public String getName(){
   123         return cls.getStringValue(name_index);
   124     }
   125 
   126     /**
   127      * Returns internal signature of a field
   128      */
   129     public String getInternalSig(){
   130         return cls.getStringValue(descriptor_index);
   131     }
   132 
   133     /**
   134      * Returns java type signature of a field.
   135      */
   136     public String getType(){
   137         return new TypeSignature(getInternalSig()).getFieldType();
   138     }
   139 
   140     /**
   141      * Returns true if field is synthetic.
   142      */
   143     public boolean isSynthetic(){
   144         return isSynthetic;
   145     }
   146 
   147     /**
   148      * Returns true if field is deprecated.
   149      */
   150     public boolean isDeprecated(){
   151         return isDeprecated;
   152     }
   153 
   154     /**
   155      * Returns index of constant value in cpool.
   156      */
   157     public int getConstantValueIndex(){
   158         return (value_cpx);
   159     }
   160 
   161     /**
   162      * Returns list of attributes of field.
   163      */
   164     public Vector getAttributes(){
   165         return attrs;
   166     }
   167 }