-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathTinyRemapper.java
More file actions
814 lines (665 loc) · 25 KB
/
TinyRemapper.java
File metadata and controls
814 lines (665 loc) · 25 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
* Copyright (C) 2016, 2018 Player, asie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.fabricmc.tinyremapper;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.util.CheckClassAdapter;
public class TinyRemapper {
public static class Builder {
private Builder() { }
public Builder withMappings(IMappingProvider provider) {
mappingProviders.add(provider);
return this;
}
public Builder threads(int threadCount) {
this.threadCount = threadCount;
return this;
}
public Builder withForcedPropagation(Set<String> entries) {
forcePropagation.addAll(entries);
return this;
}
public Builder propagatePrivate(boolean value) {
propagatePrivate = value;
return this;
}
public Builder removeFrames(boolean value) {
removeFrames = value;
return this;
}
public Builder ignoreConflicts(boolean value) {
ignoreConflicts = value;
return this;
}
public Builder resolveMissing(boolean value) {
resolveMissing = value;
return this;
}
public Builder rebuildSourceFilenames(boolean value) {
rebuildSourceFilenames = value;
return this;
}
public Builder renameInvalidLocals(boolean value) {
renameInvalidLocals = value;
return this;
}
public TinyRemapper build() {
TinyRemapper remapper = new TinyRemapper(threadCount, forcePropagation, propagatePrivate, removeFrames, ignoreConflicts, resolveMissing, rebuildSourceFilenames, renameInvalidLocals);
for (IMappingProvider provider : mappingProviders) {
provider.load(remapper.classMap, remapper.fieldMap, remapper.methodMap);
}
return remapper;
}
private int threadCount;
private final Set<String> forcePropagation = new HashSet<>();
private final Set<IMappingProvider> mappingProviders = new HashSet<>();
private boolean propagatePrivate = false;
private boolean removeFrames = false;
private boolean ignoreConflicts = false;
private boolean resolveMissing = false;
private boolean rebuildSourceFilenames = false;
private boolean renameInvalidLocals = false;
}
private TinyRemapper(int threadCount, Set<String> forcePropagation,
boolean propagatePrivate,
boolean removeFrames,
boolean ignoreConflicts,
boolean resolveMissing,
boolean rebuildSourceFilenames,
boolean renameInvalidLocals) {
this.threadCount = threadCount > 0 ? threadCount : Math.max(Runtime.getRuntime().availableProcessors(), 2);
this.threadPool = Executors.newFixedThreadPool(this.threadCount);
this.forcePropagation = forcePropagation;
this.propagatePrivate = propagatePrivate;
this.removeFrames = removeFrames;
this.ignoreConflicts = ignoreConflicts;
this.resolveMissing = resolveMissing;
this.rebuildSourceFilenames = rebuildSourceFilenames;
this.renameInvalidLocals = renameInvalidLocals;
}
public static Builder newRemapper() {
return new Builder();
}
public void finish() {
threadPool.shutdown();
try {
threadPool.awaitTermination(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void read(final Path... inputs) {
List<Future<List<TinyRemapper.RClass>>> futures = new ArrayList<>();
List<FileSystem> fsToClose = Collections.synchronizedList(new ArrayList<>());
try {
for (Path input : inputs) {
futures.addAll(read(input, TinyRemapper.Namespace.Unknown, true, fsToClose));
}
if (futures.size() > 0) {
dirty = true;
}
for (Future<List<TinyRemapper.RClass> > future : futures) {
for (TinyRemapper.RClass node : future.get()) {
nodes.put(node.name, node);
}
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
} finally {
for (FileSystem fs : fsToClose) {
try {
fs.close();
} catch (IOException e) { }
}
}
}
private List<Future<List<RClass>>> read(final Path file, final Namespace namespace, boolean saveData, final List<FileSystem> fsToClose) {
try {
return read(file, namespace, file, saveData, fsToClose);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private List<Future<List<RClass>>> read(final Path file, final Namespace namespace, final Path srcPath, final boolean saveData, final List<FileSystem> fsToClose) throws IOException {
List<Future<List<RClass>>> ret = new ArrayList<>();
Files.walkFileTree(file, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String name = file.getFileName().toString();
if (name.endsWith(".jar") ||
name.endsWith(".zip") ||
name.endsWith(".class")) {
ret.add(threadPool.submit(new Callable<List<RClass>>() {
@Override
public List<RClass> call() {
try {
return readFile(file, namespace, srcPath, saveData, fsToClose);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (IOException e) {
System.out.println(file.toAbsolutePath());
e.printStackTrace();
return Collections.emptyList();
}
}
}));
}
return FileVisitResult.CONTINUE;
}
});
return ret;
}
private List<RClass> readFile(Path file, Namespace namespace, final Path srcPath, boolean saveData, List<FileSystem> fsToClose) throws IOException, URISyntaxException {
List<RClass> ret = new ArrayList<RClass>();
if (file.toString().endsWith(".class")) {
ret.add(analyze(srcPath, Files.readAllBytes(file), saveData));
} else {
URI uri = new URI("jar:"+file.toUri().toString());
FileSystem fs = null;
try {
fs = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
}
if (fs == null) {
Map<String, Object> env = new HashMap<>();
if(System.getProperty("tiny_use_zipcache","FALSE").equals("TRUE")){
env.put("create", "true");
env.put("useTempFile", Boolean.TRUE);
}
fs = FileSystems.newFileSystem(uri, env);
fsToClose.add(fs);
}
Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".class")) {
ret.add(analyze(srcPath, Files.readAllBytes(file), saveData));
}
return FileVisitResult.CONTINUE;
}
});
}
return ret;
}
private RClass analyze(Path srcPath, byte[] data, boolean saveData) {
final RClass ret = new RClass(srcPath, saveData ? data : null);
ClassReader reader = new ClassReader(data);
reader.accept(new ClassVisitor(Opcodes.ASM7) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
ret.name = mapClass(name);
ret.superName = mapClass(superName);
ret.isInterface = (access & Opcodes.ACC_INTERFACE) != 0;
ret.interfaces = new String[interfaces.length];
for (int i = 0; i < interfaces.length; i++) ret.interfaces[i] = mapClass(interfaces[i]);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
ret.methods.put(name + desc, new Member(name, desc, access));
return null;
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
ret.fields.put(name + ";;" + desc, new Member(name, desc, access));
return null;
}
}, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
return ret;
}
String mapClass(String className) {
String ret = classMap.get(className);
return ret != null ? ret : className;
}
private void merge() {
for (RClass node : nodes.values()) {
assert node.superName != null;
RClass parent = nodes.get(node.superName);
if (parent != null) {
node.parents.add(parent);
parent.children.add(node);
}
for (String iface : node.interfaces) {
parent = nodes.get(iface);
if (parent != null) {
node.parents.add(parent);
parent.children.add(node);
}
}
}
}
private void propagate() {
List<Future<?>> futures = new ArrayList<>();
List<Map.Entry<String, String>> tasks = new ArrayList<>();
int maxTasks = methodMap.size() / threadCount / 4;
for (Map.Entry<String, String> entry : methodMap.entrySet()) {
tasks.add(entry);
if (tasks.size() >= maxTasks) {
futures.add(threadPool.submit(new Propagation(MemberType.METHOD, tasks)));
tasks.clear();
}
}
futures.add(threadPool.submit(new Propagation(MemberType.METHOD, tasks)));
tasks.clear();
for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
tasks.add(entry);
if (tasks.size() >= maxTasks) {
futures.add(threadPool.submit(new Propagation(MemberType.FIELD, tasks)));
tasks.clear();
}
}
futures.add(threadPool.submit(new Propagation(MemberType.FIELD, tasks)));
tasks.clear();
waitForAll(futures);
handleConflicts();
}
private void handleConflicts() {
if (conflicts.isEmpty()) return;
System.out.println("Mapping conflicts detected:");
boolean unfixableConflicts = false;
for (Map.Entry<TypedMember, Set<String>> entry : conflicts.entrySet()) {
TypedMember tmember = entry.getKey();
Member member = tmember.cls.getMember(tmember.type, tmember.id);
String newName = (tmember.type == MemberType.METHOD ? tmember.cls.methodsToMap : tmember.cls.fieldsToMap).get(tmember.id);
Set<String> names = entry.getValue();
names.add(tmember.cls.name+"/"+newName);
System.out.printf(" %s %s %s (%s) -> %s%n", tmember.cls.name, tmember.type.name(), member.name, member.desc, names);
if (ignoreConflicts) {
Map<String, String> mappings = tmember.type == MemberType.METHOD ? methodMap : fieldMap;
String mappingName = mappings.get(tmember.cls.name+"/"+tmember.id);
if (mappingName == null) { // no direct mapping match, try parents
Queue<RClass> queue = new ArrayDeque<>(tmember.cls.parents);
RClass cls;
while ((cls = queue.poll()) != null) {
mappingName = mappings.get(cls.name+"/"+tmember.id);
if (mappingName != null) break;
queue.addAll(cls.parents);
}
}
if (mappingName == null) {
unfixableConflicts = true;
} else {
mappingName = stripClassName(mappingName, tmember.type);
ConcurrentMap<String, String> outputMap = (tmember.type == MemberType.METHOD) ? tmember.cls.methodsToMap : tmember.cls.fieldsToMap;
outputMap.put(tmember.id, mappingName);
System.out.println(" fixable: replaced with "+mappingName);
}
}
}
if (!ignoreConflicts || unfixableConflicts) {
if (ignoreConflicts) System.out.println("There were unfixable conflicts.");
System.exit(1);
}
}
public void apply(Path srcPath, final BiConsumer<String, byte[]> outputConsumer) {
if (dirty) {
merge();
propagate();
dirty = false;
}
List<Future<?>> futures = new ArrayList<>();
for (final RClass cls : nodes.values()) {
if (cls.srcPath != srcPath) continue; // TODO: use a more elegant way to filter files to process (whether they were an input)
futures.add(threadPool.submit(() -> outputConsumer.accept(cls.name, apply(cls))));
}
waitForAll(futures);
}
private byte[] apply(final RClass cls) {
ClassReader reader = new ClassReader(cls.data);
ClassWriter writer = new ClassWriter(0);
int flags = removeFrames ? ClassReader.SKIP_FRAMES : ClassReader.EXPAND_FRAMES;
ClassVisitor visitor = writer;
if (rebuildSourceFilenames) {
visitor = new SourceNameRebuildVisitor(Opcodes.ASM7, visitor);
}
if (check) {
//noinspection UnusedAssignment
visitor = new CheckClassAdapter(visitor);
}
reader.accept(new AsmClassRemapper(visitor, remapper, renameInvalidLocals), flags);
// TODO: compute frames (-Xverify:all -XX:-FailOverToOldVerifier)
return writer.toByteArray();
}
private void waitForAll(Iterable<Future<?>> futures) {
try {
for (Future<?> future : futures) {
future.get();
}
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
String getClassName(String nameDesc, MemberType type) {
int descStart = getDescStart(nameDesc, type);
int nameStart = nameDesc.lastIndexOf('/', descStart - 1);
if (nameStart == -1) nameStart = 0;
return nameDesc.substring(0, nameStart);
}
String stripClassName(String nameDesc, MemberType type) {
int descStart = getDescStart(nameDesc, type);
int nameStart = nameDesc.lastIndexOf('/', descStart - 1);
if (nameStart == -1) nameStart = 0;
return nameDesc.substring(nameStart + 1);
}
String stripDesc(String nameDesc, MemberType type) {
return nameDesc.substring(0, getDescStart(nameDesc, type));
}
private int getDescStart(String nameDesc, MemberType type) {
int ret;
if (type == MemberType.METHOD) {
ret = nameDesc.indexOf('(');
} else {
ret = nameDesc.indexOf(";;");
}
if (ret == -1) ret = nameDesc.length();
return ret;
}
enum Namespace {
Unknown
}
class RClass {
RClass(Path srcFile, byte[] data) {
this.srcPath = srcFile;
this.data = data;
}
Member getMember(MemberType type, String id) {
if (type == MemberType.METHOD) {
return methods.get(id);
} else {
return fields.get(id);
}
}
/**
* Rename the member src to dst and continue propagating in dir.
*
* @param type Member type.
* @param idSrc Existing name.
* @param idDst New name.
* @param dir Futher propagation direction.
*/
void propagate(MemberType type, String originatingCls, String idSrc, String idDst, Direction dir, boolean isVirtual, boolean first, Set<RClass> visitedUp, Set<RClass> visitedDown) {
/*
* initial private member or static method in interface: only local
* non-virtual: up to matching member (if not already in this), then down until matching again (exclusive)
* virtual: all across the hierarchy, only non-private|static can change direction - skip private|static in interfaces
*/
Member member = getMember(type, idSrc);
if (member != null) {
if (!first && !isVirtual) { // down propagation from non-virtual (static) member matching the signature again, which starts its own namespace
return;
}
if (first // directly mapped
|| (member.access & (Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) == 0 // not private and not static
|| propagatePrivate
|| !forcePropagation.isEmpty() && forcePropagation.contains(name.replace('/', '.')+"."+member.name)) { // don't rename private members unless forced or initial (=dir any)
ConcurrentMap<String, String> outputMap = (type == MemberType.METHOD) ? methodsToMap : fieldsToMap;
String prev = outputMap.putIfAbsent(idSrc, idDst);
if (prev != null && !prev.equals(idDst)) {
conflicts.computeIfAbsent(new TypedMember(this, type, idSrc), x -> Collections.newSetFromMap(new ConcurrentHashMap<>())).add(originatingCls+"/"+idDst);
}
//System.out.printf("%s: %s %s -> %s\n", name, (type == Type.METHOD) ? "Method" : "Field", idSrc, idDst);
}
if (first
&& ((member.access & Opcodes.ACC_PRIVATE) != 0 // private members don't propagate, but they may get skipped over by overriding virtual methods
|| type == MemberType.METHOD && isInterface && !isVirtual)) { // non-virtual interface methods don't propagate either, the jvm only resolves direct accesses to them
return;
}
} else { // member == null
assert !first && (type == MemberType.FIELD || !isInterface || isVirtual);
// Java likes/allows to access members in a super class by querying the "this"
// class directly. To cover this, outputMap is being populated regardless.
ConcurrentMap<String, String> outputMap = (type == MemberType.METHOD) ? methodsToMap : fieldsToMap;
outputMap.putIfAbsent(idSrc, idDst);
}
assert isVirtual || dir == Direction.DOWN;
/*
* Propagate the mapping along the hierarchy tree.
*
* The mapping ensures that overriding and shadowing behaviors remains the same.
*
* Direction.ANY is from where the current element was the initial node as specified
* in the mappings. The member == null + dir checks above already verified that the
* member exists in the current node.
*
* Direction.UP/DOWN handle propagation skipping across nodes which don't contain the
* specific member, thus having no direct reference.
*
* isVirtual && ... handles propagation to an existing matching virtual member, which
* spawns a new initial node from the propagation perspective. This is necessary as
* different branches of the hierarchy tree that were not visited before may access it.
*/
if (dir == Direction.ANY || dir == Direction.UP || isVirtual && member != null && (member.access & (Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) == 0) {
for (RClass node : parents) {
if (visitedUp.add(node)) {
node.propagate(type, originatingCls, idSrc, idDst, Direction.UP, isVirtual, false, visitedUp, visitedDown);
}
}
}
if (dir == Direction.ANY || dir == Direction.DOWN || isVirtual && member != null && (member.access & (Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) == 0) {
for (RClass node : children) {
if (visitedDown.add(node)) {
node.propagate(type, originatingCls, idSrc, idDst, Direction.DOWN, isVirtual, false, visitedUp, visitedDown);
}
}
}
}
RClass resolve(MemberType type, String id, Set<RClass> visited, Queue<RClass> queue) {
Member member = getMember(type, id);
if (member != null) return this;
queue.add(this);
visited.add(this);
RClass cls;
// step 1
// method: search in all super classes recursively
// field: search in all direct super interfaces recursively
while ((cls = queue.poll()) != null) {
for (RClass parent : cls.parents) {
if (parent.isInterface == (type == MemberType.FIELD) && visited.add(parent)) {
if (parent.getMember(type, id) != null) return parent;
queue.add(parent);
}
}
}
visited.clear();
queue.add(this);
visited.add(this);
RClass secondaryMatch = null;
// step 2
// method: search for non-static, non-private, non-abstract in all super interfaces recursively
// (breadth first search to obtain the potentially maximally-specific superinterface directly)
// field: search in all super classes recursively
// step 3
// method: search for non-static, non-private in all super interfaces recursively
// step 3 is a super set of step 2 with any option being able to be "arbitrarily chosen" as per the jvm
// spec, so step 2 ignoring the "exactly one" match requirement doesn't matter and >potentially<
// maximally-specific superinterface is good enough
while ((cls = queue.poll()) != null) {
for (RClass parent : cls.parents) {
if (parent.isInterface != (type == MemberType.FIELD) && visited.add(parent)) {
Member parentMember = parent.getMember(type, id);
if (parentMember != null
&& (type == MemberType.FIELD || (parentMember.access & (Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) == 0)) {
if (type == MemberType.METHOD && (parentMember.access & (Opcodes.ACC_ABSTRACT)) != 0) {
secondaryMatch = parent;
} else {
return parent;
}
}
queue.add(parent);
}
}
}
return secondaryMatch;
}
@Override
public String toString() {
return name;
}
private final Path srcPath;
private final byte[] data;
private final Map<String, Member> methods = new HashMap<String, Member>();
private final Map<String, Member> fields = new HashMap<String, Member>();
private final Set<RClass> parents = new HashSet<RClass>();
private final Set<RClass> children = new HashSet<RClass>();
final ConcurrentMap<String, String> methodsToMap = new ConcurrentHashMap<String, String>();
final ConcurrentMap<String, String> fieldsToMap = new ConcurrentHashMap<String, String>();
String name;
String superName;
boolean isInterface;
String[] interfaces;
}
class Member {
Member(String name, String desc, int access) {
this.name = name;
this.desc = desc;
this.access = access;
}
String name;
String desc;
int access;
}
enum Direction {
ANY,
UP,
DOWN
}
enum MemberType {
METHOD,
FIELD
}
class TypedMember {
public TypedMember(RClass cls, MemberType type, String id) {
this.cls = cls;
this.type = type;
this.id = id;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TypedMember)) return false;
TypedMember o = (TypedMember) obj;
return cls == o.cls &&
type == o.type &&
id.equals(o.id);
}
@Override
public int hashCode() {
return (cls.hashCode() * 31 + type.hashCode()) * 31 + id.hashCode();
}
final RClass cls;
final MemberType type;
final String id;
}
class Propagation implements Runnable {
Propagation(MemberType type, List<Map.Entry<String, String> > tasks) {
this.type = type;
this.tasks.addAll(tasks);
}
@Override
public void run() {
Set<RClass> visitedUp = Collections.newSetFromMap(new IdentityHashMap<>());
Set<RClass> visitedDown = Collections.newSetFromMap(new IdentityHashMap<>());
Queue<RClass> queue = new ArrayDeque<>();
for (Map.Entry<String, String> entry : tasks) {
String className = getClassName(entry.getValue(), type);
RClass node = nodes.get(className);
if (node == null) continue; // not available for this Side
String idSrc = stripClassName(entry.getKey(), type);
String idDst = stripClassName(entry.getValue(), type);
if (idSrc.equals(idDst)) continue; // no name change
Member member = node.getMember(type, idSrc);
if (member == null) {
if (resolveMissing) {
// resolve real owner in case the mapping doesn't reference the actual class
node = node.resolve(type, idSrc, visitedUp, queue);
visitedUp.clear();
queue.clear();
// at this point node is null or the member must exist in node
if (node == null) {
// not available for this Side
//System.out.println("Unknown "+(type == MemberType.METHOD ? "method" : "field")+" referenced: "+entry.getKey());
continue;
} else {
member = node.getMember(type, idSrc);
assert member != null;
}
} else {
// not available for this Side
continue;
}
}
boolean isVirtual = type == MemberType.METHOD && (member.access & (Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) == 0;
visitedUp.add(node);
visitedDown.add(node);
node.propagate(type, className, idSrc, idDst, isVirtual ? Direction.ANY : Direction.DOWN, isVirtual, true, visitedUp, visitedDown);
visitedUp.clear();
visitedDown.clear();
}
}
private final MemberType type;
private final List<Map.Entry<String, String> > tasks = new ArrayList<Map.Entry<String,String> >();
}
private final boolean check = false;
private final Set<String> forcePropagation;
private final boolean propagatePrivate;
private final boolean removeFrames;
private final boolean ignoreConflicts;
private final boolean resolveMissing;
private final boolean rebuildSourceFilenames;
private final boolean renameInvalidLocals;
final Map<String, String> classMap = new HashMap<>();
final Map<String, String> methodMap = new HashMap<>();
final Map<String, String> fieldMap = new HashMap<>();
final Map<String, RClass> nodes = new HashMap<>();
private final Map<TypedMember, Set<String>> conflicts = new ConcurrentHashMap<>();
private final int threadCount;
private final ExecutorService threadPool;
private final AsmRemapper remapper = new AsmRemapper(this);
private boolean dirty;
}