KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > attribute > ExceptionsAttr


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile.attribute;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.io.DataInput JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.cojen.classfile.Attribute;
25 import org.cojen.classfile.ConstantPool;
26 import org.cojen.classfile.constant.ConstantClassInfo;
27
28 /**
29  * This class corresponds to the Exceptions_attribute structure as defined in
30  * section 4.7.5 of <i>The Java Virtual Machine Specification</i>.
31  *
32  * @author Brian S O'Neill
33  */

34 public class ExceptionsAttr extends Attribute {
35
36     private List JavaDoc mExceptions = new ArrayList JavaDoc(2);
37     
38     public ExceptionsAttr(ConstantPool cp) {
39         super(cp, EXCEPTIONS);
40     }
41
42     public ExceptionsAttr(ConstantPool cp, String JavaDoc name) {
43         super(cp, name);
44     }
45     
46     public ExceptionsAttr(ConstantPool cp, String JavaDoc name, int length, DataInput JavaDoc din)
47         throws IOException JavaDoc
48     {
49         super(cp, name);
50         
51         int size = din.readUnsignedShort();
52         length -= 2;
53
54         for (int i=0; i<size; i++) {
55             int index = din.readUnsignedShort();
56             length -= 2;
57             ConstantClassInfo info = (ConstantClassInfo)cp.getConstant(index);
58             addException(info);
59         }
60
61         if (length > 0) {
62             din.skipBytes(length);
63         }
64     }
65
66     public ConstantClassInfo[] getExceptions() {
67         ConstantClassInfo[] copy = new ConstantClassInfo[mExceptions.size()];
68         return (ConstantClassInfo[])mExceptions.toArray(copy);
69     }
70
71     public void addException(ConstantClassInfo type) {
72         mExceptions.add(type);
73     }
74     
75     public int getLength() {
76         return 2 + 2 * mExceptions.size();
77     }
78     
79     public void writeDataTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
80         int size = mExceptions.size();
81         dout.writeShort(size);
82         for (int i=0; i<size; i++) {
83             ConstantClassInfo info = (ConstantClassInfo)mExceptions.get(i);
84             dout.writeShort(info.getIndex());
85         }
86     }
87 }
88
Popular Tags