Skip to content

Commit e076ac7

Browse files
authored
Direct port to class tweaker (#1398)
* Direct port to class tweaker * Debugging help * Checkstyle * Update CT
1 parent c08bfbe commit e076ac7

File tree

13 files changed

+155
-152
lines changed

13 files changed

+155
-152
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ dependencies {
103103

104104
// tinyfile management
105105
implementation libs.fabric.tiny.remapper
106-
implementation libs.fabric.access.widener
106+
implementation libs.fabric.clazz.tweaker
107107
implementation libs.fabric.mapping.io
108108
implementation (libs.fabric.lorenz.tiny) {
109109
transitive = false

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ gson = "2.10.1"
55

66
stitch = "0.6.2"
77
tiny-remapper = "0.12.0"
8-
access-widener = "2.1.0"
8+
clazz-tweaker = "0.1.1"
99
mapping-io = "0.7.1"
1010
lorenz-tiny = "4.0.2"
1111
mercury = "0.4.2"
@@ -30,7 +30,7 @@ gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
3030

3131
fabric-stitch = { module = "net.fabricmc:stitch", version.ref = "stitch" }
3232
fabric-tiny-remapper = { module = "net.fabricmc:tiny-remapper", version.ref = "tiny-remapper" }
33-
fabric-access-widener = { module = "net.fabricmc:access-widener", version.ref = "access-widener" }
33+
fabric-clazz-tweaker = { module = "net.fabricmc:class-tweaker", version.ref = "clazz-tweaker" }
3434
fabric-mapping-io = { module = "net.fabricmc:mapping-io", version.ref = "mapping-io" }
3535
fabric-lorenz-tiny = { module = "net.fabricmc:lorenz-tiny", version.ref = "lorenz-tiny" }
3636
fabric-mercury = { module = "net.fabricmc:mercury", version.ref = "mercury" }

src/main/java/net/fabricmc/loom/configuration/accesswidener/AccessWidenerEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
import org.jetbrains.annotations.Nullable;
3030

31-
import net.fabricmc.accesswidener.AccessWidenerVisitor;
31+
import net.fabricmc.classtweaker.api.visitor.ClassTweakerVisitor;
3232
import net.fabricmc.loom.util.LazyCloseable;
3333
import net.fabricmc.loom.util.fmj.ModEnvironment;
3434
import net.fabricmc.tinyremapper.TinyRemapper;
@@ -44,5 +44,5 @@ public interface AccessWidenerEntry {
4444

4545
String getSortKey();
4646

47-
void read(AccessWidenerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException;
47+
void read(ClassTweakerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException;
4848
}

src/main/java/net/fabricmc/loom/configuration/accesswidener/AccessWidenerJarProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.gradle.api.file.RegularFileProperty;
3939
import org.jetbrains.annotations.Nullable;
4040

41-
import net.fabricmc.accesswidener.AccessWidener;
41+
import net.fabricmc.classtweaker.api.ClassTweaker;
4242
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
4343
import net.fabricmc.loom.api.processor.MinecraftJarProcessor;
4444
import net.fabricmc.loom.api.processor.ProcessorContext;
@@ -131,7 +131,7 @@ private static boolean isSupported(ModEnvironment modEnvironment, ProcessorConte
131131
public void processJar(Path jar, AccessWidenerJarProcessor.Spec spec, ProcessorContext context) throws IOException {
132132
final List<AccessWidenerEntry> accessWideners = spec.accessWidenersForContext(context);
133133

134-
final var accessWidener = new AccessWidener();
134+
final var accessWidener = ClassTweaker.newInstance();
135135

136136
try (LazyCloseable<TinyRemapper> remapper = context.createRemapper(MappingsNamespace.INTERMEDIARY, MappingsNamespace.NAMED)) {
137137
for (AccessWidenerEntry widener : accessWideners) {

src/main/java/net/fabricmc/loom/configuration/accesswidener/AccessWidenerTransformer.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,17 @@
3737
import org.slf4j.Logger;
3838
import org.slf4j.LoggerFactory;
3939

40-
import net.fabricmc.accesswidener.AccessWidener;
41-
import net.fabricmc.accesswidener.AccessWidenerClassVisitor;
40+
import net.fabricmc.classtweaker.api.ClassTweaker;
4241
import net.fabricmc.loom.util.Constants;
4342
import net.fabricmc.loom.util.Pair;
4443
import net.fabricmc.loom.util.ZipUtils;
4544

4645
final class AccessWidenerTransformer {
4746
private static final Logger LOGGER = LoggerFactory.getLogger(AccessWidenerTransformer.class);
4847

49-
private final AccessWidener accessWidener;
48+
private final ClassTweaker accessWidener;
5049

51-
AccessWidenerTransformer(AccessWidener accessWidener) {
50+
AccessWidenerTransformer(ClassTweaker accessWidener) {
5251
this.accessWidener = accessWidener;
5352
}
5453

@@ -57,25 +56,37 @@ final class AccessWidenerTransformer {
5756
*/
5857
void apply(Path jarFile) {
5958
try {
60-
ZipUtils.transform(jarFile, getTransformers(accessWidener.getTargets()));
59+
Set<String> targets = accessWidener.getTargets();
60+
int transformed = ZipUtils.transform(jarFile, getTransformers(targets));
61+
62+
LOGGER.debug("Applied access wideners to {} classes in {}", transformed, jarFile);
63+
64+
if (targets.size() != transformed) {
65+
LOGGER.debug("Access widener target count ({}) does not match transformed class count ({}).", targets.size(), transformed);
66+
}
6167
} catch (IOException e) {
6268
throw new UncheckedIOException("Failed to apply access wideners to %s".formatted(jarFile), e);
6369
}
6470
}
6571

6672
private List<Pair<String, ZipUtils.UnsafeUnaryOperator<byte[]>>> getTransformers(Set<String> classes) {
6773
return classes.stream()
68-
.map(string -> new Pair<>(string.replaceAll("\\.", "/") + ".class", getTransformer(string)))
74+
.map(string -> new Pair<>(string + ".class", getTransformer(string)))
6975
.collect(Collectors.toList());
7076
}
7177

7278
private ZipUtils.UnsafeUnaryOperator<byte[]> getTransformer(String className) {
7379
return input -> {
7480
ClassReader reader = new ClassReader(input);
81+
82+
if (!reader.getClassName().equals(className)) {
83+
throw new IllegalStateException("Class name mismatch: expected %s but transforming %s".formatted(className, reader.getClassName()));
84+
}
85+
7586
ClassWriter writer = new ClassWriter(0);
76-
ClassVisitor classVisitor = AccessWidenerClassVisitor.createClassVisitor(Constants.ASM_VERSION, writer, accessWidener);
87+
ClassVisitor classVisitor = accessWidener.createClassVisitor(Constants.ASM_VERSION, writer, null);
7788

78-
LOGGER.debug("Applying access widener to " + className);
89+
LOGGER.debug("Applying access widener to {}", className);
7990

8091
reader.accept(classVisitor, 0);
8192
return writer.toByteArray();

src/main/java/net/fabricmc/loom/configuration/accesswidener/LocalAccessWidenerEntry.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
import org.jetbrains.annotations.Nullable;
3232

33-
import net.fabricmc.accesswidener.AccessWidenerReader;
34-
import net.fabricmc.accesswidener.AccessWidenerVisitor;
33+
import net.fabricmc.classtweaker.api.ClassTweakerReader;
34+
import net.fabricmc.classtweaker.api.visitor.ClassTweakerVisitor;
3535
import net.fabricmc.loom.util.Checksum;
3636
import net.fabricmc.loom.util.LazyCloseable;
3737
import net.fabricmc.loom.util.fmj.ModEnvironment;
@@ -43,9 +43,9 @@ public static LocalAccessWidenerEntry create(Path path) {
4343
}
4444

4545
@Override
46-
public void read(AccessWidenerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException {
47-
var reader = new AccessWidenerReader(visitor);
48-
reader.read(Files.readAllBytes(path));
46+
public void read(ClassTweakerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException {
47+
var reader = ClassTweakerReader.create(visitor);
48+
reader.read(Files.readAllBytes(path), null);
4949
}
5050

5151
@Override

src/main/java/net/fabricmc/loom/configuration/accesswidener/ModAccessWidenerEntry.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232

3333
import org.jetbrains.annotations.Nullable;
3434

35-
import net.fabricmc.accesswidener.AccessWidenerReader;
36-
import net.fabricmc.accesswidener.AccessWidenerRemapper;
37-
import net.fabricmc.accesswidener.AccessWidenerVisitor;
38-
import net.fabricmc.accesswidener.TransitiveOnlyFilter;
35+
import net.fabricmc.classtweaker.api.ClassTweakerReader;
36+
import net.fabricmc.classtweaker.api.visitor.ClassTweakerVisitor;
37+
import net.fabricmc.classtweaker.visitors.ClassTweakerRemapperVisitor;
38+
import net.fabricmc.classtweaker.visitors.TransitiveOnlyFilter;
3939
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
4040
import net.fabricmc.loom.util.LazyCloseable;
4141
import net.fabricmc.loom.util.fmj.FabricModJson;
@@ -67,26 +67,26 @@ public String getSortKey() {
6767
}
6868

6969
@Override
70-
public void read(AccessWidenerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException {
70+
public void read(ClassTweakerVisitor visitor, LazyCloseable<TinyRemapper> remapper) throws IOException {
7171
if (transitiveOnly) {
7272
// Filter for only transitive rules
7373
visitor = new TransitiveOnlyFilter(visitor);
7474
}
7575

7676
final byte[] data = readRaw();
77-
final AccessWidenerReader.Header header = AccessWidenerReader.readHeader(data);
77+
final ClassTweakerReader.Header header = ClassTweakerReader.readHeader(data);
7878

7979
if (!header.getNamespace().equals(MappingsNamespace.NAMED.toString())) {
8080
// Remap the AW if needed
8181
visitor = getRemapper(visitor, remapper.get());
8282
}
8383

84-
var reader = new AccessWidenerReader(visitor);
85-
reader.read(data);
84+
var reader = ClassTweakerReader.create(visitor);
85+
reader.read(data, mod.getId());
8686
}
8787

88-
private static AccessWidenerRemapper getRemapper(AccessWidenerVisitor visitor, TinyRemapper tinyRemapper) {
89-
return new AccessWidenerRemapper(
88+
private static ClassTweakerRemapperVisitor getRemapper(ClassTweakerVisitor visitor, TinyRemapper tinyRemapper) {
89+
return new ClassTweakerRemapperVisitor(
9090
visitor,
9191
tinyRemapper.getEnvironment().getRemapper(),
9292
MappingsNamespace.INTERMEDIARY.toString(),

src/main/java/net/fabricmc/loom/configuration/accesswidener/TransitiveAccessWidenerMappingsProcessor.java

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.slf4j.Logger;
3232
import 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;
3636
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
3737
import net.fabricmc.loom.api.processor.MappingProcessorContext;
3838
import net.fabricmc.loom.api.processor.MinecraftJarProcessor;
@@ -44,6 +44,8 @@
4444
public 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

Comments
 (0)