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 util.DataFormatException;
033:
034: /*
035: * A class to represent the LocalVariable table Attribute
036: * of a method's code.
037: */
038:
039: public class LocalVariableTableAttribute extends Attribute {
040: public LocalVariableTableEntry data[];
041:
042: public LocalVariableTableAttribute(UnicodeConstant name, int l,
043: LocalVariableTableEntry d[]) {
044: super (name, l);
045: this .data = d;
046: }
047:
048: public void externalize(ConstantPool p) {
049: super .externalize(p);
050: for (int i = 0; i < data.length; i++) {
051: if (data[i].name != null) {
052: data[i].name = (UnicodeConstant) p.add(data[i].name);
053: }
054: if (data[i].sig != null) {
055: data[i].sig = (UnicodeConstant) p.add(data[i].sig);
056: }
057: }
058: }
059:
060: protected int writeData(DataOutput o) throws IOException {
061: int n = data.length;
062: o.writeShort(n);
063: for (int i = 0; i < n; i++) {
064: data[i].write(o);
065: }
066: return 2 + LocalVariableTableEntry.size * n;
067: }
068:
069: public void countConstantReferences(boolean isRelocatable) {
070: super .countConstantReferences(isRelocatable);
071: for (int i = 0; i < data.length; i++) {
072: data[i].name.incReference();
073: data[i].sig.incReference();
074: }
075: }
076:
077: public static Attribute readAttribute(DataInput i,
078: ConstantObject globals[]) throws IOException {
079: UnicodeConstant name;
080:
081: name = (UnicodeConstant) globals[i.readUnsignedShort()];
082: return finishReadAttribute(i, name, globals);
083: }
084:
085: //
086: // for those cases where we already read the name index
087: // and know that its not something requiring special handling.
088: //
089: public static Attribute finishReadAttribute(DataInput in,
090: UnicodeConstant name, ConstantObject globals[])
091: throws IOException {
092: int l;
093: int n;
094: LocalVariableTableEntry d[];
095:
096: l = in.readInt();
097: n = in.readUnsignedShort();
098: d = new LocalVariableTableEntry[n];
099: for (int i = 0; i < n; i++) {
100: d[i] = new LocalVariableTableEntry(in.readUnsignedShort(),
101: in.readUnsignedShort(),
102: (UnicodeConstant) globals[in.readUnsignedShort()],
103: (UnicodeConstant) globals[in.readUnsignedShort()],
104: in.readUnsignedShort());
105: }
106: return new LocalVariableTableAttribute(name, l, d);
107: }
108: }
|