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.*;
034:
035: /*
036: * A class to represent the general Attribute form.
037: * In its most uninterpreted form, "data" is just an
038: * array of byte. It can also be other things, even
039: * array of ConstantObject.
040: */
041:
042: public abstract class Attribute extends ClassComponent {
043: public UnicodeConstant name;
044: public int length;
045:
046: protected Attribute(UnicodeConstant n, int l) {
047: name = n;
048: length = l;
049: resolved = true;
050: }
051:
052: public void externalize(ConstantPool p) {
053: if (name != null) {
054: name = (UnicodeConstant) p.add(name);
055: }
056: }
057:
058: abstract protected int writeData(DataOutput o) throws IOException;
059:
060: public void write(DataOutput o) throws IOException {
061: o.writeShort(name.index);
062: o.writeInt(length);
063: int trueLength = writeData(o);
064: if (length != trueLength) {
065: throw new DataFormatException(
066: Localizer
067: .getString(
068: "attribute.bad_attribute_length_for.dataformatexception",
069: name));
070: }
071: }
072:
073: public void countConstantReferences(boolean isRelocatable) {
074: //
075: // if we are producing relocatable output, then
076: // we will need our name in the string table.
077: // Else not.
078: //
079: if (isRelocatable)
080: name.incReference();
081: }
082:
083: /*
084: * Number of bytes we'll write out
085: */
086: public int length() {
087: return 6 + length; // believe length unchanged
088: }
089:
090: public static void externalizeAttributes(Attribute a[],
091: ConstantPool p) {
092: int arryl = (a == null) ? 0 : a.length;
093: for (int i = 0; i < arryl; i++) {
094: a[i].externalize(p);
095: }
096: }
097:
098: public static void writeAttributes(Attribute a[], DataOutput o,
099: boolean verbose) throws IOException {
100: int nattr = (a == null) ? 0 : a.length;
101: if (verbose) {
102: System.out.println(Localizer.getString(
103: "attribute.writing_attributes", Integer
104: .toString(nattr)));
105: }
106: o.writeShort(nattr);
107: for (int j = 0; j < nattr; j++) {
108: if (verbose) {
109: System.out.println(" " + a[j].name.string);
110: }
111: a[j].write(o);
112: }
113: }
114:
115: public static int length(Attribute a[]) {
116: int l = 0;
117: int arryl = (a == null) ? 0 : a.length;
118: for (int i = 0; i < arryl; i++) {
119: l += a[i].length();
120: }
121: return l + 2;
122: }
123:
124: public static Attribute[] readAttributes(DataInput i,
125: ConstantObject locals[], ConstantObject globals[],
126: Hashtable typetable, boolean verbose) throws IOException {
127: int nattr = i.readUnsignedShort();
128: if (verbose) {
129: System.out.println(Localizer.getString(
130: "attribute.reading_attributes", Integer
131: .toString(nattr)));
132: }
133: if (nattr == 0)
134: return null;
135: Attribute a[] = new Attribute[nattr];
136: for (int j = 0; j < nattr; j++) {
137: UnicodeConstant name = (UnicodeConstant) globals[i
138: .readUnsignedShort()];
139: String typename = name.string.intern();
140: AttributeFactory afact = (AttributeFactory) typetable
141: .get(typename);
142: if (afact == null)
143: afact = UninterpretedAttributeFactory.instance;
144:
145: a[j] = afact.finishReadAttribute(i, name, locals, globals);
146: }
147: return a;
148: }
149:
150: public static void countConstantReferences(Attribute a[],
151: boolean isRelocatable) {
152: if (a == null)
153: return;
154: for (int i = 0; i < a.length; i++) {
155: a[i].countConstantReferences(isRelocatable);
156: }
157: }
158:
159: }
|