1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: ExceptionsAttribute_info.java,v 1.1.1.1 2004/05/09 16:57:47 vlad_r Exp $
8 */
9package com.vladium.jcd.cls.attribute;
10
11import java.io.IOException;
12
13import com.vladium.jcd.lib.UDataInputStream;
14import com.vladium.jcd.lib.UDataOutputStream;
15
16// ----------------------------------------------------------------------------
17/**
18 * The Exceptions attribute is a variable-length attribute used in the attributes
19 * table of a {@link com.vladium.jcd.cls.Method_info} structure. The Exceptions
20 * attribute indicates which checked exceptions a method may throw. There must be
21 * exactly one Exceptions attribute in each method_info structure.<P>
22 *
23 * The Exceptions attribute has the following format:
24 * <PRE>
25 * Exceptions_attribute {
26 *          u2 attribute_name_index;
27 *          u4 attribute_length;
28 *          u2 number_of_exceptions;
29 *          u2 exception_index_table[number_of_exceptions];
30 *  }
31 * </PRE>
32 * The value of the number_of_exceptions item indicates the number of entries
33 * in the exception_index_table.<P>
34 *
35 * Each nonzero value in the exception_index_table array must be a valid index
36 * into the constant_pool table. For each table item, if
37 * exception_index_table[i] != 0 , where 0 &lt; i &lt; number_of_exceptions,
38 * then the constant_pool entry at index exception_index_table[i] must be a
39 * {@link com.vladium.jcd.cls.constant.CONSTANT_Class_info} structure representing
40 * a class type that this method is declared to throw -- see {@link DeclaredExceptionTable}.
41 *
42 * @author (C) 2001, Vlad Roubtsov
43 */
44public
45final class ExceptionsAttribute_info extends Attribute_info
46{
47    // public: ................................................................
48
49    // TODO: merge IDeclaredExceptionTable into this class
50
51    public ExceptionsAttribute_info (final int attribute_name_index,
52                                     final IDeclaredExceptionTable exceptions)
53    {
54        super (attribute_name_index, exceptions.length ());
55
56        m_exceptions = exceptions;
57    }
58
59    public IDeclaredExceptionTable getDeclaredExceptions ()
60    {
61        return m_exceptions;
62    }
63
64    public long length ()
65    {
66        return 6 + m_exceptions.length ();
67    }
68
69    // Visitor:
70
71    public void accept (final IAttributeVisitor visitor, final Object ctx)
72    {
73        visitor.visit (this, ctx);
74    }
75
76    public String toString ()
77    {
78        // TODO: return more data here
79        return "ExceptionsAttribute_info: [attribute_name_index = " + m_name_index + ", attribute_length = " + m_attribute_length + ']';
80    }
81
82    // Cloneable:
83
84    /**
85     * Performs a deep copy.
86     */
87    public Object clone ()
88    {
89        final ExceptionsAttribute_info _clone = (ExceptionsAttribute_info) super.clone ();
90
91        // do deep copy:
92        _clone.m_exceptions = (IDeclaredExceptionTable) m_exceptions.clone ();
93
94        return _clone;
95    }
96
97    // IClassFormatOutput:
98
99    public void writeInClassFormat (final UDataOutputStream out) throws IOException
100    {
101        super.writeInClassFormat (out);
102
103        m_exceptions.writeInClassFormat (out);
104    }
105
106    // protected: .............................................................
107
108    // package: ...............................................................
109
110
111
112    ExceptionsAttribute_info (final int attribute_name_index, final long attribute_length,
113                              final UDataInputStream bytes)
114        throws IOException
115    {
116        super (attribute_name_index, attribute_length);
117
118        final int number_of_exceptions = bytes.readU2 ();
119        m_exceptions = new DeclaredExceptionTable (number_of_exceptions);
120
121        for (int i = 0; i < number_of_exceptions; i++)
122        {
123            final int exception_index = bytes.readU2 ();
124
125            m_exceptions.add (exception_index);
126        }
127    }
128
129    // private: ...............................................................
130
131
132    private IDeclaredExceptionTable m_exceptions;
133
134} // end of class
135// ----------------------------------------------------------------------------
136