001: /**
002: * JavaGuard -- an obfuscation package for Java classfiles.
003: *
004: * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
005: * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * The author may be contacted at theit@gmx.de.
022: *
023: *
024: * $Id: ExceptionsAttrInfo.java,v 1.2 2002/05/08 11:53:42 glurk Exp $
025: */package net.sf.javaguard.classfile;
026:
027: import java.io.*;
028:
029: /** Representation of an attribute.
030: *
031: * @author <a href="mailto:theit@gmx.de">Thorsten Heit</a>
032: * @author <a href="mailto:markw@retrologic.com">Mark Welsh</a>
033: */
034: public class ExceptionsAttrInfo extends AttrInfo {
035: /** Holds the number of exceptions. */
036: private int numberOfExceptions;
037: /** An array that stores the indices of the exceptions. */
038: private int[] exceptionIndexTable;
039:
040: /** Default constructor that creates an ExceptionsAttrInfo object.
041: * @param cf the class file the object belongs to
042: * @param attrNameIndex index into the constant pool
043: * @param attrLength the length of the additional info data in the class file
044: */
045: protected ExceptionsAttrInfo(ClassFile cf, int attrNameIndex,
046: int attrLength) {
047: super (cf, attrNameIndex, attrLength);
048: }
049:
050: /** Return the string name of the attribute.
051: * @return string name of the attribute
052: */
053: protected String getAttrName() {
054: return ATTR_Exceptions;
055: }
056:
057: /** Stores the number of exceptions in the index table.
058: * @param len the length of the index table
059: * @see #getNumberOfExceptions
060: */
061: protected void setNumberOfExceptions(int len) {
062: numberOfExceptions = len;
063: }
064:
065: /** Returns the number of exceptions.
066: * @return number of exceptions
067: * @see #setNumberOfExceptions
068: */
069: protected int getNumberOfExceptions() {
070: return numberOfExceptions;
071: }
072:
073: /** Sets the exception index table.
074: * @param indexTable the index table
075: * @see #getExceptionIndexTable
076: */
077: protected void setExceptionIndexTable(int[] indexTable) {
078: exceptionIndexTable = indexTable;
079: }
080:
081: /** Returns the exception index table.
082: * @return exception index table
083: * @see #setExceptionIndexTable
084: */
085: protected int[] getExceptionIndexTable() {
086: return exceptionIndexTable;
087: }
088:
089: /** Read the data following the header.
090: * @param din the input stream
091: * @throws IOException if an I/O error occurs
092: */
093: protected void readInfo(DataInput din) throws IOException {
094: setNumberOfExceptions(din.readUnsignedShort());
095: int[] indexTable = new int[getNumberOfExceptions()];
096: for (int i = 0; i < getNumberOfExceptions(); i++) {
097: indexTable[i] = din.readUnsignedShort();
098: }
099: setExceptionIndexTable(indexTable);
100: }
101:
102: /** Export data following the header to a DataOutput stream. Should be
103: * overwritten in subclasses.
104: * @param dout the output stream
105: * @throws IOException if an I/O error occurs
106: */
107: public void writeInfo(DataOutput dout) throws IOException {
108: dout.writeShort(getNumberOfExceptions());
109: for (int i = 0; i < getNumberOfExceptions(); i++) {
110: dout.writeShort(getExceptionIndexTable()[i]);
111: }
112: }
113:
114: /** Dump the content of the entry to the specified file (used for debugging).
115: * @param pw the print writer
116: * @param cf the class file the element belongs to
117: */
118: public void dump(PrintWriter pw, ClassFile cf) {
119: pw.println("Exception table:");
120: pw.print("Number of exceptions: ");
121: pw.println(getNumberOfExceptions());
122: for (int i = 0; i < getNumberOfExceptions(); i++) {
123: pw.print("Exception index[");
124: pw.print(i);
125: pw.print("]: ");
126: pw.println(getExceptionIndexTable()[i]);
127: }
128: }
129: }
|