-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathTinyRemapper.java
More file actions
1339 lines (1078 loc) · 43.3 KB
/
TinyRemapper.java
File metadata and controls
1339 lines (1078 loc) · 43.3 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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016, 2018, Player, asie
* Copyright (c) 2016, 2021, FabricMC
*
* 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.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.Collection;
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.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
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.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.zip.ZipError;
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.commons.Remapper;
import org.objectweb.asm.util.CheckClassAdapter;
import net.fabricmc.tinyremapper.IMappingProvider.MappingAcceptor;
import net.fabricmc.tinyremapper.IMappingProvider.Member;
import net.fabricmc.tinyremapper.api.TrClass;
import net.fabricmc.tinyremapper.api.TrEnvironment;
import net.fabricmc.tinyremapper.api.TrMember;
import net.fabricmc.tinyremapper.api.TrMember.MemberType;
public class TinyRemapper implements AutoCloseable {
public static class Builder {
private Builder() { }
public Builder withMappings(IMappingProvider provider) {
mappingProviders.add(provider);
return this;
}
public Builder ignoreFieldDesc(boolean value) {
this.ignoreFieldDesc = value;
return this;
}
public Builder threads(int threadCount) {
this.threadCount = threadCount;
return this;
}
/**
* Keep the input data after consuming it for apply(), allows multiple apply invocations() even without input tag use.
*/
public Builder keepInputData(boolean value) {
this.keepInputData = value;
return this;
}
public Builder withForcedPropagation(Set<String> entries) {
forcePropagation.addAll(entries);
return this;
}
public Builder propagatePrivate(boolean value) {
propagatePrivate = value;
return this;
}
public Builder propagateBridges(LinkedMethodPropagation value) {
propagateBridges = value;
return this;
}
public Builder propagateRecordComponents(LinkedMethodPropagation value) {
propagateRecordComponents = 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 checkPackageAccess(boolean value) {
checkPackageAccess = value;
return this;
}
public Builder fixPackageAccess(boolean value) {
fixPackageAccess = value;
return this;
}
public Builder rebuildSourceFilenames(boolean value) {
rebuildSourceFilenames = value;
return this;
}
public Builder skipLocalVariableMapping(boolean value) {
skipLocalMapping = value;
return this;
}
public Builder renameInvalidLocals(boolean value) {
renameInvalidLocals = value;
return this;
}
@Deprecated
public Builder extraAnalyzeVisitor(ClassVisitor visitor) {
return extraAnalyzeVisitor((mrjVersion, className, next) -> {
if (next != null) throw new UnsupportedOperationException("can't chain fixed instance analyze visitors");
return visitor;
});
}
public Builder extraAnalyzeVisitor(AnalyzeVisitorProvider provider) {
analyzeVisitors.add(provider);
return this;
}
public Builder extraStateProcessor(StateProcessor processor) {
stateProcessors.add(processor);
return this;
}
public Builder extraRemapper(Remapper remapper) {
extraRemapper = remapper;
return this;
}
public Builder extraPreApplyVisitor(ApplyVisitorProvider provider) {
preApplyVisitors.add(provider);
return this;
}
public Builder extraPostApplyVisitor(ApplyVisitorProvider provider) {
this.postApplyVisitors.add(provider);
return this;
}
public Builder extension(TinyRemapper.Extension extension) {
extension.attach(this);
return this;
}
public TinyRemapper build() {
TinyRemapper remapper = new TinyRemapper(mappingProviders, ignoreFieldDesc, threadCount,
keepInputData,
forcePropagation, propagatePrivate,
propagateBridges, propagateRecordComponents,
removeFrames, ignoreConflicts, resolveMissing, checkPackageAccess || fixPackageAccess, fixPackageAccess,
rebuildSourceFilenames, skipLocalMapping, renameInvalidLocals,
analyzeVisitors, stateProcessors, preApplyVisitors, postApplyVisitors,
extraRemapper);
return remapper;
}
private final Set<IMappingProvider> mappingProviders = new HashSet<>();
private boolean ignoreFieldDesc;
private int threadCount;
private final Set<String> forcePropagation = new HashSet<>();
private boolean keepInputData = false;
private boolean propagatePrivate = false;
private LinkedMethodPropagation propagateBridges = LinkedMethodPropagation.DISABLED;
private LinkedMethodPropagation propagateRecordComponents = LinkedMethodPropagation.DISABLED;
private boolean removeFrames = false;
private boolean ignoreConflicts = false;
private boolean resolveMissing = false;
private boolean checkPackageAccess = false;
private boolean fixPackageAccess = false;
private boolean rebuildSourceFilenames = false;
private boolean skipLocalMapping = false;
private boolean renameInvalidLocals = false;
private final List<AnalyzeVisitorProvider> analyzeVisitors = new ArrayList<>();
private final List<StateProcessor> stateProcessors = new ArrayList<>();
private final List<ApplyVisitorProvider> preApplyVisitors = new ArrayList<>();
private final List<ApplyVisitorProvider> postApplyVisitors = new ArrayList<>();
private Remapper extraRemapper;
}
public interface Extension {
void attach(TinyRemapper.Builder builder);
}
public interface AnalyzeVisitorProvider {
ClassVisitor insertAnalyzeVisitor(int mrjVersion, String className, ClassVisitor next);
}
public interface StateProcessor {
void process(TrEnvironment env);
}
public interface ApplyVisitorProvider {
ClassVisitor insertApplyVisitor(TrClass cls, ClassVisitor next);
}
private TinyRemapper(Collection<IMappingProvider> mappingProviders, boolean ignoreFieldDesc,
int threadCount,
boolean keepInputData,
Set<String> forcePropagation, boolean propagatePrivate,
LinkedMethodPropagation propagateBridges, LinkedMethodPropagation propagateRecordComponents,
boolean removeFrames,
boolean ignoreConflicts,
boolean resolveMissing,
boolean checkPackageAccess,
boolean fixPackageAccess,
boolean rebuildSourceFilenames,
boolean skipLocalMapping,
boolean renameInvalidLocals,
List<AnalyzeVisitorProvider> analyzeVisitors, List<StateProcessor> stateProcessors,
List<ApplyVisitorProvider> preApplyVisitors, List<ApplyVisitorProvider> postApplyVisitors,
Remapper extraRemapper) {
this.mappingProviders = mappingProviders;
this.ignoreFieldDesc = ignoreFieldDesc;
this.threadCount = threadCount > 0 ? threadCount : Math.max(Runtime.getRuntime().availableProcessors(), 2);
this.keepInputData = keepInputData;
this.threadPool = Executors.newFixedThreadPool(this.threadCount);
this.forcePropagation = forcePropagation;
this.propagatePrivate = propagatePrivate;
this.propagateBridges = propagateBridges;
this.propagateRecordComponents = propagateRecordComponents;
this.removeFrames = removeFrames;
this.ignoreConflicts = ignoreConflicts;
this.resolveMissing = resolveMissing;
this.checkPackageAccess = checkPackageAccess;
this.fixPackageAccess = fixPackageAccess;
this.rebuildSourceFilenames = rebuildSourceFilenames;
this.skipLocalMapping = skipLocalMapping;
this.renameInvalidLocals = renameInvalidLocals;
this.analyzeVisitors = analyzeVisitors;
this.stateProcessors = stateProcessors;
this.preApplyVisitors = preApplyVisitors;
this.postApplyVisitors = postApplyVisitors;
this.extraRemapper = extraRemapper;
}
public static Builder newRemapper() {
return new Builder();
}
/**
* @see #close
* @deprecated Use try-with-resources
*/
@Deprecated
public void finish() {
close();
}
@Override
public void close() {
threadPool.shutdown();
try {
threadPool.awaitTermination(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
outputBuffer = null;
defaultState.classes.clear();
mrjStates.clear();
}
public InputTag createInputTag() {
InputTag ret = new InputTag();
InputTag[] array = { ret };
Map<InputTag, InputTag[]> oldTags, newTags;
do { // cas loop
oldTags = this.singleInputTags.get();
newTags = new IdentityHashMap<>(oldTags.size() + 1);
newTags.putAll(oldTags);
newTags.put(ret, array);
} while (!singleInputTags.compareAndSet(oldTags, newTags));
return ret;
}
public void readInputs(final Path... inputs) {
readInputs(null, inputs);
}
public void readInputs(InputTag tag, Path... inputs) {
read(inputs, true, tag).join();
}
public CompletableFuture<?> readInputsAsync(Path... inputs) {
return readInputsAsync(null, inputs);
}
public CompletableFuture<?> readInputsAsync(InputTag tag, Path... inputs) {
CompletableFuture<?> ret = read(inputs, true, tag);
if (!ret.isDone()) {
pendingReads.add(ret);
} else {
ret.join();
}
return ret;
}
public void readClassPath(final Path... inputs) {
read(inputs, false, null).join();
}
public CompletableFuture<?> readClassPathAsync(final Path... inputs) {
CompletableFuture<?> ret = read(inputs, false, null);
if (!ret.isDone()) {
pendingReads.add(ret);
} else {
ret.join();
}
return ret;
}
private CompletableFuture<List<ClassInstance>> read(Path[] inputs, boolean isInput, InputTag tag) {
InputTag[] tags = singleInputTags.get().get(tag);
List<CompletableFuture<List<ClassInstance>>> futures = new ArrayList<>();
List<FileSystem> fsToClose = Collections.synchronizedList(new ArrayList<>());
for (Path input : inputs) {
futures.addAll(read(input, isInput, tags, true, fsToClose));
}
CompletableFuture<List<ClassInstance>> ret;
if (futures.isEmpty()) {
return CompletableFuture.completedFuture(Collections.emptyList());
} else if (futures.size() == 1) {
ret = futures.get(0);
} else {
ret = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(ignore -> futures.stream().flatMap(f -> f.join().stream()).collect(Collectors.toList()));
}
if (!dirty) {
dirty = true;
for (MrjState state : mrjStates.values()) {
state.dirty = true;
}
}
return ret.whenComplete((res, exc) -> {
for (FileSystem fs : fsToClose) {
try {
FileSystemHandler.close(fs);
} catch (IOException e) {
// ignore
}
}
if (res != null) {
for (ClassInstance node : res) {
addClass(node, readClasses, true);
}
}
assert dirty;
});
}
private static void addClass(ClassInstance cls, Map<String, ClassInstance> out, boolean isVersionAware) {
// two different MRJ version will not cause warning if isVersionAware is true
String name = isVersionAware ? ClassInstance.getMrjName(cls.getName(), cls.getMrjVersion()) : cls.getName();
// add new class or replace non-input class with input class, warn if two input classes clash
for (;;) {
ClassInstance prev = out.putIfAbsent(name, cls);
if (prev == null) return;
if (prev.isMrjCopy() && prev.getMrjVersion() < cls.getMrjVersion()) {
// if {@code prev} is MRJ copy and {@code prev}'s origin version is less than {@code cls}'s
// origin version, then we should update the class.
if (out.replace(name, prev, cls)) {
return;
} else {
// loop
}
} else if (cls.isInput) {
if (prev.isInput) {
System.out.printf("duplicate input class %s, from %s and %s%n", name, prev.srcPath, cls.srcPath);
prev.addInputTags(cls.getInputTags());
return;
} else if (out.replace(name, prev, cls)) { // cas with retry-loop on failure
cls.addInputTags(prev.getInputTags());
return;
} else {
// loop
}
} else {
prev.addInputTags(cls.getInputTags());
return;
}
}
}
private List<CompletableFuture<List<ClassInstance>>> read(final Path file, boolean isInput, InputTag[] tags,
boolean saveData, final List<FileSystem> fsToClose) {
try {
return read(file, isInput, tags, file, saveData, fsToClose);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private List<CompletableFuture<List<ClassInstance>>> read(final Path file, boolean isInput, InputTag[] tags, final Path srcPath,
final boolean saveData, final List<FileSystem> fsToClose) throws IOException {
List<CompletableFuture<List<ClassInstance>>> 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(CompletableFuture.supplyAsync(new Supplier<List<ClassInstance>>() {
@Override
public List<ClassInstance> get() {
try {
return readFile(file, isInput, tags, srcPath, fsToClose);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (IOException | ZipError e) {
throw new RuntimeException("Error reading file "+file, e);
}
}
}, threadPool));
}
return FileVisitResult.CONTINUE;
}
});
return ret;
}
private List<ClassInstance> readFile(Path file, boolean isInput, InputTag[] tags, final Path srcPath,
List<FileSystem> fsToClose) throws IOException, URISyntaxException {
List<ClassInstance> ret = new ArrayList<ClassInstance>();
if (file.toString().endsWith(".class")) {
ClassInstance res = analyze(isInput, tags, srcPath, file);
if (res != null) ret.add(res);
} else {
URI uri = new URI("jar:"+file.toUri().toString());
FileSystem fs = FileSystemHandler.open(uri);
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")) {
ClassInstance res = analyze(isInput, tags, srcPath, file);
if (res != null) ret.add(res);
}
return FileVisitResult.CONTINUE;
}
});
}
return ret;
}
/**
* Determine the MRJ version of the supplied class file and name.
*
* <p>This assumes that the file path follows the usual META-INF/versions/{@code <version>}/pkg/for/cls.class form.
*/
private static int analyzeMrjVersion(Path file, String name) {
assert file.getFileName().toString().endsWith(".class");
int pkgCount = 0;
int pos = 0;
while ((pos = name.indexOf('/', pos) + 1) > 0) {
pkgCount++;
}
int pathNameCount = file.getNameCount();
int pathNameOffset = pathNameCount - pkgCount - 1; // path index for root package
if (pathNameOffset >= 3
&& file.getName(pathNameOffset - 3).toString().equals("META-INF") // root pkg is in META-INF/x/x
&& file.getName(pathNameOffset - 2).toString().equals("versions") // root pkg is in META-INF/versions/x
&& file.subpath(pathNameOffset, pathNameCount).toString().replace('\\', '/').regionMatches(0, name, 0, name.length())) { // verify class name == path from root pkg dir, ignores suffix like .class
try {
return Integer.parseInt(file.getName(pathNameOffset - 1).toString());
} catch (NumberFormatException e) {
// ignore
}
}
return ClassInstance.MRJ_DEFAULT;
}
private ClassInstance analyze(boolean isInput, InputTag[] tags, Path srcPath, Path file) throws IOException {
byte[] data = Files.readAllBytes(file);
ClassReader reader = new ClassReader(data);
if ((reader.getAccess() & Opcodes.ACC_MODULE) != 0) return null; // special attribute for module-info.class, can't be a regular class
final ClassInstance ret = new ClassInstance(this, isInput, tags, srcPath, isInput ? data : null);
reader.accept(new ClassVisitor(Opcodes.ASM9) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
int mrjVersion = analyzeMrjVersion(file, name);
ret.init(mrjVersion, name, signature, superName, access, interfaces);
for (int i = analyzeVisitors.size() - 1; i >= 0; i--) {
cv = analyzeVisitors.get(i).insertAnalyzeVisitor(mrjVersion, name, cv);
}
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MemberInstance prev = ret.addMember(new MemberInstance(TrMember.MemberType.METHOD, ret, name, desc, access, ret.getMembers().size()));
if (prev != null) throw new RuntimeException(String.format("duplicate method %s/%s%s in inputs", ret.getName(), name, desc));
return super.visitMethod(access, name, desc, signature, exceptions);
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
MemberInstance prev = ret.addMember(new MemberInstance(TrMember.MemberType.FIELD, ret, name, desc, access, ret.getMembers().size()));
if (prev != null) throw new RuntimeException(String.format("duplicate field %s/%s;;%s in inputs", ret.getName(), name, desc));
return super.visitField(access, name, desc, signature, value);
}
}, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);
return ret;
}
private void loadMappings() {
MappingAcceptor acceptor = new MappingAcceptor() {
@Override
public void acceptClass(String srcName, String dstName) {
if (srcName == null) throw new NullPointerException("null src name");
if (dstName == null) throw new NullPointerException("null dst name");
classMap.put(srcName, dstName);
}
@Override
public void acceptMethod(Member method, String dstName) {
if (method == null) throw new NullPointerException("null src method");
if (method.owner == null) throw new NullPointerException("null src method owner");
if (method.name == null) throw new NullPointerException("null src method name");
if (method.desc == null) throw new NullPointerException("null src method desc");
if (dstName == null) throw new NullPointerException("null dst name");
methodMap.put(method.owner+"/"+MemberInstance.getMethodId(method.name, method.desc), dstName);
}
@Override
public void acceptMethodArg(Member method, int lvIndex, String dstName) {
if (method == null) throw new NullPointerException("null src method");
if (method.owner == null) throw new NullPointerException("null src method owner");
if (method.name == null) throw new NullPointerException("null src method name");
if (method.desc == null) throw new NullPointerException("null src method desc");
if (dstName == null) throw new NullPointerException("null dst name");
methodArgMap.put(method.owner+"/"+MemberInstance.getMethodId(method.name, method.desc)+lvIndex, dstName);
}
@Override
public void acceptMethodVar(Member method, int lvIndex, int startOpIdx, int asmIndex, String dstName) {
if (method == null) throw new NullPointerException("null src method");
if (method.owner == null) throw new NullPointerException("null src method owner");
if (method.name == null) throw new NullPointerException("null src method name");
if (method.desc == null) throw new NullPointerException("null src method desc");
if (dstName == null) throw new NullPointerException("null dst name");
// TODO Auto-generated method stub
}
@Override
public void acceptField(Member field, String dstName) {
if (field == null) throw new NullPointerException("null src field");
if (field.owner == null) throw new NullPointerException("null src field owner");
if (field.name == null) throw new NullPointerException("null src field name");
if (field.desc == null && !ignoreFieldDesc) throw new NullPointerException("null src field desc");
if (dstName == null) throw new NullPointerException("null dst name");
fieldMap.put(field.owner+"/"+MemberInstance.getFieldId(field.name, field.desc, ignoreFieldDesc), dstName);
}
};
for (IMappingProvider provider : mappingProviders) {
provider.load(acceptor);
}
}
private void checkClassMappings() {
// determine classes that map to the same target name, if there are any print duplicates and throw
Set<String> testSet = new HashSet<>(classMap.values());
if (testSet.size() != classMap.size()) { // src->target is not a 1:1 mapping
Set<String> duplicates = new HashSet<>();
for (String name : classMap.values()) {
if (!testSet.remove(name)) {
duplicates.add(name);
}
}
System.out.println("non-unique class target name mappings:");
for (String target : duplicates) {
System.out.print(" [");
boolean first = true;
for (Map.Entry<String, String> e : classMap.entrySet()) {
if (e.getValue().equals(target)) {
if (first) {
first = false;
} else {
System.out.print(", ");
}
System.out.print(e.getKey());
}
}
System.out.printf("] -> %s%n", target);
}
throw new RuntimeException("duplicate class target name mappings detected");
}
}
private void merge(MrjState state) {
for (ClassInstance node : state.classes.values()) {
assert node.getSuperName() != null;
ClassInstance parent = state.getClass(node.getSuperName());
if (parent != null) {
node.parents.add(parent);
parent.children.add(node);
}
for (String iface : node.getInterfaceNames0()) {
parent = state.getClass(iface);
if (parent != null) {
node.parents.add(parent);
parent.children.add(node);
}
}
}
}
private void propagate(MrjState state) {
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(state, TrMember.MemberType.METHOD, tasks)));
tasks.clear();
}
}
futures.add(threadPool.submit(new Propagation(state, TrMember.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(state, TrMember.MemberType.FIELD, tasks)));
tasks.clear();
}
}
futures.add(threadPool.submit(new Propagation(state, TrMember.MemberType.FIELD, tasks)));
tasks.clear();
waitForAll(futures);
handleConflicts(state);
}
private void handleConflicts(MrjState state) {
Set<String> testSet = new HashSet<>();
boolean targetNameCheckFailed = false;
for (ClassInstance cls : state.classes.values()) {
for (MemberInstance member : cls.getMembers()) {
String name = member.getNewMappedName();
if (name == null) name = member.name;
testSet.add(MemberInstance.getId(member.type, name, member.desc, ignoreFieldDesc));
}
if (testSet.size() != cls.getMembers().size()) {
if (!targetNameCheckFailed) {
targetNameCheckFailed = true;
System.out.println("Mapping target name conflicts detected:");
}
Map<String, List<MemberInstance>> duplicates = new HashMap<>();
for (MemberInstance member : cls.getMembers()) {
String name = member.getNewMappedName();
if (name == null) name = member.name;
duplicates.computeIfAbsent(MemberInstance.getId(member.type, name, member.desc, ignoreFieldDesc), ignore -> new ArrayList<>()).add(member);
}
for (Map.Entry<String, List<MemberInstance>> e : duplicates.entrySet()) {
String nameDesc = e.getKey();
List<MemberInstance> members = e.getValue();
if (members.size() < 2) continue;
MemberInstance anyMember = members.get(0);
System.out.printf(" %ss %s/[", anyMember.type, cls.getName());
for (int i = 0; i < members.size(); i++) {
if (i != 0) System.out.print(", ");
MemberInstance member = members.get(i);
if (member.newNameOriginatingCls != null && !member.newNameOriginatingCls.equals(cls.getName())) {
System.out.print(member.newNameOriginatingCls);
System.out.print('/');
}
System.out.print(member.name);
}
System.out.printf("]%s -> %s%n", MemberInstance.getId(anyMember.type, "", anyMember.desc, ignoreFieldDesc), MemberInstance.getNameFromId(anyMember.type, nameDesc, ignoreFieldDesc));
}
}
testSet.clear();
}
boolean unfixableConflicts = false;
if (!conflicts.isEmpty()) {
System.out.println("Mapping source name conflicts detected:");
for (Map.Entry<MemberInstance, Set<String>> entry : conflicts.entrySet()) {
MemberInstance member = entry.getKey();
String newName = member.getNewMappedName();
Set<String> names = entry.getValue();
names.add(member.cls.getName()+"/"+newName);
System.out.printf(" %s %s %s (%s) -> %s%n", member.cls.getName(), member.type.name(), member.name, member.desc, names);
if (ignoreConflicts) {
Map<String, String> mappings = member.type == TrMember.MemberType.METHOD ? methodMap : fieldMap;
String mappingName = mappings.get(member.cls.getName()+"/"+member.getId());
if (mappingName == null) { // no direct mapping match, try parents
Queue<ClassInstance> queue = new ArrayDeque<>(member.cls.parents);
ClassInstance cls;
while ((cls = queue.poll()) != null) {
mappingName = mappings.get(cls.getName()+"/"+member.getId());
if (mappingName != null) break;
queue.addAll(cls.parents);
}
}
if (mappingName == null) {
unfixableConflicts = true;
} else {
member.forceSetNewName(mappingName);
System.out.println(" fixable: replaced with "+mappingName);
}
}
}
}
if (!conflicts.isEmpty() && !ignoreConflicts || unfixableConflicts || targetNameCheckFailed) {
if (ignoreConflicts || targetNameCheckFailed) System.out.println("There were unfixable conflicts.");
throw new RuntimeException("Unfixable conflicts");
}
}
public void apply(final BiConsumer<String, byte[]> outputConsumer) {
apply(outputConsumer, (InputTag[]) null);
}
public void apply(final BiConsumer<String, byte[]> outputConsumer, InputTag... inputTags) {
// We expect apply() to be invoked only once if the user didn't request any input tags. Invoking it multiple
// times still works with keepInputData=true, but wastes some time by redoing most processing.
// With input tags the first apply invocation computes the entire output, but yields only what matches the given
// input tags. The output data is being kept for eventual further apply() outputs, only finish() clears it.
boolean hasInputTags = !singleInputTags.get().isEmpty();
synchronized (this) { // guard against concurrent apply invocations
refresh();
if (outputBuffer == null) { // first (inputTags present) or full (no input tags) output invocation, process everything but don't output if input tags are present
BiConsumer<ClassInstance, byte[]> immediateOutputConsumer;
if (fixPackageAccess || hasInputTags) { // need re-processing or output buffering for repeated applies
outputBuffer = new ConcurrentHashMap<>();
immediateOutputConsumer = outputBuffer::put;
} else {
immediateOutputConsumer = (cls, data) -> outputConsumer.accept(ClassInstance.getMrjName(cls.getContext().remapper.map(cls.getName()), cls.getMrjVersion()), data);
}
List<Future<?>> futures = new ArrayList<>();
for (MrjState state : mrjStates.values()) {
mrjRefresh(state);
for (final ClassInstance cls : state.classes.values()) {
if (!cls.isInput) continue;
if (cls.data == null) {
if (!hasInputTags && !keepInputData) throw new IllegalStateException("invoking apply multiple times without input tags or hasInputData");
throw new IllegalStateException("data for input class " + cls + " is missing?!");
}
futures.add(threadPool.submit(() -> immediateOutputConsumer.accept(cls, apply(cls))));
}
}
waitForAll(futures);
boolean needsFixes = !classesToMakePublic.isEmpty() || !membersToMakePublic.isEmpty();
if (fixPackageAccess) {
if (needsFixes) {
System.out.printf("Fixing access for %d classes and %d members.%n", classesToMakePublic.size(), membersToMakePublic.size());
}
for (Map.Entry<ClassInstance, byte[]> entry : outputBuffer.entrySet()) {
ClassInstance cls = entry.getKey();
byte[] data = entry.getValue();
if (needsFixes) {
data = fixClass(cls, data);
}
if (hasInputTags) {
entry.setValue(data);
} else {
outputConsumer.accept(ClassInstance.getMrjName(cls.getContext().remapper.map(cls.getName()), cls.getMrjVersion()), data);
}
}
if (!hasInputTags) outputBuffer = null; // don't expect repeat invocations
classesToMakePublic.clear();
membersToMakePublic.clear();
} else if (needsFixes) {
throw new RuntimeException(String.format("%d classes and %d members need access fixes", classesToMakePublic.size(), membersToMakePublic.size()));
}
}
assert hasInputTags == (outputBuffer != null);
if (outputBuffer != null) { // partial output selected by input tags
for (Map.Entry<ClassInstance, byte[]> entry : outputBuffer.entrySet()) {
ClassInstance cls = entry.getKey();
if (inputTags == null || cls.hasAnyInputTag(inputTags)) {
outputConsumer.accept(ClassInstance.getMrjName(cls.getContext().remapper.map(cls.getName()), cls.getMrjVersion()), entry.getValue());
}
}
}
}
}
/**
* This function will setup {@code mrjClasses} with any new MRJ version
* added. It will put the result of {@code constructMrjCopy} from lower
* MRJ version to the new version.
* @param newVersions the new versions that need to be added in to {@code mrjClasses}
*/
private void fixMrjClasses(Set<Integer> newVersions) {
// ensure the new version is added from lowest to highest
for (int newVersion: newVersions.stream().sorted().collect(Collectors.toList())) {
MrjState newState = new MrjState(this, newVersion);
if (mrjStates.put(newVersion, newState) != null) {
throw new RuntimeException("internal error: duplicate versions in mrjClasses");
}
// find the fromVersion that just lower the the toVersion
Optional<Integer> fromVersion = mrjStates.keySet().stream()
.filter(v -> v < newVersion).max(Integer::compare);
if (fromVersion.isPresent()) {
Map<String, ClassInstance> fromClasses = mrjStates.get(fromVersion.get()).classes;
for (ClassInstance cls: fromClasses.values()) {
addClass(cls.constructMrjCopy(newState), newState.classes, false);
}
}
}
}
private void refresh() {
if (!dirty) {
assert pendingReads.isEmpty();
assert readClasses.isEmpty();
return;
}
outputBuffer = null;
if (!pendingReads.isEmpty()) {
for (CompletableFuture<?> future : pendingReads) {
future.join();
}
pendingReads.clear();
}
if (!readClasses.isEmpty()) {
// fix any new adding MRJ versions
Set<Integer> versions = readClasses.values().stream().map(ClassInstance::getMrjVersion).collect(Collectors.toSet());
versions.removeAll(mrjStates.keySet());
fixMrjClasses(versions);
for (ClassInstance cls : readClasses.values()) {
// TODO: this might be able to optimize, any suggestion?
int clsVersion = cls.getMrjVersion();
MrjState state = mrjStates.get(clsVersion);
cls.setContext(state);
addClass(cls, state.classes, false);