Source code: com/chaoswg/xtc4y/classdesc/ExceptionsAttribute.java
1 //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/ExceptionsAttribute.java,v 1.1.1.1 2003/08/07 13:40:28 toggm Exp $
2 /******************************************************************************
3 * XTC4y - eXtreme Testing Collection 4 you *
4 * -------------------------------------------------------------------------- *
5 * URL: http://www.chaoswg.com/xtc4y *
6 * Author: Mike Toggweiler (2.dog@gmx.ch) *
7 * *
8 * Last Updated: $Date: 2003/08/07 13:40:28 $, by $Author: toggm $ *
9 * Version: $Revision: 1.1.1.1 $ *
10 * -------------------------------------------------------------------------- *
11 * COPYRIGHT: (c) 2003 by Mike Toggweiler *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 *****************************************************************************/
18 package com.chaoswg.xtc4y.classdesc;
19
20 import java.util.Vector;
21
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25
26 /**
27 * This class describes the information for a exceptions attribute ($4.7.4)
28 * @author Mike Toggweiler
29 **/
30 public class ExceptionsAttribute extends Attribute {
31
32 private Vector exceptions = new Vector();
33
34 public static final String NAME = "Exceptions";
35
36
37 /**
38 * Creates a ExceptionsAttribute and initializes it from a
39 * DataInputStream
40 * The attribute name and length
41 * of the attribute information was already read.
42 * @param dis the DataInputStream to read from
43 * @param cp the constant pool to resolve indices
44 * @param length the length of the attribute information provided
45 * on the DataInputStream
46 **/
47 protected ExceptionsAttribute(DataInputStream dis, ConstantPool cp,
48 int length)
49 throws IOException {
50 super(dis, cp, length);
51
52 if (!getName().equals(NAME)) {
53 throw new ClassFormatError("Attribute name does not match");
54 }
55
56 short number = (short)dis.readUnsignedShort();
57 short exIndex;
58 for (int i=0; i<number; ++i) {
59 exIndex = (short)dis.readUnsignedShort();
60 exceptions.add((ClassCPEntry)cp.getCPEntryAt(exIndex));
61 }
62 }
63
64 /**
65 * construct a new deprecated attribute
66 **/
67 public ExceptionsAttribute() {
68 }
69
70 /**
71 * Write the attribute information onto the DataOutputStream, using the
72 * constant pool to register variables. The Attribute name and the
73 * length of the information will already be written. A specific
74 * attribute implementation should overwrite this method.
75 * @param dos the DataOutputStream to write on
76 * @param cp The constant pool to register variables
77 **/
78 protected void writeAttribute(DataOutputStream dos, ConstantPool cp)
79 throws IOException {
80 dos.writeShort(exceptions.size());
81 for (int i=0; i<exceptions.size(); ++i) {
82 dos.writeShort(cp.addCPEntry((ClassCPEntry)
83 exceptions.elementAt(i)));
84 }
85 }
86
87 /**
88 * @return the name of the attribute
89 **/
90 public String getName() {
91 return NAME;
92 }
93
94 /**
95 * Add an exception
96 * @param exc a ClassCPEntry representing the exception
97 **/
98 public void addException(ClassCPEntry exc) {
99 exceptions.add(exc);
100 }
101
102 /**
103 * @return an array of ClassCPEntries representing the exceptions
104 **/
105 public ClassCPEntry[] getExceptions() {
106 return ((ClassCPEntry[])
107 exceptions.toArray(new ClassCPEntry[exceptions.size()]));
108 }
109
110 /**
111 * Remove an exception
112 * @param exc the classCPentry representing the exception to remove
113 **/
114 public void removeException(ClassCPEntry exc) {
115 exceptions.remove(exc);
116 }
117 }