blob: c47a77030cf3a89d9e60b0c0326179474d8dbf80 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.bytecode;
import java.util.Iterator;
import cuchaz.enigma.bytecode.CheckCastIterator.CheckCast;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.MethodEntry;
import cuchaz.enigma.mapping.Signature;
import javassist.bytecode.*;
public class CheckCastIterator implements Iterator<CheckCast> {
public static class CheckCast {
public final String className;
public final MethodEntry prevMethodEntry;
public CheckCast(String className, MethodEntry prevMethodEntry) {
this.className = className;
this.prevMethodEntry = prevMethodEntry;
}
}
private final ConstPool constants;
private final CodeAttribute attribute;
private final CodeIterator iter;
private CheckCast next;
public CheckCastIterator(CodeAttribute codeAttribute) throws BadBytecode {
this.constants = codeAttribute.getConstPool();
this.attribute = codeAttribute;
this.iter = this.attribute.iterator();
this.next = getNext();
}
@Override
public boolean hasNext() {
return this.next != null;
}
@Override
public CheckCast next() {
CheckCast out = this.next;
try {
this.next = getNext();
} catch (BadBytecode ex) {
throw new Error(ex);
}
return out;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
private CheckCast getNext() throws BadBytecode {
int prevPos = 0;
while (this.iter.hasNext()) {
int pos = this.iter.next();
int opcode = this.iter.byteAt(pos);
switch (opcode) {
case Opcode.CHECKCAST:
// get the type of this op code (next two bytes are a classinfo index)
MethodEntry prevMethodEntry = getMethodEntry(prevPos);
if (prevMethodEntry != null) {
return new CheckCast(this.constants.getClassInfo(this.iter.s16bitAt(pos + 1)), prevMethodEntry);
}
break;
}
prevPos = pos;
}
return null;
}
private MethodEntry getMethodEntry(int pos) {
switch (this.iter.byteAt(pos)) {
case Opcode.INVOKEVIRTUAL:
case Opcode.INVOKESTATIC:
case Opcode.INVOKEDYNAMIC:
case Opcode.INVOKESPECIAL: {
int index = this.iter.s16bitAt(pos + 1);
return new MethodEntry(
new ClassEntry(Descriptor.toJvmName(this.constants.getMethodrefClassName(index))),
this.constants.getMethodrefName(index),
new Signature(this.constants.getMethodrefType(index))
);
}
case Opcode.INVOKEINTERFACE: {
int index = this.iter.s16bitAt(pos + 1);
return new MethodEntry(
new ClassEntry(Descriptor.toJvmName(this.constants.getInterfaceMethodrefClassName(index))),
this.constants.getInterfaceMethodrefName(index),
new Signature(this.constants.getInterfaceMethodrefType(index))
);
}
}
return null;
}
}
|