Skip to content

Commit c95067a

Browse files
authored
Add additional field tools to ASMAPI (#61)
Add a buildMethodCall() with the type as first param This mimics how MethodInsnNodes are created with the opcode being fed first. Add findFirstFieldCall() methods to search for field calls Add buildFieldCall() to build field calls
1 parent cfeab74 commit c95067a

File tree

1 file changed

+82
-0
lines changed
  • src/main/java/net/minecraftforge/coremod/api

1 file changed

+82
-0
lines changed

src/main/java/net/minecraftforge/coremod/api/ASMAPI.java

+82
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ public static MethodInsnNode buildMethodCall(final String ownerName, final Strin
9090
return new MethodInsnNode(type.toOpcode(), ownerName, methodName, methodDescriptor, type == MethodType.INTERFACE);
9191
}
9292

93+
/**
94+
* Builds a new {@link MethodInsnNode} with the given parameters. The opcode of the method call is determined by the
95+
* given {@link MethodType}.
96+
*
97+
* @param type The type of method call
98+
* @param ownerName The method owner (class)
99+
* @param methodName The method name
100+
* @param methodDescriptor The method descriptor
101+
* @return The built method call node
102+
*/
103+
public static MethodInsnNode buildMethodCall(final MethodType type, final String ownerName, final String methodName, final String methodDescriptor) {
104+
return new MethodInsnNode(type.toOpcode(), ownerName, methodName, methodDescriptor, type == MethodType.INTERFACE);
105+
}
106+
107+
public static FieldInsnNode buildFieldCall(final int opcode, final String owner, final String name, final String desc) {
108+
return new FieldInsnNode(opcode, owner, name, desc);
109+
}
110+
93111
/**
94112
* Signifies the type of number constant for a {@link NumberType}.
95113
*/
@@ -383,6 +401,70 @@ public static MethodInsnNode findFirstMethodCallBefore(MethodNode method, Method
383401
return null;
384402
}
385403

404+
/**
405+
* Finds the first method call in the given method matching the given type, owner, name and descriptor.
406+
*
407+
* @param method the method to search in
408+
* @param opcode the opcode of field call to search for
409+
* @param owner the method call's owner to search for
410+
* @param name the method call's name
411+
* @param descriptor the method call's descriptor
412+
* @return the found method call node or null if none matched
413+
*/
414+
public static @Nullable FieldInsnNode findFirstFieldCall(MethodNode method, int opcode, String owner, String name, String descriptor) {
415+
return findFirstFieldCallAfter(method, opcode, owner, name, descriptor, 0);
416+
}
417+
418+
/**
419+
* Finds the first method call in the given method matching the given type, owner, name and descriptor after the
420+
* instruction given index.
421+
*
422+
* @param method the method to search in
423+
* @param opcode the opcode of field call to search for
424+
* @param owner the method call's owner to search for
425+
* @param name the method call's name
426+
* @param descriptor the method call's descriptor
427+
* @param startIndex the index after which to start searching (inclusive)
428+
* @return the found method call node, null if none matched after the given index
429+
*/
430+
public static @Nullable FieldInsnNode findFirstFieldCallAfter(MethodNode method, int opcode, String owner, String name, String descriptor, int startIndex) {
431+
for (int i = Math.max(0, startIndex); i < method.instructions.size(); i++) {
432+
if (method.instructions.get(i) instanceof FieldInsnNode insn
433+
&& insn.getOpcode() == opcode
434+
&& insn.owner.equals(owner)
435+
&& insn.name.equals(name)
436+
&& insn.desc.equals(descriptor)) {
437+
return insn;
438+
}
439+
}
440+
return null;
441+
}
442+
443+
/**
444+
* Finds the first method call in the given method matching the given type, owner, name and descriptor before the
445+
* given index in reverse search.
446+
*
447+
* @param method the method to search in
448+
* @param opcode the opcode of field call to search for
449+
* @param owner the method call's owner to search for
450+
* @param name the method call's name
451+
* @param descriptor the method call's descriptor
452+
* @param startIndex the index at which to start searching (inclusive)
453+
* @return the found method call node or null if none matched before the given startIndex
454+
*/
455+
public static @Nullable FieldInsnNode findFirstFieldCallBefore(MethodNode method, int opcode, String owner, String name, String descriptor, int startIndex) {
456+
for (int i = Math.min(method.instructions.size() - 1, startIndex); i >= 0; i--) {
457+
if (method.instructions.get(i) instanceof FieldInsnNode insn
458+
&& insn.getOpcode() == opcode
459+
&& insn.owner.equals(owner)
460+
&& insn.name.equals(name)
461+
&& insn.desc.equals(descriptor)) {
462+
return insn;
463+
}
464+
}
465+
return null;
466+
}
467+
386468
/**
387469
* Inserts/replaces a list after/before first {@link MethodInsnNode} that matches the parameters of these functions
388470
* in the method provided. Only the first node matching is targeted, all other matches are ignored.

0 commit comments

Comments
 (0)