/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This source file is part of SableVM.                            *
 *                                                                 *
 * See the file "LICENSE" for the copyright information and for    *
 * the terms and conditions for copying, distribution and          *
 * modification of this source file.                               *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * @author Patrick Latifi
 */
public class SExceptionsAttribute extends SAttributeInfo
{
    private int[] exceptionIndexTable;
    private SConstantClassInfo[] exceptionTable;

    public SExceptionsAttribute(int attributeNameIndex,
				int attributeLength, 
				int[] exceptionIndexTable)
    {
	super(attributeNameIndex, attributeLength);
	
	if(exceptionIndexTable != null)
	{
	    this.exceptionIndexTable = exceptionIndexTable;
	    this.exceptionTable = new SConstantClassInfo[exceptionIndexTable.length];
	}
	else
	{
	    this.exceptionIndexTable = new int[0];
	    this.exceptionTable = new SConstantClassInfo[0];	    
	}
    }

    public int[] getExceptionIndexTable()
    {
	return exceptionIndexTable;
    }

    public int numberOfExceptions()
    {
	    return exceptionIndexTable.length;
    }

    public int getExceptionIndexAt(int index) throws SClassFileException
    {
	try {
	    return exceptionIndexTable[index];
	}
	catch(ArrayIndexOutOfBoundsException aobe) {
	    throw new SClassFileException(
		  "Out of bound access for the exceptionIndexTable array.");
	}
    }

    public void setExceptionIndexTable(int[] exceptionIndexTable)
    {
	this.exceptionIndexTable = exceptionIndexTable;
    }

    public SConstantClassInfo[] getExceptionTable()
    {
	return exceptionTable;
    }

    public void setExceptionTable(SConstantClassInfo[] exceptionTable)
    {
	this.exceptionTable = exceptionTable;
    }

    public SConstantClassInfo getExceptionAt(int index) throws SClassFileException
    {
	try
	{
	    return exceptionTable[index];
	}
	catch(ArrayIndexOutOfBoundsException aobe)
	{
	    throw new SClassFileException("Out of bound access for the exceptions attribute.");
	}
    }

    public String toString()
    {
	    String s = new String();

	    s += "\n***Exceptions Attribute***\n";
	    s += super.toString();
	    s += "\nExceptionIndexTable\n";

	    for (int i = 0; i < exceptionIndexTable.length; i++)
		s += exceptionIndexTable[i] + "\t";
	    
	    s += "\nExceptionTable\n";
	    for (int i = 0; i < exceptionTable.length; i++)
		s += exceptionTable[i] + "\t";

	    return s;
    }
}
