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
|
/*******************************************************************************
* 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.analysis.index;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.analysis.ClassCache;
import cuchaz.enigma.analysis.ReferenceTargetType;
import cuchaz.enigma.translation.mapping.EntryResolver;
import cuchaz.enigma.translation.mapping.IndexEntryResolver;
import cuchaz.enigma.translation.representation.Lambda;
import cuchaz.enigma.translation.representation.entry.*;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.utils.Utils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import java.util.Arrays;
import java.util.Collection;
public class JarIndex implements JarIndexer {
private final EntryIndex entryIndex;
private final InheritanceIndex inheritanceIndex;
private final ReferenceIndex referenceIndex;
private final BridgeMethodIndex bridgeMethodIndex;
private final PackageVisibilityIndex packageVisibilityIndex;
private final EntryResolver entryResolver;
private final Collection<JarIndexer> indexers;
private final Multimap<String, MethodDefEntry> methodImplementations = HashMultimap.create();
public JarIndex(EntryIndex entryIndex, InheritanceIndex inheritanceIndex, ReferenceIndex referenceIndex, BridgeMethodIndex bridgeMethodIndex, PackageVisibilityIndex packageVisibilityIndex) {
this.entryIndex = entryIndex;
this.inheritanceIndex = inheritanceIndex;
this.referenceIndex = referenceIndex;
this.bridgeMethodIndex = bridgeMethodIndex;
this.packageVisibilityIndex = packageVisibilityIndex;
this.indexers = Arrays.asList(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex, packageVisibilityIndex);
this.entryResolver = new IndexEntryResolver(this);
}
public static JarIndex empty() {
EntryIndex entryIndex = new EntryIndex();
InheritanceIndex inheritanceIndex = new InheritanceIndex(entryIndex);
ReferenceIndex referenceIndex = new ReferenceIndex();
BridgeMethodIndex bridgeMethodIndex = new BridgeMethodIndex(entryIndex, inheritanceIndex, referenceIndex);
PackageVisibilityIndex packageVisibilityIndex = new PackageVisibilityIndex();
return new JarIndex(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex, packageVisibilityIndex);
}
public void indexJar(ClassCache classCache, ProgressListener progress) {
progress.init(4, I18n.translate("progress.jar.indexing"));
progress.step(1, I18n.translate("progress.jar.indexing.entries"));
classCache.visit(() -> new IndexClassVisitor(this, Utils.ASM_VERSION), ClassReader.SKIP_CODE);
progress.step(2, I18n.translate("progress.jar.indexing.references"));
classCache.visit(() -> new IndexReferenceVisitor(this, entryIndex, inheritanceIndex, Utils.ASM_VERSION), 0);
progress.step(3, I18n.translate("progress.jar.indexing.methods"));
bridgeMethodIndex.findBridgeMethods();
progress.step(4, I18n.translate("progress.jar.indexing.process"));
processIndex(this);
}
@Override
public void processIndex(JarIndex index) {
indexers.forEach(indexer -> indexer.processIndex(index));
}
@Override
public void indexClass(ClassDefEntry classEntry) {
if (classEntry.isJre()) {
return;
}
for (ClassEntry interfaceEntry : classEntry.getInterfaces()) {
if (classEntry.equals(interfaceEntry)) {
throw new IllegalArgumentException("Class cannot be its own interface! " + classEntry);
}
}
indexers.forEach(indexer -> indexer.indexClass(classEntry));
}
@Override
public void indexField(FieldDefEntry fieldEntry) {
if (fieldEntry.getParent().isJre()) {
return;
}
indexers.forEach(indexer -> indexer.indexField(fieldEntry));
}
@Override
public void indexMethod(MethodDefEntry methodEntry) {
if (methodEntry.getParent().isJre()) {
return;
}
indexers.forEach(indexer -> indexer.indexMethod(methodEntry));
if (!methodEntry.isConstructor()) {
methodImplementations.put(methodEntry.getParent().getFullName(), methodEntry);
}
}
@Override
public void indexMethodReference(MethodDefEntry callerEntry, MethodEntry referencedEntry, ReferenceTargetType targetType) {
if (callerEntry.getParent().isJre()) {
return;
}
indexers.forEach(indexer -> indexer.indexMethodReference(callerEntry, referencedEntry, targetType));
}
@Override
public void indexFieldReference(MethodDefEntry callerEntry, FieldEntry referencedEntry, ReferenceTargetType targetType) {
if (callerEntry.getParent().isJre()) {
return;
}
indexers.forEach(indexer -> indexer.indexFieldReference(callerEntry, referencedEntry, targetType));
}
@Override
public void indexLambda(MethodDefEntry callerEntry, Lambda lambda, ReferenceTargetType targetType) {
if (callerEntry.getParent().isJre()) {
return;
}
indexers.forEach(indexer -> indexer.indexLambda(callerEntry, lambda, targetType));
}
public EntryIndex getEntryIndex() {
return entryIndex;
}
public InheritanceIndex getInheritanceIndex() {
return this.inheritanceIndex;
}
public ReferenceIndex getReferenceIndex() {
return referenceIndex;
}
public BridgeMethodIndex getBridgeMethodIndex() {
return bridgeMethodIndex;
}
public PackageVisibilityIndex getPackageVisibilityIndex() {
return packageVisibilityIndex;
}
public EntryResolver getEntryResolver() {
return entryResolver;
}
}
|