001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.file;
022:
023: import java.io.*;
024:
025: import EDU.purdue.cs.bloat.reflect.*;
026:
027: /**
028: * LocalVariableTable represents debugging information that may be used by a
029: * debugger to determine the value of a given local variable during program
030: * execution. It is essentially an array of <tt>reflect.LocalDebugInfo</tt>.
031: *
032: * @see EDU.purdue.cs.bloat.reflect.LocalDebugInfo
033: *
034: * @author Nate Nystrom (<a
035: * href="mailto:nystrom@cs.purdue.edu">nystrom@cs.purdue.edu</a>)
036: */
037: public class LocalVariableTable extends Attribute {
038: private LocalDebugInfo[] locals;
039:
040: /**
041: * Constructor. Create an attribute from a data stream.
042: *
043: * @param in
044: * The data stream of the class file.
045: * @param index
046: * The index into the constant pool of the name of the attribute.
047: * @param len
048: * The length of the attribute, excluding the header.
049: * @exception IOException
050: * If an error occurs while reading.
051: */
052: public LocalVariableTable(final DataInputStream in,
053: final int index, final int len) throws IOException {
054: super (index, len);
055:
056: final int numLocals = in.readUnsignedShort();
057:
058: locals = new LocalDebugInfo[numLocals];
059:
060: for (int i = 0; i < locals.length; i++) {
061: final int startPC = in.readUnsignedShort();
062: final int length = in.readUnsignedShort();
063: final int nameIndex = in.readUnsignedShort();
064: final int typeIndex = in.readUnsignedShort();
065: final int varIndex = in.readUnsignedShort();
066: locals[i] = new LocalDebugInfo(startPC, length, nameIndex,
067: typeIndex, varIndex);
068: }
069: }
070:
071: /**
072: * Get the local variable debug info for the code.
073: *
074: * @return The local variable debug info for the code.
075: */
076: public LocalDebugInfo[] locals() {
077: return locals;
078: }
079:
080: /**
081: * Set the local variable debug info for the code.
082: *
083: * @param locals
084: * The local variable debug info for the code.
085: */
086: public void setLocals(final LocalDebugInfo[] locals) {
087: this .locals = locals;
088: }
089:
090: /**
091: * Get the length of the attribute.
092: *
093: * @return The length of the attribute.
094: */
095: public int length() {
096: return 2 + locals.length * 10;
097: }
098:
099: public String toString() {
100: String x = "(locals";
101:
102: for (int i = 0; i < locals.length; i++) {
103: x += "\n (local @" + locals[i].index() + " name="
104: + locals[i].nameIndex() + " type="
105: + locals[i].typeIndex() + " pc="
106: + locals[i].startPC() + ".."
107: + (locals[i].startPC() + locals[i].length()) + ")";
108: }
109:
110: return x + ")";
111: }
112:
113: /**
114: * Write the attribute to a data stream.
115: *
116: * @param out
117: * The data stream of the class file.
118: * @exception IOException
119: * If an error occurs while writing.
120: */
121: public void writeData(final DataOutputStream out)
122: throws IOException {
123: out.writeShort(locals.length);
124:
125: for (int i = 0; i < locals.length; i++) {
126: out.writeShort(locals[i].startPC());
127: out.writeShort(locals[i].length());
128: out.writeShort(locals[i].nameIndex());
129: out.writeShort(locals[i].typeIndex());
130: out.writeShort(locals[i].index());
131: }
132: }
133:
134: /**
135: * Private constructor used in cloning.
136: */
137: private LocalVariableTable(final LocalVariableTable other) {
138: super (other.nameIndex, other.length);
139:
140: this .locals = new LocalDebugInfo[other.locals.length];
141: for (int i = 0; i < other.locals.length; i++) {
142: this .locals[i] = (LocalDebugInfo) other.locals[i].clone();
143: }
144: }
145:
146: public Object clone() {
147: return (new LocalVariableTable(this));
148: }
149: }
|