    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;
    021import java.util.Arrays;
    022import java.util.List;
    023
    024import org.apache.commons.compress.harmony.pack200.Pack200Exception;
    025
    026/**
    027 * Abstract superclass for attributes that have some part encoded with a BCI renumbering
    028 */
    029public abstract class BCIRenumberedAttribute extends Attribute {
    030
    031    protected boolean renumbered;
    032
    033    public BCIRenumberedAttribute(final CPUTF8 attributeName) {
    034        super(attributeName);
    035    }
    036
    037    @Override
    038    protected abstract int getLength();
    039
    040    protected abstract int[] getStartPCs();
    041
    042    /*
    043     * (non-Javadoc)
    044     *
    045     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#hasBCIRenumbering()
    046     */
    047    @Override
    048    public boolean hasBCIRenumbering() {
    049        return true;
    050    }
    051
    052    /**
    053     * In Pack200, line number tables are BCI renumbered. This method takes the byteCodeOffsets (which is a List of Integers specifying the offset in the byte
    054     * code array of each instruction) and updates the start_pcs so that it points to the instruction index itself, not the BCI renumbering of the instruction.
    055     *
    056     * @param byteCodeOffsets List of Integer offsets of the byte code array
    057     * @throws Pack200Exception Thrown from a subclass.
    058     */
    059    public void renumber(final List<Integer> byteCodeOffsets) throws Pack200Exception {
    060        if (renumbered) {
    061            throw new Error("Trying to renumber a line number table that has already been renumbered");
    062        }
    063        renumbered = true;
    064        final int[] startPCs = getStartPCs();
    065        Arrays.setAll(startPCs, i -> byteCodeOffsets.get(startPCs[i]).intValue());
    066    }
    067
    068    @Override
    069    public abstract String toString();
    070
    071    @Override
    072    protected abstract void writeBody(DataOutputStream dos) throws IOException;
    073
    074}