rt/javap/src/main/java/org/apidesign/javap/FieldData.java
changeset 772 d382dacfd73f
parent 249 001389026dbf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/FieldData.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +package org.apidesign.javap;
    1.31 +
    1.32 +import java.io.*;
    1.33 +
    1.34 +/**
    1.35 + * Strores field data informastion.
    1.36 + *
    1.37 + * @author  Sucheta Dambalkar (Adopted code from jdis)
    1.38 + */
    1.39 +
    1.40 +public class FieldData implements RuntimeConstants  {
    1.41 +
    1.42 +    ClassData cls;
    1.43 +    int access;
    1.44 +    int name_index;
    1.45 +    int descriptor_index;
    1.46 +    int attributes_count;
    1.47 +    int value_cpx=0;
    1.48 +    boolean isSynthetic=false;
    1.49 +    boolean isDeprecated=false;
    1.50 +    Vector attrs;
    1.51 +
    1.52 +    public FieldData(ClassData cls){
    1.53 +        this.cls=cls;
    1.54 +    }
    1.55 +
    1.56 +    /**
    1.57 +     * Read and store field info.
    1.58 +     */
    1.59 +    public void read(DataInputStream in) throws IOException {
    1.60 +        access = in.readUnsignedShort();
    1.61 +        name_index = in.readUnsignedShort();
    1.62 +        descriptor_index = in.readUnsignedShort();
    1.63 +        // Read the attributes
    1.64 +        int attributes_count = in.readUnsignedShort();
    1.65 +        attrs=new Vector(attributes_count);
    1.66 +        for (int i = 0; i < attributes_count; i++) {
    1.67 +            int attr_name_index=in.readUnsignedShort();
    1.68 +            if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
    1.69 +            String attr_name=cls.getString(attr_name_index);
    1.70 +            if (attr_name.equals("ConstantValue")){
    1.71 +                if (in.readInt()!=2)
    1.72 +                    throw new ClassFormatError("invalid ConstantValue attr length");
    1.73 +                value_cpx=in.readUnsignedShort();
    1.74 +                AttrData attr=new AttrData(cls);
    1.75 +                attr.read(attr_name_index);
    1.76 +                attrs.addElement(attr);
    1.77 +            } else if (attr_name.equals("Synthetic")){
    1.78 +                if (in.readInt()!=0)
    1.79 +                    throw new ClassFormatError("invalid Synthetic attr length");
    1.80 +                isSynthetic=true;
    1.81 +                AttrData attr=new AttrData(cls);
    1.82 +                attr.read(attr_name_index);
    1.83 +                attrs.addElement(attr);
    1.84 +            } else if (attr_name.equals("Deprecated")){
    1.85 +                if (in.readInt()!=0)
    1.86 +                    throw new ClassFormatError("invalid Synthetic attr length");
    1.87 +                isDeprecated = true;
    1.88 +                AttrData attr=new AttrData(cls);
    1.89 +                attr.read(attr_name_index);
    1.90 +                attrs.addElement(attr);
    1.91 +            } else {
    1.92 +                AttrData attr=new AttrData(cls);
    1.93 +                attr.read(attr_name_index, in);
    1.94 +                attrs.addElement(attr);
    1.95 +            }
    1.96 +        }
    1.97 +
    1.98 +    }  // end read
    1.99 +
   1.100 +    public boolean isStatic() {
   1.101 +        return (access & ACC_STATIC) != 0;
   1.102 +    }
   1.103 +    
   1.104 +    /**
   1.105 +     * Returns access of a field.
   1.106 +     */
   1.107 +    public String[] getAccess(){
   1.108 +        Vector v = new Vector();
   1.109 +        if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
   1.110 +        if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
   1.111 +        if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
   1.112 +        if ((access & ACC_STATIC)   !=0) v.addElement("static");
   1.113 +        if ((access & ACC_FINAL)    !=0) v.addElement("final");
   1.114 +        if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
   1.115 +        if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
   1.116 +        String[] accflags = new String[v.size()];
   1.117 +        v.copyInto(accflags);
   1.118 +        return accflags;
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Returns name of a field.
   1.123 +     */
   1.124 +    public String getName(){
   1.125 +        return cls.getStringValue(name_index);
   1.126 +    }
   1.127 +
   1.128 +    /**
   1.129 +     * Returns internal signature of a field
   1.130 +     */
   1.131 +    public String getInternalSig(){
   1.132 +        return cls.getStringValue(descriptor_index);
   1.133 +    }
   1.134 +
   1.135 +    /**
   1.136 +     * Returns true if field is synthetic.
   1.137 +     */
   1.138 +    public boolean isSynthetic(){
   1.139 +        return isSynthetic;
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Returns true if field is deprecated.
   1.144 +     */
   1.145 +    public boolean isDeprecated(){
   1.146 +        return isDeprecated;
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Returns index of constant value in cpool.
   1.151 +     */
   1.152 +    public int getConstantValueIndex(){
   1.153 +        return (value_cpx);
   1.154 +    }
   1.155 +
   1.156 +    /**
   1.157 +     * Returns list of attributes of field.
   1.158 +     */
   1.159 +    public Vector getAttributes(){
   1.160 +        return attrs;
   1.161 +    }
   1.162 +
   1.163 +    public byte[] findAnnotationData(boolean classRetention) {
   1.164 +        String n = classRetention ?
   1.165 +            "RuntimeInvisibleAnnotations" : // NOI18N
   1.166 +            "RuntimeVisibleAnnotations"; // NOI18N
   1.167 +        AttrData[] arr = new AttrData[attrs.size()];
   1.168 +        attrs.copyInto(arr);
   1.169 +        return ClassData.findAttr(n, arr);
   1.170 +    }
   1.171 +}