|
23 | 23 | import java.util.ListIterator;
|
24 | 24 | import java.util.Objects;
|
25 | 25 | import java.util.Optional;
|
| 26 | +import java.util.function.Function; |
26 | 27 |
|
27 | 28 | /**
|
28 | 29 | * Helper methods for working with ASM.
|
@@ -68,6 +69,37 @@ public static MethodInsnNode buildMethodCall(final String ownerName, final Strin
|
68 | 69 | return new MethodInsnNode(type.toOpcode(), ownerName, methodName, methodDescriptor, type==MethodType.INTERFACE);
|
69 | 70 | }
|
70 | 71 |
|
| 72 | + /** |
| 73 | + * Signifies the type of number constant for a {@link LdcNumberType}. |
| 74 | + */ |
| 75 | + public enum LdcNumberType { |
| 76 | + INTEGER(Number::intValue), |
| 77 | + FLOAT(Number::floatValue), |
| 78 | + LONG(Number::longValue), |
| 79 | + DOUBLE(Number::doubleValue); |
| 80 | + |
| 81 | + private final Function<Number, Object> mapper; |
| 82 | + |
| 83 | + LdcNumberType(Function<Number, Object> mapper) { |
| 84 | + this.mapper = mapper; |
| 85 | + } |
| 86 | + |
| 87 | + public Object map(Number number) { |
| 88 | + return mapper.apply(number); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Builds a new {@link LdcInsnNode} with the given number value and type. |
| 94 | + * |
| 95 | + * @param value The number value |
| 96 | + * @param type The type of the number |
| 97 | + * @return The built LDC node |
| 98 | + */ |
| 99 | + public static LdcInsnNode buildNumberLdcInsnNode(final Number value, final LdcNumberType type) { |
| 100 | + return new LdcInsnNode(type.map(value)); |
| 101 | + } |
| 102 | + |
71 | 103 | /**
|
72 | 104 | * Maps a method from the given SRG name to the mapped name at deobfuscated runtime.
|
73 | 105 | *
|
@@ -430,6 +462,16 @@ public static String methodNodeToString(MethodNode node) {
|
430 | 462 | return toString(text);
|
431 | 463 | }
|
432 | 464 |
|
| 465 | + /** |
| 466 | + * Gets the LDC constant's class name as a string. Useful for debugging existing LDC instructions. |
| 467 | + * |
| 468 | + * @param insn The LDC instruction. |
| 469 | + * @return The class name of the LDC constant. |
| 470 | + */ |
| 471 | + public static String ldcInsnClassToString(LdcInsnNode insn) { |
| 472 | + return insn.cst.getClass().toString(); |
| 473 | + } |
| 474 | + |
433 | 475 | private static String toString(Textifier text) {
|
434 | 476 | StringWriter sw = new StringWriter();
|
435 | 477 | PrintWriter pw = new PrintWriter(sw);
|
|
0 commit comments