Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

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

License:Apache License

/**
 * Adds {@link Result#add(Object)} method.
 * @param writer the current writer//  w w  w .  j a v  a2 s .  com
 * @param callback the callback
 */
public static void defineResultAdd(ClassVisitor writer, Consumer<MethodVisitor> callback) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "add",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class)), null, null);
    callback.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

/**
 * Adds constant {@code toString()} method.
 * @param writer the current writer/*from  w w  w .  j av a2s .co m*/
 * @param value the constant value
 */
public static void defineToString(ClassVisitor writer, String value) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "toString",
            Type.getMethodDescriptor(typeOf(String.class)), null, null);
    getConst(method, value);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

private static List<Tuple<VertexElement, FieldRef>> defineDependenciesConstructor0(ClassDescription aClass,
        ClassVisitor writer, Iterable<? extends VertexElement> dependencies,
        Consumer<MethodVisitor> superConstructor, Consumer<MethodVisitor> callback) {
    List<Tuple<VertexElement, FieldRef>> results = new ArrayList<>();
    int index = 0;
    int varIndex = 1;
    List<Type> parameterTypes = new ArrayList<>();
    List<Consumer<MethodVisitor>> ctor = new ArrayList<>();
    for (VertexElement element : dependencies) {
        int current = index++;
        int currentVar = varIndex;
        String name = getDependencyId(current);
        Type type = typeOf(element.getRuntimeType());
        parameterTypes.add(type);//  w w w.j a v  a 2 s. c  o m
        FieldRef ref = defineField(writer, aClass, name, type);
        ctor.add(v -> {
            v.visitVarInsn(Opcodes.ALOAD, 0);
            v.visitVarInsn(loadOpcodeOf(element.getRuntimeType()), currentVar);
            putField(v, ref);
        });
        varIndex += categoryOf(element.getRuntimeType());
        results.add(new Tuple<>(element, ref));
    }
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, parameterTypes.stream().toArray(Type[]::new)), null, null);
    superConstructor.accept(method);
    ctor.forEach(c -> c.accept(method));
    callback.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
    return results;
}

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

License:Apache License

private static void defineString(ClassWriter writer, String name, String value) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED, name,
            Type.getMethodDescriptor(typeOf(String.class)), null, new String[0]);
    getConst(method, value);//from   ww  w  .  ja  va2 s. c om
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

private static void defineCompare(ClassWriter writer, DataModelReference reference,
        List<Group.Ordering> orderings) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "compare", DESC_COMPARE, null,
            new String[] { typeOf(IOException.class).getInternalName(), });
    LocalVarRef a = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef b = new LocalVarRef(Opcodes.ALOAD, 2);
    for (Group.Ordering ordering : orderings) {
        PropertyReference property = Invariants
                .requireNonNull(reference.findProperty(ordering.getPropertyName()));

        // int diff = ValueOptionSerDe.compareT({a, b}, {b, a});
        switch (ordering.getDirection()) {
        case ASCENDANT:
            a.load(v);//from  w ww  . j  a  v a 2  s  .c o  m
            b.load(v);
            break;
        case DESCENDANT:
            b.load(v);
            a.load(v);
            break;
        default:
            throw new AssertionError(ordering);
        }
        v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(ValueOptionSerDe.class).getInternalName(),
                Invariants.requireNonNull(METHOD_NAMES.get(property.getType())), DESC_COMPARE, false);
        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.EdgeDataTableAdapterGenerator.java

License:Apache License

private static void defineBuildKey(ClassGeneratorContext context, ClassWriter writer, TypeDescription dataType,
        Group group) {//from  www. j av  a  2  s. c  o  m
    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 ww  .j  a  va  2s  .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 putDeserialize(DataModelReference reference, List<PropertyReference> keys,
        List<PropertyReference> values, 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 keyInput = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef valueInput = new LocalVarRef(Opcodes.ALOAD, 2);
    self.load(v);/*from  w w w. ja  v  a 2s . com*/
    getField(v, buffer);
    LocalVarRef object = putLocalVar(v, Type.OBJECT, 3);
    putDeserializeBody(v, keys, keyInput, object);
    putDeserializeBody(v, values, valueInput, object);
    object.load(v);
    v.visitInsn(Opcodes.ARETURN);
    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  .  ja v  a 2  s.  c  o m*/
    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);/*  w  w w.  ja  v a 2  s.  c  o  m*/
    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();
}