Search in sources :

Example 21 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project Random-Things by lumien231.

the class ClassTransformer method patchVertexLighterFlat.

private byte[] patchVertexLighterFlat(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found VertexLighterFlat Class: " + classNode.name);
    MethodNode processQuad = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals("processQuad")) {
            processQuad = mn;
            break;
        }
    }
    if (processQuad != null) {
        logger.log(Level.DEBUG, " - Found processQuad");
        InsnList resetList = new InsnList();
        resetList.add(new VarInsnNode(ALOAD, 0));
        resetList.add(new InsnNode(ICONST_0));
        resetList.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
        processQuad.instructions.insert(resetList);
        AbstractInsnNode tintTarget = null;
        int lightMapTarget = 0;
        AbstractInsnNode updateColorTarget = null;
        LabelNode firstLabel = null;
        LabelNode lastLabel = null;
        for (int i = 0; i < processQuad.instructions.size(); i++) {
            AbstractInsnNode ain = processQuad.instructions.get(i);
            if (ain instanceof VarInsnNode) {
                VarInsnNode vin = (VarInsnNode) ain;
                if (vin.var == 5 && (processQuad.instructions.get(i - 1) instanceof MethodInsnNode)) {
                    tintTarget = vin;
                }
            } else if (ain instanceof LabelNode) {
                LabelNode ln = (LabelNode) ain;
                if (firstLabel == null) {
                    firstLabel = ln;
                }
                lastLabel = ln;
            } else if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals(MCPNames.method("func_177369_e"))) {
                    lightMapTarget = i;
                } else if (min.name.equals("updateColor")) {
                    updateColorTarget = min;
                }
            }
        }
        FieldNode fullBrightField = new FieldNode(ACC_PRIVATE, "rtFullBright", "Z", null, false);
        classNode.fields.add(fullBrightField);
        if (lightMapTarget != 0) {
            logger.log(Level.DEBUG, " - Found patch target (lightmap) (1/4)");
            for (int i = lightMapTarget; i < processQuad.instructions.size(); i++) {
                AbstractInsnNode ain = processQuad.instructions.get(i);
                if (ain instanceof InsnNode) {
                    InsnNode in = (InsnNode) ain;
                    if (in.getOpcode() == AALOAD) {
                        logger.log(Level.DEBUG, " - Found lightmap array (2/4)");
                        LabelNode l0 = new LabelNode(new Label());
                        InsnList toInsert = new InsnList();
                        toInsert.add(new VarInsnNode(ALOAD, 0));
                        toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
                        toInsert.add(new JumpInsnNode(IFEQ, l0));
                        toInsert.add(new InsnNode(POP));
                        toInsert.add(new FieldInsnNode(GETSTATIC, "lumien/randomthings/lib/Constants", "FULLBRIGHT_OVERRIDE", "[F"));
                        toInsert.add(l0);
                        processQuad.instructions.insert(in, toInsert);
                        break;
                    }
                }
            }
        }
        if (tintTarget != null) {
            logger.log(Level.DEBUG, " - Found tintTarget (3/4");
            LabelNode l0 = new LabelNode(new Label());
            LabelNode l1 = new LabelNode(new Label());
            InsnList toInsert = new InsnList();
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
            toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()Lnet/minecraft/block/state/IBlockState;", false));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
            toInsert.add(new TypeInsnNode(INSTANCEOF, "lumien/randomthings/lib/ILuminousBlock"));
            toInsert.add(new JumpInsnNode(IFEQ, l0));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "blockInfo", "Lnet/minecraftforge/client/model/pipeline/BlockInfo;"));
            toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraftforge/client/model/pipeline/BlockInfo", "getState", "()Lnet/minecraft/block/state/IBlockState;", false));
            toInsert.add(new InsnNode(DUP));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
            toInsert.add(new InsnNode(SWAP));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "tint", "I"));
            toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "lumien/randomthings/lib/ILuminousBlock", "shouldGlow", "(Lnet/minecraft/block/state/IBlockState;I)Z", true));
            toInsert.add(new JumpInsnNode(IFEQ, l0));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new InsnNode(ICONST_0));
            toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "diffuse", "Z"));
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new InsnNode(ICONST_1));
            toInsert.add(new FieldInsnNode(PUTFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
            toInsert.add(l0);
            processQuad.instructions.insertBefore(tintTarget, toInsert);
        }
        if (updateColorTarget != null) {
            logger.log(Level.DEBUG, " - Found updateColor target (tint) (4/4)");
            LabelNode l0 = new LabelNode(new Label());
            LabelNode l1 = new LabelNode(new Label());
            InsnList toInsert = new InsnList();
            toInsert.add(new VarInsnNode(ALOAD, 0));
            toInsert.add(new FieldInsnNode(GETFIELD, "net/minecraftforge/client/model/pipeline/VertexLighterFlat", "rtFullBright", "Z"));
            toInsert.add(new JumpInsnNode(IFEQ, l1));
            toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "updateColor", "([F[FFFFFI)V", false));
            toInsert.add(new InsnNode(POP));
            toInsert.add(new JumpInsnNode(GOTO, l0));
            toInsert.add(l1);
            processQuad.instructions.insertBefore(updateColorTarget, toInsert);
            processQuad.instructions.insert(updateColorTarget, l0);
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    try {
        byte[] result = writer.toByteArray();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return basicClass;
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) Label(org.objectweb.asm.Label) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 22 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project openj9 by eclipse.

the class ClassFileCompare method compareInstructions.

private void compareInstructions(ClassNode clazz1, MethodNode method1, ClassNode clazz2, MethodNode method2) {
    if (nullCheck(method1.instructions, method2.instructions, "Instructions (" + method1.name + ")")) {
        return;
    }
    if (method1.instructions.size() != method2.instructions.size()) {
        reportDifference("Method " + method1.name + ": instructions differ: " + method1.instructions.size() + ", " + method2.instructions.size());
    } else {
        @SuppressWarnings("unchecked") Iterator<AbstractInsnNode> iter1 = method1.instructions.iterator();
        @SuppressWarnings("unchecked") Iterator<AbstractInsnNode> iter2 = method2.instructions.iterator();
        int index = 0;
        while (iter1.hasNext()) {
            AbstractInsnNode instruction1 = iter1.next();
            AbstractInsnNode instruction2 = iter2.next();
            if (instruction1.getOpcode() != instruction2.getOpcode()) {
                /* Check for J9 opcode mapping */
                compareJ9OpcodeMapping(clazz2, method2, instruction1, instruction2, index);
            } else {
                switch(instruction1.getType()) {
                    case INSN:
                        /* Do nothing */
                        break;
                    case INT_INSN:
                        compare(((IntInsnNode) instruction1).operand, ((IntInsnNode) instruction2).operand, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                        break;
                    case VAR_INSN:
                        compare(((VarInsnNode) instruction1).var, ((VarInsnNode) instruction2).var, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                        break;
                    case TYPE_INSN:
                        compare(((TypeInsnNode) instruction1).desc, ((TypeInsnNode) instruction2).desc, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                        break;
                    case FIELD_INSN:
                        {
                            FieldInsnNode fieldInsn1 = (FieldInsnNode) instruction1;
                            FieldInsnNode fieldInsn2 = (FieldInsnNode) instruction2;
                            compare(fieldInsn1.owner, fieldInsn2.owner, "Owning class name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(fieldInsn1.name, fieldInsn2.name, "Field name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(fieldInsn1.desc, fieldInsn2.desc, "Descriptor of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            break;
                        }
                    case METHOD_INSN:
                        {
                            MethodInsnNode methodInsn1 = (MethodInsnNode) instruction1;
                            MethodInsnNode methodInsn2 = (MethodInsnNode) instruction2;
                            compare(methodInsn1.owner, methodInsn2.owner, "Owning class name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(methodInsn1.name, methodInsn2.name, "Method name of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(methodInsn1.desc, methodInsn2.desc, "Descriptor of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            break;
                        }
                    case INVOKE_DYNAMIC_INSN:
                        compareInvokeDynamic((InvokeDynamicInsnNode) instruction1, (InvokeDynamicInsnNode) instruction2, method1, index);
                        break;
                    case JUMP_INSN:
                        compare(method1, ((JumpInsnNode) instruction1).label, method2, ((JumpInsnNode) instruction2).label, "Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                        break;
                    case LABEL:
                        /* Do nothing */
                        break;
                    case LDC_INSN:
                        if (!((LdcInsnNode) instruction1).cst.equals(((LdcInsnNode) instruction2).cst)) {
                            reportDifference("Operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") differ: " + ((LdcInsnNode) instruction1).cst + ", " + ((LdcInsnNode) instruction2).cst);
                        }
                        break;
                    case IINC_INSN:
                        {
                            IincInsnNode iincInsn1 = (IincInsnNode) instruction1;
                            IincInsnNode iincInsn2 = (IincInsnNode) instruction2;
                            compare(iincInsn1.var, iincInsn2.var, "Variable operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            compare(iincInsn1.incr, iincInsn2.incr, "Increment operands of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            break;
                        }
                    case TABLESWITCH_INSN:
                        {
                            TableSwitchInsnNode tableSwitchInsn1 = (TableSwitchInsnNode) instruction1;
                            TableSwitchInsnNode tableSwitchInsn2 = (TableSwitchInsnNode) instruction2;
                            compare(tableSwitchInsn1.min, tableSwitchInsn2.min, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") minimum key value", false);
                            compare(tableSwitchInsn1.max, tableSwitchInsn2.max, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") maximum key value", false);
                            compare(method1, tableSwitchInsn1.dflt, method2, tableSwitchInsn2.dflt, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") default_handler_block");
                            compareLabels(method1, tableSwitchInsn1.labels, method2, tableSwitchInsn2.labels, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") handler_blocks");
                            break;
                        }
                    case LOOKUPSWITCH_INSN:
                        {
                            LookupSwitchInsnNode lookupSwitchInsn1 = (LookupSwitchInsnNode) instruction1;
                            LookupSwitchInsnNode lookupSwitchInsn2 = (LookupSwitchInsnNode) instruction2;
                            compare(method1, lookupSwitchInsn1.dflt, method2, lookupSwitchInsn2.dflt, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") default_handler_block");
                            compare(lookupSwitchInsn1.keys, lookupSwitchInsn2.keys, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") key values");
                            compareLabels(method1, lookupSwitchInsn1.labels, method2, lookupSwitchInsn2.labels, Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ") handler_blocks");
                            break;
                        }
                    case MULTIANEWARRAY_INSN:
                        {
                            MultiANewArrayInsnNode arrayInsn1 = (MultiANewArrayInsnNode) instruction1;
                            MultiANewArrayInsnNode arrayInsn2 = (MultiANewArrayInsnNode) instruction2;
                            compare(arrayInsn1.desc, arrayInsn2.desc, "Desc operand of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")");
                            compare(arrayInsn1.dims, arrayInsn2.dims, "Dimension operand of " + Printer.OPCODES[instruction1.getOpcode()] + " (" + method1.name + ":" + index + ")", false);
                            break;
                        }
                    case FRAME:
                        {
                            FrameNode frameInsn1 = (FrameNode) instruction1;
                            FrameNode frameInsn2 = (FrameNode) instruction2;
                            compare(frameInsn1.type, frameInsn2.type, "Stack map frame (" + method1.name + ":" + index + ") frame type", false);
                            compareFrames(frameInsn1.local, frameInsn2.local, "Stack map frame (" + method1.name + ":" + index + ") types of the local variables");
                            compareFrames(frameInsn1.stack, frameInsn2.stack, "Stack map frame (" + method1.name + ":" + index + ") types of the operand stack elements");
                        }
                        break;
                    case LINE:
                        {
                            assert shouldCompareDebugInfo;
                            LineNumberNode lineInsn1 = (LineNumberNode) instruction1;
                            LineNumberNode lineInsn2 = (LineNumberNode) instruction2;
                            compare(lineInsn1.line, lineInsn2.line, "Line number (" + method1.name + ":" + index + ") line No.", false);
                            compare(method1, lineInsn1.start, method2, lineInsn2.start, "Line number (" + method1.name + ":" + index + ") offset of the 1st instruction");
                            break;
                        }
                    default:
                        assert false;
                }
            }
            index++;
        }
    }
}
Also used : TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) FrameNode(org.objectweb.asm.tree.FrameNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode)

Example 23 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project phosphor by gmu-swe.

the class InstMethodSinkInterpreter method _unaryOperation.

private BasicValue _unaryOperation(AbstractInsnNode insn, BasicValue value) throws AnalyzerException {
    switch(insn.getOpcode()) {
        case INEG:
        case IINC:
        case L2I:
        case F2I:
        case D2I:
        case I2B:
        case I2C:
        case I2S:
            SinkableArrayValue ret = new SinkableArrayValue(Type.INT_TYPE);
            ret.addDep((SinkableArrayValue) value);
            return ret;
        case FNEG:
        case I2F:
        case L2F:
        case D2F:
            ret = new SinkableArrayValue(Type.FLOAT_TYPE);
            ret.addDep((SinkableArrayValue) value);
            return ret;
        case LNEG:
        case I2L:
        case F2L:
        case D2L:
            ret = new SinkableArrayValue(Type.LONG_TYPE);
            ret.addDep((SinkableArrayValue) value);
            return ret;
        case DNEG:
        case I2D:
        case L2D:
        case F2D:
            ret = new SinkableArrayValue(Type.DOUBLE_TYPE);
            ret.addDep((SinkableArrayValue) value);
            return ret;
        case IFEQ:
        case IFNE:
        case IFLT:
        case IFGE:
        case IFGT:
        case IFLE:
        case TABLESWITCH:
        case LOOKUPSWITCH:
            return null;
        case IRETURN:
        case LRETURN:
        case FRETURN:
        case DRETURN:
        case ARETURN:
        case PUTSTATIC:
            return null;
        case GETFIELD:
            return newValue(Type.getType(((FieldInsnNode) insn).desc));
        case NEWARRAY:
            ret = null;
            switch(((IntInsnNode) insn).operand) {
                case T_BOOLEAN:
                    ret = (SinkableArrayValue) newValue(Type.getType("[Z"));
                    break;
                case T_CHAR:
                    ret = (SinkableArrayValue) newValue(Type.getType("[C"));
                    break;
                case T_BYTE:
                    ret = (SinkableArrayValue) newValue(Type.getType("[B"));
                    break;
                case T_SHORT:
                    ret = (SinkableArrayValue) newValue(Type.getType("[S"));
                    break;
                case T_INT:
                    ret = (SinkableArrayValue) newValue(Type.getType("[I"));
                    break;
                case T_FLOAT:
                    ret = (SinkableArrayValue) newValue(Type.getType("[F"));
                    break;
                case T_DOUBLE:
                    ret = (SinkableArrayValue) newValue(Type.getType("[D"));
                    break;
                case T_LONG:
                    ret = (SinkableArrayValue) newValue(Type.getType("[J"));
                    break;
                default:
                    throw new AnalyzerException(insn, "Invalid array type");
            }
            if (Configuration.ARRAY_LENGTH_TRACKING)
                ret.addDep((SinkableArrayValue) value);
            ret.isNewArray = true;
            return ret;
        case ANEWARRAY:
            String desc = ((TypeInsnNode) insn).desc;
            return newValue(Type.getType("[" + Type.getObjectType(desc)));
        case ARRAYLENGTH:
            ret = (SinkableArrayValue) newValue(Type.INT_TYPE);
            if (value instanceof SinkableArrayValue && value.getType() != null && TaintUtils.isPrimitiveArrayType(value.getType()))
                ret.addDep((SinkableArrayValue) value);
            return ret;
        case ATHROW:
            return null;
        case CHECKCAST:
            desc = ((TypeInsnNode) insn).desc;
            BasicValue _ret = newValue(Type.getObjectType(desc));
            if (value instanceof SinkableArrayValue) {
                if ((_ret instanceof SinkableArrayValue))
                    ((SinkableArrayValue) _ret).addDep((SinkableArrayValue) value);
            }
            return _ret;
        case INSTANCEOF:
            return newValue(Type.INT_TYPE);
        case MONITORENTER:
        case MONITOREXIT:
        case IFNULL:
        case IFNONNULL:
            return null;
        default:
            throw new Error("Internal error.");
    }
}
Also used : AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) BasicValue(org.objectweb.asm.tree.analysis.BasicValue)

Example 24 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project runelite by runelite.

the class InstructionDeserializer method deserialize.

@Override
public AbstractInsnNode deserialize(JsonElement var1, Type var2, JsonDeserializationContext var3) {
    JsonObject var4 = (JsonObject) var1;
    int var5 = var4.get("opcode").getAsInt();
    if (var5 != 21 && var5 != 25 && var5 != 58 && var5 != 54 && var5 != 22 && var5 != 55) {
        String var7;
        String var8;
        String var10;
        if (var5 != 184 && var5 != 182 && var5 != 183) {
            if (var5 == 18) {
                try {
                    return new LdcInsnNode(Integer.valueOf(var4.get("cst").getAsInt()));
                } catch (Exception var9) {
                    return new LdcInsnNode(var4.get("cst").getAsString());
                }
            } else if (var5 != 187 && var5 != 189) {
                if (var5 != 16 && var5 != 17) {
                    if (var5 != 179 && var5 != 178 && var5 != 180 && var5 != 181) {
                        return new InsnNode(var5);
                    } else {
                        var10 = var4.get("owner").getAsString();
                        var7 = var4.get("name").getAsString();
                        var8 = var4.get("desc").getAsString();
                        return new FieldInsnNode(var5, var10, var7, var8);
                    }
                } else {
                    return new IntInsnNode(var5, var4.get("operand").getAsInt());
                }
            } else {
                return new TypeInsnNode(var5, var4.get("desc").getAsString());
            }
        } else {
            var10 = var4.get("owner").getAsString();
            var7 = var4.get("name").getAsString();
            var8 = var4.get("desc").getAsString();
            return new MethodInsnNode(var5, var10, var7, var8);
        }
    } else {
        int var6 = var4.get("var").getAsInt();
        return new VarInsnNode(var5, var6);
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JsonObject(com.google.gson.JsonObject) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 25 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project BuildCraft by BuildCraft.

the class EventBusProviderASM method generateDirectHandler.

private byte[] generateDirectHandler(Method meth, Class<?> parClass, String clsName) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    ClassNode node = new ClassNode();
    node.name = clsName.replace('.', '/');
    node.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
    node.interfaces = Lists.newArrayList(IEventHandler.class.getName().replace('.', '/'));
    node.version = Opcodes.V1_6;
    node.superName = "java/lang/Object";
    String fd = Type.getDescriptor(meth.getDeclaringClass());
    node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "listener", fd, null, null));
    // This method does:
    // public ClassName(ListenerObject obj) {
    // super();
    // this.listener = obj;
    // }
    {
        MethodNode consturctorMethod = new MethodNode();
        consturctorMethod.access = Opcodes.ACC_PUBLIC;
        consturctorMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(meth.getDeclaringClass()));
        consturctorMethod.name = "<init>";
        consturctorMethod.exceptions = Lists.newArrayList();
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        consturctorMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        consturctorMethod.instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, node.name, "listener", fd));
        consturctorMethod.instructions.add(new InsnNode(Opcodes.RETURN));
        node.methods.add(consturctorMethod);
    }
    // This method does:
    // public final void handle(Object event) {
    // if (!(event instanceof EventClass)) return;
    // listener.<method_name>((EventClass) event);
    // }
    {
        MethodNode generationMethod = new MethodNode();
        // public final void handle(Object)
        generationMethod.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
        generationMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
        generationMethod.name = "handle";
        generationMethod.exceptions = Lists.newArrayList();
        // This block does:
        // if (!(event instanceof EventClass)) return;
        {
            // ADD the first object (given as [this, event])
            // -> event
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
            // event -> event instanceof PAR_CLASS -> boolean
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.INSTANCEOF, Type.getInternalName(parClass)));
            LabelNode instanceLabel = new LabelNode();
            // boolean -> if (boolean) GOTO instanceLabel ->
            generationMethod.instructions.add(new JumpInsnNode(Opcodes.IFNE, instanceLabel));
            // return;
            generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
            generationMethod.instructions.add(instanceLabel);
        }
        // This block does:
        // listener.<method_name>(event);
        {
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
            // -> ListenerObject
            String desc = Type.getDescriptor(meth.getDeclaringClass());
            generationMethod.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, node.name, "listener", desc));
            // -> Object
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
            // CheckCast
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(parClass)));
            // ListenerObject, EventObject -> <method_name> ->
            generationMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(meth.getDeclaringClass()), meth.getName(), Type.getMethodDescriptor(meth), false));
        }
        // return;
        generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
        node.methods.add(generationMethod);
    }
    node.accept(writer);
    byte[] bytecode = writer.toByteArray();
    return bytecode;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) ClassWriter(org.objectweb.asm.ClassWriter) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)53 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)29 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)26 InsnList (org.objectweb.asm.tree.InsnList)24 InsnNode (org.objectweb.asm.tree.InsnNode)20 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)20 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)16 MethodNode (org.objectweb.asm.tree.MethodNode)15 Type (org.objectweb.asm.Type)14 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)13 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)11 ClassNode (org.objectweb.asm.tree.ClassNode)10 LabelNode (org.objectweb.asm.tree.LabelNode)9 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)8 ClassReader (org.objectweb.asm.ClassReader)7 Label (org.objectweb.asm.Label)5 FieldNode (org.objectweb.asm.tree.FieldNode)5 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)5 FrameNode (org.objectweb.asm.tree.FrameNode)4