Example usage for org.objectweb.asm MethodVisitor visitTableSwitchInsn

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

Introduction

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

Prototype

public void visitTableSwitchInsn(final int min, final int max, final Label dflt, final Label... labels) 

Source Link

Document

Visits a TABLESWITCH instruction.

Usage

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

private static void insertInvoke(ClassWriter cw, String classNameInternal, List<Method> methods) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "invoke",
            "(Ljava/lang/Object;I[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();/*from  ww  w . java  2s.com*/

    int n = methods.size();

    if (n != 0) {
        mv.visitVarInsn(ILOAD, 2);
        Label[] labels = new Label[n];
        for (int i = 0; i < n; i++)
            labels[i] = new Label();
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        StringBuilder buffer = new StringBuilder(128);
        for (int i = 0; i < n; i++) {
            Method method = methods.get(i);
            boolean isInterface = method.getDeclaringClass().isInterface();
            boolean isStatic = isStatic(method.getModifiers());

            mv.visitLabel(labels[i]);
            if (i == 0)
                mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { classNameInternal }, 0, null);
            else
                mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

            if (!isStatic) {
                mv.visitVarInsn(ALOAD, 1);
                mv.visitTypeInsn(CHECKCAST, classNameInternal);
            }

            buffer.setLength(0);
            buffer.append('(');

            String methodName = method.getName();
            Class<?>[] paramTypes = method.getParameterTypes();
            Class<?> returnType = method.getReturnType();
            for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
                mv.visitVarInsn(ALOAD, 3);
                mv.visitIntInsn(BIPUSH, paramIndex);
                mv.visitInsn(AALOAD);
                Type paramType = Type.getType(paramTypes[paramIndex]);
                unbox(mv, paramType);
                buffer.append(paramType.getDescriptor());
            }

            buffer.append(')');
            buffer.append(Type.getDescriptor(returnType));
            final int inv = isInterface ? INVOKEINTERFACE : (isStatic ? INVOKESTATIC : INVOKEVIRTUAL);
            mv.visitMethodInsn(inv, classNameInternal, methodName, buffer.toString(), isInterface);

            final Type retType = Type.getType(returnType);
            box(mv, retType);
            mv.visitInsn(ARETURN);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    }
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitLdcInsn("Method not found: ");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;",
            false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V",
            false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private void insertSetObject(ClassWriter cw, String classNameInternal, List<Field> fields) {
    int maxStack = 6;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", null,
            null);/*from www. j a  va  2s  .  c  om*/
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 2);

    if (!fields.isEmpty()) {
        maxStack--;
        Label[] labels = new Label[fields.size()];
        for (int i = 0, n = labels.length; i < n; i++)
            labels[i] = new Label();
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields.get(i);
            Type fieldType = Type.getType(field.getType());
            boolean st = isStatic(field.getModifiers());

            mv.visitLabel(labels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            if (!st) {
                mv.visitVarInsn(ALOAD, 1);
                mv.visitTypeInsn(CHECKCAST, classNameInternal);
            }
            mv.visitVarInsn(ALOAD, 3);

            unbox(mv, fieldType);

            mv.visitFieldInsn(st ? PUTSTATIC : PUTFIELD, classNameInternal, field.getName(),
                    fieldType.getDescriptor());
            mv.visitInsn(RETURN);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv = insertThrowExceptionForFieldNotFound(mv);
    mv.visitMaxs(maxStack, 4);
    mv.visitEnd();
}

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private void insertGetObject(ClassWriter cw, String classNameInternal, List<Field> fields) {
    int maxStack = 6;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, null);
    mv.visitCode();//from  w w w . ja v a 2  s . c o  m
    mv.visitVarInsn(ILOAD, 2);

    if (!fields.isEmpty()) {
        maxStack--;
        Label[] labels = new Label[fields.size()];
        for (int i = 0, n = labels.length; i < n; i++)
            labels[i] = new Label();
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields.get(i);
            mv.visitLabel(labels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            if (isStatic(field.getModifiers())) {
                mv.visitFieldInsn(GETSTATIC, classNameInternal, field.getName(),
                        Type.getDescriptor(field.getType()));
            } else {
                mv.visitVarInsn(ALOAD, 1);
                mv.visitTypeInsn(CHECKCAST, classNameInternal);
                mv.visitFieldInsn(GETFIELD, classNameInternal, field.getName(),
                        Type.getDescriptor(field.getType()));
            }
            Type fieldType = Type.getType(field.getType());
            box(mv, fieldType);
            mv.visitInsn(ARETURN);
        }
        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    insertThrowExceptionForFieldNotFound(mv);
    mv.visitMaxs(maxStack, 3);
    mv.visitEnd();
}

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private void insertSetPrimitive(ClassWriter cw, String classNameInternal, List<Field> fields,
        Type primitiveType, String setterMethodName, int loadValueInstruction) {
    int maxStack = 6;
    int maxLocals = 5;
    final String typeNameInternal = primitiveType.getDescriptor();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, setterMethodName,
            "(Ljava/lang/Object;I" + typeNameInternal + ")V", null, null);
    mv.visitCode();//  w w w  .  j  av a 2 s.  co  m
    mv.visitVarInsn(ILOAD, 2);

    if (!fields.isEmpty()) {
        maxStack--;
        Label[] labels = new Label[fields.size()];
        Label labelForInvalidTypes = new Label();
        boolean hasAnyBadTypeLabel = false;
        for (int i = 0, n = labels.length; i < n; i++) {
            if (Type.getType(fields.get(i).getType()).equals(primitiveType))
                labels[i] = new Label();
            else {
                labels[i] = labelForInvalidTypes;
                hasAnyBadTypeLabel = true;
            }
        }
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            if (!labels[i].equals(labelForInvalidTypes)) {
                Field field = fields.get(i);
                mv.visitLabel(labels[i]);
                mv.visitFrame(F_SAME, 0, null, 0, null);
                if (isStatic(field.getModifiers())) {
                    mv.visitVarInsn(loadValueInstruction, 3);
                    mv.visitFieldInsn(PUTSTATIC, classNameInternal, field.getName(), typeNameInternal);
                } else {
                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitTypeInsn(CHECKCAST, classNameInternal);
                    mv.visitVarInsn(loadValueInstruction, 3);
                    mv.visitFieldInsn(PUTFIELD, classNameInternal, field.getName(), typeNameInternal);
                }
                mv.visitInsn(RETURN);
            }
        }
        // Rest of fields: different type
        if (hasAnyBadTypeLabel) {
            mv.visitLabel(labelForInvalidTypes);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
        }
        // Default: field not found
        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv = insertThrowExceptionForFieldNotFound(mv);
    mv.visitMaxs(maxStack, maxLocals);
    mv.visitEnd();
}

From source file:net.wpm.reflectasm.ClassAccess.java

License:Open Source License

static private void insertGetPrimitive(ClassWriter cw, String classNameInternal, List<Field> fields,
        Type primitiveType, String getterMethodName, int returnValueInstruction) {
    int maxStack = 6;
    final String typeNameInternal = primitiveType.getDescriptor();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterMethodName, "(Ljava/lang/Object;I)" + typeNameInternal,
            null, null);//from   w w w.jav  a 2  s .c o  m
    mv.visitCode();
    mv.visitVarInsn(ILOAD, 2);

    if (!fields.isEmpty()) {
        maxStack--;
        Label[] labels = new Label[fields.size()];
        Label labelForInvalidTypes = new Label();
        boolean hasAnyBadTypeLabel = false;
        for (int i = 0, n = labels.length; i < n; i++) {
            if (Type.getType(fields.get(i).getType()).equals(primitiveType))
                labels[i] = new Label();
            else {
                labels[i] = labelForInvalidTypes;
                hasAnyBadTypeLabel = true;
            }
        }
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields.get(i);
            if (!labels[i].equals(labelForInvalidTypes)) {
                mv.visitLabel(labels[i]);
                mv.visitFrame(F_SAME, 0, null, 0, null);
                if (isStatic(field.getModifiers())) {
                    mv.visitFieldInsn(GETSTATIC, classNameInternal, field.getName(), typeNameInternal);
                } else {
                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitTypeInsn(CHECKCAST, classNameInternal);
                    mv.visitFieldInsn(GETFIELD, classNameInternal, field.getName(), typeNameInternal);
                }
                mv.visitInsn(returnValueInstruction);
            }
        }
        // Rest of fields: different type
        if (hasAnyBadTypeLabel) {
            mv.visitLabel(labelForInvalidTypes);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
        }
        // Default: field not found
        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }
    mv = insertThrowExceptionForFieldNotFound(mv);
    mv.visitMaxs(maxStack, 3);
    mv.visitEnd();

}

From source file:org.codehaus.groovy.classgen.AsmClassGenerator.java

License:Apache License

@Override
public void visitClosureListExpression(final ClosureListExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushVariableScope(expression.getVariableScope());

    List<Expression> expressions = expression.getExpressions();
    final int size = expressions.size();
    // init declarations
    for (int i = 0; i < size; i += 1) {
        Expression expr = expressions.get(i);
        if (expr instanceof DeclarationExpression) {
            DeclarationExpression de = (DeclarationExpression) expr;
            BinaryExpression be = new BinaryExpression(de.getLeftExpression(), de.getOperation(),
                    de.getRightExpression());
            expressions.set(i, be);//from ww w. ja v a  2s  . c  o  m
            de.setRightExpression(ConstantExpression.NULL);
            visitDeclarationExpression(de);
        }
    }

    List<Object> instructions = new LinkedList<>();
    BytecodeSequence seq = new BytecodeSequence(instructions);
    BlockStatement bs = new BlockStatement();
    bs.addStatement(seq);
    Parameter closureIndex = new Parameter(ClassHelper.int_TYPE, "__closureIndex");
    ClosureExpression ce = new ClosureExpression(new Parameter[] { closureIndex }, bs);
    ce.setVariableScope(expression.getVariableScope());

    // to keep stack height put a null on stack
    instructions.add(ConstantExpression.NULL);

    // init table
    final Label dflt = new Label();
    final Label tableEnd = new Label();
    final Label[] labels = new Label[size];
    instructions.add(new BytecodeInstruction() {
        public void visit(MethodVisitor mv) {
            mv.visitVarInsn(ILOAD, 1);
            mv.visitTableSwitchInsn(0, size - 1, dflt, labels);
        }
    });

    // visit cases
    for (int i = 0; i < size; i += 1) {
        Label label = new Label();
        Expression expr = expressions.get(i);
        labels[i] = label;
        instructions.add(new BytecodeInstruction() {
            public void visit(MethodVisitor mv) {
                mv.visitLabel(label);
                // expressions will leave a value on stack, so need to pop the alibi null
                mv.visitInsn(POP);
            }
        });
        instructions.add(expr);
        instructions.add(new BytecodeInstruction() {
            public void visit(MethodVisitor mv) {
                mv.visitJumpInsn(GOTO, tableEnd);
            }
        });
    }

    // default case
    instructions.add(new BytecodeInstruction() {
        public void visit(MethodVisitor mv) {
            mv.visitLabel(dflt);
        }
    });
    ConstantExpression text = new ConstantExpression("invalid index for closure");
    ConstructorCallExpression cce = new ConstructorCallExpression(
            ClassHelper.make(IllegalArgumentException.class), text);
    ThrowStatement ts = new ThrowStatement(cce);
    instructions.add(ts);

    // return
    instructions.add(new BytecodeInstruction() {
        public void visit(MethodVisitor mv) {
            mv.visitLabel(tableEnd);
            mv.visitInsn(ARETURN);
        }
    });

    // load main Closure
    visitClosureExpression(ce);

    // we need later an array to store the curried
    // closures, so we create it here and ave it
    // in a temporary variable
    BytecodeHelper.pushConstant(mv, size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    int listArrayVar = controller.getCompileStack().defineTemporaryVariable("_listOfClosures", true);

    // add curried versions
    for (int i = 0; i < size; i += 1) {
        // stack: closure

        // we need to create a curried closure version
        // so we store the type on stack
        mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/CurriedClosure");
        // stack: closure, type
        // for a constructor call we need the type two times

        // and the closure after them
        mv.visitInsn(DUP2);
        mv.visitInsn(SWAP);
        // stack: closure,type,type,closure

        // so we can create the curried closure
        mv.visitInsn(ICONST_1);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_0);
        mv.visitLdcInsn(i);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        mv.visitInsn(AASTORE);
        mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/CurriedClosure", "<init>",
                "(Lgroovy/lang/Closure;[Ljava/lang/Object;)V", false);
        // stack: closure,curriedClosure

        // we need to save the result
        mv.visitVarInsn(ALOAD, listArrayVar);
        mv.visitInsn(SWAP);
        BytecodeHelper.pushConstant(mv, i);
        mv.visitInsn(SWAP);
        mv.visitInsn(AASTORE);
        // stack: closure
    }

    // we don't need the closure any longer, so remove it
    mv.visitInsn(POP);
    // we load the array and create a list from it
    mv.visitVarInsn(ALOAD, listArrayVar);
    createListMethod.call(mv);

    // remove the temporary variable to keep the
    // stack clean
    controller.getCompileStack().removeVar(listArrayVar);
    controller.getOperandStack().pop();
}

From source file:org.jadira.reflection.access.asm.AsmClassAccess.java

License:Apache License

private static void enhanceForGetValueObject(ClassVisitor cw, String accessClassNm, String clazzNm,
        Field[] fields) {/*from   www  .jav a2  s . co  m*/

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getValue",
            "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", null, null);

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch",
            "([Ljava/lang/Object;Ljava/lang/Object;)I");
    mv.visitVarInsn(ISTORE, 3);
    mv.visitVarInsn(ILOAD, 3);

    final int maxStack;

    if (fields.length > 0) {
        maxStack = 5;
        Label[] labels = constructLabels(fields);

        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields[i];
            mv.visitLabel(labels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, clazzNm);
            mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), Type.getDescriptor(field.getType()));

            Type fieldType = Type.getType(field.getType());
            switch (fieldType.getSort()) {
            case Type.BOOLEAN:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
                break;
            case Type.BYTE:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
                break;
            case Type.CHAR:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
                break;
            case Type.SHORT:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
                break;
            case Type.INT:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
                break;
            case Type.FLOAT:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
                break;
            case Type.LONG:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
                break;
            case Type.DOUBLE:
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
                break;
            }

            mv.visitInsn(ARETURN);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    } else {
        maxStack = 6;
    }
    enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;",
            ALOAD, 2);
    mv.visitMaxs(maxStack, 4);
    mv.visitEnd();
}

From source file:org.jadira.reflection.access.asm.AsmClassAccess.java

License:Apache License

private static void enhanceForPutValueObject(ClassVisitor cw, String accessClassNm, String clazzNm,
        Field[] fields) {// ww  w  .  j a  va  2 s.  c o m

    int maxStack = 6;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "putValue",
            "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", null, null);

    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch",
            "([Ljava/lang/Object;Ljava/lang/Object;)I");
    mv.visitVarInsn(ISTORE, 4);
    mv.visitVarInsn(ILOAD, 4);

    if (fields.length > 0) {
        maxStack = 5;
        Label[] labels = constructLabels(fields);

        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields[i];

            mv.visitLabel(labels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, clazzNm);

            mv.visitVarInsn(ALOAD, 3);

            Type fieldType = Type.getType(field.getType());
            switch (fieldType.getSort()) {
            case Type.BOOLEAN:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
                break;
            case Type.BYTE:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
                break;
            case Type.CHAR:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
                break;
            case Type.SHORT:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
                break;
            case Type.INT:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
                break;
            case Type.FLOAT:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
                break;
            case Type.LONG:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
                break;
            case Type.DOUBLE:
                mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
                break;
            case Type.ARRAY:
                mv.visitTypeInsn(CHECKCAST, fieldType.getDescriptor());
                break;
            case Type.OBJECT:
                mv.visitTypeInsn(CHECKCAST, fieldType.getInternalName());
                break;
            }

            mv.visitFieldInsn(PUTFIELD, clazzNm, field.getName(), fieldType.getDescriptor());
            mv.visitInsn(RETURN);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    } else {
        maxStack = 6;
    }
    enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;",
            ALOAD, 2);
    mv.visitMaxs(maxStack, 5);
    mv.visitEnd();
}

From source file:org.jadira.reflection.access.asm.AsmClassAccess.java

License:Apache License

private static void enhanceForPutValuePrimitive(ClassVisitor cw, String accessClassNm, String clazzNm,
        Field[] fields, Type type) {

    final String methodName;
    final String typeNm = type.getDescriptor();
    final int instruction;

    switch (type.getSort()) {
    case BOOLEAN:
        methodName = "putBooleanValue";
        instruction = ILOAD;//from   w  ww. j a v a  2 s. c  om
        break;
    case BYTE:
        methodName = "putByteValue";
        instruction = ILOAD;
        break;
    case CHAR:
        methodName = "putCharValue";
        instruction = ILOAD;
        break;
    case SHORT:
        methodName = "putShortValue";
        instruction = ILOAD;
        break;
    case INT:
        methodName = "putIntValue";
        instruction = ILOAD;
        break;
    case FLOAT:
        methodName = "putFloatValue";
        instruction = FLOAD;
        break;
    case LONG:
        methodName = "putLongValue";
        instruction = LLOAD;
        break;
    case DOUBLE:
        methodName = "putDoubleValue";
        instruction = DLOAD;
        break;
    default:
        methodName = "put" + type.getInternalName().lastIndexOf('/') + "Value";
        instruction = ALOAD;
        break;
    }

    int offset = (instruction == LLOAD || instruction == DLOAD) ? 1 : 0;

    int maxStack = 6;
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName,
            "(Ljava/lang/Object;Ljava/lang/String;" + typeNm + ")V", null, null);

    mv.visitCode();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch",
            "([Ljava/lang/Object;Ljava/lang/Object;)I");

    mv.visitVarInsn(ISTORE, 4 + offset);
    mv.visitVarInsn(ILOAD, 4 + offset);

    if (fields.length > 0) {
        maxStack = 6;

        Label[] labels = new Label[fields.length];
        Label labelForInvalidTypes = new Label();
        boolean hasAnyBadTypeLabel = false;

        for (int i = 0, n = labels.length; i < n; i++) {
            if (Type.getType(fields[i].getType()).equals(type))
                labels[i] = new Label();
            else {
                labels[i] = labelForInvalidTypes;
                hasAnyBadTypeLabel = true;
            }
        }
        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            if (!labels[i].equals(labelForInvalidTypes)) {
                Field field = fields[i];

                mv.visitLabel(labels[i]);
                mv.visitFrame(F_SAME, 0, null, 0, null);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitTypeInsn(CHECKCAST, clazzNm);

                mv.visitVarInsn(instruction, 3);
                mv.visitFieldInsn(PUTFIELD, clazzNm, field.getName(), typeNm);
                mv.visitInsn(RETURN);
            }
        }

        if (hasAnyBadTypeLabel) {
            mv.visitLabel(labelForInvalidTypes);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            enhanceForThrowingException(mv, IllegalArgumentException.class, type.getClassName(), typeNm,
                    instruction, 3);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    }

    final int maxLocals = 5 + offset;

    enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", typeNm, instruction,
            3);
    mv.visitMaxs(maxStack, maxLocals);
    mv.visitEnd();
}

From source file:org.jadira.reflection.access.asm.AsmClassAccess.java

License:Apache License

private static void enhanceForGetValuePrimitive(ClassVisitor cw, String accessClassNm, String clazzNm,
        Field[] fields, Type type) {

    String methodName;//  w w w  . j av  a  2s .c  o m
    final String typeNm = type.getDescriptor();
    final int instruction;

    switch (type.getSort()) {
    case Type.BOOLEAN:
        methodName = "getBooleanValue";
        instruction = IRETURN;
        break;
    case Type.BYTE:
        methodName = "getByteValue";
        instruction = IRETURN;
        break;
    case Type.CHAR:
        methodName = "getCharValue";
        instruction = IRETURN;
        break;
    case Type.SHORT:
        methodName = "getShortValue";
        instruction = IRETURN;
        break;
    case Type.INT:
        methodName = "getIntValue";
        instruction = IRETURN;
        break;
    case Type.FLOAT:
        methodName = "getFloatValue";
        instruction = FRETURN;
        break;
    case Type.LONG:
        methodName = "getLongValue";
        instruction = LRETURN;
        break;
    case Type.DOUBLE:
        methodName = "getDoubleValue";
        instruction = DRETURN;
        break;
    default:
        methodName = "getValue";
        instruction = ARETURN;
        break;
    }

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljava/lang/Object;Ljava/lang/String;)" + typeNm,
            null, null);

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch",
            "([Ljava/lang/Object;Ljava/lang/Object;)I");
    mv.visitVarInsn(ISTORE, 3);
    mv.visitVarInsn(ILOAD, 3);

    final int maxStack;

    if (fields.length > 0) {
        maxStack = 5;
        Label[] labels = constructLabels(fields);

        Label defaultLabel = new Label();
        mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

        for (int i = 0, n = labels.length; i < n; i++) {
            Field field = fields[i];
            mv.visitLabel(labels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, clazzNm);
            mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), typeNm);
            mv.visitInsn(instruction);
        }

        mv.visitLabel(defaultLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
    } else {
        maxStack = 6;
    }
    enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;",
            ALOAD, 2);
    mv.visitMaxs(maxStack, 4);
    mv.visitEnd();
}