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