jtulach@144: /* jtulach@144: * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. jtulach@144: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@144: * jtulach@144: * This code is free software; you can redistribute it and/or modify it jtulach@144: * under the terms of the GNU General Public License version 2 only, as jtulach@144: * published by the Free Software Foundation. Oracle designates this jtulach@144: * particular file as subject to the "Classpath" exception as provided jtulach@144: * by Oracle in the LICENSE file that accompanied this code. jtulach@144: * jtulach@144: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@144: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@144: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@144: * version 2 for more details (a copy is included in the LICENSE file that jtulach@144: * accompanied this code). jtulach@144: * jtulach@144: * You should have received a copy of the GNU General Public License version jtulach@144: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@144: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@144: * jtulach@144: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@144: * or visit www.oracle.com if you need additional information or have any jtulach@144: * questions. jtulach@144: */ jtulach@144: jtulach@144: jtulach@167: package org.apidesign.javap; jtulach@144: jtulach@144: import java.io.*; jtulach@144: jtulach@144: /** jtulach@144: * Strores field data informastion. jtulach@144: * jtulach@144: * @author Sucheta Dambalkar (Adopted code from jdis) jtulach@144: */ jtulach@144: jtulach@144: public class FieldData implements RuntimeConstants { jtulach@144: jtulach@144: ClassData cls; jtulach@144: int access; jtulach@144: int name_index; jtulach@144: int descriptor_index; jtulach@144: int attributes_count; jtulach@144: int value_cpx=0; jtulach@144: boolean isSynthetic=false; jtulach@144: boolean isDeprecated=false; jtulach@144: Vector attrs; jtulach@144: jtulach@144: public FieldData(ClassData cls){ jtulach@144: this.cls=cls; jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Read and store field info. jtulach@144: */ jtulach@144: public void read(DataInputStream in) throws IOException { jtulach@144: access = in.readUnsignedShort(); jtulach@144: name_index = in.readUnsignedShort(); jtulach@144: descriptor_index = in.readUnsignedShort(); jtulach@144: // Read the attributes jtulach@144: int attributes_count = in.readUnsignedShort(); jtulach@144: attrs=new Vector(attributes_count); jtulach@144: for (int i = 0; i < attributes_count; i++) { jtulach@144: int attr_name_index=in.readUnsignedShort(); jtulach@144: if (cls.getTag(attr_name_index)!=CONSTANT_UTF8) continue; jtulach@144: String attr_name=cls.getString(attr_name_index); jtulach@144: if (attr_name.equals("ConstantValue")){ jtulach@144: if (in.readInt()!=2) jtulach@144: throw new ClassFormatError("invalid ConstantValue attr length"); jtulach@144: value_cpx=in.readUnsignedShort(); jtulach@144: AttrData attr=new AttrData(cls); jtulach@144: attr.read(attr_name_index); jtulach@144: attrs.addElement(attr); jtulach@144: } else if (attr_name.equals("Synthetic")){ jtulach@144: if (in.readInt()!=0) jtulach@144: throw new ClassFormatError("invalid Synthetic attr length"); jtulach@144: isSynthetic=true; jtulach@144: AttrData attr=new AttrData(cls); jtulach@144: attr.read(attr_name_index); jtulach@144: attrs.addElement(attr); jtulach@144: } else if (attr_name.equals("Deprecated")){ jtulach@144: if (in.readInt()!=0) jtulach@144: throw new ClassFormatError("invalid Synthetic attr length"); jtulach@144: isDeprecated = true; jtulach@144: AttrData attr=new AttrData(cls); jtulach@144: attr.read(attr_name_index); jtulach@144: attrs.addElement(attr); jtulach@144: } else { jtulach@144: AttrData attr=new AttrData(cls); jtulach@144: attr.read(attr_name_index, in); jtulach@144: attrs.addElement(attr); jtulach@144: } jtulach@144: } jtulach@144: jtulach@144: } // end read jtulach@144: jaroslav@151: public boolean isStatic() { jaroslav@151: return (access & ACC_STATIC) != 0; jaroslav@151: } jaroslav@151: jtulach@144: /** jtulach@144: * Returns access of a field. jtulach@144: */ jtulach@144: public String[] getAccess(){ jtulach@144: Vector v = new Vector(); jtulach@144: if ((access & ACC_PUBLIC) !=0) v.addElement("public"); jtulach@144: if ((access & ACC_PRIVATE) !=0) v.addElement("private"); jtulach@144: if ((access & ACC_PROTECTED) !=0) v.addElement("protected"); jtulach@144: if ((access & ACC_STATIC) !=0) v.addElement("static"); jtulach@144: if ((access & ACC_FINAL) !=0) v.addElement("final"); jtulach@144: if ((access & ACC_VOLATILE) !=0) v.addElement("volatile"); jtulach@144: if ((access & ACC_TRANSIENT) !=0) v.addElement("transient"); jtulach@144: String[] accflags = new String[v.size()]; jtulach@144: v.copyInto(accflags); jtulach@144: return accflags; jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns name of a field. jtulach@144: */ jtulach@144: public String getName(){ jtulach@144: return cls.getStringValue(name_index); jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns internal signature of a field jtulach@144: */ jtulach@144: public String getInternalSig(){ jtulach@144: return cls.getStringValue(descriptor_index); jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns true if field is synthetic. jtulach@144: */ jtulach@144: public boolean isSynthetic(){ jtulach@144: return isSynthetic; jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns true if field is deprecated. jtulach@144: */ jtulach@144: public boolean isDeprecated(){ jtulach@144: return isDeprecated; jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns index of constant value in cpool. jtulach@144: */ jtulach@144: public int getConstantValueIndex(){ jtulach@144: return (value_cpx); jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Returns list of attributes of field. jtulach@144: */ jtulach@144: public Vector getAttributes(){ jtulach@144: return attrs; jtulach@144: } jaroslav@240: jaroslav@240: public byte[] findAnnotationData(boolean classRetention) { jaroslav@240: String n = classRetention ? jaroslav@240: "RuntimeInvisibleAnnotations" : // NOI18N jaroslav@240: "RuntimeVisibleAnnotations"; // NOI18N jaroslav@240: AttrData[] arr = new AttrData[attrs.size()]; jaroslav@240: attrs.copyInto(arr); jaroslav@240: return ClassData.findAttr(n, arr); jaroslav@240: } jtulach@144: }