Example usage for org.objectweb.asm Opcodes ALOAD

List of usage examples for org.objectweb.asm Opcodes ALOAD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ALOAD.

Prototype

int ALOAD

To view the source code for org.objectweb.asm Opcodes ALOAD.

Click Source Link

Usage

From source file:com.github.malamut2.low.AllocationMethodAdapter.java

License:Apache License

private void dupStackElementBeforeSignatureArgs(final String sig) {
    final Label beginScopeLabel = new Label();
    final Label endScopeLabel = new Label();
    super.visitLabel(beginScopeLabel);

    Type[] argTypes = Type.getArgumentTypes(sig);
    int[] args = new int[argTypes.length];

    for (int i = argTypes.length - 1; i >= 0; --i) {
        args[i] = newLocal(argTypes[i], beginScopeLabel, endScopeLabel);
        super.visitVarInsn(argTypes[i].getOpcode(Opcodes.ISTORE), args[i]);
    }//from www .j  a v a 2s .  c o m
    super.visitInsn(Opcodes.DUP);
    for (int i = 0; i < argTypes.length; ++i) {
        int op = argTypes[i].getOpcode(Opcodes.ILOAD);
        super.visitVarInsn(op, args[i]);
        if (op == Opcodes.ALOAD) {
            super.visitInsn(Opcodes.ACONST_NULL);
            super.visitVarInsn(Opcodes.ASTORE, args[i]);
        }
    }
    super.visitLabel(endScopeLabel);
}

From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv != null && !mIgnoreClass) {
        if ("<clinit>".equals(name)) {
            isClInitExists = true;/*  w w w  . j a v  a2 s  .com*/
            // If clinit exists meaning the static fields (not final) would have be inited here.
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                private String lastStashCst;

                @Override
                public void visitCode() {
                    super.visitCode();
                    // Here init static final fields.
                    for (ClassStringField field : mStaticFinalFields) {
                        if (!canEncrypted(field.value)) {
                            continue;
                        }
                        String originValue = field.value;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        super.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name,
                                ClassStringField.STRING_DESC);
                    }
                }

                @Override
                public void visitLdcInsn(Object cst) {
                    // Here init static or static final fields, but we must check field name int 'visitFieldInsn'
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        lastStashCst = (String) cst;
                        String originValue = lastStashCst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        lastStashCst = null;
                        super.visitLdcInsn(cst);
                    }
                }

                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (mClassName.equals(owner) && lastStashCst != null) {
                        boolean isContain = false;
                        for (ClassStringField field : mStaticFields) {
                            if (field.name.equals(name)) {
                                isContain = true;
                                break;
                            }
                        }
                        if (!isContain) {
                            for (ClassStringField field : mStaticFinalFields) {
                                if (field.name.equals(name) && field.value == null) {
                                    field.value = lastStashCst;
                                    break;
                                }
                            }
                        }
                    }
                    lastStashCst = null;
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };

        } else if ("<init>".equals(name)) {
            // Here init final(not static) and normal fields
            mv = new MethodVisitor(Opcodes.ASM5, mv) {
                @Override
                public void visitLdcInsn(Object cst) {
                    // We don't care about whether the field is final or normal
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        super.visitLdcInsn(cst);
                    }
                }
            };
        } else {
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                @Override
                public void visitLdcInsn(Object cst) {
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        // If the value is a static final field
                        for (ClassStringField field : mStaticFinalFields) {
                            if (cst.equals(field.value)) {
                                super.visitFieldInsn(Opcodes.GETSTATIC, mClassName, field.name,
                                        ClassStringField.STRING_DESC);
                                return;
                            }
                        }
                        // If the value is a final field (not static)
                        for (ClassStringField field : mFinalFields) {
                            // if the value of a final field is null, we ignore it
                            if (cst.equals(field.value)) {
                                super.visitVarInsn(Opcodes.ALOAD, 0);
                                super.visitFieldInsn(Opcodes.GETFIELD, mClassName, field.name,
                                        "Ljava/lang/String;");
                                return;
                            }
                        }
                        // local variables
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        return;
                    }
                    super.visitLdcInsn(cst);
                }

            };
        }
    }
    return mv;
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

public static byte[] compileFunc(TokenSource<RPNToken> source, String variable) throws ParsingError {
    // initialize class
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null,
            CompiledClassBase.class.getName().replace('.', '/'),
            new String[] { Evaluator.Function.class.getName().replace('.', '/') });

    // create constructor
    MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    construct.visitCode();/* w  w  w  .  j a v a 2s.co  m*/
    construct.visitVarInsn(Opcodes.ALOAD, 0);
    construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'),
            "<init>", "()V");
    construct.visitInsn(Opcodes.RETURN);
    construct.visitMaxs(0, 0);
    construct.visitEnd();

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "(D)D", null,
            new String[] { "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" });

    compileCommon(source, method, variable);

    writer.visitEnd();
    return writer.toByteArray();
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

public static byte[] compileBasic(TokenSource<RPNToken> source) throws ParsingError {
    // initialize class
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/github/rgcjonas/kuemmelgtr/jit/BogusName", null,
            CompiledClassBase.class.getName().replace('.', '/'),
            new String[] { Evaluator.Basic.class.getName().replace('.', '/') });

    // create constructor
    MethodVisitor construct = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    construct.visitCode();//from w  ww  . j ava2s.c o m
    construct.visitVarInsn(Opcodes.ALOAD, 0);
    construct.visitMethodInsn(Opcodes.INVOKESPECIAL, CompiledClassBase.class.getName().replace('.', '/'),
            "<init>", "()V");
    construct.visitInsn(Opcodes.RETURN);
    construct.visitMaxs(0, 0);
    construct.visitEnd();

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "evaluate", "()D", null,
            new String[] { "com/github/rgcjonas/kuemmelgtr/core/ParsingError",
                    "com/github/rgcjonas/kuemmelgtr/core/RuntimeError" });

    compileCommon(source, method, null);

    writer.visitEnd();
    return writer.toByteArray();
}

From source file:com.github.rgcjonas.kuemmelgtr.core.Compiler.java

License:Open Source License

private static void compileCommon(TokenSource<RPNToken> source, MethodVisitor method, String variable)
        throws ParsingError {
    method.visitCode();/*from w  w w  .  j a  va 2 s . c  o  m*/

    // we now can compile our token stream
    int currentStackSize = 0; // we keep track of the current stack size to throw errors if we encounter an invalid instruction
    RPNToken t;
    while ((t = source.nextToken()) != null) {
        if (t instanceof RPNToken.Operand) {
            // we push it onto the stack
            method.visitLdcInsn(((RPNToken.Operand) t).getValue());
            currentStackSize++;
        } else if (t instanceof RPNToken.Operator) {
            RPNToken.Operator op = (RPNToken.Operator) t;

            if (currentStackSize < op.getNumOperands())
                throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand(s)");

            switch (op.getName()) {
            case "_add":
                method.visitInsn(Opcodes.DADD);
                currentStackSize--;
                break;
            case "_mult":
                method.visitInsn(Opcodes.DMUL);
                currentStackSize--;
                break;
            case "_sub":
                method.visitInsn(Opcodes.DSUB);
                currentStackSize--;
                break;
            case "_div":
                method.visitInsn(Opcodes.DDIV);
                currentStackSize--;
                break;
            case "_pow":
                method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "pow", "(DD)D");
                currentStackSize--;
                break;
            default:
                //HACK: support every function in java/lang/Math. Will be way more performant than any lookup-and-evaluate
                //TODO: implement more functions inline
                if (currentStackSize < 1)
                    throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Missing operand");
                try {
                    Method meth = Math.class.getDeclaredMethod(op.getName(), double.class); // just check if it's available and hope it returns a double
                    if (meth.getReturnType() != double.class)
                        throw new NoSuchMethodException(); // we don't want to blow up at runtime, do we?

                    method.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", op.getName(), "(D)D");
                } catch (NoSuchMethodException e) {
                    // we do not give up. The method may be available at runtime.
                    method.visitVarInsn(Opcodes.ALOAD, 0);
                    method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double
                    method.visitInsn(Opcodes.POP);
                    method.visitLdcInsn(op.getName());
                    method.visitLdcInsn((int) op.getSrcLine());
                    method.visitLdcInsn((int) op.getSrcColumn());
                    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                            CompiledClassBase.class.getName().replace('.', '/'), "resolveAndEvaluateFunction",
                            "(DLjava/lang/String;II)D");
                }
            }
        } else if (t instanceof RPNToken.VariableRecall) {
            // maybe, maybe the variable is an argument we can load
            if (variable != null && ((RPNToken.VariableRecall) t).getName().equalsIgnoreCase(variable)) {
                method.visitVarInsn(Opcodes.DLOAD, 1);
            } else {
                // we let our parent class do the hard work
                method.visitVarInsn(Opcodes.ALOAD, 0);
                method.visitLdcInsn(((RPNToken.VariableRecall) t).getName());
                method.visitLdcInsn(t.getSrcLine());
                method.visitLdcInsn(t.getSrcColumn());
                method.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                        CompiledClassBase.class.getName().replace('.', '/'), "getVariable",
                        "(Ljava/lang/String;II)D");
            }
            currentStackSize++;
        } else if (t instanceof RPNToken.VariableAssignment) {
            // also defer to our parent class
            // we do not give up. The method may be available at runtime.
            method.visitVarInsn(Opcodes.ALOAD, 0);
            method.visitInsn(Opcodes.DUP_X2); // swap the this pointer and double
            method.visitInsn(Opcodes.POP);
            method.visitLdcInsn(((RPNToken.VariableAssignment) t).getVariableName());
            method.visitLdcInsn(t.getSrcLine());
            method.visitLdcInsn(t.getSrcColumn());
            method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CompiledClassBase.class.getName().replace('.', '/'),
                    "setVariable", "(DLjava/lang/String;II)D");
        } else {
            throw new ParsingError(t.getSrcLine(), t.getSrcColumn(), "Unknown instruction: " + t);
        }
    }

    if (currentStackSize != 1)
        throw new ParsingError(0, 0, "Expected stack to be one value, found " + currentStackSize);

    method.visitInsn(Opcodes.DRETURN);

    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.github.wreulicke.bean.validation.ASMMethodParameterValidationInjector.java

License:Open Source License

private void inject(MethodNode node, ClassNode clazzNode) {
    if (Modifier.isStatic(node.access) || Modifier.isAbstract(node.access) || Modifier.isAbstract(node.access)
            || "<init>".equals(node.name) || "<clinit>".equals(node.name)) {
        return;// ww  w  . jav a2 s .c  om
    }

    InsnList list = new InsnList();
    int index = node.localVariables.size();
    list.add(new MethodInsnNode(INVOKESTATIC, "javax/validation/Validation", "buildDefaultValidatorFactory",
            "()Ljavax/validation/ValidatorFactory;", false));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/ValidatorFactory", "getValidator",
            "()Ljavax/validation/Validator;", true));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/Validator", "forExecutables",
            "()Ljavax/validation/executable/ExecutableValidator;", true));

    list.add(new VarInsnNode(Opcodes.ASTORE, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));

    list.add(new LdcInsnNode(Type.getType("L" + clazzNode.name + ";")));
    list.add(new LdcInsnNode(node.name));

    @SuppressWarnings("unchecked")
    List<LocalVariableNode> variables = node.localVariables;
    Type[] args = Type.getArgumentTypes(node.desc);

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));

    for (int i = 0; i < args.length; i++) {
        int paramIndex = 1 + i;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new LdcInsnNode(Type.getType(variables.get(paramIndex).desc)));
        list.add(new InsnNode(Opcodes.AASTORE));
    }

    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false));

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < args.length; i++) {
        int paramIndex = i + 1;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new VarInsnNode(Opcodes.ALOAD, paramIndex));
        list.add(new InsnNode(Opcodes.AASTORE));
    }
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/executable/ExecutableValidator",
            "validateParameters",
            "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/util/Set;",
            true));
    list.add(new MethodInsnNode(INVOKESTATIC, "com/github/wreulicke/bean/validation/Constraints",
            "throwIfNeeded", "(Ljava/util/Set;)V", false));
    node.instructions.insert(list);
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

private void checkProlog() {
    // Generate only once (at the beginning)
    if (prologGenerated) {
        return;//from   w  w  w .  ja va 2s  . c  o m
    }
    prologGenerated = true;

    // try...catch around the whole body. Should be added last (after all
    // other try..catch blocks).
    // Actually, we are making an assumption that reader will emit
    // visitTryCatchBlock BEFORE generating any bytecode.
    // We cannot call visitTryCatchBlock at the end since it should
    // be called before all its labels are visited (which we generate
    // in this block).
    mv.visitTryCatchBlock(tryLabel, dispatchLabel, catchLabel, null);

    // Goto to table switch dispatcher
    mv.visitJumpInsn(Opcodes.GOTO, dispatchLabel);

    // Wrap the whole original body with try...catch that will invoke
    // cont.setException(Throwable t)
    mv.visitLabel(tryLabel);

    // Restore the frame as it was before changing method arguments
    Type[] locs = info.entryLocals;
    Object[] frame = new Object[info.isStatic() ? locs.length : locs.length + 1];

    // Frame
    if (!info.isStatic()) {
        frame[0] = info.owner;
    }

    int off = info.isStatic() ? 0 : 1;
    for (int i = 0; i < locs.length; ++i) {
        frame[off + i] = Frames.toFrameType(locs[i]);
    }

    mv.visitFrame(Opcodes.F_FULL, frame.length, frame, 0, new Object[0]);
    Label label = new Label();
    dispatchTable.add(label);
    mv.visitLabel(label);

    if (!info.isStatic()) {
        locals.add(Type.getObjectType(info.owner));
    }
    Collections.addAll(locals, locs);

    // Restore the original method arguments
    // argX = state.varX
    String[] names = info.entryLocalsVars;
    for (int i = 0; i < info.entryLocals.length; ++i) {
        // We go directly to target, introduced var used
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, names[i], info.entryLocals[i].getDescriptor());
        mv.visitVarInsn(info.entryLocals[i].getOpcode(Opcodes.ISTORE), info.isStatic() ? i : i + 1);

    }
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    checkProlog();//from  ww  w  .j a  va 2 s.  co m

    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && ARETURN_NAME.equals(name)) {

        if (ARETURN_VOID_DESC.equals(desc)) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

        // state variable
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);
        mv.visitInsn(Opcodes.SWAP);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_INVOKE_NAME,
                CONTINUATION_INVOKE_DESC);

        // Will be dropped while replacing ARETURN with RETURN.
        // FIXME: Should verify this value is NOT used.
        mv.visitInsn(Opcodes.ACONST_NULL);
        return;
    }
    if (opcode == Opcodes.INVOKESTATIC && ASYNC_NAME.equals(owner) && AWAIT_NAME.equals(name)
            && AWAIT_DESC.equals(desc)) {

        // Computation<T> is on stack

        // FIXME: ...
        // if (stack.size() != 1) {
        // throw new IllegalStateException(
        // "Stack preserving is not supported!");
        // }

        int index = dispatchTable.size();

        // Save state
        List<Type> l = new ArrayList<Type>(locals);
        if (!info.isStatic()) {
            l.remove(0);
        }

        // state.varX = locX
        String[] vars = info.tracker.stateFields(l.toArray(new Type[0]));
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ILOAD), i + info.thisOffset);
            mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
        }

        // Create instance of continuation
        // new Continuation([this, ]state, index);
        mv.visitTypeInsn(Opcodes.NEW, info.continuationClassName);
        mv.visitInsn(Opcodes.DUP);

        // "this' for new Continuation([this, ]state, index)
        if (!info.isStatic()) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
        }

        // state and index
        target.visitVarInsn(Opcodes.ALOAD, 0 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, index);

        String ctorDesc;
        if (info.isStatic()) {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE });
        } else {
            ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getObjectType(info.owner), info.stateType, Type.INT_TYPE });
        }
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, info.continuationClassName, CTOR_NAME, ctorDesc);

        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, COMPUTATION_NAME, COMPUTATION_EXECUTE_NAME,
                COMPUTATION_EXECUTE_DESC);
        super.visitInsn(Opcodes.RETURN);

        // Restore state
        // mv.visitFrame(Opcodes.F_SAME, 0, new Object[0], 0, new
        // Object[0]);
        Label label = new Label();

        int invokeIndex = dispatchTable.size();
        dispatchTable.add(label); // for invoke
        dispatchTable.add(label); // for setException
        mv.visitLabel(label);
        for (int i = 0; i < vars.length; ++i) {
            // state variable
            target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
            mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, vars[i], l.get(i).getDescriptor());
            mv.visitVarInsn(l.get(i).getOpcode(Opcodes.ISTORE), i + info.thisOffset);
        }

        // if (index == invokeIndex) goto invokeLabel;
        Label invokeLabel = new Label();
        target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
        mv.visitIntInsn(Opcodes.BIPUSH, invokeIndex);
        mv.visitJumpInsn(Opcodes.IF_ICMPEQ, invokeLabel);

        // Throw exception
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "exception", THROWABLE_DESC);
        mv.visitInsn(Opcodes.ATHROW);

        // Push result value
        // invokeLabel:
        mv.visitLabel(invokeLabel);
        target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
        mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, "result", OBJECT_DESC);
        return;
    }
    super.visitMethodInsn(opcode, owner, name, desc);
}

From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java

License:Apache License

@Override
public void visitMaxs(int maxStack, int maxLocals) {
    // Table switch at the end of the method
    mv.visitLabel(dispatchLabel);//from   w w w  .  j  a v a  2s  .  co  m

    Label dflt = new Label();

    // Load index
    target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset);
    int[] keys = new int[dispatchTable.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = i;
    }
    mv.visitLookupSwitchInsn(dflt, keys, dispatchTable.toArray(new Label[0]));

    // FIXME: ...throw exception
    mv.visitLabel(dflt);
    mv.visitInsn(Opcodes.RETURN);

    // catch block
    mv.visitLabel(catchLabel);

    // invoke Continuation#setException(Throwable t)
    target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1);
    mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC);
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_SET_EXCEPTION_NAME,
            CONTINUATION_SET_EXCEPTION_DESC);
    mv.visitInsn(Opcodes.RETURN);

    // FIXME: evaluate properly
    super.visitMaxs(maxStack + 4 + info.thisOffset, maxLocals + 2);
}

From source file:com.google.code.jconts.instrument.gen.ComputationClassGenerator.java

License:Apache License

private void generateConstructor(ClassVisitor cv) {
    final String name = info.computationClassName;
    final Type outerType = Type.getObjectType(info.owner);

    // Constructor have form either <init>(OuterClass this$0, State state)
    // or <init>(State state)
    String ctorDesc;// ww  w.ja  va 2s.  c  o m
    if (info.isStatic()) {
        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType });
    } else {
        cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "this$0", 'L' + info.owner + ';', null, null);

        ctorDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { outerType, info.stateType });
    }

    // Generate constructor
    MethodVisitor mv = cv.visitMethod(0, CTOR_NAME, ctorDesc, null, null);
    mv.visitCode();
    Label start = new Label();
    Label end = new Label();
    mv.visitLabel(start);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, OBJECT_NAME, CTOR_NAME, DEFAULT_CTOR_DESC);

    // Save state field
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1 + info.thisOffset);
    mv.visitFieldInsn(Opcodes.PUTFIELD, name, "state", stateDesc);

    // Save outer this
    if (!info.isStatic()) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, name, "this$0", outerType.getDescriptor());
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitLabel(end);

    mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0);
    if (!info.isStatic()) {
        mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1);
    }
    mv.visitLocalVariable("state", stateDesc, null, start, end, 1 + info.thisOffset);

    mv.visitMaxs(2, 2 + info.thisOffset);
    mv.visitEnd();
}