Skip to content

Commit deac494

Browse files
authored
Add LDC constant helper methods (#51)
Add buildLdcInsnNode for dealing with specific number types Add ldcInsnClassToString to get LDC constants class names
1 parent d4eca3c commit deac494

File tree

1 file changed

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

1 file changed

+42
-0
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ListIterator;
2424
import java.util.Objects;
2525
import java.util.Optional;
26+
import java.util.function.Function;
2627

2728
/**
2829
* Helper methods for working with ASM.
@@ -68,6 +69,37 @@ public static MethodInsnNode buildMethodCall(final String ownerName, final Strin
6869
return new MethodInsnNode(type.toOpcode(), ownerName, methodName, methodDescriptor, type==MethodType.INTERFACE);
6970
}
7071

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+
71103
/**
72104
* Maps a method from the given SRG name to the mapped name at deobfuscated runtime.
73105
*
@@ -430,6 +462,16 @@ public static String methodNodeToString(MethodNode node) {
430462
return toString(text);
431463
}
432464

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+
433475
private static String toString(Textifier text) {
434476
StringWriter sw = new StringWriter();
435477
PrintWriter pw = new PrintWriter(sw);

0 commit comments

Comments
 (0)