javap/src/main/java/sun/tools/javap/FieldData.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 09 Nov 2012 21:33:22 +0100
branchjavap
changeset 144 b06660b614db
child 151 40f95fe90cdc
permissions -rw-r--r--
javap as of revision jdk6-4ab5d66aaf2b
     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     /**
    99      * Returns access of a field.
   100      */
   101     public String[] getAccess(){
   102         Vector v = new Vector();
   103         if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
   104         if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
   105         if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
   106         if ((access & ACC_STATIC)   !=0) v.addElement("static");
   107         if ((access & ACC_FINAL)    !=0) v.addElement("final");
   108         if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
   109         if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
   110         String[] accflags = new String[v.size()];
   111         v.copyInto(accflags);
   112         return accflags;
   113     }
   114 
   115     /**
   116      * Returns name of a field.
   117      */
   118     public String getName(){
   119         return cls.getStringValue(name_index);
   120     }
   121 
   122     /**
   123      * Returns internal signature of a field
   124      */
   125     public String getInternalSig(){
   126         return cls.getStringValue(descriptor_index);
   127     }
   128 
   129     /**
   130      * Returns java type signature of a field.
   131      */
   132     public String getType(){
   133         return new TypeSignature(getInternalSig()).getFieldType();
   134     }
   135 
   136     /**
   137      * Returns true if field is synthetic.
   138      */
   139     public boolean isSynthetic(){
   140         return isSynthetic;
   141     }
   142 
   143     /**
   144      * Returns true if field is deprecated.
   145      */
   146     public boolean isDeprecated(){
   147         return isDeprecated;
   148     }
   149 
   150     /**
   151      * Returns index of constant value in cpool.
   152      */
   153     public int getConstantValueIndex(){
   154         return (value_cpx);
   155     }
   156 
   157     /**
   158      * Returns list of attributes of field.
   159      */
   160     public Vector getAttributes(){
   161         return attrs;
   162     }
   163 }