Example usage for org.objectweb.asm.tree MethodNode visitVarInsn

List of usage examples for org.objectweb.asm.tree MethodNode visitVarInsn

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitVarInsn.

Prototype

@Override
    public void visitVarInsn(final int opcode, final int var) 

Source Link

Usage

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-getter getter method in the specified {@code ClassNode}
 *
 * @param cn The ClassNode/*from   w ww .j av  a 2  s  .c  o m*/
 */
private void createFieldGetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldGetter",
            "(Ljava/lang/String;)Ljava/util/function/Supplier;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldGetter$" + current++,
                    "()Ljava/lang/Object;", null, null);

            // Get the field value
            handle.visitVarInsn(ALOAD, 0);
            handle.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            // If the field is a primitive type, get the object representation
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKESTATIC, object, "valueOf", "(" + fn.desc + ")L" + object + ";",
                        false);

            // Return the value
            handle.visitInsn(ARETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the getter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("get", "(L" + cn.name + ";)Ljava/util/function/Supplier;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("()Ljava/lang/Object;"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("()Ljava/lang/Object;"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-setter getter method in the specified {@code ClassNode}
 *
 * @see ValueAccessor/* w  w w .  j av a  2  s  . com*/
 *
 * @param cn The ClassNode
 */
private void createFieldSetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldSetter",
            "(Ljava/lang/String;)Ljava/util/function/Consumer;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldSetter$" + current++,
                    "(Ljava/lang/Object;)V", null, null);

            handle.visitVarInsn(ALOAD, 0);
            handle.visitVarInsn(ALOAD, 1);
            handle.visitTypeInsn(CHECKCAST, getStrippedDesc(getObject(fn.desc)));

            // If the field is a primitive type, get the primitive value
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKEVIRTUAL, object, getClassName(fn.desc) + "Value", "()" + fn.desc,
                        false);

            // Set the field value
            handle.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
            handle.visitInsn(RETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the setter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("accept", "(L" + cn.name + ";)Ljava/util/function/Consumer;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("(Ljava/lang/Object;)V"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("(Ljava/lang/Object;)V"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementDefaultConstructor() {
    MethodNode ctor = findMethod("<init>", "()V");
    if (ctor == null) {
        ctor = new MethodNode(ACC_PUBLIC, "<init>", "()V", null, null);

        ctor.visitCode();/*  w w w .j a  v  a  2  s.com*/
        ctor.visitVarInsn(ALOAD, 0);
        ctor.visitMethodInsn(INVOKESPECIAL, cn.superName, "<init>", "()V");
        ctor.visitInsn(RETURN);
        ctor.visitMaxs(1, 1);
        ctor.visitEnd();

        cn.methods.add(ctor);
    } else if ((ctor.access & ACC_PUBLIC) == 0) {
        LOG.info("Class " + cn.name + " has a non-public default constructor. Making it public.");
        ctor.access = ACC_PUBLIC;
    }
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementReadExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();/*ww w . j av a  2 s . c om*/

    for (int version : properties.keySet()) {
        mn.visitVarInsn(ALOAD, 1);
        mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I");
        mn.visitIntInsn(BIPUSH, version);
        Label l = new Label();
        mn.visitJumpInsn(IF_ICMPLT, l);

        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 0);
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);

            ReadMethod readMethod = getReadMethod(fn, type);
            readMethod.createTemplate(mn, fn, type);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(),
                    readMethod.getDescriptor());
            if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) {
                mn.visitTypeInsn(CHECKCAST, type.getInternalName());
            }
            mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
        }

        mn.visitLabel(l);
        mn.visitFrame(F_SAME, 0, null, 0, null);
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementWriteExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "writeExternal", "(Lcom/tangosol/io/pof/PofWriter;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();/*w  w  w  .j a  v  a 2  s .  co  m*/

    for (int version : properties.keySet()) {
        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            addPofIndex(fn, index);
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("writing attribute " + index + " (" + fn.name + ") to POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);
            mn.visitVarInsn(ALOAD, 0);
            mn.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            WriteMethod writeMethod = getWriteMethod(fn, type);
            writeMethod.pushUniformTypes(mn);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofWriter", writeMethod.getName(),
                    writeMethod.getDescriptor());
        }
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:de.sanandrew.core.manpack.transformer.TransformBadPotionsATN.java

License:Creative Commons License

private byte[] transformPotion(byte[] bytes) {
    ClassNode cn = ASMHelper.createClassNode(bytes);

    if (ASMHelper.hasClassMethodName(cn, ASMNames.M_isBadEffect)) {
        return bytes;
    }/*from  ww w. j av a2  s. co  m*/

    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, ASMNames.M_isBadEffect, "()Z", null, null);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/potion/Potion", ASMNames.F_isBadEffect, "Z");
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", "Lnet/minecraft/potion/Potion;", null, l0, l1, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();

    cn.methods.add(method);

    bytes = ASMHelper.createBytes(cn, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAccessors(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    int complete = 0;
    for (MethodNode method : clazz.methods) {
        switch (method.name) {
        case "getELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();/*from  w w  w .j a  v  a 2s.c o  m*/
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getAttackingPlayer", "()Lnet/minecraft/entity/player/EntityPlayer;", false);
            method.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELAttackingPlayer": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setAttackingPlayer", "(Lnet/minecraft/entity/player/EntityPlayer;)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "getELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_getRecentlyHit", "()I", false);
            method.visitInsn(Opcodes.IRETURN);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        case "setELRecentlyHit": {
            method.instructions.clear();
            method.visitCode();
            Label l0 = new Label();
            method.visitLabel(l0);
            method.visitVarInsn(Opcodes.ALOAD, 1);
            method.visitVarInsn(Opcodes.ILOAD, 0);
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase",
                    "_SAP_setRecentlyHit", "(I)V", false);
            Label l1 = new Label();
            method.visitLabel(l1);
            method.visitInsn(Opcodes.RETURN);
            Label l2 = new Label();
            method.visitLabel(l2);
            method.visitLocalVariable("hit", "I", null, l0, l2, 0);
            method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1);
            method.visitMaxs(0, 0);
            method.visitEnd();
            complete++;
            continue;
        }
        }

        if (complete >= 4) {
            break;
        }
    }

    bytes = ASMHelper.createBytes(clazz, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java

License:Creative Commons License

private static byte[] transformAttackingPlayer(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    /** ADD GETTER FOR ATTACKING PLAYER **/
    {//from w  w  w .  j  a v  a  2  s .  c o  m
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getAttackingPlayer",
                "()Lnet/minecraft/entity/player/EntityPlayer;", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        method.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR ATTACKING PLAYER **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setAttackingPlayer",
                "(Lnet/minecraft/entity/player/EntityPlayer;)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ALOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase",
                ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD GETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getRecentlyHit", "()I", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        method.visitInsn(Opcodes.IRETURN);
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    /** ADD SETTER FOR RECENTLY HIT **/
    {
        MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setRecentlyHit", "(I)V", null, null);
        method.visitCode();
        Label l0 = new Label();
        method.visitLabel(l0);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitVarInsn(Opcodes.ILOAD, 1);
        method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit,
                "I");
        Label l1 = new Label();
        method.visitLabel(l1);
        method.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        method.visitLabel(l2);
        method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0);
        method.visitLocalVariable("hit", "I", null, l0, l2, 1);
        method.visitMaxs(0, 0);
        method.visitEnd();
        clazz.methods.add(method);
    }

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformEntityCollision.java

License:Creative Commons License

private static byte[] transformEntity(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX);
    method.visitCode();//  www.  j  av a  2 s . co  m
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 2);
    method.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0);
    method.visitLocalVariable("entity", ASMNames.CL_T_ENTITY, null, l0, l1, 1);
    method.visitLocalVariable("oldAABB", ASMNames.CL_T_AXIS_ALIGNED_BB, null, l0, l1, 2);
    method.visitMaxs(1, 2);
    method.visitEnd();

    clazz.methods.add(method);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
    return bytes;
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static MethodNode injectMethodGetCustomArmorItem() {
    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM);
    method.visitCode();//from  w  w w  . j  a va  2  s.  c o m
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER);
    method.visitIntInsn(Opcodes.BIPUSH, 23);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_GET_OBJ_STACK, false);
    method.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l1, 0);
    method.visitMaxs(2, 1);
    method.visitEnd();

    return method;
}