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