BCIRenumberedAttribute.java

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

  20. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import java.util.List;

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

  25. /**
  26.  * Abstract superclass for attributes that have some part encoded with Byte Code Injection (BCI) renumbering.
  27.  */
  28. public abstract class BCIRenumberedAttribute extends Attribute {

  29.     /**
  30.      * Whether renumbering has occurred.
  31.      */
  32.     protected boolean renumbered;

  33.     /**
  34.      * Constructs a new instance for an attribute name.
  35.      *
  36.      * @param attributeName an attribute name.
  37.      */
  38.     public BCIRenumberedAttribute(final CPUTF8 attributeName) {
  39.         super(attributeName);
  40.     }

  41.     @Override
  42.     protected abstract int getLength();

  43.     /**
  44.      * Gets the array of indices for the start of line numbers.
  45.      *
  46.      * @return the array of indices for the start of line numbers.
  47.      */
  48.     protected abstract int[] getStartPCs();

  49.     /*
  50.      * (non-Javadoc)
  51.      *
  52.      * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#hasBCIRenumbering()
  53.      */
  54.     @Override
  55.     public boolean hasBCIRenumbering() {
  56.         return true;
  57.     }

  58.     /**
  59.      * 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
  60.      * 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.
  61.      *
  62.      * @param byteCodeOffsets List of Integer offsets of the byte code array.
  63.      * @throws Pack200Exception Thrown from a subclass.
  64.      */
  65.     public void renumber(final List<Integer> byteCodeOffsets) throws Pack200Exception {
  66.         if (renumbered) {
  67.             throw new Error("Trying to renumber a line number table that has already been renumbered");
  68.         }
  69.         renumbered = true;
  70.         final int[] startPCs = getStartPCs();
  71.         Arrays.setAll(startPCs, i -> byteCodeOffsets.get(startPCs[i]).intValue());
  72.     }

  73.     @Override
  74.     public abstract String toString();

  75.     @Override
  76.     protected abstract void writeBody(DataOutputStream dos) throws IOException;

  77. }