mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-20 12:19:52 +00:00
Require pre-super redirector handlers to be static, closes #174, v0.6.10
This commit is contained in:
1 parent
3329a3d084
commit
8a169bb7c0
7 files changed
+178
-63
No files matched your search
@@ -62,7 +62,7 @@ public abstract class MixinBootstrap {
|
||||
/**
|
||||
* Subsystem version
|
||||
*/
|
||||
public static final String VERSION = "0.6.9";
|
||||
public static final String VERSION = "0.6.10";
|
||||
|
||||
// Consts
|
||||
private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch.";
|
||||
|
||||
@@ -47,15 +47,14 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* object instance the method was going to be invoked on. For example when
|
||||
* hooking the following call:</p>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* public void baz(int someInt, String someString) {
|
||||
* int abc = 0;
|
||||
* int def = 1;
|
||||
* Foo someObject = new Foo();
|
||||
*
|
||||
* // Hooking this method
|
||||
* boolean xyz = someObject.bar(abc, def);
|
||||
* }</pre>
|
||||
* <blockquote><pre>public void baz(int someInt, String someString) {
|
||||
* int abc = 0;
|
||||
* int def = 1;
|
||||
* Foo someObject = new Foo();
|
||||
*
|
||||
* // Hooking this method
|
||||
* boolean xyz = someObject.bar(abc, def);
|
||||
*}</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>The signature of the redirected method should be:</p>
|
||||
@@ -75,7 +74,7 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
*
|
||||
* <blockquote>
|
||||
* <pre>public boolean barProxy(Foo someObject, int abc, int def,
|
||||
* int someInt, String someString)</pre>
|
||||
* int someInt, String someString)</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <h4>Field Access Redirect Mode</h4>
|
||||
@@ -121,12 +120,11 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* possible to redirect access to individual array elements. Consider the
|
||||
* following example:</p>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* private String[] strings = { "foo", "bar", "baz" };
|
||||
*
|
||||
* public void print(int index) {
|
||||
* System.err.println(this.strings[index]);
|
||||
* }</pre>
|
||||
* <blockquote><pre>private String[] strings = { "foo", "bar", "baz" };
|
||||
*
|
||||
*public void print(int index) {
|
||||
* System.err.println(this.strings[index]);
|
||||
*}</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>It may be desirable to redirect the access to this array element. To do
|
||||
@@ -162,11 +160,10 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
* and the return type must match the type of object being constructed. For
|
||||
* example to redirect the following constructor call:</p>
|
||||
*
|
||||
* <blockquote><pre>
|
||||
* public void baz(int someInt, String someString) {
|
||||
* // Hooking this constructor
|
||||
* Foo someObject = new Foo("bar");
|
||||
* }</pre>
|
||||
* <blockquote><pre>public void baz(int someInt, String someString) {
|
||||
* // Hooking this constructor
|
||||
* Foo someObject = new Foo("bar");
|
||||
*}</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>The signature of the handler method should be:</p>
|
||||
@@ -177,6 +174,34 @@ import org.spongepowered.asm.util.ConstraintParser.Constraint;
|
||||
*
|
||||
* <p>Note that like other redirectors, it is possible to capture the target
|
||||
* method's arguments by appending them to the handler method's signature.</p>
|
||||
*
|
||||
* <h4>A note on <tt>static</tt> modifiers for handler methods</h4>
|
||||
*
|
||||
* <p>In general, when declaring a redirect handler the <tt>static</tt> modifier
|
||||
* of the handler method must always match the target method. The exception to
|
||||
* this rule is application of <tt>@Redirect</tt> to instructions which are
|
||||
* effectively <b>before</b> the call to <tt>super()</tt> in a constructor.</p>
|
||||
*
|
||||
* <p>Consider the following code:</p>
|
||||
*
|
||||
* <blockquote><pre>class Foo : Bar {
|
||||
* Foo(int arg) {
|
||||
* super(<span style="background: #FF0">Foo.isEven(arg)</span>);
|
||||
* }
|
||||
*
|
||||
* static int isEven(int arg) {
|
||||
* return arg % 2 == 0;
|
||||
* }
|
||||
*}</pre>
|
||||
* </blockquote>
|
||||
*
|
||||
* <p>In this example, the inline call to <tt>Foo.isEven</tt> takes place
|
||||
* <em>before</em> the call to <tt>super()</tt> and (per Java rules) this method
|
||||
* must be static (because the instance (<tt>this</tt>) is unavailable until the
|
||||
* initialiser is complete. Thus if you wish to <tt>@Redirect</tt> the call
|
||||
* to <tt>isEven</tt> then the handler method must also be <tt>static</tt>. The
|
||||
* injection subsystem will raise an exception for non-static handlers in this
|
||||
* situation to indicate that the staticness of the handler is incorrect.</p>
|
||||
*/
|
||||
@Target({ ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
package org.spongepowered.asm.mixin.injection.invoke;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -51,9 +50,8 @@ import org.spongepowered.asm.mixin.injection.points.BeforeNew;
|
||||
import org.spongepowered.asm.mixin.injection.struct.InjectionInfo;
|
||||
import org.spongepowered.asm.mixin.injection.struct.Target;
|
||||
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionException;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Constants;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
@@ -163,7 +161,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
*/
|
||||
@Override
|
||||
protected void checkTarget(Target target) {
|
||||
// Overridden so we can do this check later
|
||||
// Overridden so we can do this check later in a location-aware manner
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -227,13 +225,13 @@ public class RedirectInjector extends InvokeInjector {
|
||||
}
|
||||
|
||||
if (node.getCurrentTarget() instanceof MethodInsnNode) {
|
||||
super.checkTarget(target);
|
||||
this.checkTargetForNode(target, node);
|
||||
this.injectAtInvoke(target, node);
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.getCurrentTarget() instanceof FieldInsnNode) {
|
||||
super.checkTarget(target);
|
||||
this.checkTargetForNode(target, node);
|
||||
this.injectAtFieldAccess(target, node);
|
||||
return;
|
||||
}
|
||||
@@ -250,6 +248,32 @@ public class RedirectInjector extends InvokeInjector {
|
||||
+ target + " in " + this);
|
||||
}
|
||||
|
||||
/**
|
||||
* The normal staticness check is not location-aware, in that it merely
|
||||
* enforces static modifiers of handlers to match their targets. For
|
||||
* injecting into constructors however (which are ostensibly instance
|
||||
* methods) calls which are redirected <em>before</em> the call to <tt>
|
||||
* super()</tt> cannot access <tt>this</tt> and must therefore be declared
|
||||
* as static.
|
||||
*
|
||||
* @param target Target method
|
||||
* @param node Injection location
|
||||
*/
|
||||
private void checkTargetForNode(Target target, InjectionNode node) {
|
||||
if (target.isCtor) {
|
||||
MethodInsnNode superCall = target.findSuperInitNode();
|
||||
int superCallIndex = target.indexOf(superCall);
|
||||
int targetIndex = target.indexOf(node.getCurrentTarget());
|
||||
if (targetIndex < superCallIndex) {
|
||||
if (!this.isStatic) {
|
||||
throw new InvalidInjectionException(this.info, "@Redirect of pre-super invocation must be static in " + this);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
super.checkTarget(target);
|
||||
}
|
||||
|
||||
protected boolean preInject(InjectionNode node) {
|
||||
Meta other = node.getDecoration(Meta.KEY);
|
||||
if (other.getOwner() != this) {
|
||||
@@ -513,7 +537,7 @@ public class RedirectInjector extends InvokeInjector {
|
||||
|
||||
final TypeInsnNode newNode = (TypeInsnNode)node.getCurrentTarget();
|
||||
final AbstractInsnNode dupNode = target.get(target.indexOf(newNode) + 1);
|
||||
final MethodInsnNode initNode = this.findInitNode(target, newNode);
|
||||
final MethodInsnNode initNode = target.findInitNodeFor(newNode);
|
||||
|
||||
if (initNode == null) {
|
||||
if (!wildcard) {
|
||||
@@ -572,20 +596,6 @@ public class RedirectInjector extends InvokeInjector {
|
||||
target.replaceNode(initNode, insns);
|
||||
meta.injected++;
|
||||
}
|
||||
|
||||
protected MethodInsnNode findInitNode(Target target, TypeInsnNode newNode) {
|
||||
int indexOf = target.indexOf(newNode);
|
||||
for (Iterator<AbstractInsnNode> iter = target.insns.iterator(indexOf); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) {
|
||||
MethodInsnNode methodNode = (MethodInsnNode)insn;
|
||||
if (Constants.CTOR.equals(methodNode.name) && methodNode.owner.equals(newNode.desc)) {
|
||||
return methodNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Type[] getArrayArgs(Type fieldType, Type... extra) {
|
||||
int dimensions = fieldType.getDimensions() + 1;
|
||||
|
||||
@@ -26,14 +26,19 @@ package org.spongepowered.asm.mixin.injection.struct;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.spongepowered.asm.lib.Opcodes;
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.lib.tree.InsnList;
|
||||
import org.spongepowered.asm.lib.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
import org.spongepowered.asm.lib.tree.TypeInsnNode;
|
||||
import org.spongepowered.asm.mixin.injection.InjectionNodes;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.Constants;
|
||||
|
||||
/**
|
||||
* Information about the current injection target, mainly just convenience
|
||||
@@ -61,6 +66,11 @@ public class Target implements Comparable<Target>, Iterable<AbstractInsnNode> {
|
||||
*/
|
||||
public final boolean isStatic;
|
||||
|
||||
/**
|
||||
* True if the method is a constructor
|
||||
*/
|
||||
public final boolean isCtor;
|
||||
|
||||
/**
|
||||
* Method arguments
|
||||
*/
|
||||
@@ -111,6 +121,7 @@ public class Target implements Comparable<Target>, Iterable<AbstractInsnNode> {
|
||||
this.method = method;
|
||||
this.insns = method.instructions;
|
||||
this.isStatic = Bytecode.methodIsStatic(method);
|
||||
this.isCtor = method.name.equals(Constants.CTOR);
|
||||
this.arguments = Type.getArgumentTypes(method.desc);
|
||||
this.argIndices = this.calcArgIndices(this.isStatic ? 0 : 1);
|
||||
|
||||
@@ -337,6 +348,42 @@ public class Target implements Comparable<Target>, Iterable<AbstractInsnNode> {
|
||||
return this.insns.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first <tt><init></tt> invocation after the specified
|
||||
* <tt>NEW</tt> insn
|
||||
*
|
||||
* @param newNode NEW insn
|
||||
* @return INVOKESPECIAL opcode of ctor, or null if not found
|
||||
*/
|
||||
public MethodInsnNode findInitNodeFor(TypeInsnNode newNode) {
|
||||
int start = this.indexOf(newNode);
|
||||
for (Iterator<AbstractInsnNode> iter = this.insns.iterator(start); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) {
|
||||
MethodInsnNode methodNode = (MethodInsnNode)insn;
|
||||
if (Constants.CTOR.equals(methodNode.name) && methodNode.owner.equals(newNode.desc)) {
|
||||
return methodNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the call to <tt>super()</tt> in a constructor. This attempts to
|
||||
* locate the first call to <tt><init></tt> which isn't an inline call
|
||||
* to another object ctor being passed into the super invocation.
|
||||
*
|
||||
* @return Call to <tt>super()</tt> or <tt>null</tt> if not found
|
||||
*/
|
||||
public MethodInsnNode findSuperInitNode() {
|
||||
if (!this.isCtor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Bytecode.findSuperInit(this.method, ClassInfo.forName(this.classNode.name).getSuperName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace an instruction in this target with the specified instruction and
|
||||
* mark the node as replaced for other injectors
|
||||
|
||||
@@ -588,7 +588,7 @@ class MixinApplicatorStandard {
|
||||
MethodNode target = this.findTargetMethod(method);
|
||||
|
||||
if (target != null) {
|
||||
AbstractInsnNode returnNode = MixinApplicatorStandard.findInsn(target, Opcodes.RETURN);
|
||||
AbstractInsnNode returnNode = Bytecode.findInsn(target, Opcodes.RETURN);
|
||||
|
||||
if (returnNode != null) {
|
||||
Iterator<AbstractInsnNode> injectIter = method.instructions.iterator();
|
||||
@@ -1059,24 +1059,6 @@ class MixinApplicatorStandard {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first insn node with a matching opcode in the specified method
|
||||
*
|
||||
* @param method method to search
|
||||
* @param opcode opcode to search for
|
||||
* @return found node or null if not found
|
||||
*/
|
||||
protected static AbstractInsnNode findInsn(MethodNode method, int opcode) {
|
||||
Iterator<AbstractInsnNode> findReturnIter = method.instructions.iterator();
|
||||
while (findReturnIter.hasNext()) {
|
||||
AbstractInsnNode insn = findReturnIter.next();
|
||||
if (insn.getOpcode() == opcode) {
|
||||
return insn;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the supplied method contains any line number information
|
||||
*
|
||||
|
||||
@@ -165,6 +165,57 @@ public final class Bytecode {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first insn node with a matching opcode in the specified method
|
||||
*
|
||||
* @param method method to search
|
||||
* @param opcode opcode to search for
|
||||
* @return found node or null if not found
|
||||
*/
|
||||
public static AbstractInsnNode findInsn(MethodNode method, int opcode) {
|
||||
Iterator<AbstractInsnNode> findReturnIter = method.instructions.iterator();
|
||||
while (findReturnIter.hasNext()) {
|
||||
AbstractInsnNode insn = findReturnIter.next();
|
||||
if (insn.getOpcode() == opcode) {
|
||||
return insn;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the call to <tt>super()</tt> in a constructor. This attempts to
|
||||
* locate the first call to <tt><init></tt> which isn't an inline call
|
||||
* to another object ctor being passed into the super invocation.
|
||||
*
|
||||
* @param method ctor to scan
|
||||
* @param superName name of superclass
|
||||
* @return Call to <tt>super()</tt> or <tt>null</tt> if not found
|
||||
*/
|
||||
public static MethodInsnNode findSuperInit(MethodNode method, String superName) {
|
||||
if (!Constants.CTOR.equals(method.name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int news = 0;
|
||||
for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext();) {
|
||||
AbstractInsnNode insn = iter.next();
|
||||
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
|
||||
news++;
|
||||
} else if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) {
|
||||
MethodInsnNode methodNode = (MethodInsnNode)insn;
|
||||
if (Constants.CTOR.equals(methodNode.name)) {
|
||||
if (news > 0) {
|
||||
news--;
|
||||
} else if (methodNode.owner.equals(superName)) {
|
||||
return methodNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs textifier on the specified class node and dumps the output to the
|
||||
* specified output stream
|
||||
|
||||
Reference in new issue
Block a user