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

Scala example source code file (JLineNumberTableAttribute.java)

This example Scala source code file (JLineNumberTableAttribute.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, io, ioexception, ioexception, jattribute, jclass, jcode, jcode, jlinenumbertableattribute, jlinenumbertableattribute, linenumbertable, object, string, string, stringbuffer

The Scala JLineNumberTableAttribute.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;

/**
 * Attribute storing correspondance between instructions and source
 * line numbers.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JLineNumberTableAttribute extends JAttribute {
    protected final JCode code;

    public JLineNumberTableAttribute(FJBGContext context,
                                     JClass clazz,
                                     JCode owner) {
        super(context, clazz);
        this.code = owner;

        assert owner.getOwner().getOwner() == clazz;
    }

    public JLineNumberTableAttribute(FJBGContext context,
                                     JClass clazz,
                                     Object owner,
                                     String name,
                                     int size,
                                     DataInputStream stream)
        throws IOException {
        super(context, clazz, name);
        this.code = (JCode)owner;

        int[] mapping = new int[code.getSize()];

        int count = stream.readShort();
        for (int i = 0; i < count; ++i) {
            int startPC = stream.readShort();
            int lineNum = stream.readShort();
            mapping[startPC] = lineNum;
        }

        // Avoids duplication of LineNumberTable attribute
        // (see method ensureLineNumberCapacity in class JCode).
        assert code.lineNumbers == null;
        code.lineNumbers = new int[0];

        int lineNum = 0;
        for (int pc = 0; pc < mapping.length; ++pc) {
            if (mapping[pc] != 0) lineNum = mapping[pc];
            if (lineNum != 0) code.setLineNumber(pc, lineNum);
        }

        assert name.equals(getName());
    }

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

    // Follows javap output format for LineNumberTable attribute.
    /*@Override*/ public String toString() {
        StringBuffer buf = new StringBuffer("  LineNumberTable: ");
        int[] encoding = encode();
        for (int i = 0; i < encoding.length/2; ++i) {
            buf.append("\n   line ");
            buf.append(encoding[i * 2 + 1]);
            buf.append(": ");
            buf.append(encoding[i * 2]);
        }
        buf.append("\n");
        return buf.toString();
    }

    protected int[] encoding;
    protected int[] encode() {
        if (encoding == null) {
            int[] lineNumbers = code.getLineNumbers();
            int[] preEncoding = new int[lineNumbers.length * 2];
            int prevLineNum = 0;

            int i = 0;
            for (int pc = 0; pc < lineNumbers.length; ++pc) {
                int lineNum = lineNumbers[pc];
                if (lineNum != 0 & lineNum != prevLineNum) {
                    preEncoding[i++] = pc;
                    preEncoding[i++] = lineNum;
                    prevLineNum = lineNum;
                }
            }
            if (i == preEncoding.length)
                encoding = preEncoding;
            else {
                encoding = new int[i];
                System.arraycopy(preEncoding, 0, encoding, 0, i);
            }
        }
        return encoding;
    }

    protected int getSize() {
        int[] encoding = encode();
        return 2 + encoding.length * 2;
    }

    protected void writeContentsTo(DataOutputStream stream) throws IOException {
        int[] encoding = encode();
        int entries = encoding.length / 2;
        stream.writeShort(entries);
        for (int i = 0; i < entries; ++i) {
            stream.writeShort(encoding[i * 2]);
            stream.writeShort(encoding[i * 2 + 1]);
        }
    }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala JLineNumberTableAttribute.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.