Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LDC constant helper methods #51

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/net/minecraftforge/coremod/api/ASMAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ListIterator;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

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

/**
* Signifies the type of number constant for a {@link LdcNumberType}.
*/
public enum LdcNumberType {
INTEGER(Number::intValue),
FLOAT(Number::floatValue),
LONG(Number::longValue),
DOUBLE(Number::doubleValue);

private final Function<Number, Object> mapper;

LdcNumberType(Function<Number, Object> mapper) {
this.mapper = mapper;
}

public Object map(Number number) {
return mapper.apply(number);
}
}

/**
* Builds a new {@link LdcInsnNode} with the given number value and type.
*
* @param value The number value
* @param type The type of the number
* @return The built LDC node
*/
public static LdcInsnNode buildNumberLdcInsnNode(final Number value, final LdcNumberType type) {
return new LdcInsnNode(type.map(value));
}

/**
* Maps a method from the given SRG name to the mapped name at deobfuscated runtime.
*
Expand Down Expand Up @@ -430,6 +462,16 @@ public static String methodNodeToString(MethodNode node) {
return toString(text);
}

/**
* Gets the LDC constant's class name as a string. Useful for debugging existing LDC instructions.
*
* @param insn The LDC instruction.
* @return The class name of the LDC constant.
*/
public static String ldcInsnClassToString(LdcInsnNode insn) {
return insn.cst.getClass().toString();
}

private static String toString(Textifier text) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Expand Down