Skip to content

Commit 6f8cada

Browse files
Expose visitors to API (#153)
1 parent 4b5cf69 commit 6f8cada

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

src/main/java/net/fabricmc/tinyremapper/AsmClassRemapper.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected void onVisit(VisitKind kind) {
167167
private boolean sourceNameVisited;
168168
private MethodNode methodNode;
169169

170-
private static class AsmFieldRemapper extends FieldRemapper {
170+
static class AsmFieldRemapper extends FieldRemapper {
171171
AsmFieldRemapper(FieldVisitor fieldVisitor, AsmRemapper remapper) {
172172
super(fieldVisitor, remapper);
173173
}
@@ -178,8 +178,29 @@ public AnnotationVisitor createAnnotationRemapper(String descriptor, AnnotationV
178178
}
179179
}
180180

181-
private static class AsmMethodRemapper extends MethodRemapper {
181+
static class AsmMethodRemapper extends MethodRemapper {
182182
private final TinyRemapper tr;
183+
AsmMethodRemapper(MethodVisitor methodVisitor,
184+
AsmRemapper remapper,
185+
String owner,
186+
int access,
187+
String name,
188+
String desc,
189+
boolean skipLocalMapping,
190+
boolean renameInvalidLocals,
191+
Pattern invalidLvNamePattern,
192+
boolean inferNameFromSameLvIndex) {
193+
this(methodVisitor,
194+
remapper,
195+
owner,
196+
!skipLocalMapping || renameInvalidLocals ? new MethodNode(Opcodes.ASM9, access, name, desc, null, null) : null,
197+
false,
198+
skipLocalMapping,
199+
renameInvalidLocals,
200+
invalidLvNamePattern,
201+
inferNameFromSameLvIndex);
202+
}
203+
183204
AsmMethodRemapper(MethodVisitor methodVisitor,
184205
AsmRemapper remapper,
185206
String owner,
@@ -699,7 +720,7 @@ private static boolean isJavaKeyword(String s) {
699720
private final boolean inferNameFromSameLvIndex;
700721
}
701722

702-
private static class AsmRecordComponentRemapper extends RecordComponentRemapper {
723+
static class AsmRecordComponentRemapper extends RecordComponentRemapper {
703724
AsmRecordComponentRemapper(RecordComponentVisitor recordComponentVisitor, AsmRemapper remapper) {
704725
super(recordComponentVisitor, remapper);
705726
}
@@ -714,7 +735,7 @@ public AnnotationVisitor createAnnotationRemapper(String descriptor, AnnotationV
714735
* Since sfPlayer want to infer the method descriptor when possible, we need to implement all remapping logic by
715736
* ourselves.
716737
*/
717-
private static class AsmAnnotationRemapper extends AnnotationVisitor {
738+
static class AsmAnnotationRemapper extends AnnotationVisitor {
718739
protected final String descriptor;
719740
protected final AsmRemapper remapper;
720741

src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@
5252
import java.util.stream.Collectors;
5353
import java.util.zip.ZipError;
5454

55+
import org.objectweb.asm.AnnotationVisitor;
5556
import org.objectweb.asm.ClassReader;
5657
import org.objectweb.asm.ClassVisitor;
5758
import org.objectweb.asm.ClassWriter;
5859
import org.objectweb.asm.FieldVisitor;
5960
import org.objectweb.asm.MethodVisitor;
6061
import org.objectweb.asm.Opcodes;
62+
import org.objectweb.asm.RecordComponentVisitor;
6163
import org.objectweb.asm.commons.Remapper;
6264
import org.objectweb.asm.util.CheckClassAdapter;
6365

@@ -1138,6 +1140,69 @@ private byte[] apply(final ClassInstance cls) {
11381140
return writer.toByteArray();
11391141
}
11401142

1143+
/**
1144+
* Creates a class visitor which remaps the visited class before passing it to the downstream visitor.
1145+
*
1146+
* <p><strong>This class visitor will only remap, any registered pre- and post-visitors will not be returned by
1147+
* this method! Since the class is only analyzed in isolation, package access fixes will also not be applied.</strong>
1148+
*
1149+
* <p>This method will use the default multi-release jar (MRJ) context, i.e. no context from classes in
1150+
* {@code META-INF/versions/*}.
1151+
*
1152+
* @param delegate The downstream visitor called with the remapped class.
1153+
* @return The remapping class visitor.
1154+
*/
1155+
public ClassVisitor createClassRemapperVisitor(ClassVisitor delegate) {
1156+
return new AsmClassRemapper(delegate, defaultState.remapper, rebuildSourceFilenames,
1157+
false, skipLocalMapping, renameInvalidLocals, invalidLvNamePattern, inferNameFromSameLvIndex);
1158+
}
1159+
1160+
/**
1161+
* Creates a field visitor which remaps the visited field before passing it to the downstream visitor.
1162+
*
1163+
* @param delegate The downstream visitor called with the remapped field.
1164+
* @return The remapping field visitor.
1165+
*/
1166+
public FieldVisitor createFieldRemapperVisitor(FieldVisitor delegate) {
1167+
return new AsmClassRemapper.AsmFieldRemapper(delegate, defaultState.remapper);
1168+
}
1169+
1170+
/**
1171+
* Creates a method visitor which remaps the visited method before passing it to the downstream visitor.
1172+
*
1173+
* @param delegate The downstream visitor called with the remapped method.
1174+
* @param owner The internal name of the class owning the method.
1175+
* @param access The access flags of the method.
1176+
* @param name The name of the method being visited.
1177+
* @param desc The descriptor of the method being visited.
1178+
* @return The remapping method visitor.
1179+
*/
1180+
public MethodVisitor createMethodRemapperVisitor(MethodVisitor delegate, String owner, int access, String name, String desc) {
1181+
return new AsmClassRemapper.AsmMethodRemapper(delegate, defaultState.remapper, owner, access, name, desc,
1182+
skipLocalMapping, renameInvalidLocals, invalidLvNamePattern, inferNameFromSameLvIndex);
1183+
}
1184+
1185+
/**
1186+
* Creates a record component visitor which remaps the visited record component before passing it to the downstream visitor.
1187+
*
1188+
* @param delegate The downstream visitor called with the remapped record component.
1189+
* @return The remapping record component visitor.
1190+
*/
1191+
public RecordComponentVisitor createRecordComponentRemapperVisitor(RecordComponentVisitor delegate) {
1192+
return new AsmClassRemapper.AsmRecordComponentRemapper(delegate, defaultState.remapper);
1193+
}
1194+
1195+
/**
1196+
* Creates an annotation visitor which remaps the visited annotation before passing it to the downstream visitor.
1197+
*
1198+
* @param delegate The downstream visitor that receives the remapped annotation.
1199+
* @param desc The descriptor of the annotation.
1200+
* @return The remapping annotation visitor.
1201+
*/
1202+
public AnnotationVisitor createAnnotationRemapperVisitor(AnnotationVisitor delegate, String desc) {
1203+
return new AsmClassRemapper.AsmAnnotationRemapper(desc, delegate, defaultState.remapper);
1204+
}
1205+
11411206
private byte[] fixClass(ClassInstance cls, byte[] data) {
11421207
boolean makeClsPublic = classesToMakePublic.contains(cls);
11431208
Set<String> clsMembersToMakePublic = null;

0 commit comments

Comments
 (0)