2929import dev .denwav .hypo .hydrate .generic .HypoHydration ;
3030import dev .denwav .hypo .hydrate .generic .MethodClosure ;
3131import dev .denwav .hypo .model .data .ClassData ;
32- import dev .denwav .hypo .model .data .ClassKind ;
33- import dev .denwav .hypo .model .data .FieldData ;
3432import dev .denwav .hypo .model .data .HypoKey ;
3533import dev .denwav .hypo .model .data .MethodData ;
3634import dev .denwav .hypo .model .data .types .JvmType ;
4038import java .util .HashSet ;
4139import java .util .List ;
4240import java .util .Map ;
41+ import java .util .Optional ;
4342import java .util .Set ;
4443import java .util .concurrent .ConcurrentHashMap ;
4544import 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 ;
4648import org .checkerframework .checker .nullness .qual .Nullable ;
4749import org .objectweb .asm .tree .LocalVariableNode ;
4850import org .objectweb .asm .tree .MethodNode ;
4951import org .objectweb .asm .tree .ParameterNode ;
50- import org .parchmentmc .feather .mapping .MappingDataContainer ;
5152
5253public class LvtNamer {
5354
5455 public static final HypoKey <Set <String >> SCOPED_NAMES = HypoKey .create ("Scoped Names" );
5556
56- private final MappingDataContainer mappings ;
5757 private final HypoContext context ;
58+ private final MappingSet mappings ;
5859 private final LvtSuggester lvtSuggester ;
60+
5961 public final Map <String , AtomicInteger > missedNameSuggestions = new ConcurrentHashMap <>();
6062
61- public LvtNamer (final HypoContext context , final MappingDataContainer mappings ) throws IOException {
63+ public LvtNamer (final HypoContext context , final MappingSet mappings ) throws IOException {
6264 this .mappings = mappings ;
6365 this .context = context ;
6466 this .lvtSuggester = new LvtSuggester (context , this .missedNameSuggestions );
@@ -74,6 +76,10 @@ public void fillNames(final MethodData method) throws IOException {
7476 final @ Nullable Set <String > names = method .get (SCOPED_NAMES );
7577 if (names != null ) {
7678 // 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.
7783 return ;
7884 }
7985
@@ -131,13 +137,9 @@ public void fillNames(final MethodData method) throws IOException {
131137 this .fillNames (outerMethod );
132138 }
133139
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 ()));
141143
142144 // We inherit names from our outer scope, if it exists. These names will be included in our scope for any
143145 // 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 {
162164
163165 for (int i = 0 ; i < paramCount ; i ++) {
164166 // 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 );
171173
172174 if (paramName == null ) {
173175 paramName = LvtTypeSuggester .suggestNameFromType (this .context , paramTypes .get (i ));
@@ -267,31 +269,18 @@ public void fillNames(final MethodData method) throws IOException {
267269 }
268270 }
269271
272+ final @ Nullable String paramName = methodMapping
273+ .flatMap (m -> m .getParameterMapping (lvt .index ))
274+ .map (Mapping ::getDeobfuscatedName )
275+ .orElse (null );
276+
270277 @ 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 );
280280 }
281281
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 );
295284
296285 lvt .name = selectedName ;
297286 usedNames [usedNameIndex ++] = new UsedLvtName (lvt .name , lvt .desc , lvt .index );
@@ -308,30 +297,6 @@ public void fillNames(final MethodData method) throws IOException {
308297
309298 private record UsedLvtName (String name , String desc , int index ) {}
310299
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-
335300 private static int find (final int [] array , final int value ) {
336301 return find (array , value , array .length );
337302 }
0 commit comments