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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
package cuchaz.enigma.translation.mapping;
import com.google.common.collect.Sets;
import cuchaz.enigma.analysis.IndexTreeBuilder;
import cuchaz.enigma.analysis.MethodImplementationsTreeNode;
import cuchaz.enigma.analysis.MethodInheritanceTreeNode;
import cuchaz.enigma.analysis.index.BridgeMethodIndex;
import cuchaz.enigma.analysis.index.EntryIndex;
import cuchaz.enigma.analysis.index.InheritanceIndex;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.VoidTranslator;
import cuchaz.enigma.translation.representation.AccessFlags;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.Entry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;
public class IndexEntryResolver implements EntryResolver {
private final EntryIndex entryIndex;
private final InheritanceIndex inheritanceIndex;
private final BridgeMethodIndex bridgeMethodIndex;
private final IndexTreeBuilder treeBuilder;
public IndexEntryResolver(JarIndex index) {
this.entryIndex = index.getEntryIndex();
this.inheritanceIndex = index.getInheritanceIndex();
this.bridgeMethodIndex = index.getBridgeMethodIndex();
this.treeBuilder = new IndexTreeBuilder(index);
}
@Override
@SuppressWarnings("unchecked")
public <E extends Entry<?>> Collection<E> resolveEntry(E entry, ResolutionStrategy strategy) {
if (entry == null) {
return Collections.emptySet();
}
Entry<ClassEntry> classChild = getClassChild(entry);
if (classChild != null && !(classChild instanceof ClassEntry)) {
AccessFlags access = entryIndex.getEntryAccess(classChild);
// If we're looking for the closest and this entry exists, we're done looking
if (strategy == ResolutionStrategy.RESOLVE_CLOSEST && access != null) {
return Collections.singleton(entry);
}
if (access == null || !access.isPrivate()) {
Collection<Entry<ClassEntry>> resolvedChildren = resolveChildEntry(classChild, strategy);
if (!resolvedChildren.isEmpty()) {
return resolvedChildren.stream()
.map(resolvedChild -> (E) entry.replaceAncestor(classChild, resolvedChild))
.collect(Collectors.toList());
}
}
}
return Collections.singleton(entry);
}
@Nullable
private Entry<ClassEntry> getClassChild(Entry<?> entry) {
if (entry instanceof ClassEntry) {
return null;
}
// get the entry in the hierarchy that is the child of a class
List<Entry<?>> ancestry = entry.getAncestry();
for (int i = ancestry.size() - 1; i > 0; i--) {
Entry<?> child = ancestry.get(i);
Entry<ClassEntry> cast = child.castParent(ClassEntry.class);
if (cast != null && !(cast instanceof ClassEntry)) {
// we found the entry which is a child of a class, we are now able to resolve the owner of this entry
return cast;
}
}
return null;
}
private Set<Entry<ClassEntry>> resolveChildEntry(Entry<ClassEntry> entry, ResolutionStrategy strategy) {
ClassEntry ownerClass = entry.getParent();
if (entry instanceof MethodEntry) {
MethodEntry bridgeMethod = bridgeMethodIndex.getBridgeFromSpecialized((MethodEntry) entry);
if (bridgeMethod != null && ownerClass.equals(bridgeMethod.getParent())) {
Set<Entry<ClassEntry>> resolvedBridge = resolveChildEntry(bridgeMethod, strategy);
if (!resolvedBridge.isEmpty()) {
return resolvedBridge;
} else {
return Collections.singleton(bridgeMethod);
}
}
}
Set<Entry<ClassEntry>> resolvedEntries = new HashSet<>();
for (ClassEntry parentClass : inheritanceIndex.getParents(ownerClass)) {
Entry<ClassEntry> parentEntry = entry.withParent(parentClass);
if (strategy == ResolutionStrategy.RESOLVE_ROOT) {
resolvedEntries.addAll(resolveRoot(parentEntry, strategy));
} else {
resolvedEntries.addAll(resolveClosest(parentEntry, strategy));
}
}
return resolvedEntries;
}
private Collection<Entry<ClassEntry>> resolveRoot(Entry<ClassEntry> entry, ResolutionStrategy strategy) {
// When resolving root, we want to first look for the lowest entry before returning ourselves
Set<Entry<ClassEntry>> parentResolution = resolveChildEntry(entry, strategy);
if (parentResolution.isEmpty()) {
AccessFlags parentAccess = entryIndex.getEntryAccess(entry);
if (parentAccess != null && !parentAccess.isPrivate()) {
return Collections.singleton(entry);
}
}
return parentResolution;
}
private Collection<Entry<ClassEntry>> resolveClosest(Entry<ClassEntry> entry, ResolutionStrategy strategy) {
// When resolving closest, we want to first check if we exist before looking further down
AccessFlags parentAccess = entryIndex.getEntryAccess(entry);
if (parentAccess != null && !parentAccess.isPrivate()) {
return Collections.singleton(entry);
} else {
return resolveChildEntry(entry, strategy);
}
}
@Override
public Set<Entry<?>> resolveEquivalentEntries(Entry<?> entry) {
MethodEntry relevantMethod = entry.findAncestor(MethodEntry.class);
if (relevantMethod == null || !entryIndex.hasMethod(relevantMethod)) {
return Collections.singleton(entry);
}
Set<MethodEntry> equivalentMethods = resolveEquivalentMethods(relevantMethod);
Set<Entry<?>> equivalentEntries = new HashSet<>(equivalentMethods.size());
for (MethodEntry equivalentMethod : equivalentMethods) {
Entry<?> equivalentEntry = entry.replaceAncestor(relevantMethod, equivalentMethod);
equivalentEntries.add(equivalentEntry);
}
return equivalentEntries;
}
@Override
public Set<MethodEntry> resolveEquivalentMethods(MethodEntry methodEntry) {
AccessFlags access = entryIndex.getMethodAccess(methodEntry);
if (access == null) {
throw new IllegalArgumentException("Could not find method " + methodEntry);
}
if (!canInherit(methodEntry, access)) {
return Collections.singleton(methodEntry);
}
Set<MethodEntry> methodEntries = Sets.newHashSet();
resolveEquivalentMethods(methodEntries, treeBuilder.buildMethodInheritance(VoidTranslator.INSTANCE, methodEntry));
return methodEntries;
}
private void resolveEquivalentMethods(Set<MethodEntry> methodEntries, MethodInheritanceTreeNode node) {
MethodEntry methodEntry = node.getMethodEntry();
if (methodEntries.contains(methodEntry)) {
return;
}
AccessFlags flags = entryIndex.getMethodAccess(methodEntry);
if (flags != null && canInherit(methodEntry, flags)) {
// collect the entry
methodEntries.add(methodEntry);
}
// look at bridge methods!
MethodEntry bridgedMethod = bridgeMethodIndex.getBridgeFromSpecialized(methodEntry);
while (bridgedMethod != null) {
methodEntries.addAll(resolveEquivalentMethods(bridgedMethod));
bridgedMethod = bridgeMethodIndex.getBridgeFromSpecialized(bridgedMethod);
}
// look at interface methods too
for (MethodImplementationsTreeNode implementationsNode : treeBuilder.buildMethodImplementations(VoidTranslator.INSTANCE, methodEntry)) {
resolveEquivalentMethods(methodEntries, implementationsNode);
}
// recurse
for (int i = 0; i < node.getChildCount(); i++) {
resolveEquivalentMethods(methodEntries, (MethodInheritanceTreeNode) node.getChildAt(i));
}
}
private void resolveEquivalentMethods(Set<MethodEntry> methodEntries, MethodImplementationsTreeNode node) {
MethodEntry methodEntry = node.getMethodEntry();
AccessFlags flags = entryIndex.getMethodAccess(methodEntry);
if (flags != null && !flags.isPrivate() && !flags.isStatic()) {
// collect the entry
methodEntries.add(methodEntry);
}
// look at bridge methods!
MethodEntry bridgedMethod = bridgeMethodIndex.getBridgeFromSpecialized(methodEntry);
while (bridgedMethod != null) {
methodEntries.addAll(resolveEquivalentMethods(bridgedMethod));
bridgedMethod = bridgeMethodIndex.getBridgeFromSpecialized(bridgedMethod);
}
// recurse
for (int i = 0; i < node.getChildCount(); i++) {
resolveEquivalentMethods(methodEntries, (MethodImplementationsTreeNode) node.getChildAt(i));
}
}
private boolean canInherit(MethodEntry entry, AccessFlags access) {
return !entry.isConstructor() && !access.isPrivate() && !access.isStatic() && !access.isFinal();
}
}
|