Files
EndlessIDs/src/main/java/com/falsepattern/endlessids/asm/AsmUtil.java
T

90 lines
3.4 KiB
Java
Raw Normal View History

2022-03-17 15:21:30 +01:00
package com.falsepattern.endlessids.asm;
2022-07-02 20:21:50 +02:00
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
2022-03-17 15:21:30 +01:00
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodNode;
2022-07-02 20:21:50 +02:00
import java.util.ListIterator;
public class AsmUtil {
2022-03-17 15:21:30 +01:00
public AsmUtil() {
super();
}
2022-07-02 20:21:50 +02:00
2022-03-17 15:21:30 +01:00
public static FieldNode findField(final ClassNode cn, final String name, final boolean optional) {
for (final FieldNode ret : cn.fields) {
if (name.equals(ret.name)) {
return ret;
}
}
if (optional) {
return null;
}
throw new FieldNotFoundException(name);
}
2022-07-02 20:21:50 +02:00
2022-03-17 15:21:30 +01:00
public static boolean transformInlinedSizeMethod(final ClassNode cn, final MethodNode method, final int oldValue, final int newValue, final boolean optional) {
boolean found = false;
boolean foundOnce = false;
final int i = 0;
2022-03-22 13:39:04 +01:00
final ListIterator<AbstractInsnNode> it = method.instructions.iterator();
2022-03-17 15:21:30 +01:00
while (it.hasNext()) {
final AbstractInsnNode insn = it.next();
if (insn.getOpcode() == 3 && oldValue == 0) {
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 4 && oldValue == 1) {
2022-03-17 15:21:30 +01:00
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 5 && oldValue == 2) {
2022-03-17 15:21:30 +01:00
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 6 && oldValue == 3) {
2022-03-17 15:21:30 +01:00
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 7 && oldValue == 4) {
2022-03-17 15:21:30 +01:00
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 8 && oldValue == 5) {
2022-03-17 15:21:30 +01:00
found = true;
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 18) {
final LdcInsnNode node = (LdcInsnNode) insn;
if (node.cst instanceof Integer && (int) node.cst == oldValue) {
2022-03-17 15:21:30 +01:00
found = true;
}
2022-07-02 20:21:50 +02:00
} else if (insn.getOpcode() == 17 || insn.getOpcode() == 16) {
final IntInsnNode node2 = (IntInsnNode) insn;
2022-03-17 15:21:30 +01:00
if (node2.operand == oldValue) {
found = true;
}
}
if (found) {
foundOnce = true;
if (newValue == 0) {
it.set(new InsnNode(3));
2022-07-02 20:21:50 +02:00
} else if (newValue == 1) {
2022-03-17 15:21:30 +01:00
it.set(new InsnNode(4));
2022-07-02 20:21:50 +02:00
} else if (newValue == 2) {
2022-03-17 15:21:30 +01:00
it.set(new InsnNode(5));
2022-07-02 20:21:50 +02:00
} else if (newValue == 3) {
2022-03-17 15:21:30 +01:00
it.set(new InsnNode(6));
2022-07-02 20:21:50 +02:00
} else if (newValue == 4) {
2022-03-17 15:21:30 +01:00
it.set(new InsnNode(7));
2022-07-02 20:21:50 +02:00
} else if (newValue == 5) {
2022-03-17 15:21:30 +01:00
it.set(new InsnNode(8));
2022-07-02 20:21:50 +02:00
} else if (newValue >= -128 && newValue <= 127) {
2022-03-17 15:21:30 +01:00
it.set(new IntInsnNode(16, newValue));
2022-07-02 20:21:50 +02:00
} else if (newValue >= -32768 && newValue <= 32767) {
2022-03-17 15:21:30 +01:00
it.set(new IntInsnNode(17, newValue));
2022-07-02 20:21:50 +02:00
} else {
2022-03-22 13:39:04 +01:00
it.set(new LdcInsnNode(newValue));
2022-03-17 15:21:30 +01:00
}
found = false;
}
}
if (!foundOnce && !optional) {
throw new AsmTransformException("can't find constant value " + oldValue + " in method " + method.name);
}
return foundOnce;
}
2022-07-02 20:21:50 +02:00
2022-03-17 15:21:30 +01:00
}