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.codegen.EdgeDataTableAdapterGenerator.java

License:Apache License

private static void defineBuildKey(ClassGeneratorContext context, ClassWriter writer, TypeDescription dataType,
        Group group) {/*from  www . j av  a 2s .  c om*/
    DataModelReference type = context.getDataModelLoader().load(dataType);
    List<PropertyReference> props = group.getGrouping().stream()
            .map(p -> Invariants.requireNonNull(type.findProperty(p))).collect(Collectors.toList());

    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "buildKey",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(KeyBuffer.class), typeOf(Object.class)), null,
            null);

    LocalVarRef key = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef object = cast(v, 2, dataType);
    for (PropertyReference p : props) {
        key.load(v);
        object.load(v);
        getOption(v, p);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(KeyBuffer.class).getInternalName(), "append",
                Type.getMethodDescriptor(typeOf(KeyBuffer.class), typeOf(Object.class)), true);
        v.visitInsn(Opcodes.POP);
    }

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

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

License:Apache License

private static void putSerialize(String methodName, DataModelReference reference,
        List<PropertyReference> properties, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(DataOutput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    if (properties.isEmpty()) {
        LocalVarRef output = new LocalVarRef(Opcodes.ALOAD, 2);
        output.load(v);/*w  w w . j  a  va 2  s.  c  o  m*/
        getConst(v, 0);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(DataOutput.class).getInternalName(), "writeByte",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE), true);
    } else {
        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.codegen.KeyValueSerDeGenerator.java

License:Apache License

private static void putDeserializeBody(MethodVisitor v, List<PropertyReference> props, LocalVarRef input,
        LocalVarRef object) {/*from  w w  w  . j av  a 2  s . c om*/
    if (props.isEmpty()) {
        input.load(v);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(DataInput.class).getInternalName(), "readByte",
                Type.getMethodDescriptor(Type.BYTE_TYPE), true);
        v.visitInsn(Opcodes.POP);
    } else {
        for (PropertyReference property : props) {
            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);
        }
    }
}

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

License:Apache License

private static void defineCompare(ClassWriter writer, DataModelReference reference,
        List<Group.Ordering> orderings) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "compare",
            Type.getMethodDescriptor(typeOf(int.class), typeOf(Object.class), typeOf(Object.class)), null,
            null);/*from  ww  w . ja  v  a 2  s .  c om*/
    LocalVarRef a = cast(v, 1, reference.getDeclaration());
    LocalVarRef b = cast(v, 2, reference.getDeclaration());
    for (Group.Ordering ordering : orderings) {
        LocalVarRef left;
        LocalVarRef right;
        switch (ordering.getDirection()) {
        case ASCENDANT:
            left = a;
            right = b;
            break;
        case DESCENDANT:
            left = b;
            right = a;
            break;
        default:
            throw new AssertionError(ordering);
        }

        // int diff = left.getXOption().compareTo(right.getXOption());
        PropertyReference property = Invariants
                .requireNonNull(reference.findProperty(ordering.getPropertyName()));
        left.load(v);
        getOption(v, property);
        right.load(v);
        getOption(v, property);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, TYPE_COMPARABLE.getInternalName(), "compareTo",
                Type.getMethodDescriptor(typeOf(int.class), typeOf(Object.class)), true);
        LocalVarRef cmp = putLocalVar(v, Type.INT, 3);
        Label eq = new Label();

        // if (diff != 0) {
        cmp.load(v);
        v.visitJumpInsn(Opcodes.IFEQ, eq);

        // return diff;
        cmp.load(v);
        v.visitInsn(Opcodes.IRETURN);

        // } @ eq
        v.visitLabel(eq);
    }
    getConst(v, 0);
    v.visitInsn(Opcodes.IRETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

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

License:Apache License

private static void addConstructor(ClassWriter writer, ClassDescription target, List<VertexElement> elements,
        Function<VertexElement, String> ids) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(OperationAdapter.Context.class)), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(Object.class).getInternalName(), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE), false);

    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    method.visitFieldInsn(Opcodes.PUTFIELD, target.getInternalName(), FIELD_CONTEXT,
            typeOf(OperationAdapter.Context.class).getDescriptor());

    for (VertexElement element : elements) {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitMethodInsn(Opcodes.INVOKESPECIAL, target.getInternalName(), ids.apply(element),
                Type.getMethodDescriptor(Type.VOID_TYPE), false);
    }//w  w  w .ja  v  a2 s  . co  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 getEmptyDataTable(MethodVisitor method, ClassDescription target,
        Function<VertexElement, String> ids) {
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(BasicDataTable.class).getInternalName(), "empty", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(DataTable.class)), false);
}

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

License:Apache License

private static void getClass(MethodVisitor method, ClassDescription target, ClassNode element,
        Function<VertexElement, String> ids) {
    method.visitTypeInsn(Opcodes.NEW, element.getImplementationType().getInternalName());
    method.visitInsn(Opcodes.DUP);/* w  w w.j a va2  s  . c  o  m*/
    List<Type> parameterTypes = new ArrayList<>();
    for (VertexElement dep : element.getDependencies()) {
        parameterTypes.add(typeOf(dep.getRuntimeType()));
        get(method, target, dep, ids);
    }
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, element.getImplementationType().getInternalName(),
            CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, parameterTypes.stream().toArray(Type[]::new)), false);
}

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

License:Apache License

private static void getOutput(MethodVisitor method, ClassDescription target, OutputNode element,
        Function<VertexElement, String> ids) {
    getContext(method, target, ids);//  www.  jav  a2  s.  c  o m
    getConst(method, element.getDataType());
    getConst(method, element.getId());
    method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(OperationAdapter.Context.class).getInternalName(),
            "getSink",
            Type.getMethodDescriptor(typeOf(Result.class), typeOf(Class.class), typeOf(String.class)), true);
}

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

License:Apache License

private static void getDataTable(MethodVisitor method, ClassDescription target, DataTableNode element,
        Function<VertexElement, String> ids) {
    getContext(method, target, ids);/*from w  w  w  . j a v a 2  s .com*/
    getConst(method, element.getDataType());
    getConst(method, element.getId());
    method.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(OperationAdapter.Context.class).getInternalName(),
            "getDataTable",
            Type.getMethodDescriptor(typeOf(DataTable.class), typeOf(Class.class), typeOf(String.class)), true);
}

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 ww  w .j  a v a2  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();
}