Example usage for org.objectweb.asm Opcodes ARETURN

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

Introduction

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

Prototype

int ARETURN

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

Click Source Link

Usage

From source file:gemlite.core.internal.asm.AsmHelper.java

License:Apache License

public final static int getReturnCodeByType(String typeDesc) {
    switch (typeDesc) {
    case "I":
        return Opcodes.IRETURN;
    case "J":
        return Opcodes.LRETURN;
    case "D":
        return Opcodes.DRETURN;
    case "F":
        return Opcodes.FRETURN;
    }/*from  ww w .  ja v  a  2s.  c o m*/

    return Opcodes.ARETURN;
}

From source file:gemlite.core.internal.measurement.MeasureHelper.java

License:Apache License

public final static void instrumentCheckPoint(String className, MethodNode mn) {
    if (LogUtil.getCoreLog().isTraceEnabled())
        LogUtil.getCoreLog().trace("Found check point, class:" + className + " method:" + mn.name);

    InsnList insn = mn.instructions;//from   ww  w .  j a  v a  2 s.  c  om
    List<AbstractInsnNode> returnIndex = new ArrayList<>();
    // return
    int localVarCount = mn.localVariables.size();
    for (int i = 0; i < insn.size(); i++) {
        AbstractInsnNode insnNode = (AbstractInsnNode) insn.get(i);
        switch (insnNode.getOpcode()) {
        case Opcodes.ARETURN:
        case Opcodes.IRETURN:
        case Opcodes.DRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
            returnIndex.add(insnNode.getPrevious());
            break;
        case Opcodes.RETURN:
            returnIndex.add(insnNode);
            break;
        }
    }
    // 
    insn.insert(new VarInsnNode(Opcodes.LSTORE, localVarCount + 2));
    insn.insert(
            new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false));
    // ?
    for (AbstractInsnNode insnNode : returnIndex) {
        insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System",
                "currentTimeMillis", "()J", false));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LSTORE, localVarCount + 4));

        insn.insertBefore(insnNode, new LdcInsnNode(className));
        insn.insertBefore(insnNode, new LdcInsnNode(mn.name));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 2));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 4));
        insn.insertBefore(insnNode,
                new MethodInsnNode(Opcodes.INVOKESTATIC, "gemlite/core/internal/measurement/MeasureHelper",
                        "recordCheckPoint", "(Ljava/lang/String;Ljava/lang/String;JJ)V", false));
    }
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private static void generateClassForNamer(ClassVisitor cls) {
    MethodVisitor cv = cls.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC,
            forName,//from   www.j  a v  a 2 s .c o m
            Type.getMethodDescriptor(Type.getType(Class.class), new Type[] { Type.getType(String.class) }),
            null, null);

    Label start = new Label();
    cv.visitLabel(start);
    cv.visitVarInsn(Opcodes.ALOAD, 0);
    cv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Class.class), "forName",
            Type.getMethodDescriptor(Type.getType(Class.class), new Type[] { Type.getType(String.class) }));
    cv.visitInsn(Opcodes.ARETURN);

    Label handler = new Label();
    cv.visitLabel(handler);
    cv.visitVarInsn(Opcodes.ASTORE, 1);
    cv.visitTypeInsn(Opcodes.NEW, typeArg(NoClassDefFoundError.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitVarInsn(Opcodes.ALOAD, 1);
    cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ClassNotFoundException.class), "getMessage",
            Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoClassDefFoundError.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    cv.visitInsn(Opcodes.ATHROW);
    cv.visitTryCatchBlock(start, handler, handler, Type.getInternalName(ClassNotFoundException.class));
    cv.visitMaxs(-1, -1);
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateSkel() throws IOException {
    skelname = fullclassname + "_Skel";
    String skelclassname = classname + "_Skel";
    File file = new File(destination == null ? ""
            : destination + File.separator + skelname.replace('.', File.separatorChar) + ".class");
    if (verbose)//from   w ww.  jav a 2 s.  co  m
        System.out.println("[Generating class " + skelname + "]");

    final ClassWriter skel = new ClassWriter(true);
    classInternalName = skelname.replace('.', '/');
    skel.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName,
            Type.getInternalName(Object.class), null,
            new String[] { Type.getType(Skeleton.class).getInternalName() });

    skel.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash",
            Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz)));

    skel.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations",
            Type.getDescriptor(Operation[].class), null, null);

    MethodVisitor clinit = skel.visitMethod(Opcodes.ACC_STATIC, "<clinit>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);

    fillOperationArray(clinit);
    clinit.visitInsn(Opcodes.RETURN);

    clinit.visitMaxs(-1, -1);

    // no arg public constructor
    MethodVisitor init = skel.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    init.visitInsn(Opcodes.RETURN);
    init.visitMaxs(-1, -1);

    /*
     * public Operation[] getOperations()
     * returns a clone of the operations array
     */
    MethodVisitor getOp = skel.visitMethod(Opcodes.ACC_PUBLIC, "getOperations",
            Type.getMethodDescriptor(Type.getType(Operation[].class), new Type[] {}), null, null);
    getOp.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations",
            Type.getDescriptor(Operation[].class));
    getOp.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "clone",
            Type.getMethodDescriptor(Type.getType(Object.class), new Type[] {}));
    getOp.visitTypeInsn(Opcodes.CHECKCAST, typeArg(Operation[].class));
    getOp.visitInsn(Opcodes.ARETURN);
    getOp.visitMaxs(-1, -1);

    // public void dispatch(Remote, RemoteCall, int opnum, long hash)
    MethodVisitor dispatch = skel.visitMethod(Opcodes.ACC_PUBLIC, "dispatch",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Remote.class),
                    Type.getType(RemoteCall.class), Type.INT_TYPE, Type.LONG_TYPE }),
            null, new String[] { Type.getInternalName(Exception.class) });

    Variables var = new Variables();
    var.declare("this");
    var.declare("remoteobj");
    var.declare("remotecall");
    var.declare("opnum");
    var.declareWide("hash");

    /*
     * if opnum >= 0
     * XXX it is unclear why there is handling of negative opnums
     */
    dispatch.visitVarInsn(Opcodes.ILOAD, var.get("opnum"));
    Label nonNegativeOpnum = new Label();
    Label opnumSet = new Label();
    dispatch.visitJumpInsn(Opcodes.IFGE, nonNegativeOpnum);

    for (int i = 0; i < remotemethods.length; i++) {
        // assign opnum if hash matches supplied hash
        dispatch.visitVarInsn(Opcodes.LLOAD, var.get("hash"));
        dispatch.visitLdcInsn(new Long(remotemethods[i].hash));
        Label notIt = new Label();
        dispatch.visitInsn(Opcodes.LCMP);
        dispatch.visitJumpInsn(Opcodes.IFNE, notIt);

        // opnum = <opnum>
        dispatch.visitLdcInsn(new Integer(i));
        dispatch.visitVarInsn(Opcodes.ISTORE, var.get("opnum"));
        dispatch.visitJumpInsn(Opcodes.GOTO, opnumSet);
        dispatch.visitLabel(notIt);
    }

    // throw new SkeletonMismatchException
    Label mismatch = new Label();
    dispatch.visitJumpInsn(Opcodes.GOTO, mismatch);

    dispatch.visitLabel(nonNegativeOpnum);

    // if opnum is already set, check that the hash matches the interface
    dispatch.visitVarInsn(Opcodes.LLOAD, var.get("hash"));
    dispatch.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash",
            Type.LONG_TYPE.getDescriptor());
    dispatch.visitInsn(Opcodes.LCMP);
    dispatch.visitJumpInsn(Opcodes.IFEQ, opnumSet);

    dispatch.visitLabel(mismatch);
    dispatch.visitTypeInsn(Opcodes.NEW, typeArg(SkeletonMismatchException.class));
    dispatch.visitInsn(Opcodes.DUP);
    dispatch.visitLdcInsn("interface hash mismatch");
    dispatch.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(SkeletonMismatchException.class),
            "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    dispatch.visitInsn(Opcodes.ATHROW);

    // opnum has been set
    dispatch.visitLabel(opnumSet);

    dispatch.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj"));
    dispatch.visitTypeInsn(Opcodes.CHECKCAST, typeArg(clazz));
    dispatch.visitVarInsn(Opcodes.ASTORE, var.get("remoteobj"));

    Label deflt = new Label();
    Label[] methLabels = new Label[remotemethods.length];
    for (int i = 0; i < methLabels.length; i++)
        methLabels[i] = new Label();

    // switch on opnum
    dispatch.visitVarInsn(Opcodes.ILOAD, var.get("opnum"));
    dispatch.visitTableSwitchInsn(0, remotemethods.length - 1, deflt, methLabels);

    // Method dispatch
    for (int i = 0; i < remotemethods.length; i++) {
        dispatch.visitLabel(methLabels[i]);
        Method m = remotemethods[i].meth;
        generateMethodSkel(dispatch, m, var);
    }

    dispatch.visitLabel(deflt);
    dispatch.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
    dispatch.visitInsn(Opcodes.DUP);
    dispatch.visitLdcInsn("invalid method number");
    dispatch.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
    dispatch.visitInsn(Opcodes.ATHROW);

    dispatch.visitMaxs(-1, -1);

    skel.visitEnd();
    byte[] classData = skel.toByteArray();
    if (!noWrite) {
        if (file.exists())
            file.delete();
        if (file.getParentFile() != null)
            file.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(classData);
        fos.flush();
        fos.close();
    }
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private static int returnOpcode(Class cls) {
    int returncode;
    if (cls.equals(Boolean.TYPE))
        returncode = Opcodes.IRETURN;/*w w w .j  a  va2s  .com*/
    else if (cls.equals(Byte.TYPE))
        returncode = Opcodes.IRETURN;
    else if (cls.equals(Character.TYPE))
        returncode = Opcodes.IRETURN;
    else if (cls.equals(Short.TYPE))
        returncode = Opcodes.IRETURN;
    else if (cls.equals(Integer.TYPE))
        returncode = Opcodes.IRETURN;
    else if (cls.equals(Long.TYPE))
        returncode = Opcodes.LRETURN;
    else if (cls.equals(Float.TYPE))
        returncode = Opcodes.FRETURN;
    else if (cls.equals(Double.TYPE))
        returncode = Opcodes.DRETURN;
    else if (cls.equals(Void.TYPE))
        returncode = Opcodes.RETURN;
    else
        returncode = Opcodes.ARETURN;

    return returncode;
}

From source file:hellfirepvp.astralsorcery.core.patch.helper.PatchBlockModify.java

License:Open Source License

@Override
public void patch(ClassNode cn) {
    MethodNode mn = getMethod(cn, "setBlockState", "func_177436_a",
            "(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/block/state/IBlockState;");
    for (int i = 0; i < mn.instructions.size(); i++) {
        AbstractInsnNode aNode = mn.instructions.get(i);
        if (aNode.getOpcode() == Opcodes.ARETURN) {
            AbstractInsnNode prev = aNode.getPrevious();
            if (prev instanceof VarInsnNode && prev.getOpcode() == Opcodes.ALOAD
                    && ((VarInsnNode) prev).var == 8) {
                mn.instructions.insertBefore(prev,
                        new FieldInsnNode(Opcodes.GETSTATIC, "net/minecraftforge/common/MinecraftForge",
                                "EVENT_BUS", "Lnet/minecraftforge/fml/common/eventhandler/EventBus;"));
                mn.instructions.insertBefore(prev, new TypeInsnNode(Opcodes.NEW,
                        "hellfirepvp/astralsorcery/common/event/BlockModifyEvent"));
                mn.instructions.insertBefore(prev, new InsnNode(Opcodes.DUP));
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 0)); //Chunk
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 1)); //Pos
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 8)); //OldState
                mn.instructions.insertBefore(prev, new VarInsnNode(Opcodes.ALOAD, 2)); //NewState
                mn.instructions.insertBefore(prev, new MethodInsnNode(Opcodes.INVOKESPECIAL,
                        "hellfirepvp/astralsorcery/common/event/BlockModifyEvent", "<init>",
                        "(Lnet/minecraft/world/chunk/Chunk;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/block/state/IBlockState;)V",
                        false));/*w  w w  . j  a  v  a 2s  . co m*/
                mn.instructions.insertBefore(prev,
                        new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                                "net/minecraftforge/fml/common/eventhandler/EventBus", "post",
                                "(Lnet/minecraftforge/fml/common/eventhandler/Event;)Z", false));
                mn.instructions.insertBefore(prev, new InsnNode(Opcodes.POP));
                return;
            }
        }
    }
    throw new ASMTransformationException(
            "Could not find the expected return statement in the setBlockState method!");
}

From source file:instrumentation.InstrumentationMethodAdapter.java

License:Open Source License

public void visitInsn(int opcode) {

    switch (opcode) {
    case Opcodes.MONITORENTER:
        insertCode(opcode, "MonitorEnterBefore");
        mv.visitInsn(Opcodes.DUP);/*  w ww .  ja va 2s. c  om*/
        mv.visitInsn(opcode);
        insertCode(opcode, "MonitorEnterAfter");
        mv.visitInsn(Opcodes.POP);
        break;
    case Opcodes.MONITOREXIT:
        insertCode(opcode, "MonitorExitBefore");
        mv.visitInsn(opcode);
        break;
    case Opcodes.ATHROW:
    case Opcodes.RETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
        addMonitorExitBeforeReturn();
        mv.visitInsn(opcode);
        break;
    default:
        mv.visitInsn(opcode);
    }

}

From source file:io.awacs.plugin.springmvc.ServiceWrapper.java

License:Apache License

/**
 * origin method: (XXX)X/* www.j a v a 2s  . com*/
 * <p>
 * proxy method: (XXXLjavax/servlet/http/HttpServletRequest;)X
 * req -> localIndex - 1
 *
 * @param cn
 * @param origin
 * @return
 */
private MethodNode doProxy(ClassNode cn, MethodNode origin) {

    MethodNode newNode = copyMethod(origin);
    //move annotations
    hideAnnotations(origin);

    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();

    String returnType = origin.desc.substring(origin.desc.indexOf(')') + 1);

    int localIndex = copyParameters(origin, newNode, l0, l2);

    newNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Exception"));

    //localIndex -> elapsedTime
    newNode.localVariables.add(new LocalVariableNode("elapsedTime", "J", null, l0, l1, localIndex));

    //
    newNode.instructions.add(l0);

    // long elapsedTime = System.currentTimeMillis();
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    int paramCount = getParamCount(origin.desc, origin.access);

    //load?
    for (int i = 0; i < paramCount; i++) {
        LocalVariableNode node = (LocalVariableNode) newNode.localVariables.get(i);
        switch (node.desc) {
        case "J":
            newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, node.index));
            break;
        case "D":
            newNode.instructions.add(new VarInsnNode(Opcodes.DLOAD, node.index));
            break;
        case "F":
            newNode.instructions.add(new VarInsnNode(Opcodes.FLOAD, node.index));
            break;
        case "I":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "S":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "Z":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "B":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "C":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        default:
            newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, node.index));
            break;
        }
    }
    //
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, cn.name, origin.name, origin.desc));

    //
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.LSUB));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    //?
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    //SpringmvcPlugin.incrAccess(uri, elapsedTime);
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/springmvc/SpringmvcPlugin", "incrAccess", "(Ljava/lang/String;J)V"));
    //
    newNode.instructions.add(l1);
    switch (returnType) {
    case "J":
        newNode.instructions.add(new InsnNode(Opcodes.LRETURN));
        break;
    case "D":
        newNode.instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case "F":
        newNode.instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    case "I":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "S":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "C":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "B":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "Z":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    default:
        newNode.instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    }

    newNode.instructions.add(l2);
    newNode.instructions
            .add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }));
    newNode.instructions.add(new VarInsnNode(Opcodes.ASTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/springmvc/SpringmvcPlugin",
                    "incrFailure", "(Ljava/lang/String;Ljava/lang/Throwable;)V"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.ATHROW));
    newNode.maxLocals = origin.maxLocals + 3;
    newNode.maxStack = Math.max(localIndex + 2, 6);
    return newNode;
}

From source file:io.awacs.plugin.stacktrace.ClassTransformer.java

License:Apache License

/**
 * ?// ww w.  j  ava  2s. c  o  m
 * 1?try catch?                 try{
 * 2????        io.awacs.plugin.stacktrace.StackFrames.init();
 * 3????                io.awacs.plugin.stacktrace.StackFrames.push(className,methodName,0);
 * 4?                          Object val = methodName_origin_className(args);
 * 5?????                io.awacs.plugin.stacktrace.StackFrames.push(className,methodName,1);
 * 5???          List list = io.awacs.plugin.stacktrace.StackFrames.dump();
 * 7?????          io.awacs.plugin.stacktrace.StackTracePlugin.incrAccess(list);
 * 8?                          return val;
 * }catch(java.lang.Exception e){
 * 9??                io.awacs.plugin.stacktrace.StackTracePlugin.incrFailure(e);
 * 10?               throw e;
 * }
 */
private void transformTerminatedMethod(MethodNode origin, MethodNode proxy, ClassNode owner) {
    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();
    //try catch?
    proxy.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Exception"));
    proxy.instructions.add(l0);
    //?
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "init", "()V", false));
    proxy.instructions.add(new LdcInsnNode(owner.name.replaceAll("/", ".")));
    proxy.instructions.add(new LdcInsnNode(proxy.name));
    proxy.instructions.add(new LdcInsnNode(0));
    //
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "push", "(Ljava/lang/String;Ljava/lang/String;I)V", false));
    int varIndex = 0;//???
    //???,????this?
    if ((proxy.access & Opcodes.ACC_STATIC) != Opcodes.ACC_STATIC) {
        proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }
    List<String> parameters = resolveParameters(proxy.desc);
    //???
    for (String param : parameters) {
        VarInsnNode insnNode;
        switch (param) {
        case "J":
            insnNode = new VarInsnNode(Opcodes.LLOAD, varIndex);
            varIndex += 2;
            break;
        case "D":
            insnNode = new VarInsnNode(Opcodes.DLOAD, varIndex);
            varIndex += 2;
            break;
        case "F":
            insnNode = new VarInsnNode(Opcodes.FLOAD, varIndex++);
            break;
        case "I":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "S":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "Z":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "B":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        case "C":
            insnNode = new VarInsnNode(Opcodes.ILOAD, varIndex++);
            break;
        default:
            insnNode = new VarInsnNode(Opcodes.ALOAD, varIndex++);
            break;
        }
        proxy.instructions.add(insnNode);
    }
    //
    if ((origin.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
        proxy.instructions
                .add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner.name, origin.name, origin.desc, false));
    else
        proxy.instructions
                .add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, owner.name, origin.name, origin.desc, false));
    proxy.instructions.add(new LdcInsnNode(owner.name.replaceAll("/", ".")));
    proxy.instructions.add(new LdcInsnNode(proxy.name));
    proxy.instructions.add(new LdcInsnNode(1));
    //?
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "push", "(Ljava/lang/String;Ljava/lang/String;I)V", false));
    //??
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/stacktrace/StackFrames",
            "dump", "()Ljava/util/List;", false));
    //???
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/stacktrace/StackTracePlugin", "incrAccess", "(Ljava/util/List;)V", false));
    proxy.instructions.add(l1);
    //
    String returnType = origin.desc.substring(origin.desc.indexOf(')') + 1);
    switch (returnType) {
    case "J":
        proxy.instructions.add(new InsnNode(Opcodes.LRETURN));
        break;
    case "D":
        proxy.instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case "F":
        proxy.instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    case "I":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "S":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "C":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "B":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "Z":
        proxy.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "V":
        proxy.instructions.add(new InsnNode(Opcodes.RETURN));
        break;
    default:
        proxy.instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    }
    proxy.instructions.add(l2);
    //?
    proxy.instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }));
    proxy.instructions.add(new VarInsnNode(Opcodes.ASTORE, varIndex));
    proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, varIndex));
    proxy.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/stacktrace/StackTracePlugin", "incrFailure", "(Ljava/lang/Throwable;)V", false));
    proxy.instructions.add(new VarInsnNode(Opcodes.ALOAD, varIndex));
    proxy.instructions.add(new InsnNode(Opcodes.ATHROW));
    proxy.maxLocals = varIndex + 1;
    proxy.maxStack = Math.max(varIndex, 5);
}

From source file:io.syncframework.optimizer.OControllerClassVisitor.java

License:Apache License

/**
 * Generates _asActions() method as following:
 * /*from  ww w  . j  a v a2s  .c  o m*/
 * if(name.equals("upload"))
 *    return upload();
 * else if(name.equals("save"))
 *    return save();
 * else if(name.equals("main"))
 *    return main();
 * else if(name.equals("redir"))
 *    return redir();
 * else
 *    throw new RuntimeException("no action named "+name);
 */
private void createActionMethod() {
    StringBuilder sb = new StringBuilder();
    sb.append("(").append(Type.getType(String.class)).append(")").append(Type.getType(Result.class));
    String desc = sb.toString();

    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asAction", desc, null, null);

    Label start = new Label();
    Label next = new Label();
    Label variable = new Label();
    boolean first = true;

    for (String name : reflector.getActions().keySet()) {
        Label l0 = null;
        Label l1 = new Label();

        if (first) {
            l0 = new Label();
            first = false;
        } else {
            l0 = next;
            next = new Label();
        }

        mv.visitLabel(l0);
        if (!first) {
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(name);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        mv.visitJumpInsn(Opcodes.IFEQ, next);

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), name,
                "()" + Type.getType(Result.class), false);
        mv.visitInsn(Opcodes.ARETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/NoSuchMethodException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("no @Action named ");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;",
            false);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/NoSuchMethodException", "<init>",
            "(Ljava/lang/String;)V", false);
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, next, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, next, 1);

    mv.visitMaxs(5, 2);
    mv.visitEnd();
}