use of org.objectweb.asm.tree.AbstractInsnNode in project MinecraftForge by MinecraftForge.
the class ItemStackTransformer method transform.
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (!"net.minecraft.item.ItemStack".equals(name))
return basicClass;
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
FieldNode itemField = null;
for (FieldNode f : classNode.fields) {
if (ITEM_TYPE.equals(f.desc) && itemField == null) {
itemField = f;
} else if (ITEM_TYPE.equals(f.desc)) {
throw new RuntimeException("Error processing ItemStack - found a duplicate Item field");
}
}
if (itemField == null) {
throw new RuntimeException("Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)");
}
MethodNode getItemMethod = null;
for (MethodNode m : classNode.methods) {
if (m.name.equals("getItemRaw"))
continue;
if (GETITEM_DESC.equals(m.desc) && getItemMethod == null) {
getItemMethod = m;
} else if (GETITEM_DESC.equals(m.desc)) {
throw new RuntimeException("Error processing ItemStack - duplicate getItem method found");
}
}
if (getItemMethod == null) {
throw new RuntimeException("Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)");
}
for (MethodNode m : classNode.methods) {
if (m.name.equals("getItemRaw"))
continue;
for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); ) {
AbstractInsnNode insnNode = it.next();
if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
FieldInsnNode fi = (FieldInsnNode) insnNode;
if (itemField.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
it.remove();
MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack", getItemMethod.name, getItemMethod.desc, false);
it.add(replace);
}
}
}
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.tree.AbstractInsnNode in project Bookshelf by Darkhax-Minecraft.
the class ASMUtils method removeNeedleFromHaystack.
/**
* Removes a specific set of instructions (the needle) from a much larger set of
* instructions (the hay stack). Be cautious when using this method, as it is almost never
* a good idea to remove instructions.
*
* @param haystack: A large list of instructions which is being searched through.
* @param needle: A specific list of instructions which are to be removed from the larger
* instruction list.
*/
public static void removeNeedleFromHaystack(InsnList haystack, InsnList needle) {
final int firstInd = haystack.indexOf(findFirstNodeFromNeedle(haystack, needle));
final int lastInd = haystack.indexOf(findLastNodeFromNeedle(haystack, needle));
final List<AbstractInsnNode> realNeedle = new ArrayList<>();
for (int i = firstInd; i <= lastInd; i++) {
realNeedle.add(haystack.get(i));
}
for (final AbstractInsnNode node : realNeedle) {
haystack.remove(node);
}
}
use of org.objectweb.asm.tree.AbstractInsnNode in project Bookshelf by Darkhax-Minecraft.
the class InstructionComparator method getImportantList.
// TODO: Add documentation
public static InsnList getImportantList(InsnList list) {
if (list.size() == 0) {
return list;
}
final HashMap<LabelNode, LabelNode> labels = new HashMap<>();
for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
if (insn instanceof LabelNode) {
labels.put((LabelNode) insn, (LabelNode) insn);
}
}
final InsnList importantNodeList = new InsnList();
for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
continue;
}
importantNodeList.add(insn.clone(labels));
}
return importantNodeList;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project bytecode-viewer by Konloch.
the class NumberNode method number.
public int number() {
AbstractInsnNode insn = insn();
int op = insn.opcode();
switch(op) {
case NEWARRAY:
case BIPUSH:
case SIPUSH:
{
return ((IntInsnNode) insn).operand;
}
case ICONST_M1:
case ICONST_0:
case ICONST_1:
case ICONST_2:
case ICONST_3:
case ICONST_4:
case ICONST_5:
{
return op - ICONST_0;
}
case LCONST_0:
case LCONST_1:
{
return op - LCONST_0;
}
case FCONST_0:
case FCONST_1:
case FCONST_2:
{
return op - FCONST_0;
}
case DCONST_0:
case DCONST_1:
{
return op - DCONST_0;
}
case LDC:
{
Object cst = ((LdcInsnNode) insn).cst;
if (cst instanceof Number) {
return ((Number) cst).intValue();
}
}
default:
{
return -1;
}
}
}
use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.
the class MethodAnalyzer method findRepresentative.
private AbstractInsnNode findRepresentative(AbstractInsnNode i) {
AbstractInsnNode r = merged.get(i);
while (r != null) {
i = r;
r = merged.get(i);
}
return i;
}
Aggregations