29
29
import dev .denwav .hypo .hydrate .generic .HypoHydration ;
30
30
import dev .denwav .hypo .hydrate .generic .MethodClosure ;
31
31
import dev .denwav .hypo .model .data .ClassData ;
32
- import dev .denwav .hypo .model .data .ClassKind ;
33
- import dev .denwav .hypo .model .data .FieldData ;
34
32
import dev .denwav .hypo .model .data .HypoKey ;
35
33
import dev .denwav .hypo .model .data .MethodData ;
36
34
import dev .denwav .hypo .model .data .types .JvmType ;
40
38
import java .util .HashSet ;
41
39
import java .util .List ;
42
40
import java .util .Map ;
41
+ import java .util .Optional ;
43
42
import java .util .Set ;
44
43
import java .util .concurrent .ConcurrentHashMap ;
45
44
import java .util .concurrent .atomic .AtomicInteger ;
45
+ import org .cadixdev .lorenz .MappingSet ;
46
+ import org .cadixdev .lorenz .model .Mapping ;
47
+ import org .cadixdev .lorenz .model .MethodMapping ;
46
48
import org .checkerframework .checker .nullness .qual .Nullable ;
47
49
import org .objectweb .asm .tree .LocalVariableNode ;
48
50
import org .objectweb .asm .tree .MethodNode ;
49
51
import org .objectweb .asm .tree .ParameterNode ;
50
- import org .parchmentmc .feather .mapping .MappingDataContainer ;
51
52
52
53
public class LvtNamer {
53
54
54
55
public static final HypoKey <Set <String >> SCOPED_NAMES = HypoKey .create ("Scoped Names" );
55
56
56
- private final MappingDataContainer mappings ;
57
57
private final HypoContext context ;
58
+ private final MappingSet mappings ;
58
59
private final LvtSuggester lvtSuggester ;
60
+
59
61
public final Map <String , AtomicInteger > missedNameSuggestions = new ConcurrentHashMap <>();
60
62
61
- public LvtNamer (final HypoContext context , final MappingDataContainer mappings ) throws IOException {
63
+ public LvtNamer (final HypoContext context , final MappingSet mappings ) throws IOException {
62
64
this .mappings = mappings ;
63
65
this .context = context ;
64
66
this .lvtSuggester = new LvtSuggester (context , this .missedNameSuggestions );
@@ -74,6 +76,10 @@ public void fillNames(final MethodData method) throws IOException {
74
76
final @ Nullable Set <String > names = method .get (SCOPED_NAMES );
75
77
if (names != null ) {
76
78
// If scoped names is already filled out, this method has already been visited
79
+ // We don't need to be concerned with a single thread being processed by multiple threads,
80
+ // it can happen, but that will simply result in the same output, which is still consistent.
81
+ // This is fast enough that a little bit of duplicate work is acceptable, not worth trying
82
+ // to prevent it.
77
83
return ;
78
84
}
79
85
@@ -131,13 +137,9 @@ public void fillNames(final MethodData method) throws IOException {
131
137
this .fillNames (outerMethod );
132
138
}
133
139
134
- final MappingDataContainer .@ Nullable MethodData methodMapping ;
135
- final MappingDataContainer .@ Nullable ClassData classMapping = this .mappings .getClass (parentClass .name ());
136
- if (classMapping == null ) {
137
- methodMapping = null ;
138
- } else {
139
- methodMapping = classMapping .getMethod (method .name (), method .descriptorText ());
140
- }
140
+ final Optional <MethodMapping > methodMapping = this .mappings
141
+ .getClassMapping (parentClass .name ())
142
+ .flatMap (c -> c .getMethodMapping (method .name (), method .descriptorText ()));
141
143
142
144
// We inherit names from our outer scope, if it exists. These names will be included in our scope for any
143
145
// potential inner scopes (other nested lambdas or local classes) that are present in this method too
@@ -162,12 +164,12 @@ public void fillNames(final MethodData method) throws IOException {
162
164
163
165
for (int i = 0 ; i < paramCount ; i ++) {
164
166
// always (i + 1) because abstract methods are never static
165
- final MappingDataContainer . @ Nullable ParameterData paramMapping =
166
- methodMapping != null ? methodMapping . getParameter (( byte ) ( i + 1 )) : null ;
167
- @ Nullable String paramName = null ;
168
- if ( paramMapping != null ) {
169
- paramName = paramMapping . getName ();
170
- }
167
+ final int fi = i ;
168
+ @ Nullable
169
+ String paramName = methodMapping
170
+ . flatMap ( m -> m . getParameterMapping ( fi + 1 ))
171
+ . map ( Mapping :: getDeobfuscatedName )
172
+ . orElse ( null );
171
173
172
174
if (paramName == null ) {
173
175
paramName = LvtTypeSuggester .suggestNameFromType (this .context , paramTypes .get (i ));
@@ -267,31 +269,18 @@ public void fillNames(final MethodData method) throws IOException {
267
269
}
268
270
}
269
271
272
+ final @ Nullable String paramName = methodMapping
273
+ .flatMap (m -> m .getParameterMapping (lvt .index ))
274
+ .map (Mapping ::getDeobfuscatedName )
275
+ .orElse (null );
276
+
270
277
@ Nullable String mappedName = null ;
271
- if (methodMapping != null ) {
272
- final MappingDataContainer .@ Nullable ParameterData paramMapping =
273
- methodMapping .getParameter ((byte ) lvt .index );
274
- if (paramMapping != null ) {
275
- final @ Nullable String paramName = paramMapping .getName ();
276
- if (paramName != null ) {
277
- mappedName = LvtSuggester .determineFinalName (paramName , scopedNames );
278
- }
279
- }
278
+ if (paramName != null ) {
279
+ mappedName = LvtSuggester .determineFinalName (paramName , scopedNames );
280
280
}
281
281
282
- final String selectedName ;
283
- if (mappedName != null ) {
284
- selectedName = mappedName ;
285
- } else {
286
- @ Nullable String name = null ;
287
- if (parentClass .kind () == ClassKind .RECORD && method .name ().equals ("<init>" )) {
288
- name = this .remapRecordParameter (lvt , method );
289
- }
290
- if (name == null ) {
291
- name = this .lvtSuggester .suggestName (node , lvt , scopedNames );
292
- }
293
- selectedName = name ;
294
- }
282
+ final String selectedName =
283
+ mappedName != null ? mappedName : this .lvtSuggester .suggestName (node , lvt , scopedNames );
295
284
296
285
lvt .name = selectedName ;
297
286
usedNames [usedNameIndex ++] = new UsedLvtName (lvt .name , lvt .desc , lvt .index );
@@ -308,30 +297,6 @@ public void fillNames(final MethodData method) throws IOException {
308
297
309
298
private record UsedLvtName (String name , String desc , int index ) {}
310
299
311
- private @ Nullable String remapRecordParameter (final LocalVariableNode lvt , final MethodData method ) {
312
- // use record component names for primary constructor
313
- final int paramIndex = fromLvtToParamIndex (lvt .index , method );
314
- if (paramIndex == -1 ) {
315
- return null ;
316
- }
317
- final @ Nullable List <FieldData > comp = method .parentClass ().recordComponents ();
318
- if (comp == null ) {
319
- throw new IllegalStateException ("No record components found on record" );
320
- }
321
-
322
- if (comp .size () != method .params ().size ()) {
323
- return null ;
324
- } else {
325
- for (int i = 0 ; i < comp .size (); i ++) {
326
- if (!comp .get (i ).fieldType ().equals (method .param (i ))) {
327
- return null ;
328
- }
329
- }
330
- }
331
-
332
- return comp .get (paramIndex ).name ();
333
- }
334
-
335
300
private static int find (final int [] array , final int value ) {
336
301
return find (array , value , array .length );
337
302
}
0 commit comments