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  * Portions Copyright (c) 2011 Jonas Maebe
27  */
28 
29 
30 package fpc.tools.javapp;
31 
32 import java.util.*;
33 import java.io.*;
34 
35 /**
36  * Strores field data informastion.
37  *
38  * @author  Sucheta Dambalkar (Adopted code from jdis)
39  */
40 
41 public class FieldData implements RuntimeConstants  {
42 
43     ClassData cls;
44     int access;
45     int name_index;
46     int descriptor_index;
47     int attributes_count;
48     int value_cpx=0;
49     boolean isSynthetic=false;
50     boolean isDeprecated=false;
51     Vector attrs;
52     protected JavapEnvironment env;
53 
FieldData(JavapEnvironment env, ClassData cls)54     public FieldData(JavapEnvironment env, ClassData cls){
55         this.env=env;
56         this.cls=cls;
57     }
58 
59     /**
60      * Read and store field info.
61      */
read(DataInputStream in)62     public void read(DataInputStream in) throws IOException {
63         access = in.readUnsignedShort();
64         name_index = in.readUnsignedShort();
65         descriptor_index = in.readUnsignedShort();
66         // Read the attributes
67         int attributes_count = in.readUnsignedShort();
68         attrs=new Vector(attributes_count);
69         for (int i = 0; i < attributes_count; i++) {
70             int attr_name_index=in.readUnsignedShort();
71             if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
72             String attr_name=cls.getString(attr_name_index);
73             if (attr_name.equals("ConstantValue")){
74                 if (in.readInt()!=2)
75                     throw new ClassFormatError("invalid ConstantValue attr length");
76                 value_cpx=in.readUnsignedShort();
77                 AttrData attr=new AttrData(cls);
78                 attr.read(attr_name_index);
79                 attrs.addElement(attr);
80             } else if (attr_name.equals("Synthetic")){
81                 if (in.readInt()!=0)
82                     throw new ClassFormatError("invalid Synthetic attr length");
83                 isSynthetic=true;
84                 AttrData attr=new AttrData(cls);
85                 attr.read(attr_name_index);
86                 attrs.addElement(attr);
87             } else if (attr_name.equals("Deprecated")){
88                 if (in.readInt()!=0)
89                     throw new ClassFormatError("invalid Synthetic attr length");
90                 isDeprecated = true;
91                 AttrData attr=new AttrData(cls);
92                 attr.read(attr_name_index);
93                 attrs.addElement(attr);
94             } else {
95                 AttrData attr=new AttrData(cls);
96                 attr.read(attr_name_index, in);
97                 attrs.addElement(attr);
98             }
99         }
100 
101     }  // end read
102 
103     /**
104      * Returns access of a field.
105      */
getAccess()106     public String[] getAccess(){
107         Vector v = new Vector();
108         if ((access & ACC_PUBLIC)   !=0) v.addElement("public");
109         if ((access & ACC_PRIVATE)   !=0) v.addElement("private");
110         if ((access & ACC_PROTECTED)   !=0) v.addElement("protected");
111         if ((access & ACC_STATIC)   !=0) v.addElement("static");
112         if ((access & ACC_FINAL)    !=0) v.addElement("final");
113         if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
114         if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
115         String[] accflags = new String[v.size()];
116         v.copyInto(accflags);
117         return accflags;
118     }
119 
120     /**
121      * Returns name of a field.
122      */
getName()123     public String getName(){
124         return cls.getStringValue(name_index);
125     }
126 
127     /**
128      * Returns internal signature of a field
129      */
getInternalSig()130     public String getInternalSig(){
131         return cls.getStringValue(descriptor_index);
132     }
133 
134     /**
135      * Returns java type signature of a field.
136      */
getType()137     public String getType(){
138         return new TypeSignature(getInternalSig()).getFieldType();
139     }
140 
141     /**
142      * Returns true if field is synthetic.
143      */
isSynthetic()144     public boolean isSynthetic(){
145         return isSynthetic;
146     }
147 
148     /**
149      * Returns true if field is deprecated.
150      */
isDeprecated()151     public boolean isDeprecated(){
152         return isDeprecated;
153     }
154 
155     /**
156      * Returns index of constant value in cpool.
157      */
getConstantValueIndex()158     public int getConstantValueIndex(){
159         return (value_cpx);
160     }
161 
162     /**
163      * Returns list of attributes of field.
164      */
getAttributes()165     public Vector getAttributes(){
166         return attrs;
167     }
168 }
169