Example usage for org.objectweb.asm MethodVisitor visitMaxs

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

Introduction

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

Prototype

public void visitMaxs(final int maxStack, final int maxLocals) 

Source Link

Document

Visits the maximum stack size and the maximum number of local variables of the method.

Usage

From source file:com.asakusafw.dag.compiler.codegen.OperationGenerator.java

License:Apache License

private static void addProcessMethod(ClassWriter writer, ClassDescription target, OperationSpec graph) {
    VertexElement consumer = graph.getInput().getConsumer();
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "process",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class)), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitFieldInsn(Opcodes.GETFIELD, target.getInternalName(), graph.getId(consumer),
            typeOf(consumer.getRuntimeType()).getDescriptor());
    method.visitVarInsn(Opcodes.ALOAD, 1);
    invokeResultAdd(method);//from   ww w.  j av  a  2s .  c  o m
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.OperationGenerator.java

License:Apache License

private static void addElementMethods(ClassWriter writer, ClassDescription target, List<VertexElement> elements,
        Function<VertexElement, String> ids) {
    for (VertexElement element : elements) {
        String id = ids.apply(element);
        MethodVisitor method = writer.visitMethod(Opcodes.ACC_PRIVATE, id,
                Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
        method.visitVarInsn(Opcodes.ALOAD, 0);
        switch (element.getElementKind()) {
        case VALUE:
            getValue(method, target, (ValueElement) element, ids);
            break;
        case OPERATOR:
        case AGGREGATE:
            getClass(method, target, (ClassNode) element, ids);
            break;
        case OUTPUT:
            getOutput(method, target, (OutputNode) element, ids);
            break;
        case DATA_TABLE:
            getDataTable(method, target, (DataTableNode) element, ids);
            break;
        case CONTEXT:
            getContext(method, target, ids);
            break;
        case EMPTY_DATA_TABLE:
            getEmptyDataTable(method, target, ids);
            break;
        default://from   w  w  w.java 2s .  c  o m
            throw new AssertionError(element.getElementKind());
        }
        method.visitFieldInsn(Opcodes.PUTFIELD, target.getInternalName(), id,
                typeOf(element.getRuntimeType()).getDescriptor());
        method.visitInsn(Opcodes.RETURN);
        method.visitMaxs(0, 0);
        method.visitEnd();
    }
}

From source file:com.asakusafw.dag.compiler.codegen.ValueSerDeGenerator.java

License:Apache License

private static void putSerialize(DataModelReference reference, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "serialize",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(DataOutput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    LocalVarRef object = cast(v, 1, reference.getDeclaration());
    LocalVarRef output = new LocalVarRef(Opcodes.ALOAD, 2);
    for (PropertyReference property : reference.getProperties()) {
        object.load(v);//from   w ww  .  java2 s  .  c  o m
        getOption(v, property);
        output.load(v);
        v.visitMethodInsn(Opcodes.INVOKESTATIC, SERDE.getInternalName(), "serialize",
                Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(property.getType()), typeOf(DataOutput.class)),
                false);
    }
    v.visitInsn(Opcodes.RETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.ValueSerDeGenerator.java

License:Apache License

private static void putDeserialize(DataModelReference reference, FieldRef buffer, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(DataInput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef input = new LocalVarRef(Opcodes.ALOAD, 1);

    self.load(v);//from   w ww .j  av a  2s  .  com
    getField(v, buffer);
    LocalVarRef object = putLocalVar(v, Type.OBJECT, 2);
    for (PropertyReference property : reference.getProperties()) {
        object.load(v);
        getOption(v, property);
        input.load(v);
        v.visitMethodInsn(Opcodes.INVOKESTATIC, SERDE.getInternalName(), "deserialize",
                Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(property.getType()), typeOf(DataInput.class)),
                false);
    }
    object.load(v);
    v.visitInsn(Opcodes.ARETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.directio.OutputPatternSerDeGenerator.java

License:Apache License

private static void putGetProperty(ClassWriter writer, DataModelReference reference, OutputPattern pattern) {
    List<PropertyReference> properties = pattern.getResourcePattern().stream()
            .filter(s -> s.getKind() == OutputPattern.SourceKind.PROPERTY).map(CompiledSegment::getTarget)
            .collect(Collectors.toList());
    if (properties.isEmpty()) {
        return;/*w  w w. ja  v a 2  s .  c  o  m*/
    }
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "getProperty",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(Object.class), typeOf(int.class)), null,
            null);
    LocalVarRef object = cast(v, 1, reference.getDeclaration());
    LocalVarRef index = new LocalVarRef(Opcodes.ILOAD, 2);
    Label[] caseLabels = properties.stream().map(o -> new Label()).toArray(Label[]::new);
    Label defaultLabel = new Label();

    index.load(v);
    v.visitTableSwitchInsn(0, caseLabels.length - 1, defaultLabel, caseLabels);

    for (int i = 0, n = properties.size(); i < n; i++) {
        v.visitLabel(caseLabels[i]);
        PropertyReference property = properties.get(i);
        object.load(v);
        getOption(v, property);
        v.visitInsn(Opcodes.ARETURN);
    }
    v.visitLabel(defaultLabel);
    getNew(v, Descriptions.typeOf(AssertionError.class));
    v.visitInsn(Opcodes.ATHROW);

    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.directio.OutputPatternSerDeGenerator.java

License:Apache License

private static void putSerialize(DataModelReference reference, List<PropertyReference> properties,
        ClassWriter writer) {/*from   w  w w  .j av  a 2s . c o m*/
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeValue",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(DataOutput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    LocalVarRef object = cast(v, 1, reference.getDeclaration());
    LocalVarRef output = new LocalVarRef(Opcodes.ALOAD, 2);
    for (PropertyReference property : properties) {
        object.load(v);
        getOption(v, property);
        output.load(v);
        v.visitMethodInsn(Opcodes.INVOKESTATIC, SERDE.getInternalName(), "serialize",
                Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(property.getType()), typeOf(DataOutput.class)),
                false);
    }
    v.visitInsn(Opcodes.RETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.directio.OutputPatternSerDeGenerator.java

License:Apache License

private static void putDeserialize(DataModelReference reference, List<PropertyReference> properties,
        FieldRef buffer, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserializePair",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(DataInput.class), typeOf(DataInput.class)),
            null, new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef valueInput = new LocalVarRef(Opcodes.ALOAD, 2);
    self.load(v);/*from w w  w .ja va2s .  c  om*/
    getField(v, buffer);
    LocalVarRef object = putLocalVar(v, Type.OBJECT, 3);
    for (PropertyReference property : properties) {
        object.load(v);
        getOption(v, property);
        valueInput.load(v);
        v.visitMethodInsn(Opcodes.INVOKESTATIC, SERDE.getInternalName(), "deserialize",
                Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(property.getType()), typeOf(DataInput.class)),
                false);
    }
    object.load(v);
    v.visitInsn(Opcodes.ARETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

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

License:Apache License

private static void defineBody(ClassWriter writer, DataModelReference dataType,
        List<PropertyReference> properties, Optional<FieldRef> dateBuf) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "drive", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(void.class), typeOf(PreparedStatement.class), typeOf(Object.class)),
            null, new String[] { typeOf(SQLException.class).getInternalName(), });
    LocalVarRef row = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef object = cast(v, 2, dataType.getDeclaration());

    int columnIndex = 0;
    for (PropertyReference property : properties) {
        columnIndex++;//from   ww  w.  jav a  2s  .  c o  m

        object.load(v);
        getOption(v, property);
        LocalVarRef option = putLocalVar(v, Type.OBJECT, 3);

        Label elseIf = new Label();
        Label endIf = new Label();

        // if (option.isNull()) {
        option.load(v);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(ValueOption.class).getInternalName(), "isNull", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(boolean.class)), false);
        v.visitJumpInsn(Opcodes.IFEQ, elseIf);

        row.load(v);
        getConst(v, columnIndex);
        doSetNull(v, property);

        v.visitJumpInsn(Opcodes.GOTO, endIf);

        // } else { @elseIf
        v.visitLabel(elseIf);

        row.load(v);
        getConst(v, columnIndex);
        option.load(v);
        doSetValue(v, property, dateBuf);

        // } @endIf
        v.visitLabel(endIf);
    }

    v.visitInsn(Opcodes.RETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

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

License:Apache License

private static void defineBody(ClassWriter writer, DataModelReference dataType,
        List<PropertyReference> properties, FieldRef valueBuffer, Optional<FieldRef> calendarBuf) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "extract",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(ResultSet.class)), null,
            new String[] { typeOf(SQLException.class).getInternalName(), });
    LocalVarRef rs = new LocalVarRef(Opcodes.ALOAD, 1);
    valueBuffer.load(v);/* w w  w  .  jav a  2s.  c o  m*/
    LocalVarRef buf = putLocalVar(v, Type.OBJECT, 2);

    int columnIndex = 0;
    Set<PropertyName> sawSet = new HashSet<>();
    for (PropertyReference property : properties) {
        columnIndex++;
        Label elseIf = new Label();
        Label endIf = new Label();

        buf.load(v);
        getOption(v, property);

        rs.load(v);
        getConst(v, columnIndex);
        doGetValue(v, property, calendarBuf);

        // if (rs.wasNull()) {
        rs.load(v);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "wasNull",
                Type.getMethodDescriptor(typeOf(boolean.class)), true);
        v.visitJumpInsn(Opcodes.IFEQ, elseIf);
        if (isWideType(property)) {
            v.visitInsn(Opcodes.POP2);
        } else {
            v.visitInsn(Opcodes.POP);
        }
        doSetNull(v);
        v.visitJumpInsn(Opcodes.GOTO, endIf);

        // } else { @elseIf
        v.visitLabel(elseIf);
        doSetValue(v, property);

        // } @endIf
        v.visitLabel(endIf);

        sawSet.add(property.getName());
    }

    // resets other properties
    for (PropertyReference property : dataType.getProperties()) {
        if (sawSet.contains(property.getName())) {
            continue;
        }
        buf.load(v);
        getOption(v, property);
        doSetNull(v);
    }

    buf.load(v);
    v.visitInsn(Opcodes.ARETURN);
    v.visitMaxs(0, 0);
    v.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.
 * /*from   w ww  . j av a 2 s .  c om*/
 * 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();
}