-
Notifications
You must be signed in to change notification settings - Fork 18
/
RegularAsFlatMappingVisitor.java
378 lines (317 loc) · 16.1 KB
/
RegularAsFlatMappingVisitor.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
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
/*
* Copyright (c) 2021 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.List;
import java.util.Set;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.mappingio.FlatMappingVisitor;
import net.fabricmc.mappingio.MappedElementKind;
import net.fabricmc.mappingio.MappingFlag;
import net.fabricmc.mappingio.MappingVisitor;
/**
* A {@link FlatMappingVisitor} that forwards all relevant data to a regular {@link MappingVisitor}.
*/
public final class RegularAsFlatMappingVisitor implements FlatMappingVisitor {
public RegularAsFlatMappingVisitor(MappingVisitor next) {
this.next = next;
}
@Override
public Set<MappingFlag> getFlags() {
return next.getFlags();
}
@Override
public void reset() {
lastClass = null;
lastMemberName = lastMemberDesc = null;
lastArgPosition = lastLvIndex = lastStartOpIdx = -1;
next.reset();
}
@Override
public boolean visitHeader() throws IOException {
return next.visitHeader();
}
@Override
public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) throws IOException {
next.visitNamespaces(srcNamespace, dstNamespaces);
Set<MappingFlag> flags = next.getFlags();
relayDstFieldDescs = flags.contains(MappingFlag.NEEDS_DST_FIELD_DESC);
relayDstMethodDescs = flags.contains(MappingFlag.NEEDS_DST_METHOD_DESC);
}
@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
next.visitMetadata(key, value);
}
@Override
public boolean visitContent() throws IOException {
return next.visitContent();
}
@Override
public boolean visitClass(String srcName, String[] dstNames) throws IOException {
return visitClass(srcName, dstNames, null);
}
@Override
public boolean visitClass(String srcName, String dstName) throws IOException {
return visitClass(srcName, null, dstName);
}
private boolean visitClass(String srcName, @Nullable String[] dstNames, @Nullable String dstName) throws IOException {
if (!srcName.equals(lastClass)) {
lastClass = srcName;
lastMemberName = lastMemberDesc = null;
lastArgPosition = lastLvIndex = lastStartOpIdx = -1;
relayLastClass = next.visitClass(srcName) && visitDstNames(MappedElementKind.CLASS, dstNames, dstName);
}
return relayLastClass;
}
@Override
public void visitClassComment(String srcName, @Nullable String[] dstNames, String comment) throws IOException {
if (!visitClass(srcName, dstNames, null)) return;
next.visitComment(MappedElementKind.CLASS, comment);
}
@Override
public void visitClassComment(String srcName, @Nullable String dstName, String comment) throws IOException {
if (!visitClass(srcName, null, dstName)) return;
next.visitComment(MappedElementKind.CLASS, comment);
}
@Override
public boolean visitField(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, String[] dstNames, @Nullable String[] dstDescs) throws IOException {
return visitField(srcClsName, srcName, srcDesc, dstClsNames, dstNames, dstDescs, null, null, null);
}
@Override
public boolean visitField(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String dstClsName, String dstName, @Nullable String dstDesc) throws IOException {
return visitField(srcClsName, srcName, srcDesc, null, null, null, dstClsName, dstName, dstDesc);
}
private boolean visitField(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs,
@Nullable String dstClsName, @Nullable String dstName, @Nullable String dstDesc) throws IOException {
if (!visitClass(srcClsName, dstClsNames, dstClsName)) return false;
if (!lastMemberIsField || !srcName.equals(lastMemberName) || srcDesc != null && !srcDesc.equals(lastMemberDesc)) {
lastMemberName = srcName;
lastMemberDesc = srcDesc;
lastMemberIsField = true;
lastArgPosition = lastLvIndex = lastStartOpIdx = -1;
relayLastMember = next.visitField(srcName, srcDesc) && visitDstNamesDescs(MappedElementKind.FIELD, dstNames, dstDescs, dstName, dstDesc);
}
return relayLastMember;
}
@Override
public void visitFieldComment(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs,
String comment) throws IOException {
if (!visitField(srcClsName, srcName, srcDesc, dstClsNames, dstNames, dstDescs, null, null, null)) return;
next.visitComment(MappedElementKind.FIELD, comment);
}
@Override
public void visitFieldComment(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String dstClsName, @Nullable String dstName, @Nullable String dstDesc,
String comment) throws IOException {
if (!visitField(srcClsName, srcName, srcDesc, null, null, null, dstClsName, dstName, dstDesc)) return;
next.visitComment(MappedElementKind.FIELD, comment);
}
@Override
public boolean visitMethod(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, String[] dstNames, @Nullable String[] dstDescs) throws IOException {
return visitMethod(srcClsName, srcName, srcDesc, dstClsNames, dstNames, dstDescs, null, null, null);
}
@Override
public boolean visitMethod(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String dstClsName, String dstName, @Nullable String dstDesc) throws IOException {
return visitMethod(srcClsName, srcName, srcDesc, null, null, null, dstClsName, dstName, dstDesc);
}
private boolean visitMethod(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs,
@Nullable String dstClsName, @Nullable String dstName, @Nullable String dstDesc) throws IOException {
if (!visitClass(srcClsName, dstClsNames, dstClsName)) return false;
if (lastMemberIsField || !srcName.equals(lastMemberName) || srcDesc != null && !srcDesc.equals(lastMemberDesc)) {
lastMemberName = srcName;
lastMemberDesc = srcDesc;
lastMemberIsField = false;
lastArgPosition = lastLvIndex = lastStartOpIdx = -1;
relayLastMember = next.visitMethod(srcName, srcDesc) && visitDstNamesDescs(MappedElementKind.METHOD, dstNames, dstDescs, dstName, dstDesc);
}
return relayLastMember;
}
@Override
public void visitMethodComment(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String[] dstClsNames, @Nullable String[] dstNames, @Nullable String[] dstDescs,
String comment) throws IOException {
if (!visitMethod(srcClsName, srcName, srcDesc, dstClsNames, dstNames, dstDescs, null, null, null)) return;
next.visitComment(MappedElementKind.METHOD, comment);
}
@Override
public void visitMethodComment(String srcClsName, String srcName, @Nullable String srcDesc,
@Nullable String dstClsName, @Nullable String dstName, @Nullable String dstDesc,
String comment) throws IOException {
if (!visitMethod(srcClsName, srcName, srcDesc, null, null, null, dstClsName, dstName, dstDesc)) return;
next.visitComment(MappedElementKind.METHOD, comment);
}
@Override
public boolean visitMethodArg(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int argPosition, int lvIndex, @Nullable String srcArgName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames,
@Nullable String[] dstMethodDescs, String[] dstArgNames) throws IOException {
return visitMethodArg(srcClsName, srcMethodName, srcMethodDesc, argPosition, lvIndex, srcArgName,
dstClsNames, dstMethodNames, dstMethodDescs, dstArgNames, null, null, null, null);
}
@Override
public boolean visitMethodArg(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int argPosition, int lvIndex, @Nullable String srcArgName,
@Nullable String dstClsName, @Nullable String dstMethodName,
@Nullable String dstMethodDesc, String dstArgName) throws IOException {
return visitMethodArg(srcClsName, srcMethodName, srcMethodDesc, argPosition, lvIndex, srcArgName,
null, null, null, null, dstClsName, dstMethodName, dstMethodDesc, dstArgName);
}
private boolean visitMethodArg(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int argPosition, int lvIndex, @Nullable String srcName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames,
@Nullable String[] dstMethodDescs, @Nullable String[] dstNames,
@Nullable String dstClsName, @Nullable String dstMethodName,
@Nullable String dstMethodDesc, @Nullable String dstName) throws IOException {
if (!visitMethod(srcClsName, srcMethodName, srcMethodDesc, dstClsNames, dstMethodNames, dstMethodDescs, dstClsName, dstMethodName, dstMethodDesc)) return false;
if (!lastMethodSubIsArg || argPosition != lastArgPosition || lvIndex != lastLvIndex) {
lastArgPosition = argPosition;
lastLvIndex = lvIndex;
lastMethodSubIsArg = true;
relayLastMethodSub = next.visitMethodArg(argPosition, lvIndex, srcName) && visitDstNames(MappedElementKind.METHOD_ARG, dstNames, dstName);
}
return relayLastMethodSub;
}
@Override
public void visitMethodArgComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int argPosition, int lvIndex, @Nullable String srcArgName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames,
@Nullable String[] dstMethodDescs, @Nullable String[] dstArgNames, String comment) throws IOException {
if (!visitMethodArg(srcClsName, srcMethodName, srcMethodDesc, argPosition, lvIndex, srcArgName,
dstClsNames, dstMethodNames, dstMethodDescs, dstArgNames, null, null, null, null)) {
return;
}
next.visitComment(MappedElementKind.METHOD_ARG, comment);
}
@Override
public void visitMethodArgComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int argPosition, int lvIndex, @Nullable String srcArgName,
@Nullable String dstClsName, @Nullable String dstMethodName,
@Nullable String dstMethodDesc, @Nullable String dstArgName, String comment) throws IOException {
if (!visitMethodArg(srcClsName, srcMethodName, srcMethodDesc, argPosition, lvIndex, srcArgName,
null, null, null, null, dstClsName, dstMethodName, dstMethodDesc, dstArgName)) {
return;
}
next.visitComment(MappedElementKind.METHOD_ARG, comment);
}
@Override
public boolean visitMethodVar(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcVarName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames,
@Nullable String[] dstMethodDescs, String[] dstVarNames) throws IOException {
return visitMethodVar(srcClsName, srcMethodName, srcMethodDesc, lvtRowIndex, lvIndex, startOpIdx, endOpIdx, srcVarName,
dstClsNames, dstMethodNames, dstMethodDescs, dstVarNames, null, null, null, null);
}
@Override
public boolean visitMethodVar(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcVarName,
@Nullable String dstClsName, @Nullable String dstMethodName,
@Nullable String dstMethodDesc, String dstVarName) throws IOException {
return visitMethodVar(srcClsName, srcMethodName, srcMethodDesc, lvtRowIndex, lvIndex, startOpIdx, endOpIdx, srcVarName,
null, null, null, null, dstClsName, dstMethodName, dstMethodDesc, dstVarName);
}
private boolean visitMethodVar(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs, @Nullable String[] dstNames,
@Nullable String dstClsName, @Nullable String dstMethodName, @Nullable String dstMethodDesc, @Nullable String dstName) throws IOException {
if (!visitMethod(srcClsName, srcMethodName, srcMethodDesc, dstClsNames, dstMethodNames, dstMethodDescs, dstClsName, dstMethodName, dstMethodDesc)) return false;
if (lastMethodSubIsArg || lvtRowIndex != lastArgPosition || lvIndex != lastLvIndex || startOpIdx != lastStartOpIdx) {
lastArgPosition = lvtRowIndex;
lastLvIndex = lvIndex;
lastStartOpIdx = startOpIdx;
lastMethodSubIsArg = false;
relayLastMethodSub = next.visitMethodVar(lvtRowIndex, lvIndex, startOpIdx, endOpIdx, srcName) && visitDstNames(MappedElementKind.METHOD_VAR, dstNames, dstName);
}
return relayLastMethodSub;
}
@Override
public void visitMethodVarComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcVarName,
@Nullable String[] dstClsNames, @Nullable String[] dstMethodNames, @Nullable String[] dstMethodDescs,
@Nullable String[] dstVarNames, String comment) throws IOException {
if (!visitMethodVar(srcClsName, srcMethodName, srcMethodDesc, lvtRowIndex, lvIndex, startOpIdx, endOpIdx, srcVarName,
dstClsNames, dstMethodNames, dstMethodDescs, dstVarNames, null, null, null, null)) {
return;
}
next.visitComment(MappedElementKind.METHOD_VAR, comment);
}
@Override
public void visitMethodVarComment(String srcClsName, String srcMethodName, @Nullable String srcMethodDesc,
int lvtRowIndex, int lvIndex, int startOpIdx, int endOpIdx, @Nullable String srcVarName,
@Nullable String dstClsName, @Nullable String dstMethodName, @Nullable String dstMethodDesc,
@Nullable String dstVarName, String comment) throws IOException {
if (!visitMethodVar(srcClsName, srcMethodName, srcMethodDesc, lvtRowIndex, lvIndex, startOpIdx, endOpIdx, srcVarName,
null, null, null, null, dstClsName, dstMethodName, dstMethodDesc, dstVarName)) {
return;
}
next.visitComment(MappedElementKind.METHOD_VAR, comment);
}
@Override
public boolean visitEnd() throws IOException {
return next.visitEnd();
}
private boolean visitDstNames(MappedElementKind targetKind, @Nullable String[] dstNames, @Nullable String dstName) throws IOException {
if (dstNames != null) {
for (int i = 0; i < dstNames.length; i++) {
String name = dstNames[i];
if (name != null) next.visitDstName(targetKind, i, name);
}
} else if (dstName != null) {
next.visitDstName(targetKind, 0, dstName);
}
return next.visitElementContent(targetKind);
}
private boolean visitDstNamesDescs(MappedElementKind targetKind, @Nullable String[] dstNames,
@Nullable String[] dstDescs, @Nullable String dstName, @Nullable String dstDesc) throws IOException {
boolean relayMemberDesc = targetKind == MappedElementKind.FIELD && relayDstFieldDescs
|| targetKind != MappedElementKind.FIELD && relayDstMethodDescs;
if (dstNames != null || dstDescs != null) {
if (dstNames != null) {
for (int i = 0; i < dstNames.length; i++) {
String name = dstNames[i];
if (name != null) next.visitDstName(targetKind, i, name);
}
}
if (dstDescs != null && relayMemberDesc) {
for (int i = 0; i < dstDescs.length; i++) {
String desc = dstDescs[i];
if (desc != null) next.visitDstDesc(targetKind, i, desc);
}
}
} else {
if (dstName != null) next.visitDstName(targetKind, 0, dstName);
if (dstDesc != null && relayMemberDesc) next.visitDstDesc(targetKind, 0, dstDesc);
}
return next.visitElementContent(targetKind);
}
private final MappingVisitor next;
private boolean relayDstFieldDescs;
private boolean relayDstMethodDescs;
private String lastClass;
private boolean relayLastClass;
private String lastMemberName, lastMemberDesc;
private boolean lastMemberIsField;
private boolean relayLastMember;
private int lastArgPosition, lastLvIndex, lastStartOpIdx;
private boolean lastMethodSubIsArg;
private boolean relayLastMethodSub;
}