Enforce staticness of pre-super ModifyArg invocations

This commit is contained in:
Mumfrey committed 2017-06-27 13:33:54 +01:00
1 parent 66212a28a2
commit e2a3b30b8b
4 files changed
+38 -26

No files matched your search

@@ -77,6 +77,30 @@ public abstract class InvokeInjector extends Injector {
}
}
/**
* 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 injected <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
*/
protected void checkTargetForNode(Target target, InjectionNode node) {
if (!target.isCtor) {
return;
}
MethodInsnNode superCall = target.findSuperInitNode();
int superCallIndex = target.indexOf(superCall);
int targetIndex = target.indexOf(node.getCurrentTarget());
if (targetIndex <= superCallIndex && !this.isStatic) {
throw new InvalidInjectionException(this.info, "Pre-super " + this.annotationType + " invocation must be static in " + this);
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.injection.callback.BytecodeInjector
* #inject(org.spongepowered.asm.mixin.injection.callback.Target,
@@ -94,6 +94,12 @@ public class ModifyArgInjector extends InvokeInjector {
}
}
@Override
protected void inject(Target target, InjectionNode node) {
this.checkTargetForNode(target, node);
super.inject(target, node);
}
/**
* Do the injection
*/
@@ -62,6 +62,12 @@ public class ModifyArgsInjector extends InvokeInjector {
}
}
@Override
protected void inject(Target target, InjectionNode node) {
this.checkTargetForNode(target, node);
super.inject(target, node);
}
/**
* Do the injection
*/
@@ -226,12 +226,14 @@ public class RedirectInjector extends InvokeInjector {
if (node.getCurrentTarget() instanceof MethodInsnNode) {
this.checkTargetForNode(target, node);
super.checkTarget(target);
this.injectAtInvoke(target, node);
return;
}
if (node.getCurrentTarget() instanceof FieldInsnNode) {
this.checkTargetForNode(target, node);
super.checkTarget(target);
this.injectAtFieldAccess(target, node);
return;
}
@@ -248,32 +250,6 @@ 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) {