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