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