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:instrumentation.InstrumentationMethodAdapter.java

License:Open Source License

private void loadSyncObject() {
    if (isStatic) {
        mv.visitLdcInsn(Type.getObjectType(ClassTransformer.className()));
    } else {//from w  w  w  . j  av  a2 s.c o m
        mv.visitVarInsn(Opcodes.ALOAD, 0);
    }
}

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

License:Apache License

/**
 * origin method: (XXX)X/*from  w  w  w .  j a  v a2 s .c  o m*/
 * <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

/**
 * ?/*from   w  w w. j  a va2s . c om*/
 * 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 ava  2  s .c om
 * 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();
}

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

License:Apache License

/**
 * Generates code:/*from   w w w .j a v a 2  s .  c o m*/
 * 
 * public Class<?>[] _asActionInterceptors(String name) {
 *    return _asInterceptors.get(name);
 * }
 */
public void createActionInterceptorsMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asActionInterceptors",
            "(Ljava/lang/String;)[Ljava/lang/Class;", null, null);

    Label l0 = new Label();
    Label l1 = new Label();
    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asInterceptors",
            "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;");
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(l1);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitMaxs(2, 2);

    mv.visitEnd();
}

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

License:Apache License

/**
 * Generates this code://w ww  .  ja va 2  s . co m
 * 
 * public boolean _asActionIsDefined(String name) {
 *    if(_asActions.containsKey(name))
 *       return true;
 *    return false;
 * }
 */
public void createActionIsDefinedMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asActionIsDefined", "(Ljava/lang/String;)Z", null,
            null);
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();

    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "containsKey", "(Ljava/lang/Object;)Z", true);
    mv.visitJumpInsn(Opcodes.IFEQ, l1);

    mv.visitLabel(l2);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);

    mv.visitLabel(l3);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l3, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l3, 1);
    mv.visitMaxs(2, 2);

    mv.visitEnd();
}

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

License:Apache License

/**
 * Generates context method setter: //from w  ww . jav a  2  s  .  c  o  m
 * 
 * public void _as(Application|Error|Message|...)Context((Application|Error|Message|...)Context variable) {
 *    this.variable = variable
 *       or
 *    // do nothing
 * }
 */
private void createContextMethod(String methodName, String typeDescriptor, String variableName) {
    if (variableName != null) {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + typeDescriptor + ")V", null,
                null);
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, reflector.getClazzInternalName(), variableName, typeDescriptor);
        mv.visitLabel(l1);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l2, 0);
        mv.visitLocalVariable(variableName, typeDescriptor, null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    } else {
        MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + typeDescriptor + ")V", null,
                null);
        Label l0 = new Label();
        Label l1 = new Label();
        mv.visitLabel(l0);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
        mv.visitLocalVariable("argument", typeDescriptor, null, l0, l1, 1);
    }
}

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

License:Apache License

/**
 * Generates the _asParameter() getter as
 * /*from www  .j  av  a2 s.  c  o m*/
 * public Object _asParameter(String name) {
 *    if(name.equals("name"))
 *       return getName();
 *    if(name.equals("date"))
 *       return getDate();
 *    ...
 *    return null;
 * }
 */
private void createParametersGetterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameter",
            "(Ljava/lang/String;)Ljava/lang/Object;", null, null);

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

    for (String name : reflector.getParameters().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);

        String methodGetterName = reflector.getGetters().get(name).getName();
        String methodGetterDesc = "()" + Type.getDescriptor(reflector.getParameters().get(name));

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), methodGetterName,
                methodGetterDesc, false);
        mv.visitInsn(Opcodes.ARETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, variable, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, variable, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

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

License:Apache License

/**
 * Creates the code as:/*from w  w  w .  j  a v a 2s .c om*/
 * 
 * public void _asParameter(String name, Object value) {
 *    if(name.equals("name") {
 *       setName((String)value);
 *       return;
 *    }
 *   if(name.equals("date") {
 *      setDate((Date)value);
 *      return;
 *   }
 *   ...
 *    return;
 * } 
 */
private void createParametersSetterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameter",
            "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);

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

    for (String name : reflector.getParameters().keySet()) {
        Label l0 = null;
        Label l1 = new Label();
        Label l2 = 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);

        Class<?> parameterType = reflector.getParameters().get(name);

        String setterMethodName = reflector.getSetters().get(name).getName();
        String setterMethodDesc = "(" + Type.getDescriptor(parameterType) + ")V";

        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(parameterType));
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, reflector.getClazzInternalName(), setterMethodName,
                setterMethodDesc, false);

        mv.visitLabel(l2);
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitLabel(next);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(variable);
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, start, next, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, start, next, 1);
    mv.visitLocalVariable("value", "Ljava/lang/Object;", null, start, next, 2);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

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

License:Apache License

/**
 * public Class<?> _asParameterConverter(String name) {
 *    return _asConverters.get(name);/*from w  w w . j av  a2  s  .  c o m*/
 * }
 */
public void createParameterConverterMethod() {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asParameterConverter",
            "(Ljava/lang/String;)Ljava/lang/Class;", null, null);
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Class");
    mv.visitInsn(Opcodes.ARETURN);

    Label l1 = new Label();
    mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l1, 0);
    mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l1, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}