alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (JExceptionsAttribute.java)

This example Scala source code file (JExceptionsAttribute.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

datainputstream, exceptions, exceptions, io, ioexception, ioexception, jclass, jconstantpool, jexceptionsattribute, jexceptionsattribute, jmethod, object, string, string, stringbuffer

The Scala JExceptionsAttribute.java source code

/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2011 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Exceptions attribute

 * This table is used by compilers to indicate which Exceptions a method
 * is declared to throw. See section 2.6.4 of the JVM specification.
 *
 * @author Stephane Micheloud
 * @version 1.0
 */

public class JExceptionsAttribute extends JAttribute {
    /** Constant pool of the current classfile. */
    private JConstantPool pool;

    protected int[] indexTable;
    protected int count;

    public JExceptionsAttribute(FJBGContext context,
                                JClass clazz,
                                JMethod owner) {
        super(context, clazz);
        this.pool = clazz.pool;

        this.count = 0;
        this.indexTable = new int[8]; // some size > count

        assert clazz == owner.getOwner();
    }

    public JExceptionsAttribute(FJBGContext context,
                                JClass clazz,
                                Object owner, //JMethod
                                String name,
                                int size,
                                DataInputStream stream)
        throws IOException {
        super(context, clazz, name);
        this.pool = clazz.pool;

        this.count = stream.readShort();
        this.indexTable = new int[count];
        for (int i = 0; i < count; ++i)
            indexTable[i] = stream.readShort();

        assert name.equals(getName());
    }

    public void addEntry(int classIndex) {
        if (count >= indexTable.length) {
            int[] newIT = new int[indexTable.length * 2];
            System.arraycopy(indexTable, 0, newIT, 0, indexTable.length);
            indexTable = newIT;
        }
        indexTable[count++] = classIndex;
    }

    public String getName() { return "Exceptions"; }

    // Follows javap output format for Exceptions attribute.
    /*@Override*/ public String toString() {
        StringBuffer buf = new StringBuffer("  Exceptions: ");
        for (int i = 0; i < indexTable.length; ++i) {
            buf.append("\n   throws ");
            buf.append(JClass.toExternalName(pool.lookupClass(indexTable[i])));
        }
        buf.append("\n");
        return buf.toString();
    }

    protected int getSize() {
        return 2 + indexTable.length * 2;
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        stream.writeShort(count);
        for (int i = 0; i < count; ++i)
            stream.writeShort(indexTable[i]);
    }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala JExceptionsAttribute.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.