KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > classfile > LocalVariableInfo


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at theit@gmx.de.
22  *
23  *
24  * $Id: LocalVariableInfo.java,v 1.2 2002/05/08 11:53:42 glurk Exp $
25  */

26 package net.sf.javaguard.classfile;
27
28 import java.io.*;
29
30
31 /** Representation of a local variable table entry.
32  *
33  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
34  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
35  */

36 public class LocalVariableInfo {
37   /** Start of the Program Counter (PC). */
38   private int startPc;
39   /** The length. */
40   private int length;
41   /** Name index into the constant pool. */
42   private int nameIndex;
43   /** Descriptor index into the constant pool. */
44   private int descriptorIndex;
45   /** Index into the constant pool. */
46   private int index;
47   
48   
49   
50   
51   /** Creates a new LocalVariableInfo object from the given input stream.
52    * @param din the input stream
53    * @return the LocalVariableInfo object created from the input stream
54    * @throws IOException if an I/O error occurs
55    */

56   public static LocalVariableInfo create(DataInput din)
57   throws IOException {
58     LocalVariableInfo lvi = new LocalVariableInfo();
59     lvi.read(din);
60     return lvi;
61   }
62   
63   
64   
65   
66   /** Private constructor that creates a new LocalVariableInfo object.
67    */

68   private LocalVariableInfo() {
69   }
70   
71   
72   
73   /** Sets the address of the program counter.
74    * @param pc the address of the program counter
75    * @see #getStartPc
76    */

77   protected void setStartPc(int pc) {
78     this.startPc = pc;
79   }
80   
81   
82   /** Returns the address of the program counter.
83    * @return address of the program counter
84    * @see #setStartPc
85    */

86   protected int getStartPc() {
87     return startPc;
88   }
89   
90   
91   
92   
93   /** Sets the length.
94    * @param len the length
95    * @see #getLength
96    */

97   protected void setLength(int len) {
98     this.length = len;
99   }
100   
101   
102   /** Returns the length.
103    * @return length
104    * @see #setLength
105    */

106   protected int getLength() {
107     return length;
108   }
109   
110   
111   
112   
113   /** Set the name index.
114    * @param index index into the constant pool
115    * @see #getNameIndex
116    */

117   protected void setNameIndex(int index) {
118     nameIndex = index;
119   }
120   
121   
122   /** Return the name index into the constant pool.
123    * @return index into the constant pool
124    * @see #setNameIndex
125    */

126   protected int getNameIndex() {
127     return nameIndex;
128   }
129   
130   
131   
132   
133   /** Set the descriptor index.
134    * @param index index into the constant pool
135    * @see #getDescriptorIndex
136    */

137   protected void setDescriptorIndex(int index) {
138     descriptorIndex = index;
139   }
140   
141   
142   /** Return the descriptor index into constant pool.
143    * @return index into the constant pool
144    * @see #setDescriptorIndex
145    */

146   protected int getDescriptorIndex() {
147     return descriptorIndex;
148   }
149   
150   
151   
152   /** Set the index.
153    * @param index index into the constant pool
154    * @see #getIndex
155    */

156   protected void setIndex(int index) {
157     this.index = index;
158   }
159   
160   
161   /** Return the index into the constant pool.
162    * @return index into the constant pool
163    * @see #setIndex
164    */

165   protected int getIndex() {
166     return index;
167   }
168   
169   
170   
171   
172   /** Check for Utf8 references to constant pool and mark them.
173    * @param pool the constant pool the element belongs to
174    */

175   protected void markUtf8Refs(ConstantPool pool) {
176     pool.incRefCount(getNameIndex());
177     pool.incRefCount(getDescriptorIndex());
178   }
179   
180   
181   
182   
183   /** Read the data following the header.
184    * @param din the input stream
185    * @throws IOException if an I/O error occurs
186    */

187   private void read(DataInput din)
188   throws IOException {
189     setStartPc(din.readUnsignedShort());
190     setLength(din.readUnsignedShort());
191     setNameIndex(din.readUnsignedShort());
192     setDescriptorIndex(din.readUnsignedShort());
193     setIndex(din.readUnsignedShort());
194   }
195   
196   
197   /** Export data following the header to a DataOutput stream.
198    * @param dout the output stream
199    * @throws IOException if an I/O error occurs
200    */

201   public void write(DataOutput dout)
202   throws IOException {
203     dout.writeShort(getStartPc());
204     dout.writeShort(getLength());
205     dout.writeShort(getNameIndex());
206     dout.writeShort(getDescriptorIndex());
207     dout.writeShort(getIndex());
208   }
209   
210   
211   
212   
213   /** Dump the content of the entry to the specified file (used for debugging).
214    * @param pw the print writer
215    * @param cf the class file the element belongs to
216    */

217   public void dump(PrintWriter pw, ClassFile cf) {
218     pw.print("StartPC: ");
219     pw.println(getStartPc());
220     pw.print("Length: ");
221     pw.println(getLength());
222     pw.print("Name index: ");
223     pw.println(getNameIndex());
224     pw.print("Descriptor index: ");
225     pw.println(getDescriptorIndex());
226     pw.print("Index: ");
227     pw.println(getIndex());
228   }
229 }
230
Popular Tags