Example usage for org.objectweb.asm MethodVisitor visitIntInsn

List of usage examples for org.objectweb.asm MethodVisitor visitIntInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitIntInsn.

Prototype

public void visitIntInsn(final int opcode, final int operand) 

Source Link

Document

Visits an instruction with a single int operand.

Usage

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformExtendedBlockStorage(byte[] bytes) {
    /*try/*w w w.j ava  2s. c  o m*/
     {
         FileOutputStream fos = new FileOutputStream("d:/ExtendedBlockStorage.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching ExtendedBlockStorage.class ....");

    boolean ok = false;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    MethodNode m;
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        m = methods.next();

        if (m.name.equals("<init>") && m.desc.equals("(IZ)V")) {
            for (int index = m.instructions.size() - 1; index >= 0; index--) {
                if (m.instructions.get(index).getOpcode() == RETURN) {
                    InsnList toInject = new InsnList();

                    //toInject.add(new FrameNode(Opcodes.F_FULL, 3, new Object[] {"abx", Opcodes.INTEGER, Opcodes.INTEGER}, 0, new Object[] {}));
                    toInject.add(new VarInsnNode(ALOAD, 0));
                    toInject.add(new IntInsnNode(SIPUSH, 4096));
                    toInject.add(new IntInsnNode(NEWARRAY, T_BYTE));
                    toInject.add(new FieldInsnNode(PUTFIELD, "adr", "blockBPdataArray", "[B"));

                    m.instructions.insertBefore(m.instructions.get(index), toInject);

                    ok = true;
                    break;
                }
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getBlockBPdata", "(III)I", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(SIPUSH, 256);
    mv.visitInsn(IMUL);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 16);
    mv.visitInsn(IMUL);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitInsn(IADD);
    mv.visitInsn(BALOAD);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(4, 4);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBlockBPdata", "(IIII)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(SIPUSH, 256);
    mv.visitInsn(IMUL);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitIntInsn(BIPUSH, 16);
    mv.visitInsn(IMUL);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitInsn(IADD);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitInsn(I2B);
    mv.visitInsn(BASTORE);
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 5);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "getBPdataArray", "()[B", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBPdataArray", "([B)V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, "adr", "blockBPdataArray", "[B");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    FieldVisitor fv;

    fv = cw.visitField(ACC_PRIVATE, "blockBPdataArray", "[B", null, null);
    fv.visitEnd();

    cw.visitEnd();

    if (ok)
        System.out.println("OK");
    else
        System.out.println("Failed." + ok);

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/ExtendedBlockStorage.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:blockphysics.asm.BPTransformer.java

License:Open Source License

private byte[] transformChunk(byte[] bytes) {
    /*try/*from w ww  .  java2s .  c o  m*/
     {
         FileOutputStream fos = new FileOutputStream("d:/Chunk.orig.class");
         fos.write(bytes);
       fos.close();
     } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }*/

    System.out.print("[BlockPhysics] Patching Chunk.class ...................");

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(cw);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getBlockBPdata", "(III)I", null, null);
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitInsn(ARRAYLENGTH);
    Label l0 = new Label();
    mv.visitJumpInsn(IF_ICMPLT, l0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 4);
    mv.visitVarInsn(ALOAD, 4);
    Label l1 = new Label();
    mv.visitJumpInsn(IFNULL, l1);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER });
    mv.visitInsn(IRETURN);
    mv.visitMaxs(4, 5);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "setBlockBPdata", "(IIII)Z", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "adq", "r", "[Ladr;");
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(ICONST_4);
    mv.visitInsn(ISHR);
    mv.visitInsn(AALOAD);
    mv.visitVarInsn(ASTORE, 5);
    mv.visitVarInsn(ALOAD, 5);
    Label lab0 = new Label();
    mv.visitJumpInsn(IFNONNULL, lab0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab0);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "adr" }, 0, null);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "getBlockBPdata", "(III)I");
    mv.visitVarInsn(ISTORE, 6);
    mv.visitVarInsn(ILOAD, 6);
    mv.visitVarInsn(ILOAD, 4);
    Label lab1 = new Label();
    mv.visitJumpInsn(IF_ICMPNE, lab1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(lab1);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitFieldInsn(PUTFIELD, "adq", "l", "Z");
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitIntInsn(BIPUSH, 15);
    mv.visitInsn(IAND);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitMethodInsn(INVOKEVIRTUAL, "adr", "setBlockBPdata", "(IIII)V");
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(5, 7);
    mv.visitEnd();

    cw.visitEnd();

    System.out.println("OK");

    /*try
    {
       FileOutputStream fos = new FileOutputStream("d:/Chunk.mod.class");
       fos.write(cw.toByteArray());
     fos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }*/

    return cw.toByteArray();
}

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void generate(PrimInfo pi) {
    for (int i = 0; i < pi.arity; i++) {
        Label start = new Label();
        Label end = new Label();
        String method = getMethodName(pi, i);
        String cont = getContName(pi, i);
        MethodVisitor v = cv.visitMethod(ACC_PUBLIC, method, DESC, null, null);
        generateAnn(v, pi, i);//w  ww .j a  v a2s.  c o m
        v.visitCode();
        v.visitLabel(start);
        if (i == 0) {
            v.visitIntInsn(ALOAD, 1);
            pushIntConst(v, pi.arity);
            v.visitMethodInsn(INVOKEVIRTUAL, CTX, "stackCheck", "(I)V");
        }
        v.visitIntInsn(ALOAD, 1);
        pushIntConst(v, i << 1);
        v.visitLdcInsn(cont);
        v.visitMethodInsn(INVOKEVIRTUAL, CTX, "evalVar", "(ILjava/lang/String;)V");
        v.visitLabel(end);
        v.visitInsn(RETURN);
        v.visitLocalVariable("ctx", Type.getDescriptor(ExecutionContext.class), null, start, end, 1);
        v.visitMaxs(3, 2);
        v.visitEnd();
    }
}

From source file:bluejelly.PrimTransformer.java

License:BSD License

private void pushIntConst(MethodVisitor v, int n) {
    if (ICONSTS.containsKey(n)) {
        v.visitInsn(ICONSTS.get(n));//from  ww w  . ja  v  a2s . c o m
    } else if (n >= Byte.MIN_VALUE && n <= Byte.MAX_VALUE) {
        v.visitIntInsn(Opcodes.BIPUSH, n);
    } else if (n >= Short.MIN_VALUE && n <= Short.MAX_VALUE) {
        v.visitIntInsn(Opcodes.SIPUSH, n);
    } else {
        v.visitLdcInsn(n);
    }
}

From source file:boilerplate.processor.adapters.EqualsAdapter.java

License:Open Source License

private void addHashCodeMethod(List<FieldInfo> fields) {
    // CODE: public int hashCode() {
    MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "hashCode", "()I", null, null);
    mv.visitCode();//from  ww w . java  2 s .  c o m

    // CODE: final int prime = 31;
    mv.visitIntInsn(BIPUSH, 31);
    mv.visitVarInsn(ISTORE, 1);
    mv.visitInsn(ICONST_1);

    // CODE: int result = 1;
    mv.visitVarInsn(ISTORE, 2);

    for (FieldInfo fieldInfo : fields) {
        addCalculation(mv, fieldInfo);
    }

    // CODE: return result; }
    mv.visitVarInsn(ILOAD, 2);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(1, 3);
    mv.visitEnd();
}

From source file:br.usp.each.saeg.badua.core.internal.instr.InstrSupport.java

License:Open Source License

/**
 * Generates the instruction to push the given int value on the stack.
 * Implementation taken from//from  w  w w .j  a  va 2  s  .  c  om
 * {@link org.objectweb.asm.commons.GeneratorAdapter#push(int)}.
 *
 * @param mv
 *            visitor to emit the instruction
 * @param value
 *            the value to be pushed on the stack.
 */
public static void push(final MethodVisitor mv, final int value) {
    if (value >= -1 && value <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + value);
    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        mv.visitLdcInsn(Integer.valueOf(value));
    }
}

From source file:ch.sourcepond.utils.podescoin.internal.SerializableClassVisitor.java

License:Apache License

protected void pushByteConstant(final MethodVisitor mv, final int idx) {
    switch (idx) {
    case _ICONST_0: {
        mv.visitInsn(ICONST_0);/*from   ww  w  . jav a  2s. c o  m*/
        break;
    }
    case _ICONST_1: {
        mv.visitInsn(ICONST_1);
        break;
    }
    case _ICONST_2: {
        mv.visitInsn(ICONST_2);
        break;
    }
    case _ICONST_3: {
        mv.visitInsn(ICONST_3);
        break;
    }
    case _ICONST_4: {
        mv.visitInsn(ICONST_4);
        break;
    }
    case _ICONST_5: {
        mv.visitInsn(ICONST_5);
        break;
    }
    default: {
        mv.visitIntInsn(BIPUSH, idx);
    }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private static void emitConst(MethodVisitor mv, int value) {
    if (value >= -1 && value <= 5) {
        mv.visitInsn(Opcodes.ICONST_0 + value);
    } else if ((byte) value == value) {
        mv.visitIntInsn(Opcodes.BIPUSH, value);
    } else if ((short) value == value) {
        mv.visitIntInsn(Opcodes.SIPUSH, value);
    } else {/*from  w  w  w .j  a va  2  s.c om*/
        mv.visitLdcInsn(value);
    }
}

From source file:com.android.build.gradle.internal.transforms.InstantRunTransform.java

License:Apache License

/**
 * Use asm to generate a concrete subclass of the AppPathLoaderImpl class.
 * It only implements one method ://from  w  w w . j ava  2s. c  om
 *      String[] getPatchedClasses();
 *
 * The method is supposed to return the list of classes that were patched in this iteration.
 * This will be used by the InstantRun runtime to load all patched classes and register them
 * as overrides on the original classes.2 class files.
 *
 * @param patchFileContents list of patched class names.
 * @param outputDir output directory where to generate the .class file in.
 */
private static void writePatchFileContents(@NonNull ImmutableList<String> patchFileContents,
        @NonNull File outputDir, long buildId) {

    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, IncrementalVisitor.APP_PATCHES_LOADER_IMPL,
            null, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);

    // Add the build ID to force the patch file to be repackaged.
    cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "BUILD_ID", "J", null, buildId);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, "<init>",
                "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClasses", "()[Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitIntInsn(Opcodes.BIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] classBytes = cw.toByteArray();
    File outputFile = new File(outputDir, IncrementalVisitor.APP_PATCHES_LOADER_IMPL + ".class");
    try {
        Files.createParentDirs(outputFile);
        Files.write(classBytes, outputFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a constant value on to the top of the stack.
 * @param method the current method visitor
 * @param value the target value//from  ww  w.j  a  v a 2s.c  om
 */
public static void getInt(MethodVisitor method, int value) {
    if (-1 <= value && value <= 5) {
        method.visitInsn(Opcodes.ICONST_0 + value);
    } else if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) {
        method.visitIntInsn(Opcodes.BIPUSH, value);
    } else if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) {
        method.visitIntInsn(Opcodes.SIPUSH, value);
    } else {
        method.visitLdcInsn(value);
    }
}