Skip to content

Commit

Permalink
Properly handle empty args window and report name of the original method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Jul 6, 2024
1 parent c8be3ca commit 7d7d4dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ protected void injectAtInvoke(Target target, InjectionNode node) {
boolean nested = node.hasDecoration(ArgOffsets.KEY);
Type[] originalArgs = offsets.apply(args);

if (originalArgs.length == 0) {
throw new InvalidInjectionException(this.info, "@ModifyArg injector " + this + " targets a method invocation "
+ ((MethodInsnNode)node.getOriginalTarget()).name + "()" + Type.getReturnType(methodNode.desc) + " with no arguments!");
}

int argIndex = offsets.getArgIndex(this.findArgIndex(target, originalArgs));
int baseIndex = offsets.getStartIndex();

Expand All @@ -125,7 +130,7 @@ protected void injectAtInvoke(Target target, InjectionNode node) {
this.injectSingleArgHandler(target, extraLocals, args, argIndex, insns, nested);
} else {
if (!Arrays.equals(originalArgs, this.methodArgs)) {
throw new InvalidInjectionException(this.info, "@ModifyArg method " + this + " targets a method with an invalid signature "
throw new InvalidInjectionException(this.info, "@ModifyArg injector " + this + " targets a method with an invalid signature "
+ Bytecode.getDescriptor(originalArgs) + ", expected " + Bytecode.getDescriptor(this.methodArgs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void injectAtInvoke(Target target, InjectionNode node) {

if (originalArgs.length == 0) {
throw new InvalidInjectionException(this.info, "@ModifyArgs injector " + this + " targets a method invocation "
+ methodNode.name + targetMethodDesc + " with no arguments!");
+ ((MethodInsnNode)node.getOriginalTarget()).name + targetMethodDesc + " with no arguments!");
}

String clArgs = this.argsClassGenerator.getArgsClass(targetMethodDesc, this.info.getMixin().getMixin()).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ public Type[] apply(Type[] args) {
* @param length length
*/
public ArgOffsets(int offset, int length) {
if (length < 1) {
throw new IllegalArgumentException("Invalid length " + length + " for ArgOffsets window");
}
this.offset = offset;
this.length = length;
}
Expand All @@ -121,12 +118,19 @@ public void replace(ArgOffsets old) {
}

/**
* Get the size of this mapping collection
* Get the size of the offset window
*/
public int getLength() {
return this.length;
}

/**
* Get whether this argument offset window is empty
*/
public boolean isEmpty() {
return this.length == 0;
}

/**
* Compute the argument index for the start of the window (offet 0)
*
Expand All @@ -142,7 +146,7 @@ public int getStartIndex() {
* @return the offset index for the end of the window (inclusive)
*/
public int getEndIndex() {
return this.getArgIndex(this.length - 1);
return this.isEmpty() ? this.getStartIndex() : this.getArgIndex(this.length - 1);
}

/**
Expand Down

0 comments on commit 7d7d4dc

Please sign in to comment.