BCIRenumberedAttribute.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.harmony.unpack200.bytecode;

  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.Arrays;
  21. import java.util.List;

  22. import org.apache.commons.compress.harmony.pack200.Pack200Exception;

  23. /**
  24.  * Abstract superclass for attributes that have some part encoded with a BCI renumbering
  25.  */
  26. public abstract class BCIRenumberedAttribute extends Attribute {

  27.     protected boolean renumbered;

  28.     public BCIRenumberedAttribute(final CPUTF8 attributeName) {
  29.         super(attributeName);
  30.     }

  31.     @Override
  32.     protected abstract int getLength();

  33.     protected abstract int[] getStartPCs();

  34.     /*
  35.      * (non-Javadoc)
  36.      *
  37.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#hasBCIRenumbering()
  38.      */
  39.     @Override
  40.     public boolean hasBCIRenumbering() {
  41.         return true;
  42.     }

  43.     /**
  44.      * 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
  45.      * 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.
  46.      *
  47.      * @param byteCodeOffsets List of Integer offsets of the byte code array
  48.      * @throws Pack200Exception Thrown from a subclass.
  49.      */
  50.     public void renumber(final List<Integer> byteCodeOffsets) throws Pack200Exception {
  51.         if (renumbered) {
  52.             throw new Error("Trying to renumber a line number table that has already been renumbered");
  53.         }
  54.         renumbered = true;
  55.         final int[] startPCs = getStartPCs();
  56.         Arrays.setAll(startPCs, i -> byteCodeOffsets.get(startPCs[i]).intValue());
  57.     }

  58.     @Override
  59.     public abstract String toString();

  60.     @Override
  61.     protected abstract void writeBody(DataOutputStream dos) throws IOException;

  62. }