Example usage for org.objectweb.asm MethodVisitor visitEnd

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

Introduction

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

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the method.

Usage

From source file:jaspex.transactifier.FieldTransactifierClassVisitor.java

License:Open Source License

@Override
public void visitEnd() {
    if (_injectStaticFieldBase) {
        cv.visitField(ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, "$staticFieldBase",
                Options.STATICWORKAROUND ? CommonTypes.STATICFIELDBASE.bytecodeName()
                        : Type.OBJECT.bytecodeName(),
                null, null);/*from  w  w  w  .  j  a v a2 s.c  o  m*/
    }

    // Adicionar <clinit> para classes que no o tinham j
    if (_injectClinit && _foundFields) {
        MethodVisitor mv = cv.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    cv.visitEnd();
}

From source file:javalin.expr.Exp.java

License:Open Source License

byte[] compile(final String name) {
    // class header
    final String[] itfs = { DoubleExpression.class.getName().replaceAll("\\.", "/") };
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, name, null, "java/lang/Object", itfs);

    // default public constructor
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(ALOAD, 0);//from  w ww .j av  a  2s. com
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // eval method
    mv = cw.visitMethod(ACC_PUBLIC, "eval", "(Ljava/util/Map;)D",
            "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Double;>;)D", null);
    compile(mv);
    mv.visitInsn(DRETURN);
    // max stack and max locals automatically computed
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitLdcInsn(toString());
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    return cw.toByteArray();
}

From source file:javaone2015.con7442.indyprotector.BootstrapMethodGenerator.java

License:Apache License

/**
 * Generate bootstrap method// w  w  w.j  av  a2s. c o m
 */
public void insertMethod(ClassVisitor target) {
    MethodVisitor mv = target.visitMethod(ACC_PRIVATE + ACC_STATIC, BSM_NAME, BSM_SIG, null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
            false);
    mv.visitVarInsn(ASTORE, 8);
    mv.visitLdcInsn(Type.getType(targetClassName));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false);
    mv.visitVarInsn(ASTORE, 9);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitVarInsn(ALOAD, 9);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "fromMethodDescriptorString",
            "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/invoke/MethodType;", false);
    mv.visitVarInsn(ASTORE, 10);
    mv.visitVarInsn(ILOAD, 3);
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    mv.visitTableSwitchInsn(182, 185, l4, new Label[] { l3, l4, l5, l3 });
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_FULL, 11,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle", "java/lang/Class",
                    "java/lang/ClassLoader", "java/lang/invoke/MethodType" },
            0, new Object[] {});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findStatic",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    Label l6 = new Label();
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "asType",
            "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l1);
    Label l7 = new Label();
    mv.visitJumpInsn(GOTO, l7);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 8,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle" },
            1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 8);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l7);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>",
            "(Ljava/lang/invoke/MethodHandle;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 11);
    mv.visitEnd();

}

From source file:jerl.bcm.inj.InjectionClassMethodAdd.java

License:Apache License

@Override
public void inject(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(getAccess(), getName(), getDesc(), getSignature(), getExceptions());
    mv.visitCode();//from   w  w w  . jav a 2  s  .  c  o  m

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 0);
    mv.visitEnd();
}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private void addConstructor(ClassVisitor cv) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();// w  w w .j av a  2s. c  o  m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, objectInternalName, "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private void addNonStaticMethods(CompilerContext context, ClassVisitor cv) {
    MethodVisitor mv;

    // public int exec(int returnAddress, int alternativeReturnAddress, boolean isJump) throws Exception;
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getExecMethodName(), context.getExecMethodDesc(), null,
            exceptions);/*w  ww. j  a va  2 s. c om*/
    mv.visitCode();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, getClassName(), context.getStaticExecMethodName(),
            context.getStaticExecMethodDesc());
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // private static IExecutable e;
    FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, context.getReplaceFieldName(),
            executableDescriptor, null, null);
    fv.visitEnd();

    // public void setExecutable(IExecutable e);
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getReplaceMethodName(), context.getReplaceMethodDesc(),
            null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();

    // public IExecutable getExecutable();
    mv = cv.visitMethod(Opcodes.ACC_PUBLIC, context.getGetMethodName(), context.getGetMethodDesc(), null,
            exceptions);
    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, getClassName(), context.getReplaceFieldName(), executableDescriptor);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private Class<IExecutable> interpret(CompilerContext context) {
    Class<IExecutable> compiledClass = null;

    context.setCodeBlock(this);
    String className = getInternalClassName();
    if (log.isInfoEnabled()) {
        log.info("Compiling for Interpreter " + className);
    }//from  w  ww  .  ja  v a 2  s  .co  m

    int computeFlag = ClassWriter.COMPUTE_FRAMES;
    if (context.isAutomaticMaxLocals() || context.isAutomaticMaxStack()) {
        computeFlag |= ClassWriter.COMPUTE_MAXS;
    }
    ClassWriter cw = new ClassWriter(computeFlag);
    ClassVisitor cv = cw;
    if (log.isDebugEnabled()) {
        cv = new CheckClassAdapter(cv);
    }

    StringWriter debugOutput = null;
    if (log.isDebugEnabled()) {
        debugOutput = new StringWriter();
        PrintWriter debugPrintWriter = new PrintWriter(debugOutput);
        cv = new TraceClassVisitor(cv, debugPrintWriter);
    }
    cv.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, className, null, objectInternalName,
            interfacesForExecutable);
    context.startClass(cv);

    addConstructor(cv);
    addNonStaticMethods(context, cv);

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            context.getStaticExecMethodName(), context.getStaticExecMethodDesc(), null, exceptions);
    mv.visitCode();
    context.setMethodVisitor(mv);
    context.startMethod();

    context.compileExecuteInterpreter(getStartAddress());

    mv.visitMaxs(context.getMaxStack(), context.getMaxLocals());
    mv.visitEnd();

    cv.visitEnd();

    if (debugOutput != null) {
        log.debug(debugOutput.toString());
    }

    compiledClass = loadExecutable(context, className, cw.toByteArray());

    return compiledClass;
}

From source file:jpcsp.Allegrex.compiler.CodeBlock.java

License:Open Source License

private Class<IExecutable> compile(CompilerContext context) throws ClassFormatError {
    Class<IExecutable> compiledClass = null;

    context.setCodeBlock(this);
    String className = getInternalClassName();
    if (log.isDebugEnabled()) {
        String functionName = Utilities.getFunctionNameByAddress(getStartAddress());

        if (functionName != null) {
            log.debug(String.format("Compiling %s (%s)", className, functionName));
        } else {/*from   ww  w . j  a va2  s .c o  m*/
            log.debug(String.format("Compiling %s", className));
        }
    }

    prepare(context, context.getMethodMaxInstructions());

    currentSequence = null;
    int computeFlag = ClassWriter.COMPUTE_FRAMES;
    if (context.isAutomaticMaxLocals() || context.isAutomaticMaxStack()) {
        computeFlag |= ClassWriter.COMPUTE_MAXS;
    }
    ClassWriter cw = new ClassWriter(computeFlag);
    ClassVisitor cv = cw;
    if (log.isDebugEnabled()) {
        cv = new CheckClassAdapter(cv);
    }
    StringWriter debugOutput = null;
    if (log.isTraceEnabled()) {
        debugOutput = new StringWriter();
        PrintWriter debugPrintWriter = new PrintWriter(debugOutput);
        cv = new TraceClassVisitor(cv, debugPrintWriter);
    }
    cv.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, className, null, objectInternalName,
            interfacesForExecutable);
    context.startClass(cv);

    addConstructor(cv);
    addNonStaticMethods(context, cv);

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            context.getStaticExecMethodName(), context.getStaticExecMethodDesc(), null, exceptions);
    mv.visitCode();
    context.setMethodVisitor(mv);
    context.startMethod();

    // Jump to the block start if other instructions have been inserted in front
    if (!codeInstructions.isEmpty() && codeInstructions.getFirst().getAddress() != getStartAddress()) {
        mv.visitJumpInsn(Opcodes.GOTO, getCodeInstruction(getStartAddress()).getLabel());
    }

    compile(context, mv, codeInstructions);
    mv.visitMaxs(context.getMaxStack(), context.getMaxLocals());
    mv.visitEnd();

    for (SequenceCodeInstruction sequenceCodeInstruction : sequenceCodeInstructions) {
        if (log.isDebugEnabled()) {
            log.debug("Compiling Sequence " + sequenceCodeInstruction.getMethodName(context));
        }
        currentSequence = sequenceCodeInstruction;
        mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
                sequenceCodeInstruction.getMethodName(context), "()V", null, exceptions);
        mv.visitCode();
        context.setMethodVisitor(mv);
        context.startSequenceMethod();

        compile(context, mv, sequenceCodeInstruction.getCodeSequence().getInstructions());

        context.endSequenceMethod();
        mv.visitMaxs(context.getMaxStack(), context.getMaxLocals());
        mv.visitEnd();
    }
    currentSequence = null;

    cv.visitEnd();

    if (debugOutput != null) {
        log.trace(debugOutput.toString());
    }

    try {
        compiledClass = loadExecutable(context, className, cw.toByteArray());
    } catch (NullPointerException e) {
        log.error("Error while compiling " + className + ": " + e);
    }

    return compiledClass;
}

From source file:jtaint.ByteCodeUtil.java

License:Apache License

public static void buildGetter(ClassVisitor cv, String owner, String fieldName, String fieldDesc, int accFlags,
        String methodName) {/*from w  ww .  j a v  a 2 s .c  o  m*/
    MethodVisitor mv = cv.visitMethod(accFlags + ACC_PUBLIC, methodName, "()" + fieldDesc, null, null);
    Type t = Type.getType(fieldDesc);

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, owner, fieldName, fieldDesc);
    mv.visitInsn(t.getOpcode(IRETURN));

    mv.visitMaxs(t.getSize(), 1);
    mv.visitEnd();
}

From source file:jtaint.ByteCodeUtil.java

License:Apache License

public static void buildSetter(ClassVisitor cv, String owner, String fieldName, String fieldDesc, int accFlags,
        String methodName) {//  w ww  . j a  va 2  s .  c  o  m
    MethodVisitor mv = cv.visitMethod(accFlags + ACC_PUBLIC, methodName, "(" + fieldDesc + ")V", null, null);
    Type t = Type.getType(fieldDesc);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(t.getOpcode(ILOAD), 1);
    mv.visitFieldInsn(PUTFIELD, owner, fieldName, fieldDesc);
    mv.visitInsn(RETURN);

    mv.visitMaxs(1 + t.getSize(), 1 + t.getSize());
    mv.visitEnd();
}