KickJava   Java API By Example, From Geeks To Geeks.

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


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: ExceptionsAttrInfo.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 an attribute.
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 ExceptionsAttrInfo extends AttrInfo {
37   /** Holds the number of exceptions. */
38   private int numberOfExceptions;
39   /** An array that stores the indices of the exceptions. */
40   private int[] exceptionIndexTable;
41   
42   
43   
44   
45   /** Default constructor that creates an ExceptionsAttrInfo object.
46    * @param cf the class file the object belongs to
47    * @param attrNameIndex index into the constant pool
48    * @param attrLength the length of the additional info data in the class file
49    */

50   protected ExceptionsAttrInfo(ClassFile cf, int attrNameIndex, int attrLength) {
51     super(cf, attrNameIndex, attrLength);
52   }
53   
54   
55   
56   
57   /** Return the string name of the attribute.
58    * @return string name of the attribute
59    */

60   protected String JavaDoc getAttrName() {
61     return ATTR_Exceptions;
62   }
63   
64   
65   
66   
67   /** Stores the number of exceptions in the index table.
68    * @param len the length of the index table
69    * @see #getNumberOfExceptions
70    */

71   protected void setNumberOfExceptions(int len) {
72     numberOfExceptions = len;
73   }
74   
75   
76   /** Returns the number of exceptions.
77    * @return number of exceptions
78    * @see #setNumberOfExceptions
79    */

80   protected int getNumberOfExceptions() {
81     return numberOfExceptions;
82   }
83   
84   
85   
86   
87   /** Sets the exception index table.
88    * @param indexTable the index table
89    * @see #getExceptionIndexTable
90    */

91   protected void setExceptionIndexTable(int[] indexTable) {
92     exceptionIndexTable = indexTable;
93   }
94   
95   
96   /** Returns the exception index table.
97    * @return exception index table
98    * @see #setExceptionIndexTable
99    */

100   protected int[] getExceptionIndexTable() {
101     return exceptionIndexTable;
102   }
103   
104   
105   
106   
107   /** Read the data following the header.
108    * @param din the input stream
109    * @throws IOException if an I/O error occurs
110    */

111   protected void readInfo(DataInput din)
112   throws IOException {
113     setNumberOfExceptions(din.readUnsignedShort());
114     int[] indexTable = new int[getNumberOfExceptions()];
115     for (int i = 0; i < getNumberOfExceptions(); i++) {
116       indexTable[i] = din.readUnsignedShort();
117     }
118     setExceptionIndexTable(indexTable);
119   }
120   
121   
122   /** Export data following the header to a DataOutput stream. Should be
123    * overwritten in subclasses.
124    * @param dout the output stream
125    * @throws IOException if an I/O error occurs
126    */

127   public void writeInfo(DataOutput dout)
128   throws IOException {
129     dout.writeShort(getNumberOfExceptions());
130     for (int i = 0; i < getNumberOfExceptions(); i++) {
131       dout.writeShort(getExceptionIndexTable()[i]);
132     }
133   }
134   
135   
136   
137   
138   /** Dump the content of the entry to the specified file (used for debugging).
139    * @param pw the print writer
140    * @param cf the class file the element belongs to
141    */

142   public void dump(PrintWriter pw, ClassFile cf) {
143     pw.println("Exception table:");
144     pw.print("Number of exceptions: ");
145     pw.println(getNumberOfExceptions());
146     for (int i=0; i<getNumberOfExceptions(); i++) {
147       pw.print("Exception index[");
148       pw.print(i);
149       pw.print("]: ");
150       pw.println(getExceptionIndexTable()[i]);
151     }
152   }
153 }
154
Popular Tags