    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,
    038        final int[] lineNumbers) {
    039        super(attributeName);
    040        this.lineNumberTableLength = lineNumberTableLength;
    041        this.startPcs = startPcs;
    042        this.lineNumbers = lineNumbers;
    043    }
    044
    045    @Override
    046    public boolean equals(final Object obj) {
    047        return this == obj;
    048    }
    049
    050    @Override
    051    protected int getLength() {
    052        return 2 + (4 * lineNumberTableLength);
    053    }
    054
    055    /*
    056     * (non-Javadoc)
    057     *
    058     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#getNestedClassFileEntries()
    059     */
    060    @Override
    061    protected ClassFileEntry[] getNestedClassFileEntries() {
    062        return new ClassFileEntry[] {getAttributeName()};
    063    }
    064
    065    @Override
    066    protected int[] getStartPCs() {
    067        return startPcs;
    068    }
    069
    070    /*
    071     * (non-Javadoc)
    072     *
    073     * @see
    074     * org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#resolve(org.apache.commons.compress.harmony.
    075     * unpack200.bytecode.ClassConstantPool)
    076     */
    077    @Override
    078    protected void resolve(final ClassConstantPool pool) {
    079        super.resolve(pool);
    080    }
    081
    082    /*
    083     * (non-Javadoc)
    084     *
    085     * @see org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry#toString()
    086     */
    087    @Override
    088    public String toString() {
    089        return "LineNumberTable: " + lineNumberTableLength + " lines";
    090    }
    091
    092    @Override
    093    protected void writeBody(final DataOutputStream dos) throws IOException {
    094        dos.writeShort(lineNumberTableLength);
    095        for (int i = 0; i < lineNumberTableLength; i++) {
    096            dos.writeShort(startPcs[i]);
    097            dos.writeShort(lineNumbers[i]);
    098        }
    099    }
    100}