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

public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.asakusafw.dag.compiler.jdbc.ResultSetAdapterGenerator.java

License:Apache License

private static void doGetValue(MethodVisitor method, PropertyReference property,
        Optional<FieldRef> calendarBuf) {
    PropertyTypeKind kind = PropertyTypeKind.fromOptionType(property.getType());
    switch (kind) {
    case BOOLEAN:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getBoolean", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;//from ww w  .  j  ava2  s  .  co m
    case BYTE:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getByte", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case SHORT:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getShort", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case INT:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getInt", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case LONG:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getLong", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case FLOAT:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getFloat", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case DOUBLE:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getDouble", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case DECIMAL:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(),
                "getBigDecimal", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getRawType()), typeOf(int.class)), true);
        break;
    case STRING:
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getString", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(String.class), typeOf(int.class)), true);
        break;
    case DATE:
        calendarBuf.get().load(method);
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "getDate", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(java.sql.Date.class), typeOf(int.class),
                        typeOf(java.util.Calendar.class)),
                true);
        break;
    case DATE_TIME:
        calendarBuf.get().load(method);
        method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(),
                "getTimestamp", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(java.sql.Timestamp.class), typeOf(int.class),
                        typeOf(java.util.Calendar.class)),
                true);
        break;
    default:
        throw new AssertionError(property);
    }
}

From source file:com.asakusafw.dag.compiler.jdbc.ResultSetAdapterGenerator.java

License:Apache License

private static void doSetNull(MethodVisitor method) {
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(ValueOption.class).getInternalName(), "setNull", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(ValueOption.class)), false);
    method.visitInsn(Opcodes.POP);//  ww  w  .  ja  v a2s  . c  o  m
}

From source file:com.asakusafw.dag.compiler.jdbc.ResultSetAdapterGenerator.java

License:Apache License

private static void doSetValue(MethodVisitor method, PropertyReference property) {
    PropertyTypeKind kind = PropertyTypeKind.fromOptionType(property.getType());
    switch (kind) {
    case BOOLEAN:
    case BYTE://from  w ww  . ja  v  a  2 s. c  o  m
    case SHORT:
    case INT:
    case LONG:
    case FLOAT:
    case DOUBLE:
    case DECIMAL:
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(kind.getRawType())), false);
        method.visitInsn(Opcodes.POP);
        break;
    case STRING:
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(String.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    case DATE:
        method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(DateUtil.class).getInternalName(), "getDayFromDate", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(int.class), typeOf(java.util.Date.class)), false);
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(int.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    case DATE_TIME:
        method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(DateUtil.class).getInternalName(),
                "getSecondFromDate", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(long.class), typeOf(java.util.Date.class)), false);
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(long.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    default:
        throw new AssertionError(property);
    }
}

From source file:com.asakusafw.dag.compiler.jdbc.windgate.WindGateJdbcOutputProcessorGenerator.java

License:Apache License

private static void truncate(ClassGeneratorContext context, MethodVisitor v, Spec spec) {
    if (spec.initialize == false) {
        return;//from w w w. java 2s  . c o m
    }
    getConst(v, spec.id);
    getConst(v, spec.model.getProfileName());

    getConst(v, spec.model.getProfileName());
    getConst(v, spec.model.getTableName());
    getList(v, Lang.project(spec.model.getColumnMappings(), Tuple::left));
    v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(WindGateJdbcDirect.class).getInternalName(), "truncate",
            Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class), // profileName
                    typeOf(String.class), // tableName
                    typeOf(List.class)), // columnNames
            false);
    Lang.forEach(Optionals.of(spec.model.getCustomTruncate()), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "withCustomTruncate", //$NON-NLS-1$
                Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class)), false);
    });
    Lang.forEach(spec.model.getOptions(), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "withOption", //$NON-NLS-1$
                Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class)), false);
    });
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "build", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(Function.class)), false);
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(JdbcOutputProcessor.class).getInternalName(), "initialize", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(JdbcOutputProcessor.class), typeOf(String.class),
                    typeOf(String.class), typeOf(Function.class)),
            false);
}

From source file:com.asakusafw.dag.compiler.jdbc.windgate.WindGateJdbcOutputProcessorGenerator.java

License:Apache License

private static void output(ClassGeneratorContext context, MethodVisitor v, Spec spec) {
    if (spec.output == false) {
        return;//from  w w  w.j  a v a2 s. com
    }
    getConst(v, spec.id);
    getConst(v, spec.model.getProfileName());

    getConst(v, spec.model.getProfileName());
    getConst(v, spec.model.getTableName());
    getList(v, Lang.project(spec.model.getColumnMappings(), Tuple::left));
    getNew(v, getAdapter(context, spec));
    v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(WindGateJdbcDirect.class).getInternalName(), "output",
            Type.getMethodDescriptor(OUTPUT_BUILDER, typeOf(String.class), // profileName
                    typeOf(String.class), // tableName
                    typeOf(List.class), // columnNames
                    typeOf(Supplier.class)), // adapter
            false);
    Lang.forEach(spec.model.getOptions(), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, OUTPUT_BUILDER.getInternalName(), "withOption", //$NON-NLS-1$
                Type.getMethodDescriptor(OUTPUT_BUILDER, typeOf(String.class)), false);
    });
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, OUTPUT_BUILDER.getInternalName(), "build", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(Function.class)), false);
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(JdbcOutputProcessor.class).getInternalName(), "output", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(JdbcOutputProcessor.class), typeOf(String.class),
                    typeOf(String.class), typeOf(Function.class)),
            false);
}

From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java

License:Open Source License

/**
 * Generates the new method body, copy the old to a new method and connect them.
 * /*from  w w w .j a  v a  2s. c o  m*/
 * the structure of the new method is:<br>
 * <br><b><div style="background:lightgray">
 * <pre>
 * public [CollectionType] [getterName]() {
 *    if ([fieldName] == null){
 *       try {
 *          if (null == db || db.isClosed())
 *             throw new RuntimeException("Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class.");
 *          Method method = db.getClass().getDeclaredMethod("getRelationFromDb", String.class, Object.class, Class.class);
 *          method.setAccessible(true);
 *          children = (Collection<TestTable>)method.invoke(db, [fieldName], this, TestTable.class);
 *          method.setAccessible(false);
 *       }
 *       catch (Exception e) {
 *          if (e instanceof RuntimeException)
 *             throw (RuntimeException)e;
 *          throw new RuntimeException(e.getMessage(), e);
 *       }
 *    }
 * return $orig_[getterName]();
 * }
 * </pre>
 * </div>
 * 
 * @param access
 * @param desc
 * @param signature
 * @param exceptions
 * @param name
 * @param newName
 * @param fieldName
 */
private void generateNewMethodBody(int access, String desc, String signature, String[] exceptions, String name,
        String newName, String fieldName) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    String fieldSignature = signature.substring(signature.indexOf(')') + 1, signature.lastIndexOf('<')) + ";";
    String type = signature.substring(signature.indexOf('<') + 1, signature.indexOf('>'));
    String cast = desc.substring(desc.indexOf("java/"), desc.indexOf(';'));

    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    Label l3 = new Label();
    mv.visitJumpInsn(IFNONNULL, l3);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    Label l4 = new Label();
    mv.visitJumpInsn(IFNULL, l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false);
    Label l5 = new Label();
    mv.visitJumpInsn(IFEQ, l5);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class.");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitLdcInsn("getRelationFromDb");
    mv.visitInsn(ICONST_3);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitLdcInsn(Type.getType("Ljava/lang/Object;"));
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    mv.visitLdcInsn(Type.getType("Ljava/lang/Class;"));
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitInsn(ICONST_3);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitLdcInsn(fieldName);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_2);
    mv.visitLdcInsn(Type.getType(type));
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, cast);
    mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException");
    Label l6 = new Label();
    mv.visitJumpInsn(IFEQ, l6);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc, false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(7, 2);
    mv.visitEnd();
}

From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java

License:Open Source License

/**
 * Generates the new method body, copy the old to a new method and connect them.
 * the structure of the new method is:<br>
 * <br><b><div style="background:lightgray">
 * <pre>//from   ww  w .j a  va 2 s .  c  om
 * public [entityType] [getterName]() {
 *   if ([field] == null) {
 *      try {
 *         if (null == db)
 *            throw new RuntimeException("Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class.");
 *         
 *         [parentType] parent = this.getClass().newInstance();
 *         [entityType] desc = TestB.class.newInstance();
 *         
 *         // get the primary key
 *         Object pk = db.getPrimaryKey(this);
 *         
 *         // get the object
 *         [entityValue] = db.from(desc).innerJoin(parent).on(parent.[entityValue]).is(desc).where(db.getPrimaryKey(parent)).is(pk).selectFirst();
 *      }
 *      catch (Exception e) {
 *         if (e instanceof RuntimeException)
 *            throw (RuntimeException)e;
 *         throw new RuntimeException(e.getMessage(), e);
 *      }
 *   }
 *   return $orig_[getterName]();
 *  }
 * </pre>
 * </div>
 * 
 * @param access
 * @param desc
 * @param exceptions
 * @param name - current method name
 * @param newName - new method name (the orig...)
 * @param fieldName
 */
public void generateLazyRelation(int access, String desc, String[] exceptions, String name, String newName,
        String fieldName) {
    MethodVisitor mv = cv.visitMethod(access, name, desc, null, exceptions);
    String fieldSignature = desc.substring(desc.indexOf(')') + 1);
    String fieldClassName = desc.substring(desc.indexOf(')') + 2, desc.length() - 1);

    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    Label l3 = new Label();
    mv.visitJumpInsn(IFNULL, l3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    mv.visitFieldInsn(GETFIELD, fieldClassName, "isLazy", "Z");
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    Label l4 = new Label();
    mv.visitJumpInsn(IFNULL, l4);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false);
    Label l5 = new Label();
    mv.visitJumpInsn(IFEQ, l5);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class.");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, className);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitLdcInsn(Type.getType(fieldSignature));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, fieldClassName);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey",
            "(Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitVarInsn(ASTORE, 3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "from",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryInterface;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, "com/centimia/orm/jaqu/QueryInterface", "innerJoin",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoin;", true);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoin", "on",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinCondition;", false);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinCondition", "is",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinWhere;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey",
            "(Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinWhere", "where",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryCondition;", false);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryCondition", "is",
            "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryWhere;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryWhere", "selectFirst", "()Ljava/lang/Object;",
            false);
    mv.visitTypeInsn(CHECKCAST, fieldClassName);
    mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException");
    Label l6 = new Label();
    mv.visitJumpInsn(IFEQ, l6);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, className, newName, desc, false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 4);
    mv.visitEnd();
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateConstructorAndFields(Schema schema, ClassVisitor cv, String genClassInternalName,
        boolean javaClassCodec) {
    MethodVisitor ctormv;
    ctormv = cv.visitMethod(ACC_PUBLIC, "<init>", "(L" + blinkCodecIName + ";Lcom/cinnober/msgcodec/Schema;)V",
            null, null);// w ww .  j a v  a2 s.co m
    int nextCtorVar = 3;
    ctormv.visitCode();
    ctormv.visitVarInsn(ALOAD, 0);
    ctormv.visitVarInsn(ALOAD, 1);
    ctormv.visitMethodInsn(INVOKESPECIAL, baseclassIName, "<init>", "(L" + blinkCodecIName + ";)V", false);

    // schema field
    FieldVisitor fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "schema", "Lcom/cinnober/msgcodec/Schema;", null,
            null);
    fv.visitEnd();
    ctormv.visitVarInsn(ALOAD, 0); // this
    ctormv.visitVarInsn(ALOAD, 2); // schema
    ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "schema", "Lcom/cinnober/msgcodec/Schema;");

    if (!javaClassCodec) {
        // store the group type accessor

        // field
        fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "groupTypeAccessor",
                "Lcom/cinnober/msgcodec/GroupTypeAccessor;", null, null);
        fv.visitEnd();

        // ctor, init field
        ctormv.visitVarInsn(ALOAD, 0); // this
        ctormv.visitVarInsn(ALOAD, 2); // schema
        ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getBinding",
                "()Lcom/cinnober/msgcodec/SchemaBinding;", false);
        ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/SchemaBinding", "getGroupTypeAccessor",
                "()Lcom/cinnober/msgcodec/GroupTypeAccessor;", false);
        ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "groupTypeAccessor",
                "Lcom/cinnober/msgcodec/GroupTypeAccessor;");
    }

    for (GroupDef group : schema.getGroups()) {
        if (!javaClassCodec) {
            // store the group type
            // field
            fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "groupType_" + group.getName(), "Ljava/lang/Object;",
                    null, null);
            fv.visitEnd();

            // ctor, init field
            ctormv.visitVarInsn(ALOAD, 0); // this
            ctormv.visitVarInsn(ALOAD, 2); // schema
            ctormv.visitLdcInsn(group.getName());
            ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup",
                    "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false);
            ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getGroupType",
                    "()Ljava/lang/Object;", false);
            ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "groupType_" + group.getName(),
                    "Ljava/lang/Object;");
        }

        Factory<?> factory = group.getFactory();
        if (isPublicConstructorFactory(factory)) {
            // no factory is needed
        } else {
            // field
            fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "factory_" + group.getName(),
                    "Lcom/cinnober/msgcodec/Factory;", null, null);
            fv.visitEnd();

            // ctor, init field
            ctormv.visitVarInsn(ALOAD, 0); // this
            ctormv.visitVarInsn(ALOAD, 2); // schema
            ctormv.visitLdcInsn(group.getName());
            ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup",
                    "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false);
            ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getFactory",
                    "()Lcom/cinnober/msgcodec/Factory;", false);
            ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "factory_" + group.getName(),
                    "Lcom/cinnober/msgcodec/Factory;");
        }

        for (FieldDef field : group.getFields()) {
            Accessor<?, ?> accessor = field.getAccessor();
            SymbolMapping<?> symbolMapping = field.getBinding().getSymbolMapping();
            TypeDef type = schema.resolveToType(field.getType(), true);
            TypeDef componentType = null;

            if (type.getType() == TypeDef.Type.SEQUENCE) {
                componentType = schema.resolveToType(((TypeDef.Sequence) type).getComponentType(), false);
            }

            if (isPublicFieldAccessor(accessor)) {
                // no accessor needed
            } else {
                // field
                fv = cv.visitField(ACC_PRIVATE + ACC_FINAL,
                        "accessor_" + group.getName() + "_" + field.getName(),
                        "Lcom/cinnober/msgcodec/Accessor;", null, null);
                fv.visitEnd();

                // ctor, init field
                ctormv.visitVarInsn(ALOAD, 0); // this
                ctormv.visitVarInsn(ALOAD, 2); // schema
                ctormv.visitLdcInsn(group.getName());
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup",
                        "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false);
                ctormv.visitLdcInsn(field.getName());
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getField",
                        "(Ljava/lang/String;)Lcom/cinnober/msgcodec/FieldDef;", false);
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldDef", "getAccessor",
                        "()Lcom/cinnober/msgcodec/Accessor;", false);
                ctormv.visitFieldInsn(PUTFIELD, genClassInternalName,
                        "accessor_" + group.getName() + "_" + field.getName(),
                        "Lcom/cinnober/msgcodec/Accessor;");
            }

            if (accessor.getClass() == CreateAccessor.class) {
                // No symbol map needed
            } else if ((type != null && type.getType() == TypeDef.Type.ENUM)
                    || (componentType != null && componentType.getType() == TypeDef.Type.ENUM)) {
                // If there is an enum and we need the symbol map
                Objects.requireNonNull(symbolMapping);

                // Create field
                String symbolMappingFieldName = "symbolMapping_" + group.getName() + "_" + field.getName();

                fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, symbolMappingFieldName,
                        "Lcom/cinnober/msgcodec/SymbolMapping;", null, null);
                fv.visitEnd();

                // Init field in the constructor
                ctormv.visitVarInsn(ALOAD, 0); // this
                ctormv.visitVarInsn(ALOAD, 2); // schema
                ctormv.visitLdcInsn(group.getName());
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup",
                        "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false);
                ctormv.visitLdcInsn(field.getName());
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getField",
                        "(Ljava/lang/String;)Lcom/cinnober/msgcodec/FieldDef;", false);
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldDef", "getBinding",
                        "()Lcom/cinnober/msgcodec/FieldBinding;", false);
                ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldBinding", "getSymbolMapping",
                        "()Lcom/cinnober/msgcodec/SymbolMapping;", false);

                ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, symbolMappingFieldName,
                        "Lcom/cinnober/msgcodec/SymbolMapping;");
            }
        }
    }

    ctormv.visitInsn(RETURN);
    ctormv.visitMaxs(3, nextCtorVar);
    ctormv.visitEnd();
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateWriteStaticGroup(Schema schema, ClassVisitor cv, String genClassInternalName,
        boolean javaClassCodec) {
    // method writeStaticGroupWithId - switch
    MethodVisitor mv = cv.visitMethod(ACC_PROTECTED, "writeStaticGroupWithId",
            "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Object;)V", null,
            new String[] { "java/io/IOException" });
    int nextVar = 3;
    mv.visitCode();// w  w  w. ja va  2  s . co m

    mv.visitVarInsn(ALOAD, 2);
    if (javaClassCodec) {
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, genClassInternalName, "groupTypeAccessor",
                "Lcom/cinnober/msgcodec/GroupTypeAccessor;");
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/GroupTypeAccessor", "getGroupType",
                "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    }
    int groupTypeVar = nextVar++;
    mv.visitInsn(DUP);
    mv.visitVarInsn(ASTORE, groupTypeVar);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I", false);

    // switch on class.hashCode()
    Map<Integer, ObjectHashCodeSwitchCase<Object>> casesByHashCode = new TreeMap<>();
    Map<Integer, Label> labelsByGroupId = new TreeMap<>();
    for (GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        int groupHash = groupType.hashCode();
        ObjectHashCodeSwitchCase<Object> hashCase = casesByHashCode.get(groupHash);
        if (hashCase == null) {
            hashCase = new ObjectHashCodeSwitchCase<>(groupHash);
            casesByHashCode.put(hashCase.hashCode, hashCase);
        }
        hashCase.add(groupType);
    }

    Label unknownHashLabel = new Label();
    {
        int[] caseValues = new int[casesByHashCode.size()];
        int i = 0;
        for (int hashCode : casesByHashCode.keySet()) {
            caseValues[i++] = hashCode;
        }
        Label[] caseLabels = new Label[casesByHashCode.size()];
        i = 0;
        for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) {
            caseLabels[i++] = hashCase.label;
        }
        mv.visitLookupSwitchInsn(unknownHashLabel, caseValues, caseLabels);
    }
    for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) {
        mv.visitLabel(hashCase.label);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        for (ObjectSwitchCase<Object> classCase : hashCase.cases) {
            GroupDef group = schema.getGroup(classCase.object);
            int groupId = schema.getGroup(classCase.object).getId();
            if (groupId != -1) {
                labelsByGroupId.put(groupId, classCase.label);
            }

            mv.visitVarInsn(ALOAD, groupTypeVar);
            if (javaClassCodec) {
                mv.visitLdcInsn(getJavaType(classCase.object));
                mv.visitJumpInsn(IF_ACMPEQ, classCase.label);
            } else {
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, genClassInternalName, "groupType_" + group.getName(),
                        "Ljava/lang/Object;");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
                mv.visitJumpInsn(IFNE, classCase.label); // IFNE = if not false
            }
        }
    }
    // Default case for class hashcode switch, do lookup using schema.getGroup(Object)
    {
        Label unknownGroupIdLabel = new Label();
        mv.visitLabel(unknownHashLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        Label[] groupIdLabels = new Label[labelsByGroupId.size()];
        int[] groupIds = new int[groupIdLabels.length];

        {
            int i = 0;
            for (Entry<Integer, Label> entry : labelsByGroupId.entrySet()) {
                groupIds[i] = entry.getKey();
                groupIdLabels[i] = new Label();
                i++;
            }
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, genClassInternalName, "schema", "Lcom/cinnober/msgcodec/Schema;");

        mv.visitVarInsn(ALOAD, groupTypeVar);
        mv.visitMethodInsn(INVOKEVIRTUAL, SCHEMA_INAME, "getGroup",
                "(Ljava/lang/Object;)Lcom/cinnober/msgcodec/GroupDef;", false);

        // check for null result from getGroup
        Label noGroupDefLabel = new Label();
        mv.visitInsn(DUP);
        mv.visitJumpInsn(IFNULL, noGroupDefLabel);

        mv.visitMethodInsn(INVOKEVIRTUAL, GROUPDEF_INAME, "getId", "()I", false);

        // Switch on the group id
        mv.visitLookupSwitchInsn(unknownHashLabel, groupIds, groupIdLabels);

        // Cases for the group ids
        for (int i = 0; i < groupIds.length; i++) {
            mv.visitLabel(groupIdLabels[i]);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitJumpInsn(GOTO, labelsByGroupId.get(groupIds[i]));
        }

        mv.visitLabel(noGroupDefLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitInsn(POP);

        // Throw exception if there is no match on class or group id
        mv.visitLabel(unknownGroupIdLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, groupTypeVar);
        mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownGroupType",
                "(Ljava/lang/Object;)Ljava/lang/IllegalArgumentException;", false);
        mv.visitInsn(ATHROW);
    }

    // Generate the labeled calls to group writer methods
    for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) {
        for (ObjectSwitchCase<Object> classCase : hashCase.cases) {
            Object groupType = classCase.object;
            GroupDef group = schema.getGroup(groupType);
            String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
            mv.visitLabel(classCase.label);
            mv.visitFrame(F_SAME, 0, null, 0, null);
            mv.visitVarInsn(ALOAD, 0); // this
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitVarInsn(ALOAD, 2); // obj
            if (javaClassCodec) {
                mv.visitTypeInsn(CHECKCAST, getTypeInternalName(groupType, javaClassCodec));
            }
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroupWithId_" + group.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", false);
            mv.visitInsn(RETURN);
        }
    }

    mv.visitMaxs(4, nextVar);
    mv.visitEnd();
}

From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java

License:Open Source License

protected void generateWriteStaticGroupForTypeWithId(Schema schema, ClassVisitor cv,
        String genClassInternalName, boolean javaClassCodec) {
    for (GroupDef group : schema.getGroups()) {
        Object groupType = group.getGroupType();
        String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec);
        MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "writeStaticGroupWithId_" + group.getName(),
                "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", null,
                new String[] { "java/io/IOException" });
        mv.visitCode();/*from  w  ww.j a  va  2 s.  co  m*/
        int nextWriteidVar = 3;

        if (group.getId() != -1) {
            // write with id
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitLdcInsn(group.getId());
            mv.visitMethodInsn(INVOKESTATIC, blinkOutputIName, "writeUInt32",
                    "(Lcom/cinnober/msgcodec/io/ByteSink;I)V", false);
            mv.visitVarInsn(ALOAD, 0); // this
            mv.visitVarInsn(ALOAD, 1); // out
            mv.visitVarInsn(ALOAD, 2); // obj
            mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroup_" + group.getName(),
                    "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", false);
            mv.visitInsn(RETURN);
        } else {
            // write with id
            mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
            mv.visitInsn(DUP);
            mv.visitLdcInsn("No group id");
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>",
                    "(Ljava/lang/String;)V", false);
            mv.visitInsn(ATHROW);
        }

        // end
        mv.visitMaxs(3, nextWriteidVar);
        mv.visitEnd();
    }
}