@@ -90,6 +90,24 @@ public static MethodInsnNode buildMethodCall(final String ownerName, final Strin
90
90
return new MethodInsnNode (type .toOpcode (), ownerName , methodName , methodDescriptor , type == MethodType .INTERFACE );
91
91
}
92
92
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
+
93
111
/**
94
112
* Signifies the type of number constant for a {@link NumberType}.
95
113
*/
@@ -383,6 +401,70 @@ public static MethodInsnNode findFirstMethodCallBefore(MethodNode method, Method
383
401
return null ;
384
402
}
385
403
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
+
386
468
/**
387
469
* Inserts/replaces a list after/before first {@link MethodInsnNode} that matches the parameters of these functions
388
470
* in the method provided. Only the first node matching is targeted, all other matches are ignored.
0 commit comments