1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javap/src/main/java/org/apidesign/javap/FieldData.java Fri Nov 16 08:08:36 2012 +0100
1.3 @@ -0,0 +1,167 @@
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.util.*;
1.33 +import java.io.*;
1.34 +
1.35 +/**
1.36 + * Strores field data informastion.
1.37 + *
1.38 + * @author Sucheta Dambalkar (Adopted code from jdis)
1.39 + */
1.40 +
1.41 +public class FieldData implements RuntimeConstants {
1.42 +
1.43 + ClassData cls;
1.44 + int access;
1.45 + int name_index;
1.46 + int descriptor_index;
1.47 + int attributes_count;
1.48 + int value_cpx=0;
1.49 + boolean isSynthetic=false;
1.50 + boolean isDeprecated=false;
1.51 + Vector attrs;
1.52 +
1.53 + public FieldData(ClassData cls){
1.54 + this.cls=cls;
1.55 + }
1.56 +
1.57 + /**
1.58 + * Read and store field info.
1.59 + */
1.60 + public void read(DataInputStream in) throws IOException {
1.61 + access = in.readUnsignedShort();
1.62 + name_index = in.readUnsignedShort();
1.63 + descriptor_index = in.readUnsignedShort();
1.64 + // Read the attributes
1.65 + int attributes_count = in.readUnsignedShort();
1.66 + attrs=new Vector(attributes_count);
1.67 + for (int i = 0; i < attributes_count; i++) {
1.68 + int attr_name_index=in.readUnsignedShort();
1.69 + if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue;
1.70 + String attr_name=cls.getString(attr_name_index);
1.71 + if (attr_name.equals("ConstantValue")){
1.72 + if (in.readInt()!=2)
1.73 + throw new ClassFormatError("invalid ConstantValue attr length");
1.74 + value_cpx=in.readUnsignedShort();
1.75 + AttrData attr=new AttrData(cls);
1.76 + attr.read(attr_name_index);
1.77 + attrs.addElement(attr);
1.78 + } else if (attr_name.equals("Synthetic")){
1.79 + if (in.readInt()!=0)
1.80 + throw new ClassFormatError("invalid Synthetic attr length");
1.81 + isSynthetic=true;
1.82 + AttrData attr=new AttrData(cls);
1.83 + attr.read(attr_name_index);
1.84 + attrs.addElement(attr);
1.85 + } else if (attr_name.equals("Deprecated")){
1.86 + if (in.readInt()!=0)
1.87 + throw new ClassFormatError("invalid Synthetic attr length");
1.88 + isDeprecated = true;
1.89 + AttrData attr=new AttrData(cls);
1.90 + attr.read(attr_name_index);
1.91 + attrs.addElement(attr);
1.92 + } else {
1.93 + AttrData attr=new AttrData(cls);
1.94 + attr.read(attr_name_index, in);
1.95 + attrs.addElement(attr);
1.96 + }
1.97 + }
1.98 +
1.99 + } // end read
1.100 +
1.101 + public boolean isStatic() {
1.102 + return (access & ACC_STATIC) != 0;
1.103 + }
1.104 +
1.105 + /**
1.106 + * Returns access of a field.
1.107 + */
1.108 + public String[] getAccess(){
1.109 + Vector v = new Vector();
1.110 + if ((access & ACC_PUBLIC) !=0) v.addElement("public");
1.111 + if ((access & ACC_PRIVATE) !=0) v.addElement("private");
1.112 + if ((access & ACC_PROTECTED) !=0) v.addElement("protected");
1.113 + if ((access & ACC_STATIC) !=0) v.addElement("static");
1.114 + if ((access & ACC_FINAL) !=0) v.addElement("final");
1.115 + if ((access & ACC_VOLATILE) !=0) v.addElement("volatile");
1.116 + if ((access & ACC_TRANSIENT) !=0) v.addElement("transient");
1.117 + String[] accflags = new String[v.size()];
1.118 + v.copyInto(accflags);
1.119 + return accflags;
1.120 + }
1.121 +
1.122 + /**
1.123 + * Returns name of a field.
1.124 + */
1.125 + public String getName(){
1.126 + return cls.getStringValue(name_index);
1.127 + }
1.128 +
1.129 + /**
1.130 + * Returns internal signature of a field
1.131 + */
1.132 + public String getInternalSig(){
1.133 + return cls.getStringValue(descriptor_index);
1.134 + }
1.135 +
1.136 + /**
1.137 + * Returns java type signature of a field.
1.138 + */
1.139 + public String getType(){
1.140 + return new TypeSignature(getInternalSig()).getFieldType();
1.141 + }
1.142 +
1.143 + /**
1.144 + * Returns true if field is synthetic.
1.145 + */
1.146 + public boolean isSynthetic(){
1.147 + return isSynthetic;
1.148 + }
1.149 +
1.150 + /**
1.151 + * Returns true if field is deprecated.
1.152 + */
1.153 + public boolean isDeprecated(){
1.154 + return isDeprecated;
1.155 + }
1.156 +
1.157 + /**
1.158 + * Returns index of constant value in cpool.
1.159 + */
1.160 + public int getConstantValueIndex(){
1.161 + return (value_cpx);
1.162 + }
1.163 +
1.164 + /**
1.165 + * Returns list of attributes of field.
1.166 + */
1.167 + public Vector getAttributes(){
1.168 + return attrs;
1.169 + }
1.170 +}