A small library mod that allows other mods to use the Java 24 ClassFile API to directly edit the code of classes as they are loaded.
A simple example implementation could be:
public class Example implements McfaInitializer {
@Override
public void initializeTransformers(@NotNull Mcfa registry) {
registry.registerTransformer(
"net.minecraft.class_310",
ClassTransform.transformingMethods(
MethodTransform.transformingCode(
(builder, element) -> {
var result = switch(element) {
case ConstantInstruction instruction -> switch(instruction.typeKind()) {
case REFERENCE -> {
var value = instruction.constantValue();
if(value.equals("Setting user: {}")) {
builder.ldc("This got changed via the ClassFile API :-) {}");
yield true;
}
yield false;
}
default -> false;
};
default -> false;
};
if(result) {
return;
}
builder.with(element);
}
)
)
);
}
}