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
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.apache.commons.compress.harmony.pack200.Pack200Exception;
25
26 /**
27 * Abstract superclass for attributes that have some part encoded with a BCI renumbering
28 */
29 public abstract class BCIRenumberedAttribute extends Attribute {
30
31 protected boolean renumbered;
32
33 public BCIRenumberedAttribute(final CPUTF8 attributeName) {
34 super(attributeName);
35 }
36
37 @Override
38 protected abstract int getLength();
39
40 protected abstract int[] getStartPCs();
41
42 /*
43 * (non-Javadoc)
44 *
45 * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#hasBCIRenumbering()
46 */
47 @Override
48 public boolean hasBCIRenumbering() {
49 return true;
50 }
51
52 /**
53 * 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
54 * 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.
55 *
56 * @param byteCodeOffsets List of Integer offsets of the byte code array
57 * @throws Pack200Exception Thrown from a subclass.
58 */
59 public void renumber(final List<Integer> byteCodeOffsets) throws Pack200Exception {
60 if (renumbered) {
61 throw new Error("Trying to renumber a line number table that has already been renumbered");
62 }
63 renumbered = true;
64 final int[] startPCs = getStartPCs();
65 Arrays.setAll(startPCs, i -> byteCodeOffsets.get(startPCs[i]).intValue());
66 }
67
68 @Override
69 public abstract String toString();
70
71 @Override
72 protected abstract void writeBody(DataOutputStream dos) throws IOException;
73
74 }