rt/javap/src/main/java/org/apidesign/javap/FieldData.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 249 javap/src/main/java/org/apidesign/javap/FieldData.java@001389026dbf
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 org.apidesign.javap;
    28 
    29 import java.io.*;
    30 
    31 /**
    32  * Strores field data informastion.
    33  *
    34  * @author  Sucheta Dambalkar (Adopted code from jdis)
    35  */
    36 
    37 public class FieldData implements RuntimeConstants  {
    38 
    39     ClassData cls;
    40     int access;
    41     int name_index;
    42     int descriptor_index;
    43     int attributes_count;
    44     int value_cpx=0;
    45     boolean isSynthetic=false;
    46     boolean isDeprecated=false;
    47     Vector attrs;
    48 
    49     public FieldData(ClassData cls){
    50         this.cls=cls;
    51     }
    52 
    53     /**
    54      * Read and store field info.
    55      */
    56     public void read(DataInputStream in) throws IOException {
    57         access = in.readUnsignedShort();
    58         name_index = in.readUnsignedShort();
    59         descriptor_index = in.readUnsignedShort();
    60         // Read the attributes
    61         int attributes_count = in.readUnsignedShort();
    62         attrs=new Vector(attributes_count);
    63         for (int i = 0; i < attributes_count; i++) {
    64             int attr_name_index=in.readUnsignedShort();
    65             if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
    66             String attr_name=cls.getString(attr_name_index);
    67             if (attr_name.equals("ConstantValue")){
    68                 if (in.readInt()!=2)
    69                     throw new ClassFormatError("invalid ConstantValue attr length");
    70                 value_cpx=in.readUnsignedShort();
    71                 AttrData attr=new AttrData(cls);
    72                 attr.read(attr_name_index);
    73                 attrs.addElement(attr);
    74             } else if (attr_name.equals("Synthetic")){
    75                 if (in.readInt()!=0)
    76                     throw new ClassFormatError("invalid Synthetic attr length");
    77                 isSynthetic=true;
    78                 AttrData attr=new AttrData(cls);
    79                 attr.read(attr_name_index);
    80                 attrs.addElement(attr);
    81             } else if (attr_name.equals("Deprecated")){
    82                 if (in.readInt()!=0)
    83                     throw new ClassFormatError("invalid Synthetic attr length");
    84                 isDeprecated = true;
    85                 AttrData attr=new AttrData(cls);
    86                 attr.read(attr_name_index);
    87                 attrs.addElement(attr);
    88             } else {
    89                 AttrData attr=new AttrData(cls);
    90                 attr.read(attr_name_index, in);
    91                 attrs.addElement(attr);
    92             }
    93         }
    94 
    95     }  // end read
    96 
    97     public boolean isStatic() {
    98         return (access & ACC_STATIC) != 0;
    99     }
   100     
   101     /**
   102      * Returns access of a field.
   103      */
   104     public String[] getAccess(){
   105         Vector v = new Vector();
   106         if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
   107         if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
   108         if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
   109         if ((access & ACC_STATIC)   !=0) v.addElement("static");
   110         if ((access & ACC_FINAL)    !=0) v.addElement("final");
   111         if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
   112         if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
   113         String[] accflags = new String[v.size()];
   114         v.copyInto(accflags);
   115         return accflags;
   116     }
   117 
   118     /**
   119      * Returns name of a field.
   120      */
   121     public String getName(){
   122         return cls.getStringValue(name_index);
   123     }
   124 
   125     /**
   126      * Returns internal signature of a field
   127      */
   128     public String getInternalSig(){
   129         return cls.getStringValue(descriptor_index);
   130     }
   131 
   132     /**
   133      * Returns true if field is synthetic.
   134      */
   135     public boolean isSynthetic(){
   136         return isSynthetic;
   137     }
   138 
   139     /**
   140      * Returns true if field is deprecated.
   141      */
   142     public boolean isDeprecated(){
   143         return isDeprecated;
   144     }
   145 
   146     /**
   147      * Returns index of constant value in cpool.
   148      */
   149     public int getConstantValueIndex(){
   150         return (value_cpx);
   151     }
   152 
   153     /**
   154      * Returns list of attributes of field.
   155      */
   156     public Vector getAttributes(){
   157         return attrs;
   158     }
   159 
   160     public byte[] findAnnotationData(boolean classRetention) {
   161         String n = classRetention ?
   162             "RuntimeInvisibleAnnotations" : // NOI18N
   163             "RuntimeVisibleAnnotations"; // NOI18N
   164         AttrData[] arr = new AttrData[attrs.size()];
   165         attrs.copyInto(arr);
   166         return ClassData.findAttr(n, arr);
   167     }
   168 }