blob: 7ed5d7fbb3d28f9daefc9825b203df67854cf3c8 (
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.bytecode;
import java.util.Iterator;
import javassist.bytecode.BadBytecode;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.Opcode;
import cuchaz.enigma.bytecode.CheckCastIterator.CheckCast;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.MethodEntry;
public class CheckCastIterator implements Iterator<CheckCast>
{
public static class CheckCast
{
public String className;
public MethodEntry prevMethodEntry;
public CheckCast( String className, MethodEntry prevMethodEntry )
{
this.className = className;
this.prevMethodEntry = prevMethodEntry;
}
}
private ConstPool m_constants;
private CodeAttribute m_attribute;
private CodeIterator m_iter;
private CheckCast m_next;
public CheckCastIterator( CodeAttribute codeAttribute )
throws BadBytecode
{
m_constants = codeAttribute.getConstPool();
m_attribute = codeAttribute;
m_iter = m_attribute.iterator();
m_next = getNext();
}
@Override
public boolean hasNext( )
{
return m_next != null;
}
@Override
public CheckCast next( )
{
CheckCast out = m_next;
try
{
m_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( m_iter.hasNext() )
{
int pos = m_iter.next();
int opcode = m_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(
m_constants.getClassInfo( m_iter.s16bitAt( pos + 1 ) ),
prevMethodEntry
);
}
break;
}
prevPos = pos;
}
return null;
}
private MethodEntry getMethodEntry( int pos )
{
switch( m_iter.byteAt( pos ) )
{
case Opcode.INVOKEVIRTUAL:
case Opcode.INVOKESTATIC:
case Opcode.INVOKEDYNAMIC:
case Opcode.INVOKESPECIAL:
{
int index = m_iter.s16bitAt( pos + 1 );
return new MethodEntry(
new ClassEntry( Descriptor.toJvmName( m_constants.getMethodrefClassName( index ) ) ),
m_constants.getMethodrefName( index ),
m_constants.getMethodrefType( index )
);
}
case Opcode.INVOKEINTERFACE:
{
int index = m_iter.s16bitAt( pos + 1 );
return new MethodEntry(
new ClassEntry( Descriptor.toJvmName( m_constants.getInterfaceMethodrefClassName( index ) ) ),
m_constants.getInterfaceMethodrefName( index ),
m_constants.getInterfaceMethodrefType( index )
);
}
}
return null;
}
public Iterable<CheckCast> casts( )
{
return new Iterable<CheckCast>( )
{
@Override
public Iterator<CheckCast> iterator( )
{
return CheckCastIterator.this;
}
};
}
}
|