1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.harmony.unpack200.bytecode.forms;
18
19 import java.util.Arrays;
20
21 import org.apache.commons.compress.harmony.unpack200.bytecode.ByteCode;
22 import org.apache.commons.compress.harmony.unpack200.bytecode.OperandManager;
23
24 public class TableSwitchForm extends SwitchForm {
25
26 public TableSwitchForm(final int opcode, final String name) {
27 super(opcode, name);
28 }
29
30
31
32
33
34
35
36 @Override
37 public void setByteCodeOperands(final ByteCode byteCode, final OperandManager operandManager, final int codeLength) {
38 final int caseCount = operandManager.nextCaseCount();
39 final int defaultPc = operandManager.nextLabel();
40 int caseValue = -1;
41 caseValue = operandManager.nextCaseValues();
42
43 final int[] casePcs = new int[caseCount];
44 Arrays.setAll(casePcs, i -> operandManager.nextLabel());
45
46 final int[] labelsArray = new int[caseCount + 1];
47 labelsArray[0] = defaultPc;
48 System.arraycopy(casePcs, 0, labelsArray, 1, caseCount + 1 - 1);
49 byteCode.setByteCodeTargets(labelsArray);
50
51 final int lowValue = caseValue;
52 final int highValue = lowValue + caseCount - 1;
53
54
55
56
57
58
59
60
61
62
63
64
65 final int padLength = 3 - codeLength % 4;
66 final int rewriteSize = 1 + padLength + 4
67 + 4
68 + 4
69 + 4 * casePcs.length;
70
71 final int[] newRewrite = new int[rewriteSize];
72 int rewriteIndex = 0;
73
74
75
76 newRewrite[rewriteIndex++] = byteCode.getOpcode();
77
78
79 for (int index = 0; index < padLength; index++) {
80 newRewrite[rewriteIndex++] = 0;
81 }
82
83
84
85 newRewrite[rewriteIndex++] = -1;
86 newRewrite[rewriteIndex++] = -1;
87 newRewrite[rewriteIndex++] = -1;
88 newRewrite[rewriteIndex++] = -1;
89
90
91 final int lowbyteIndex = rewriteIndex;
92 setRewrite4Bytes(lowValue, lowbyteIndex, newRewrite);
93 rewriteIndex += 4;
94
95
96 final int highbyteIndex = rewriteIndex;
97 setRewrite4Bytes(highValue, highbyteIndex, newRewrite);
98 rewriteIndex += 4;
99
100
101
102 for (int index = 0; index < caseCount; index++) {
103
104 newRewrite[rewriteIndex++] = -1;
105 newRewrite[rewriteIndex++] = -1;
106 newRewrite[rewriteIndex++] = -1;
107 newRewrite[rewriteIndex++] = -1;
108 }
109 byteCode.setRewrite(newRewrite);
110 }
111 }