001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package components;
028:
029: import java.io.DataOutput;
030: import java.io.DataInput;
031: import java.io.IOException;
032: import java.util.Hashtable;
033: import util.DataFormatException;
034: import jcc.Const;
035:
036: /*
037: * A Field of a class. Includes statics, static finals, and
038: * of course instance fields.
039: * In Java, we don't have enumerations, but we do have
040: * static final fields, and these have a value attribute.
041: */
042: public class FieldInfo extends ClassMemberInfo {
043:
044: private Attribute fieldAttributes[];
045: public ConstantObject value;
046: private ConstantValueAttribute valueAttribute;
047: public int instanceOffset = -1; // for instance fields only, of course
048: public int nSlots = -1; // ditto
049:
050: public FieldInfo(int name, int sig, int access, ClassInfo p) {
051: super (name, sig, access, p);
052: }
053:
054: /*
055: * The only field attribute we care about is
056: * the ConstantValue attribute
057: */
058: private static Hashtable fieldAttributeTypes = new Hashtable();
059: static {
060: fieldAttributeTypes.put("ConstantValue",
061: ConstantValueAttributeFactory.instance);
062: }
063:
064: // Read attributes from classfile
065: private void readAttributes(DataInput in) throws IOException {
066: fieldAttributes = Attribute.readAttributes(in,
067: parent.constants, parent.symbols, fieldAttributeTypes,
068: false);
069: }
070:
071: // Read field from classfile
072: public static FieldInfo readField(DataInput in, ClassInfo p)
073: throws IOException {
074: int acc = in.readUnsignedShort();
075: int name = in.readUnsignedShort();
076: int sig = in.readUnsignedShort();
077: FieldInfo fi = new FieldInfo(name, sig, acc, p);
078: fi.readAttributes(in);
079: fi.resolve(p.symbols);
080: return fi;
081: }
082:
083: public void resolve(ConstantObject table[]) {
084: if (resolved)
085: return;
086: super .resolve(table);
087: /*
088: * Parse attributes.
089: * If we find a value attribute, pick it out
090: * for special handling.
091: */
092: if (fieldAttributes != null) {
093: for (int i = 0; i < fieldAttributes.length; i++) {
094: Attribute a = fieldAttributes[i];
095: if (a.name.string.equals("ConstantValue")) {
096: valueAttribute = (ConstantValueAttribute) a;
097: value = valueAttribute.data;
098: }
099: }
100: }
101: switch (type.string.charAt(0)) {
102: case 'D':
103: case 'J':
104: nSlots = 2;
105: access |= Const.ACC_DOUBLEWORD;
106: break;
107: case 'L':
108: case '[':
109: nSlots = 1;
110: access |= Const.ACC_REFERENCE;
111: break;
112: default:
113: nSlots = 1;
114: break;
115: }
116: resolved = true;
117: }
118:
119: public void externalize(ConstantPool p) {
120: super .externalize(p);
121: Attribute.externalizeAttributes(fieldAttributes, p);
122: if (value != null) {
123: value = valueAttribute.data;
124: }
125: }
126:
127: public void countConstantReferences(boolean isRelocatable) {
128: super .countConstantReferences();
129: Attribute.countConstantReferences(fieldAttributes,
130: isRelocatable);
131: }
132:
133: public void write(DataOutput o) throws IOException {
134: o.writeShort(access);
135: if (resolved) {
136: o.writeShort(name.index);
137: o.writeShort(type.index);
138: Attribute.writeAttributes(fieldAttributes, o, false);
139: } else {
140: o.writeShort(nameIndex);
141: o.writeShort(typeIndex);
142: o.writeShort(0); // no attribute!
143: }
144: }
145:
146: public String toString() {
147: String r = "Field: " + super .toString();
148: if (value != null) {
149: r += " Value: " + value.toString();
150: }
151: return r;
152: }
153:
154: }
|