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: ExceptionHandlerTable.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;
12import java.util.ArrayList;
13import java.util.List;
14
15import com.vladium.jcd.lib.UDataOutputStream;
16
17// ----------------------------------------------------------------------------
18/**
19 * @author (C) 2001, Vlad Roubtsov
20 */
21final class ExceptionHandlerTable implements IExceptionHandlerTable
22{
23    // public: ................................................................
24
25    // ACCESSORS:
26
27    public Exception_info get (final int offset)
28    {
29        return (Exception_info) m_exceptions.get (offset);
30    }
31
32    public int size ()
33    {
34        return m_exceptions.size ();
35    }
36
37    public long length ()
38    {
39        return 2 + (m_exceptions.size () << 3); // use size() if class becomes non-final
40    }
41
42    // Cloneable:
43
44    /**
45     * Performs a deep copy.
46     */
47    public Object clone ()
48    {
49        try
50        {
51            final ExceptionHandlerTable _clone = (ExceptionHandlerTable) super.clone ();
52
53            // deep clone:
54            final int exceptions_count = m_exceptions.size (); // use size() if class becomes non-final
55            _clone.m_exceptions = new ArrayList (exceptions_count);
56            for (int e = 0; e < exceptions_count; ++ e)
57            {
58                _clone.m_exceptions.add (((Exception_info) m_exceptions.get (e)).clone ());
59            }
60
61            return _clone;
62        }
63        catch (CloneNotSupportedException e)
64        {
65            throw new InternalError (e.toString ());
66        }
67    }
68
69    // IClassFormatOutput:
70
71    public void writeInClassFormat (final UDataOutputStream out) throws IOException
72    {
73        int exception_table_length = m_exceptions.size (); // use size() if class becomes non-final
74        out.writeU2 (exception_table_length);
75
76        for (int i = 0; i < exception_table_length; i++)
77        {
78            get (i).writeInClassFormat (out);
79        }
80    }
81
82
83    // MUTATORS:
84
85    public int add (final Exception_info exception)
86    {
87        final int newoffset = m_exceptions.size (); // use size() if class becomes non-final
88        m_exceptions.add (exception);
89
90        return newoffset;
91    }
92
93    public Exception_info set (final int offset, final Exception_info exception)
94    {
95        return (Exception_info) m_exceptions.set (offset, exception);
96    }
97
98    // protected: .............................................................
99
100    // package: ...............................................................
101
102
103    ExceptionHandlerTable (final int capacity)
104    {
105        m_exceptions = capacity < 0 ? new ArrayList () : new ArrayList (capacity);
106    }
107
108    // private: ...............................................................
109
110
111    private List/* Exception_info */ m_exceptions;
112
113} // end of class
114// ----------------------------------------------------------------------------
115