01: /*
02: * Copyright 2004 Brian S O'Neill
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.util.List;
21: import java.io.DataInput;
22: import java.io.DataOutput;
23: import java.io.IOException;
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 mExceptions = new ArrayList(2);
37:
38: public ExceptionsAttr(ConstantPool cp) {
39: super (cp, EXCEPTIONS);
40: }
41:
42: public ExceptionsAttr(ConstantPool cp, String name) {
43: super (cp, name);
44: }
45:
46: public ExceptionsAttr(ConstantPool cp, String name, int length,
47: DataInput din) throws IOException {
48: super (cp, name);
49:
50: int size = din.readUnsignedShort();
51: length -= 2;
52:
53: for (int i = 0; i < size; i++) {
54: int index = din.readUnsignedShort();
55: length -= 2;
56: ConstantClassInfo info = (ConstantClassInfo) cp
57: .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
68: .size()];
69: return (ConstantClassInfo[]) mExceptions.toArray(copy);
70: }
71:
72: public void addException(ConstantClassInfo type) {
73: mExceptions.add(type);
74: }
75:
76: public int getLength() {
77: return 2 + 2 * mExceptions.size();
78: }
79:
80: public void writeDataTo(DataOutput dout) throws IOException {
81: int size = mExceptions.size();
82: dout.writeShort(size);
83: for (int i = 0; i < size; i++) {
84: ConstantClassInfo info = (ConstantClassInfo) mExceptions
85: .get(i);
86: dout.writeShort(info.getIndex());
87: }
88: }
89: }
|