KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > ExceptionsAttribute


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.*;
28 import java.util.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 /**
32  * ExceptionsAttribute represents a method attribute in a class file
33  * listing the checked exceptions for the method.
34  */

35
36 public class ExceptionsAttribute extends ClassAttribute {
37     public final static String JavaDoc expectedAttrName = "Exceptions";//NOI18N
38

39   /* The list of checked exceptions */
40   private Vector JavaDoc exceptionTable;
41
42   /* public accessors */
43
44   /**
45    * Return an enumeration of the checked exceptions
46    */

47   public Enumeration JavaDoc exceptions() {
48     return exceptionTable.elements();
49   }
50
51   /**
52    * Constructor
53    */

54   public ExceptionsAttribute(ConstUtf8 attrName, Vector JavaDoc excTable) {
55     super(attrName);
56     exceptionTable = excTable;
57   }
58
59   /**
60    * Convenience Constructor - for single exception
61    */

62   public ExceptionsAttribute(ConstUtf8 attrName, ConstClass exc) {
63     super(attrName);
64     exceptionTable = new Vector JavaDoc(1);
65     exceptionTable.addElement(exc);
66   }
67
68   /* package local methods */
69
70   static ExceptionsAttribute read(ConstUtf8 attrName,
71                   DataInputStream data, ConstantPool pool)
72     throws IOException {
73     int nExcepts = data.readUnsignedShort();
74     Vector JavaDoc excTable = new Vector JavaDoc();
75     while (nExcepts-- > 0) {
76       int excIndex = data.readUnsignedShort();
77       ConstClass exc_class = null;
78       if (excIndex != 0)
79     exc_class = (ConstClass) pool.constantAt(excIndex);
80       excTable.addElement(exc_class);
81     }
82         
83     return new ExceptionsAttribute(attrName, excTable);
84   }
85
86   void write(DataOutputStream out) throws IOException {
87     out.writeShort(attrName().getIndex());
88     out.writeInt(2+2*exceptionTable.size());
89     out.writeShort(exceptionTable.size());
90     for (int i=0; i<exceptionTable.size(); i++)
91       out.writeShort(((ConstClass) exceptionTable.elementAt(i)).getIndex());
92   }
93
94   void print(PrintStream out, int indent) {
95     ClassPrint.spaces(out, indent);
96     out.print("Exceptions:");//NOI18N
97
for (int i=0; i<exceptionTable.size(); i++)
98         out.print(" " + ((ConstClass) exceptionTable.elementAt(i)).asString());//NOI18N
99
out.println();
100   }
101   
102 }
103
104
Popular Tags