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