3131import org .slf4j .Logger ;
3232import org .slf4j .LoggerFactory ;
3333
34- import net .fabricmc .accesswidener . AccessWidenerReader ;
35- import net .fabricmc .accesswidener . AccessWidenerVisitor ;
34+ import net .fabricmc .classtweaker . api . visitor . AccessWidenerVisitor ;
35+ import net .fabricmc .classtweaker . api . visitor . ClassTweakerVisitor ;
3636import net .fabricmc .loom .api .mappings .layered .MappingsNamespace ;
3737import net .fabricmc .loom .api .processor .MappingProcessorContext ;
3838import net .fabricmc .loom .api .processor .MinecraftJarProcessor ;
4444public final class TransitiveAccessWidenerMappingsProcessor implements MinecraftJarProcessor .MappingsProcessor <AccessWidenerJarProcessor .Spec > {
4545 public static final TransitiveAccessWidenerMappingsProcessor INSTANCE = new TransitiveAccessWidenerMappingsProcessor ();
4646
47+ private static final Logger LOGGER = LoggerFactory .getLogger (TransitiveAccessWidenerMappingsProcessor .class );
48+
4749 private TransitiveAccessWidenerMappingsProcessor () {
4850 }
4951
@@ -63,7 +65,7 @@ public boolean transform(MemoryMappingTree mappings, AccessWidenerJarProcessor.S
6365
6466 try (LazyCloseable <TinyRemapper > remapper = context .createRemapper (MappingsNamespace .INTERMEDIARY , MappingsNamespace .NAMED )) {
6567 for (AccessWidenerEntry accessWidener : accessWideners ) {
66- var visitor = new MappingCommentVisitor (accessWidener .mappingId (), mappings );
68+ var visitor = new MappingCommentClassTweakerVisitor (accessWidener .mappingId (), mappings );
6769 accessWidener .read (visitor , remapper );
6870 }
6971 } catch (IOException e ) {
@@ -73,80 +75,91 @@ public boolean transform(MemoryMappingTree mappings, AccessWidenerJarProcessor.S
7375 return true ;
7476 }
7577
76- private record MappingCommentVisitor (String modId , MemoryMappingTree mappingTree ) implements AccessWidenerVisitor {
77- private static final Logger LOGGER = LoggerFactory .getLogger (MappingCommentVisitor .class );
78-
78+ private record MappingCommentClassTweakerVisitor (String modId , MemoryMappingTree mappingTree ) implements ClassTweakerVisitor {
7979 @ Override
80- public void visitClass (String name , AccessWidenerReader .AccessType access , boolean transitive ) {
81- MappingTree .ClassMapping classMapping = mappingTree .getClass (name );
80+ public AccessWidenerVisitor visitAccessWidener (String owner ) {
81+ return new MappingCommentAccessWidenerVisitor (owner );
82+ }
8283
83- if (classMapping == null ) {
84- LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , name , modId ());
85- return ;
86- }
84+ private class MappingCommentAccessWidenerVisitor implements AccessWidenerVisitor {
85+ private final String className ;
8786
88- classMapping .setComment (appendComment (classMapping .getComment (), access ));
89- }
87+ private MappingCommentAccessWidenerVisitor (String className ) {
88+ this .className = className ;
89+ }
9090
91- @ Override
92- public void visitMethod (String owner , String name , String descriptor , AccessWidenerReader .AccessType access , boolean transitive ) {
93- // Access is also applied to the class, so also add the comment to the class
94- visitClass (owner , access , transitive );
91+ @ Override
92+ public void visitClass (AccessType access , boolean transitive ) {
93+ MappingTree .ClassMapping classMapping = mappingTree .getClass (className );
9594
96- MappingTree .ClassMapping classMapping = mappingTree .getClass (owner );
95+ if (classMapping == null ) {
96+ LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , className , modId ());
97+ return ;
98+ }
9799
98- if (classMapping == null ) {
99- LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , owner , modId ());
100- return ;
100+ classMapping .setComment (appendComment (classMapping .getComment (), access ));
101101 }
102102
103- MappingTree .MethodMapping methodMapping = classMapping .getMethod (name , descriptor );
103+ @ Override
104+ public void visitMethod (String name , String descriptor , AccessType access , boolean transitive ) {
105+ // Access is also applied to the class, so also add the comment to the class
106+ visitClass (access , transitive );
104107
105- if (methodMapping == null ) {
106- LOGGER .info ("Failed to find method ({}) in ({}) to mark access widened by mod ({})" , name , owner , modId ());
107- return ;
108- }
108+ MappingTree .ClassMapping classMapping = mappingTree .getClass (className );
109109
110- methodMapping .setComment (appendComment (methodMapping .getComment (), access ));
111- }
110+ if (classMapping == null ) {
111+ LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , className , modId ());
112+ return ;
113+ }
112114
113- @ Override
114- public void visitField (String owner , String name , String descriptor , AccessWidenerReader .AccessType access , boolean transitive ) {
115- // Access is also applied to the class, so also add the comment to the class
116- visitClass (owner , access , transitive );
115+ MappingTree .MethodMapping methodMapping = classMapping .getMethod (name , descriptor );
117116
118- MappingTree .ClassMapping classMapping = mappingTree .getClass (owner );
117+ if (methodMapping == null ) {
118+ LOGGER .info ("Failed to find method ({}) in ({}) to mark access widened by mod ({})" , name , className , modId ());
119+ return ;
120+ }
119121
120- if (classMapping == null ) {
121- LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , name , modId ());
122- return ;
122+ methodMapping .setComment (appendComment (methodMapping .getComment (), access ));
123123 }
124124
125- MappingTree .FieldMapping fieldMapping = classMapping .getField (name , descriptor );
125+ @ Override
126+ public void visitField (String name , String descriptor , AccessType access , boolean transitive ) {
127+ // Access is also applied to the class, so also add the comment to the class
128+ visitClass (access , transitive );
126129
127- if (fieldMapping == null ) {
128- LOGGER .info ("Failed to find field ({}) in ({}) to mark access widened by mod ({})" , name , owner , modId ());
129- return ;
130- }
130+ MappingTree .ClassMapping classMapping = mappingTree .getClass (className );
131131
132- fieldMapping .setComment (appendComment (fieldMapping .getComment (), access ));
133- }
132+ if (classMapping == null ) {
133+ LOGGER .info ("Failed to find class ({}) to mark access widened by mod ({})" , name , modId ());
134+ return ;
135+ }
134136
135- private String appendComment (String comment , AccessWidenerReader .AccessType access ) {
136- if (comment == null ) {
137- comment = "" ;
138- } else {
139- comment += "\n " ;
140- }
137+ MappingTree .FieldMapping fieldMapping = classMapping .getField (name , descriptor );
141138
142- String awComment = "Access widened by %s to %s" .formatted (modId (), access );
139+ if (fieldMapping == null ) {
140+ LOGGER .info ("Failed to find field ({}) in ({}) to mark access widened by mod ({})" , name , className , modId ());
141+ return ;
142+ }
143143
144- if (!comment .contains (awComment )) {
145- // Ensure we don't comment the same thing twice. A bit of a cheap way to do this, but should work ok.
146- comment += awComment ;
144+ fieldMapping .setComment (appendComment (fieldMapping .getComment (), access ));
147145 }
148146
149- return comment ;
147+ private String appendComment (String comment , AccessType access ) {
148+ if (comment == null ) {
149+ comment = "" ;
150+ } else {
151+ comment += "\n " ;
152+ }
153+
154+ String awComment = "Access widened by %s to %s" .formatted (modId (), access );
155+
156+ if (!comment .contains (awComment )) {
157+ // Ensure we don't comment the same thing twice. A bit of a cheap way to do this, but should work ok.
158+ comment += awComment ;
159+ }
160+
161+ return comment ;
162+ }
150163 }
151164 }
152165}
0 commit comments