Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

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

Introduction

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

Prototype

@Deprecated
public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private void implementDecode() throws OrmException {
    final Type retType = object;
    final MethodVisitor mv = cw.visitMethod(ACC_PROTECTED, "decode",
            Type.getMethodDescriptor(retType, new Type[] { codedInputStream }), null, new String[] {});
    mv.visitCode();/*from  www  . java  2s  .com*/
    final DecodeCGS cgs = new DecodeCGS(mv);

    cgs.setEntityType(pojoType);

    mv.visitTypeInsn(NEW, pojoType.getInternalName());
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, pojoType.getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    mv.visitVarInsn(ASTORE, cgs.objVar);

    final int tagVar = cgs.newLocal();
    decodeMessage(myFields, mv, cgs);

    cgs.pushEntity();
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void decodeMessage(final JavaColumnModel[] myFields, final MethodVisitor mv, final DecodeCGS cgs)
        throws OrmException {
    final Label nextField = new Label();
    final Label end = new Label();
    mv.visitLabel(nextField);//from   w w  w .jav  a2 s .c  om

    // while (!ci.isAtEnd) { ...
    cgs.call("readTag", Type.INT_TYPE);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, cgs.tagVar);

    cgs.push(3);
    mv.visitInsn(IUSHR);

    final Label badField = new Label();
    final int[] caseTags = new int[1 + myFields.length];
    final Label[] caseLabels = new Label[caseTags.length];

    caseTags[0] = 0;
    caseLabels[0] = new Label();

    int gaps = 0;
    for (int i = 1; i < caseTags.length; i++) {
        caseTags[i] = myFields[i - 1].getColumnID();
        caseLabels[i] = new Label();
        gaps += caseTags[i] - (caseTags[i - 1] + 1);
    }

    if (2 * gaps / 3 <= myFields.length) {
        final int min = 0;
        final int max = caseTags[caseTags.length - 1];
        final Label[] table = new Label[max + 1];
        Arrays.fill(table, badField);
        for (int idx = 0; idx < caseTags.length; idx++) {
            table[caseTags[idx]] = caseLabels[idx];
        }
        mv.visitTableSwitchInsn(min, max, badField, table);
    } else {
        mv.visitLookupSwitchInsn(badField, caseTags, caseLabels);
    }

    mv.visitLabel(caseLabels[0]);
    mv.visitJumpInsn(GOTO, end);

    for (int idx = 1; idx < caseTags.length; idx++) {
        final JavaColumnModel f = myFields[idx - 1];
        mv.visitLabel(caseLabels[idx]);
        if (f.isNested()) {
            final Label load = new Label();
            cgs.setFieldReference(f);
            cgs.pushFieldValue();
            mv.visitJumpInsn(IFNONNULL, load);
            cgs.fieldSetBegin();
            mv.visitTypeInsn(NEW, Type.getType(f.getNestedClass()).getInternalName());
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, Type.getType(f.getNestedClass()).getInternalName(), "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
            cgs.fieldSetEnd();

            // read the length, set a new limit, decode the message, validate
            // we stopped at the end of it as expected.
            //
            mv.visitLabel(load);
            final int limitVar = cgs.newLocal();
            cgs.pushCodedInputStream();
            cgs.call("readRawVarint32", Type.INT_TYPE);
            cgs.ncallInt("pushLimit", Type.INT_TYPE);
            mv.visitVarInsn(ISTORE, limitVar);

            decodeMessage(sort(f.getNestedColumns()), mv, cgs);

            cgs.pushCodedInputStream();
            mv.visitVarInsn(ILOAD, limitVar);
            cgs.ncallInt("popLimit", Type.VOID_TYPE);
            cgs.freeLocal(limitVar);

        } else {
            decodeScalar(mv, cgs, f);
        }
        mv.visitJumpInsn(GOTO, nextField);
    }

    // default:
    mv.visitLabel(badField);
    cgs.pushCodedInputStream();
    mv.visitVarInsn(ILOAD, cgs.tagVar);
    cgs.ncallInt("skipField", Type.BOOLEAN_TYPE);
    mv.visitInsn(POP);
    mv.visitJumpInsn(GOTO, nextField);

    mv.visitLabel(end);
    cgs.pushCodedInputStream();
    cgs.push(0);
    cgs.ncallInt("checkLastTagWas", Type.VOID_TYPE);
}

From source file:com.google.gwtorm.protobuf.CodecGen.java

License:Apache License

private static void decodeScalar(final MethodVisitor mv, final DecodeCGS cgs, final JavaColumnModel f)
        throws OrmException {
    cgs.setFieldReference(f);// w  w  w  .  j a v  a2s .  c o m
    cgs.fieldSetBegin();
    switch (Type.getType(f.getPrimitiveType()).getSort()) {
    case Type.BOOLEAN:
        cgs.call("readBool", Type.BOOLEAN_TYPE);
        break;

    case Type.CHAR:
        cgs.call("readUInt32", Type.INT_TYPE);
        break;

    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        cgs.call("readSInt32", Type.INT_TYPE);
        break;

    case Type.FLOAT:
        cgs.call("readFloat", Type.FLOAT_TYPE);
        break;

    case Type.DOUBLE:
        cgs.call("readDouble", Type.DOUBLE_TYPE);
        break;

    case Type.LONG:
        cgs.call("readSInt64", Type.LONG_TYPE);
        break;

    default:
        if (f.getPrimitiveType() == byte[].class) {
            cgs.call("readBytes", byteString);
            mv.visitMethodInsn(INVOKEVIRTUAL, byteString.getInternalName(), "toByteArray",
                    Type.getMethodDescriptor(Type.getType(byte[].class), new Type[] {}));

        } else if (f.getPrimitiveType() == String.class) {
            cgs.call("readString", string);

        } else if (f.getPrimitiveType() == java.sql.Timestamp.class
                || f.getPrimitiveType() == java.util.Date.class
                || f.getPrimitiveType() == java.sql.Date.class) {
            String tsType = Type.getType(f.getPrimitiveType()).getInternalName();
            mv.visitTypeInsn(NEW, tsType);
            mv.visitInsn(DUP);
            cgs.call("readFixed64", Type.LONG_TYPE);
            mv.visitMethodInsn(INVOKESPECIAL, tsType, "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));

        } else {
            throw new OrmException(
                    "Type " + f.getPrimitiveType() + " not supported for field " + f.getPathToFieldName());
        }
        break;
    }
    cgs.fieldSetEnd();
}

From source file:com.google.gwtorm.server.SchemaConstructorGen.java

License:Apache License

private void implementConstructor() {
    final Type ft = Type.getType(schemaArg.getClass());
    final String consName = "<init>";
    final String consDesc = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { ft });
    final MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, consName, consDesc, null, null);
    mv.visitCode();//from   w ww.ja v a2 s  .c o m

    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), consName,
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, implTypeName, CTX, ft.getDescriptor());

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

From source file:com.google.gwtorm.server.SchemaConstructorGen.java

License:Apache License

private void implementNewInstance() {
    final Type ft = Type.getType(schemaArg.getClass());
    final String typeName = Type.getType(schemaImpl).getInternalName();
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "open",
            Type.getMethodDescriptor(Type.getType(Schema.class), new Type[] {}), null, null);
    mv.visitCode();/*from ww  w . jav a 2  s  . c om*/

    Constructor<?> c = schemaImpl.getDeclaredConstructors()[0];
    Type argType = Type.getType(c.getParameterTypes()[0]);

    mv.visitTypeInsn(NEW, typeName);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implTypeName, CTX, ft.getDescriptor());
    mv.visitMethodInsn(INVOKESPECIAL, typeName, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { argType }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementConstructor() {
    final String consName = "<init>";
    final Type superType = Type.getType(schemaSuperClass);
    final Type dbType = Type.getType(databaseClass);

    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, consName,
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { dbType }), null, null);
    mv.visitCode();/*ww w .java2s.  c om*/

    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, superType.getInternalName(), consName, Type.getMethodDescriptor(
            Type.VOID_TYPE,
            new Type[] { Type.getType(schemaSuperClass.getDeclaredConstructors()[0].getParameterTypes()[0]) }));

    for (final RelationGen info : relations) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, info.accessType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, info.accessType.getInternalName(), consName,
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { superType }));
        mv.visitFieldInsn(PUTFIELD, implTypeName, info.getAccessInstanceFieldName(),
                info.accessType.getDescriptor());
    }

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

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementSequenceMethods() {
    for (final SequenceModel seq : schema.getSequences()) {
        final Type retType = Type.getType(seq.getResultType());
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, seq.getMethodName(),
                Type.getMethodDescriptor(retType, new Type[] {}), null,
                new String[] { Type.getType(OrmException.class).getInternalName() });
        mv.visitCode();/*from   www  .j  av a  2  s  .c  om*/

        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(seq.getSequenceName());
        mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(schemaSuperClass), "nextLong",
                Type.getMethodDescriptor(Type.getType(Long.TYPE), new Type[] { Type.getType(String.class) }));
        if (retType.getSize() == 1) {
            mv.visitInsn(L2I);
            mv.visitInsn(IRETURN);
        } else {
            mv.visitInsn(LRETURN);
        }
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
    }
}

From source file:com.google.gwtorm.server.SchemaGen.java

License:Apache License

private void implementAllRelationsMethod() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_FINAL, "allRelations",
            Type.getMethodDescriptor(Type.getType(Access[].class), new Type[] {}), null, null);
    mv.visitCode();/*from  w  w  w.j a  v  a 2s.  c  om*/

    final int r = 1;
    CodeGenSupport cgs = new CodeGenSupport(mv);
    cgs.push(relations.size());
    mv.visitTypeInsn(ANEWARRAY, Type.getType(Access.class).getInternalName());
    mv.visitVarInsn(ASTORE, r);

    int index = 0;
    for (RelationGen info : relations) {
        mv.visitVarInsn(ALOAD, r);
        cgs.push(index++);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, getImplTypeName(), info.model.getMethodName(), info.getDescriptor());

        mv.visitInsn(AASTORE);
    }

    mv.visitVarInsn(ALOAD, r);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}

From source file:com.googlecode.d2j.converter.IR2JConverter.java

License:Apache License

private static void reBuildEnExpression(EnExpr value, MethodVisitor asm) {
    if (value.vt == VT.FILLED_ARRAY) {
        FilledArrayExpr fae = (FilledArrayExpr) value;
        reBuildE1Expression(Exprs.nNewArray(fae.type, Exprs.nInt(fae.ops.length)), asm);
        String tp1 = fae.valueType;
        int xastore = IASTORE;
        String elementType = null;
        if (tp1.charAt(0) == '[') {
            elementType = tp1.substring(1);
            xastore = getOpcode(elementType, IASTORE);
        }/*from  ww  w  .  j  a  v a2 s.co m*/

        for (int i = 0; i < fae.ops.length; i++) {
            if (fae.ops[i] == null)
                continue;
            asm.visitInsn(DUP);
            asm.visitLdcInsn(i);
            accept(fae.ops[i], asm);
            String tp2 = fae.ops[i].valueType;
            if (elementType != null) {
                insertI2x(tp2, elementType, asm);
            }
            asm.visitInsn(xastore);
        }
        return;
    }

    switch (value.vt) {
    case NEW_MUTI_ARRAY:
        for (Value vb : value.ops) {
            accept(vb, asm);
        }
        NewMutiArrayExpr nmae = (NewMutiArrayExpr) value;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < nmae.dimension; i++) {
            sb.append('[');
        }
        sb.append(nmae.baseType);
        asm.visitMultiANewArrayInsn(sb.toString(), nmae.dimension);
        break;
    case INVOKE_NEW:
        asm.visitTypeInsn(NEW, toInternal(((InvokeExpr) value).owner));
        asm.visitInsn(DUP);
        // pass through
    case INVOKE_INTERFACE:
    case INVOKE_SPECIAL:
    case INVOKE_STATIC:
    case INVOKE_VIRTUAL:
        InvokeExpr ie = (InvokeExpr) value;
        int i = 0;
        if (value.vt != VT.INVOKE_STATIC && value.vt != VT.INVOKE_NEW) {
            i = 1;
            accept(value.ops[0], asm);
        }
        for (int j = 0; i < value.ops.length; i++, j++) {
            Value vb = value.ops[i];
            accept(vb, asm);
            insertI2x(vb.valueType, ie.args[j], asm);
        }

        int opcode;
        switch (value.vt) {
        case INVOKE_VIRTUAL:
            opcode = INVOKEVIRTUAL;
            break;
        case INVOKE_INTERFACE:
            opcode = INVOKEINTERFACE;
            break;
        case INVOKE_NEW:
        case INVOKE_SPECIAL:
            opcode = INVOKESPECIAL;
            break;
        case INVOKE_STATIC:
            opcode = INVOKESTATIC;
            break;
        default:
            opcode = -1;
        }

        asm.visitMethodInsn(opcode, toInternal(ie.owner), ie.name,
                buildMethodDesc(ie.vt == VT.INVOKE_NEW ? "V" : ie.ret, ie.args));
        break;
    }
}

From source file:com.googlecode.d2j.dex.BaseDexExceptionHandler.java

License:Apache License

@Override
public void handleMethodTranslateException(Method method, DexMethodNode methodNode, MethodVisitor mv,
        Exception e) {//  w  w w .j av a  2 s  .c  o  m
    // replace the generated code with
    // 'return new RuntimeException("D2jFail translate: xxxxxxxxxxxxx");'
    StringWriter s = new StringWriter();
    s.append("d2j fail translate: ");
    e.printStackTrace(new PrintWriter(s));
    String msg = s.toString();
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn(msg);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Runtime", "<init>", "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);
}