mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-24 22:14:38 +00:00
refactor: filter Java mixins by side
This commit is contained in:
1 parent
a4818598cd
commit
f5d449d041
7 files changed
+160
-48
No files matched your search
+50
-7
@@ -19,6 +19,7 @@ import org.objectweb.asm.tree.TypeInsnNode;
|
||||
|
||||
import codechicken.lib.asm.ASMHelper;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.MixinInfo;
|
||||
import cpw.mods.fml.common.asm.transformers.SideTransformer;
|
||||
import scala.MatchError;
|
||||
import scala.collection.JavaConversions;
|
||||
|
||||
@@ -162,7 +163,7 @@ class JavaTraitRegistrationFunctionalTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void sideOnlyAnnotationsDoNotStripJavaMembersOrInitializationOnTheServer() throws Exception {
|
||||
void sideOnlyAnnotationsStripOppositeJavaMembersAndInitializationOnTheServer() throws Exception {
|
||||
try (Scope scope = new Scope()) {
|
||||
ClassNode parent = base("SideJavaBase");
|
||||
scope.define(parent);
|
||||
@@ -191,21 +192,63 @@ class JavaTraitRegistrationFunctionalTest {
|
||||
constant(input, "clientInvisible", "()I", 41, ACC_PUBLIC);
|
||||
method(input, "clientInvisible").visitAnnotation("Lcpw/mods/fml/relauncher/SideOnly;", false)
|
||||
.visitEnum("value", "Lcpw/mods/fml/relauncher/Side;", "CLIENT");
|
||||
MethodVisitor clientInvalid = input.visitMethod(ACC_PUBLIC, "clientInvalid", "()[I", null, null);
|
||||
clientInvalid.visitInsn(ICONST_1);
|
||||
clientInvalid.visitIntInsn(NEWARRAY, T_INT);
|
||||
clientInvalid.visitInsn(ARETURN);
|
||||
clientInvalid.visitMaxs(1, 1);
|
||||
clientInvalid.visitAnnotation("Lcpw/mods/fml/relauncher/SideOnly;", true)
|
||||
.visitEnum("value", "Lcpw/mods/fml/relauncher/Side;", "CLIENT");
|
||||
constant(input, "serverVisible", "()I", 43, ACC_PUBLIC);
|
||||
method(input, "serverVisible").visitAnnotation("Lcpw/mods/fml/relauncher/SideOnly;", true)
|
||||
.visitEnum("value", "Lcpw/mods/fml/relauncher/Side;", "SERVER");
|
||||
COMPILER.registerJavaTrait(input);
|
||||
MixinInfo info = COMPILER.getMixinInfo(input.name).get();
|
||||
assertEquals(Arrays.asList("clientVisible()I", "clientInvisible()I", "serverVisible()I"), signatures(info));
|
||||
assertEquals(Arrays.asList("serverVisible()I"), signatures(info));
|
||||
assertEquals(1, info.fields().size());
|
||||
assertEquals("serverField", info.fields().apply(0).name());
|
||||
Object value = COMPILER.mixinClasses(ROOT + "SideComposite", parent.name, seq(input.name))
|
||||
.getConstructor(int.class).newInstance(0);
|
||||
assertEquals(37, value.getClass().getMethod("clientVisible").invoke(value));
|
||||
assertEquals(41, value.getClass().getMethod("clientInvisible").invoke(value));
|
||||
assertThrows(NoSuchMethodException.class, () -> value.getClass().getMethod("clientVisible"));
|
||||
assertThrows(NoSuchMethodException.class, () -> value.getClass().getMethod("clientInvisible"));
|
||||
assertThrows(NoSuchMethodException.class, () -> value.getClass().getMethod("clientInvalid"));
|
||||
assertEquals(43, value.getClass().getMethod("serverVisible").invoke(value));
|
||||
String fieldPrefix = input.name.replace('/', '$') + "$$";
|
||||
assertEquals(11, value.getClass().getMethod(fieldPrefix + "clientVisibleField").invoke(value));
|
||||
assertEquals(13, value.getClass().getMethod(fieldPrefix + "clientInvisibleField").invoke(value));
|
||||
assertEquals(17, value.getClass().getMethod(fieldPrefix + "serverField").invoke(value));
|
||||
assertThrows(
|
||||
NoSuchMethodException.class,
|
||||
() -> value.getClass().getMethod(fieldPrefix + "clientVisibleField"));
|
||||
assertThrows(
|
||||
NoSuchMethodException.class,
|
||||
() -> value.getClass().getMethod(fieldPrefix + "clientInvisibleField"));
|
||||
assertEquals(0, value.getClass().getMethod(fieldPrefix + "serverField").invoke(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void alreadyStrippedForgeConstructorStillSuppliesAnEmptyMixinInitializer() throws Exception {
|
||||
try (Scope scope = new Scope()) {
|
||||
ClassNode parent = base("PrestrippedSideJavaBase");
|
||||
scope.define(parent);
|
||||
ClassNode input = input("PrestrippedSide", parent.name);
|
||||
input.visitField(ACC_PRIVATE, "state", "I", null, null);
|
||||
MethodNode constructor = method(input, "<init>");
|
||||
constructor.visitAnnotation("Lcpw/mods/fml/relauncher/SideOnly;", true)
|
||||
.visitEnum("value", "Lcpw/mods/fml/relauncher/Side;", "CLIENT");
|
||||
constructor.instructions.remove(constructor.instructions.getLast());
|
||||
initialize(constructor, input.name, "state", 29);
|
||||
constructor.visitInsn(RETURN);
|
||||
constructor.visitMaxs(2, 1);
|
||||
constant(input, "available", "()I", 47, ACC_PUBLIC);
|
||||
ClassNode stripped = ASMHelper.createClassNode(
|
||||
new SideTransformer().transform(input.name, input.name, ASMHelper.createBytes(input, 0)),
|
||||
0);
|
||||
assertTrue(stripped.methods.stream().noneMatch(m -> ((MethodNode) m).name.equals("<init>")));
|
||||
COMPILER.registerJavaTrait(stripped);
|
||||
Object value = COMPILER.mixinClasses(ROOT + "PrestrippedSideComposite", parent.name, seq(input.name))
|
||||
.getConstructor(int.class).newInstance(7);
|
||||
assertEquals(7, value.getClass().getMethod("score").invoke(value));
|
||||
assertEquals(47, value.getClass().getMethod("available").invoke(value));
|
||||
assertEquals(0, value.getClass().getMethod(input.name.replace('/', '$') + "$$state").invoke(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.AbstractInsnNode;
|
||||
import org.objectweb.asm.tree.AnnotationNode;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldInsnNode;
|
||||
import org.objectweb.asm.tree.FieldNode;
|
||||
@@ -24,6 +25,8 @@ import codechicken.lib.asm.InsnComparator;
|
||||
import codechicken.lib.asm.InsnListSection;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.FieldMixin;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.MixinInfo;
|
||||
import cpw.mods.fml.relauncher.FMLLaunchHandler;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import scala.Function1;
|
||||
import scala.Option;
|
||||
import scala.Tuple2;
|
||||
@@ -48,14 +51,21 @@ final class JavaTraitRegistration {
|
||||
"Inner classes are not permitted for " + input.name + " as a java mixin trait. Use scala");
|
||||
ASMMixinCompiler$ compiler = ASMMixinCompiler$.MODULE$;
|
||||
Option<MixinInfo> parentTrait = compiler.getMixinInfo(input.superName);
|
||||
Buffer<FieldNode> sourceFields = JavaConversions.asScalaBuffer(input.fields);
|
||||
Map<String, FieldMixin> fields = scala.collection.immutable.Map$.MODULE$.empty();
|
||||
for (FieldNode field : iterable(sourceFields))
|
||||
fields = fields.$plus(new Tuple2<>(field.name, new FieldMixin(field.name, field.desc, field.access)));
|
||||
for (FieldNode field : input.fields) {
|
||||
if (!excludedOnCurrentSide(field.visibleAnnotations, field.invisibleAnnotations))
|
||||
fields = fields.$plus(new Tuple2<>(field.name, new FieldMixin(field.name, field.desc, field.access)));
|
||||
}
|
||||
ListBuffer<String> supers = new ListBuffer<>();
|
||||
ListBuffer<MethodNode> methods = new ListBuffer<>();
|
||||
java.util.Set<String> methodSignatures = new LinkedHashSet<>();
|
||||
for (MethodNode method : input.methods) methodSignatures.add(method.name + method.desc);
|
||||
List<MethodNode> sourceMethods = new ArrayList<>();
|
||||
for (MethodNode method : input.methods) {
|
||||
if (!excludedOnCurrentSide(method.visibleAnnotations, method.invisibleAnnotations)) {
|
||||
sourceMethods.add(method);
|
||||
methodSignatures.add(method.name + method.desc);
|
||||
}
|
||||
}
|
||||
|
||||
ClassNode implementation = new ClassNode();
|
||||
implementation.visit(V1_6, ACC_ABSTRACT | ACC_PUBLIC, input.name + "$class", null, "java/lang/Object", null);
|
||||
@@ -78,13 +88,19 @@ final class JavaTraitRegistration {
|
||||
}
|
||||
|
||||
Context context = new Context(input, implementation, contract, fields, methodSignatures, supers, methods);
|
||||
Buffer<MethodNode> sourceMethods = JavaConversions.asScalaBuffer(input.methods);
|
||||
Buffer<MethodNode> selected = (Buffer<MethodNode>) ((TraversableLike) sourceMethods)
|
||||
.filterNot(fn(context::isGeneratedFieldAccessor));
|
||||
Buffer<MethodNode> selected = (Buffer<MethodNode>) ((TraversableLike) JavaConversions
|
||||
.asScalaBuffer(sourceMethods)).filterNot(fn(context::isGeneratedFieldAccessor));
|
||||
selected.foreach(fn(method -> {
|
||||
context.convertMethod(method);
|
||||
return scala.runtime.BoxedUnit.UNIT;
|
||||
}));
|
||||
// Forge may have stripped the constructor before this ClassNode reaches the compiler.
|
||||
if (!methodSignatures.contains("<init>()V")) {
|
||||
MethodNode initializer = (MethodNode) implementation
|
||||
.visitMethod(ACC_PUBLIC | ACC_STATIC, "$init$", "(L" + input.name + ";)V", null, null);
|
||||
initializer.visitInsn(RETURN);
|
||||
initializer.visitMaxs(0, 1);
|
||||
}
|
||||
|
||||
compiler.define(implementation.name, ASMHelper.createBytes(implementation, 0));
|
||||
compiler.define(contract.name, ASMHelper.createBytes(contract, 0));
|
||||
@@ -102,6 +118,27 @@ final class JavaTraitRegistration {
|
||||
supers));
|
||||
}
|
||||
|
||||
private static boolean excludedOnCurrentSide(List<AnnotationNode> visible, List<AnnotationNode> invisible) {
|
||||
return excludedOnCurrentSide(visible) || excludedOnCurrentSide(invisible);
|
||||
}
|
||||
|
||||
private static boolean excludedOnCurrentSide(List<AnnotationNode> annotations) {
|
||||
if (annotations == null) return false;
|
||||
String side = FMLLaunchHandler.side().name();
|
||||
String descriptor = Type.getDescriptor(SideOnly.class);
|
||||
for (AnnotationNode annotation : annotations) {
|
||||
if (!Objects.equals(annotation.desc, descriptor) || annotation.values == null) continue;
|
||||
for (int i = 0; i < annotation.values.size() - 1; i += 2) {
|
||||
if (!Objects.equals(annotation.values.get(i), "value")) continue;
|
||||
Object value = annotation.values.get(i + 1);
|
||||
if (value instanceof String[] && ((String[]) value).length > 1
|
||||
&& !Objects.equals(((String[]) value)[1], side))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final class Context {
|
||||
|
||||
final ClassNode input;
|
||||
|
||||
Reference in new issue
Block a user