-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOuterClassNamePropagator.java
270 lines (223 loc) · 8.94 KB
/
OuterClassNamePropagator.java
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
/*
* Copyright (c) 2023 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.mappingio.adapter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingFlag;
import net.fabricmc.mappingio.MappingUtil;
import net.fabricmc.mappingio.MappingVisitor;
/**
* Searches for inner classes whose effective destination name contains outer classes referenced via their source name,
* waits for mappings for these enclosing classes, and applies the latters' destination names
* to the formers' fully qualified name.
*
* <p>For example, it takes a class {@code class_1$class_2} that doesn't have a mapping,
* tries to find {@code class_1}, which let's say has the mapping {@code SomeClass},
* and changes the former's destination name to {@code SomeClass$class_2}.
*
* @implNote This visitor requires two pre-passes: one to collect all classes and their mappings,
* the other to actually apply the outer names. The third pass onwards will then emit the final mappings.
*/
public class OuterClassNamePropagator extends ForwardingMappingVisitor {
/**
* Constructs a new {@link OuterClassNamePropagator} which processes all destination namespaces,
* including already remapped class destination names therein.
*/
public OuterClassNamePropagator(MappingVisitor next) {
this(next, null, true);
}
/**
* Constructs a new {@link OuterClassNamePropagator} which processes the selected destination namespaces.
*
* @param namespaces The destination namespaces where outer class names shall be propagated. Pass {@code null} to process all destination namespaces.
* @param processRemappedDstNames Whether already remapped destination names should also get their unmapped outer classes replaced.
*/
public OuterClassNamePropagator(MappingVisitor next, @Nullable Collection<String> namespaces, boolean processRemappedDstNames) {
super(next);
this.dstNamespacesToProcess = namespaces;
this.processRemappedDstNames = processRemappedDstNames;
}
@Override
public Set<MappingFlag> getFlags() {
Set<MappingFlag> ret = EnumSet.noneOf(MappingFlag.class);
ret.addAll(next.getFlags());
ret.add(MappingFlag.NEEDS_MULTIPLE_PASSES);
return ret;
}
@Override
public boolean visitHeader() throws IOException {
if (pass < FIST_EMIT_PASS) return true;
return super.visitHeader();
}
@Override
@SuppressWarnings("unchecked")
public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) throws IOException {
if (pass == COLLECT_CLASSES_PASS) {
if (dstNamespacesToProcess == null) {
dstNamespacesToProcess = dstNamespaces;
} else {
if (dstNamespacesToProcess.contains(srcNamespace)) {
throw new UnsupportedOperationException(srcNamespace + " was passed as a destination namespace"
+ " to propagate outer class names in, but has been visited as the source namespace.");
}
for (String ns : dstNamespacesToProcess) {
if (!dstNamespaces.contains(ns)) {
throw new IllegalArgumentException(ns + " was passed as a destination namespace to propagate outer class names in,"
+ " but is not present in the namespaces of the current visitation pass.");
}
}
}
this.dstNamespaces = dstNamespaces;
this.dstNsCount = dstNamespaces.size();
if (dstNamespaceIndicesToProcess == null) {
dstNamespaceIndicesToProcess = new ArrayList<>();
for (int i = 0; i < dstNsCount; i++) {
if (dstNamespacesToProcess.contains(dstNamespaces.get(i))) {
dstNamespaceIndicesToProcess.add(i);
}
}
}
visitedDstName = new boolean[dstNsCount];
dstNameBySrcNameByNamespace = new HashMap[dstNsCount];
} else if (pass >= FIST_EMIT_PASS) {
super.visitNamespaces(srcNamespace, dstNamespaces);
}
}
@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
if (pass < FIST_EMIT_PASS) return;
super.visitMetadata(key, value);
}
@Override
public boolean visitContent() throws IOException {
if (pass < FIST_EMIT_PASS) return true;
return super.visitContent();
}
@Override
public boolean visitClass(String srcName) throws IOException {
this.srcName = srcName;
if (pass == COLLECT_CLASSES_PASS) {
dstNamesBySrcName.putIfAbsent(srcName, new String[dstNsCount]);
} else if (pass >= FIST_EMIT_PASS) {
super.visitClass(srcName);
}
return true;
}
@Override
public void visitDstName(MappedElementKind targetKind, int namespace, String name) throws IOException {
if (pass == COLLECT_CLASSES_PASS) {
if (targetKind != MappedElementKind.CLASS) return;
dstNamesBySrcName.get(srcName)[namespace] = name;
} else if (pass >= FIST_EMIT_PASS) {
if (targetKind == MappedElementKind.CLASS) {
visitedDstName[namespace] = true;
name = dstNamesBySrcName.get(srcName)[namespace];
}
super.visitDstName(targetKind, namespace, name);
}
}
@Override
public void visitDstDesc(MappedElementKind targetKind, int namespace, String desc) throws IOException {
if (pass < FIST_EMIT_PASS) return;
if (modifiedClasses.contains(srcName)) {
Map<String, String> nsDstNameBySrcName = dstNameBySrcNameByNamespace[namespace];
if (nsDstNameBySrcName == null) {
dstNameBySrcNameByNamespace[namespace] = nsDstNameBySrcName = dstNamesBySrcName.entrySet()
.stream()
.filter(entry -> entry.getValue()[namespace] != null)
.collect(HashMap::new, (map, entry) -> map.put(entry.getKey(), entry.getValue()[namespace]), HashMap::putAll);
}
desc = MappingUtil.mapDesc(desc, nsDstNameBySrcName);
}
super.visitDstDesc(targetKind, namespace, desc);
}
@Override
public boolean visitElementContent(MappedElementKind targetKind) throws IOException {
if (targetKind == MappedElementKind.CLASS && pass > COLLECT_CLASSES_PASS) {
String[] dstNames = dstNamesBySrcName.get(srcName);
for (int ns = 0; ns < dstNames.length; ns++) {
if (!dstNamespacesToProcess.contains(dstNamespaces.get(ns))) {
continue;
}
String dstName = dstNames[ns];
if (pass == FIX_OUTER_CLASSES_PASS) {
if (!processRemappedDstNames && dstName != null && !dstName.equals(srcName)) {
continue;
}
String[] srcParts = srcName.split(Pattern.quote("$"));
String[] dstParts = dstName == null ? srcParts : dstName.split(Pattern.quote("$"));
assert dstParts.length == srcParts.length;
for (int pos = srcParts.length - 2; pos >= 0; pos--) {
String outerSrcName = String.join("$", Arrays.copyOfRange(srcParts, 0, pos + 1));
if (dstName != null && !dstParts[pos].equals(srcParts[pos])) {
// That part already has a different mapping
continue;
}
String outerDstName = dstNamesBySrcName.get(outerSrcName)[ns];
if (outerDstName != null && !outerDstName.equals(outerSrcName)) {
dstName = outerDstName + "$" + String.join("$", Arrays.copyOfRange(dstParts, pos + 1, dstParts.length));
dstNames[ns] = dstName;
modifiedClasses.add(srcName);
break;
}
}
} else if (!visitedDstName[ns]) {
if (dstName == null) continue; // skip if not mapped
// Class didn't have a mapping before we added one,
// so we have to call visitDstName manually.
super.visitDstName(targetKind, ns, dstName);
}
}
}
if (pass < FIST_EMIT_PASS) {
return false; // prevent other element visits, we only care about classes here
}
Arrays.fill(visitedDstName, false);
return super.visitElementContent(targetKind);
}
@Override
public boolean visitEnd() throws IOException {
if (pass++ < FIST_EMIT_PASS) {
return false;
}
return super.visitEnd();
}
private static final int COLLECT_CLASSES_PASS = 1;
private static final int FIX_OUTER_CLASSES_PASS = 2;
private static final int FIST_EMIT_PASS = 3;
private final boolean processRemappedDstNames;
private final Map<String, String[]> dstNamesBySrcName = new HashMap<>();
private final Set<String> modifiedClasses = new HashSet<>();
private List<String> dstNamespaces;
private Collection<String> dstNamespacesToProcess;
private Collection<Integer> dstNamespaceIndicesToProcess;
private int pass = 1;
private int dstNsCount = -1;
private String srcName;
private boolean[] visitedDstName;
private Map<String, String>[] dstNameBySrcNameByNamespace;
}