001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.harmony.unpack200.bytecode;
018
019import java.io.DataOutputStream;
020import java.io.IOException;
021
022/**
023 * Line number table
024 */
025public class LineNumberTableAttribute extends BCIRenumberedAttribute {
026
027    private static CPUTF8 attributeName;
028
029    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
030        attributeName = cpUTF8Value;
031    }
032
033    private final int lineNumberTableLength;
034    private final int[] startPcs;
035    private final int[] lineNumbers;
036
037    public LineNumberTableAttribute(final int lineNumberTableLength, final int[] startPcs, final int[] lineNumbers) {
038        super(attributeName);
039        this.lineNumberTableLength = lineNumberTableLength;
040        this.startPcs = startPcs;
041        this.lineNumbers = lineNumbers;
042    }
043
044    @Override
045    public boolean equals(final Object obj) {
046        return this == obj;
047    }
048
049    @Override
050    protected int getLength() {
051        return 2 + 4 * lineNumberTableLength;
052    }
053
054    /*
055     * (non-Javadoc)
056     *
057     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getNestedClassFileEntries()
058     */
059    @Override
060    protected ClassFileEntry[] getNestedClassFileEntries() {
061        return new ClassFileEntry[] { getAttributeName() };
062    }
063
064    @Override
065    protected int[] getStartPCs() {
066        return startPcs;
067    }
068
069    /*
070     * (non-Javadoc)
071     *
072     * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
073     */
074    @Override
075    public String toString() {
076        return "LineNumberTable: " + lineNumberTableLength + " lines";
077    }
078
079    @Override
080    protected void writeBody(final DataOutputStream dos) throws IOException {
081        dos.writeShort(lineNumberTableLength);
082        for (int i = 0; i < lineNumberTableLength; i++) {
083            dos.writeShort(startPcs[i]);
084            dos.writeShort(lineNumbers[i]);
085        }
086    }
087}