    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
    054     * Integers specifying the offset in the byte code array of each instruction) and updates the start_pcs so that it
    055     * points to the instruction index itself, not the BCI renumbering of the instruction.
    056     *
    057     * @param byteCodeOffsets List of Integer offsets of the bytecode array
    058     * @throws Pack200Exception TODO
    059     */
    060    public void renumber(final List<Integer> byteCodeOffsets) throws Pack200Exception {
    061        if (renumbered) {
    062            throw new Error("Trying to renumber a line number table that has already been renumbered");
    063        }
    064        renumbered = true;
    065        final int[] startPCs = getStartPCs();
    066        Arrays.setAll(startPCs, i -> byteCodeOffsets.get(startPCs[i]).intValue());
    067    }
    068
    069    @Override
    070    public abstract String toString();
    071
    072    @Override
    073    protected abstract void writeBody(DataOutputStream dos) throws IOException;
    074
    075}