KickJava   Java API By Example, From Geeks To Geeks.

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


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: CodeAttrInfo.java,v 1.4 2002/05/11 23:21:48 glurk Exp $
25  */

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

37 public class CodeAttrInfo extends AttrInfo {
38   /** Constant that describes the size of the constant field. */
39   public static final int CONSTANT_FIELD_SIZE = 12;
40   
41   /** The maximum depth of the operand stack of this method at any point during
42    * execution of the method. */

43   private int maxStack;
44   /** The number of local variables in the local variable table. */
45   private int maxLocals;
46   /** The number of bytes in the code array for this method. */
47   private int codeLength;
48   /** The actual bytes of Java virtual machine code that implement the method. */
49   private byte[] byteCode;
50   /** The number of entries in the exception table. */
51   private int exceptionTableLength;
52   /** Holds the exception handlers. */
53   private ExceptionInfo[] exceptionTable;
54   /** The number of attributes. */
55   private int attributesLength;
56   /** The array of attributes. */
57   private AttrInfo[] attributeInfo;
58   
59   
60   
61   
62   /** Default constructor that creates a CodeAttrInfo object.
63    * @param cf the class file the object belongs to
64    * @param attrNameIndex index into the constant pool
65    * @param attrLength the length of the additional info data in the class file
66    */

67   protected CodeAttrInfo(ClassFile cf, int attrNameIndex, int attrLength) {
68     super(cf, attrNameIndex, attrLength);
69   }
70   
71   
72   
73   
74   /** Return the length in bytes of the attribute.
75    * @return length in bytes of the attribute
76    */

77   protected int getAttrInfoLength() {
78     int length = CONSTANT_FIELD_SIZE + getCodeLength()
79     + getExceptionTableLength() * ExceptionInfo.CONSTANT_FIELD_SIZE;
80     for (int i = 0; i < getAttributesLength(); i++) {
81       length += AttrInfo.CONSTANT_FIELD_SIZE + getAttributes()[i].getAttrInfoLength();
82     }
83     return length;
84   }
85   
86   
87   
88   
89   /** Return the string name of the attribute.
90    * @return string name of the attribute
91    */

92   protected String JavaDoc getAttrName() {
93     return ATTR_Code;
94   }
95   
96   
97   
98   
99   /** Sets the maximum depth of the operand stack of this method at any point
100    * during execution of the method.
101    * @param depth maximum depth
102    * @see #getMaxStack
103    */

104   protected void setMaxStack(int depth) {
105     maxStack = depth;
106   }
107   
108   
109   /** Returns the maximum depth of the operand stack of this method at any
110    * point during execution of the method.
111    * @return maximum depth
112    * @see #setMaxStack
113    */

114   protected int getMaxStack() {
115     return maxStack;
116   }
117   
118   
119   
120   
121   /** Sets the number of local variables in the local variable table.
122    * @param len number of entries in the local variable table
123    * @see #getMaxLocals
124    */

125   protected void setMaxLocals(int len) {
126     maxLocals = len;
127   }
128   
129   
130   /** Returns the number of local variables in the local variable table.
131    * @return number of entries in the local variable table
132    * @see #setMaxLocals
133    */

134   protected int getMaxLocals() {
135     return maxLocals;
136   }
137   
138   
139   
140   
141   /** Sets the number of bytes in the code array for this method.
142    * @param len the number of bytes in the code array
143    * @see #getCodeLength
144    */

145   protected void setCodeLength(int len) {
146     codeLength = len;
147   }
148   
149   
150   /** Returns the number of bytes in the code array for this method.
151    * @return the number of bytes in the code array
152    * @see #setCodeLength
153    */

154   protected int getCodeLength() {
155     return codeLength;
156   }
157   
158   
159   
160   
161   /** Sets the actual bytes of Java virtual machine code that implement the
162    * method.
163    * @param code byte array with Java virtual machine code
164    * @see #getCode
165    */

166   protected void setCode(byte[] code) {
167     byteCode = code;
168   }
169   
170   
171   /** Returns the actual bytes of Java virtua machine code that implement the
172    * method.
173    * @return byte array with Java virtual machine code
174    * @see #setCode
175    */

176   protected byte[] getCode() {
177     return byteCode;
178   }
179   
180   
181   
182   
183   /** Sets the number of entries in the exception table.
184    * @param len the number of entries in the exception table
185    * @see #getExceptionTableLength
186    */

187   protected void setExceptionTableLength(int len) {
188     exceptionTableLength = len;
189   }
190   
191   
192   /** Returns the number of entries in the exception table.
193    * @return the number of entries in the exception table
194    * @see #setExceptionTableLength
195    */

196   protected int getExceptionTableLength() {
197     return exceptionTableLength;
198   }
199   
200   
201   
202   
203   /** Sets the array that contain the exception handlers.
204    * @param table array with exception handlers
205    * @see #getExceptionTable
206    */

207   protected void setExceptionTable(ExceptionInfo[] table) {
208     exceptionTable = table;
209   }
210   
211   
212   /** Returns the array that contains the exception handlers.
213    * @return array with exception handlers
214    * @see #setExceptionTable
215    */

216   protected ExceptionInfo[] getExceptionTable() {
217     return exceptionTable;
218   }
219   
220   
221   
222   
223   /** Sets the number of attributes.
224    * @param len the number of attributes
225    * @see #getAttributesLength
226    */

227   protected void setAttributesLength(int len) {
228     attributesLength = len;
229   }
230   
231   
232   /** Returns the number of attributes.
233    * @return number of attributes
234    * @see #setAttributesLength
235    */

236   protected int getAttributesLength() {
237     return attributesLength;
238   }
239   
240   
241   
242   
243   /** Sets the array that contains the attributes.
244    * @param array the array that contains the attributes
245    * @see #getAttributes
246    */

247   protected void setAttributes(AttrInfo[] array) {
248     attributeInfo = array;
249   }
250   
251   
252   /** Returns the array that contains the attributes.
253    * @return array containing the attributes
254    * @see #setAttributes
255    */

256   protected AttrInfo[] getAttributes() {
257     return attributeInfo;
258   }
259   
260   
261   
262   
263   /** Trim attributes from the classfile ('Code', 'Exceptions', 'ConstantValue'
264    * are preserved, all others except the list in the String[] are killed).
265    * @param keepAttrs array with attributes to keep
266    */

267   protected void trimAttrsExcept(String JavaDoc[] keepAttrs) {
268     // Traverse all attributes, removing all except those on 'keep' list
269
for (int i = 0; i < getAttributesLength(); i++) {
270       if (Tools.isInArray(getAttributes()[i].getAttrName(), keepAttrs)) {
271         getAttributes()[i].trimAttrsExcept(keepAttrs);
272       }
273       else {
274         getAttributes()[i] = null;
275       }
276     }
277     
278     // Delete the marked attributes
279
AttrInfo[] left = new AttrInfo[getAttributesLength()];
280     int j = 0;
281     for (int i = 0; i < getAttributesLength(); i++) {
282       if (getAttributes()[i] != null) {
283         left[j++] = getAttributes()[i];
284       }
285     }
286     AttrInfo[] attrs = new AttrInfo[j];
287     System.arraycopy(left, 0, attrs, 0, j);
288     setAttributes(attrs);
289     setAttributesLength(j);
290   }
291   
292   
293   
294   
295   /** Check for references in the 'info' data to the constant pool and mark
296    * them.
297    * @param pool the constant pool the element belonts to
298    */

299   protected void markUtf8RefsInInfo(ConstantPool pool) {
300     for (int i = 0; i < getAttributesLength(); i++) {
301       getAttributes()[i].markUtf8Refs(pool);
302     }
303   }
304   
305   
306   
307   
308   /** Read the data following the header.
309    * @param din the input stream
310    * @throws IOException if an I/O error occurs
311    */

312   protected void readInfo(DataInput din)
313   throws IOException {
314     setMaxStack(din.readUnsignedShort());
315     setMaxLocals(din.readUnsignedShort());
316     setCodeLength(din.readInt());
317     byte[] code = new byte[getCodeLength()];
318     din.readFully(code);
319     setCode(code);
320     setExceptionTableLength(din.readUnsignedShort());
321     ExceptionInfo[] exTable = new ExceptionInfo[getExceptionTableLength()];
322     for (int i = 0; i < getExceptionTableLength(); i++) {
323       exTable[i] = ExceptionInfo.create(din);
324     }
325     setExceptionTable(exTable);
326     setAttributesLength(din.readUnsignedShort());
327     AttrInfo[] attributes = new AttrInfo[getAttributesLength()];
328     for (int i = 0; i < getAttributesLength(); i++) {
329       attributes[i] = AttrInfo.create(din, owner);
330     }
331     setAttributes(attributes);
332   }
333   
334   
335   /** Export data following the header to a DataOutput stream. Should be
336    * overwritten in subclasses.
337    * @param dout the output stream
338    * @throws IOException if an I/O error occurs
339    */

340   public void writeInfo(DataOutput dout)
341   throws IOException {
342     dout.writeShort(getMaxStack());
343     dout.writeShort(getMaxLocals());
344     dout.writeInt(getCodeLength());
345     dout.write(getCode());
346     dout.writeShort(getExceptionTableLength());
347     for (int i = 0; i < getExceptionTableLength(); i++) {
348       getExceptionTable()[i].write(dout);
349     }
350     dout.writeShort(getAttributesLength());
351     for (int i = 0; i < getAttributesLength(); i++) {
352       getAttributes()[i].write(dout);
353     }
354   }
355   
356   
357   
358   
359   /** Dump the content of the entry to the specified file (used for debugging).
360    * @param pw the print writer
361    * @param cf the class file the element belongs to
362    */

363   public void dump(PrintWriter pw, ClassFile cf) {
364     pw.println(getAttrName());
365     pw.print("maxStack: "); pw.println(getMaxStack());
366     pw.print("maxLocals: "); pw.println(getMaxLocals());
367     pw.print("codeLength: "); pw.println(getCodeLength());
368     pw.print("exceptions: "); pw.println(getExceptionTableLength());
369     for (int i=0; i<getExceptionTableLength(); i++) {
370       getExceptionTable()[i].dump(pw, cf);
371     }
372     pw.print("attributes: "); pw.println(getAttributesLength());
373     for (int i=0; i<getAttributesLength(); i++) {
374       getAttributes()[i].dump(pw, cf);
375     }
376   }
377 }
378
Popular Tags