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.builtin.MasterJoinLikeOperatorGenerator.java

License:Apache License

private static void defineSelection(Context context, ClassWriter writer, UserOperator operator, FieldRef impl,
        Map<OperatorProperty, FieldRef> dependencies) {
    Method selector = Invariants.safe(() -> {
        return MasterJoinOperatorUtil.getSelection(context.getClassLoader(), operator);
    });/*from w w w . j  a  v a  2s .  c  om*/
    if (selector == null) {
        return;
    }
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "selectMaster",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(List.class), typeOf(Object.class)), null,
            null);
    cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType());

    List<ValueRef> arguments = new ArrayList<>();
    impl.load(method);
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 1));
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 2));
    arguments
            .addAll(Lang.project(getExtraViews(operator), v -> Invariants.requireNonNull(dependencies.get(v))));
    arguments
            .addAll(Lang.project(operator.getArguments(), v -> Invariants.requireNonNull(dependencies.get(v))));
    for (int i = 0, n = selector.getParameterCount(); i < n; i++) {
        arguments.get(i).load(method);
    }
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(selector.getDeclaringClass()).getInternalName(),
            selector.getName(), Type.getMethodDescriptor(selector), false);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.builtin.MasterJoinLikeOperatorGenerator.java

License:Apache License

private void defineProcess(Context context, ClassWriter writer, UserOperator operator, FieldRef impl,
        Map<OperatorProperty, FieldRef> dependencies, ClassDescription target) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "process",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(Object.class)), null, null);
    cast(method, 1, MasterJoinOperatorUtil.getMasterInput(operator).getDataType());
    cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType());
    defineProcess(method, context, operator, new LocalVarRef(Opcodes.ALOAD, 1),
            new LocalVarRef(Opcodes.ALOAD, 2), impl, dependencies, target);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);/*  w  w w . j a  v  a2  s . com*/
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

static ClassDescription generateMapperClass(Context context, UserOperator operator, ClassDescription outer) {
    ClassDescription target = getMapperName(outer);

    OperatorInput input = operator.getInput(Summarize.ID_INPUT);
    OperatorOutput output = operator.getOutput(Summarize.ID_OUTPUT);

    ClassWriter writer = newWriter(target, Object.class, Function.class);
    writer.visitOuterClass(outer.getInternalName(), target.getSimpleName(), null);

    FieldRef buffer = defineField(writer, target, "buffer", typeOf(output.getDataType()));
    defineEmptyConstructor(writer, Object.class, method -> {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        getNew(method, output.getDataType());
        putField(method, buffer);/*from   w w  w  .ja  v a  2  s.com*/
    });

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "apply",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(Object.class)), null, null);

    LocalVarRef inputVar = cast(method, 1, input.getDataType());
    buffer.load(method);
    LocalVarRef outputVar = putLocalVar(method, Type.OBJECT, 2);

    outputVar.load(method);
    resetDataModel(method, output.getDataType());

    List<PropertyFolding> foldings = Invariants
            .safe(() -> SummarizedModelUtil.getPropertyFoldings(context.getClassLoader(), operator));
    DataModelReference inputModel = context.getDataModelLoader().load(input.getDataType());
    DataModelReference outputModel = context.getDataModelLoader().load(output.getDataType());
    Set<PropertyReference> nullChecked = new HashSet<>();
    for (PropertyFolding folding : foldings) {
        PropertyMapping mapping = folding.getMapping();
        Aggregation aggregation = folding.getAggregation();
        PropertyReference src = Invariants.requireNonNull(inputModel.findProperty(mapping.getSourceProperty()));
        PropertyReference dst = Invariants
                .requireNonNull(outputModel.findProperty(mapping.getDestinationProperty()));
        mapping(method, target, aggregation, src, dst, inputVar, outputVar, nullChecked);
    }

    outputVar.load(method);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();

    if (nullChecked.isEmpty() == false) {
        defineCheckNull(writer, operator, inputModel);
    }

    return context.addClassFile(new ClassData(target, writer::toByteArray));
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static void sumMapper(MethodVisitor method, ClassDescription target, PropertyReference src,
        PropertyReference dst, LocalVarRef srcVar, LocalVarRef dstVar, Set<PropertyReference> nullChecked) {
    dstVar.load(method);//www.  jav a  2s.  com
    getOption(method, dst);
    srcVar.load(method);
    getOption(method, src);
    checkNull(method, target, src, srcVar, nullChecked);
    TypeDescription srcOptionType = src.getType();
    Type srcEntityType = Invariants.requireNonNull(ENTITY_TYPE_MAP.get(srcOptionType));
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(srcOptionType).getInternalName(), "get",
            Type.getMethodDescriptor(srcEntityType), false);
    if (srcOptionType.equals(BYTE_DESC) || srcOptionType.equals(SHORT_DESC) || srcOptionType.equals(INT_DESC)) {
        method.visitInsn(Opcodes.I2L);
    } else if (srcOptionType.equals(FLOAT_DESC)) {
        method.visitInsn(Opcodes.F2D);
    }
    TypeDescription dstOptionType = dst.getType();
    Type dstEntityType = Invariants.requireNonNull(ENTITY_TYPE_MAP.get(dstOptionType));
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(dstOptionType).getInternalName(), "modify",
            Type.getMethodDescriptor(typeOf(dstOptionType), dstEntityType), false);
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static void checkNull(MethodVisitor method, ClassDescription target, PropertyReference property,
        LocalVarRef variable, Set<PropertyReference> finished) {
    if (finished.contains(property)) {
        return;//from  w  w w.  j a  v a2 s  .  c  o  m
    }
    finished.add(property);

    method.visitInsn(Opcodes.DUP);
    variable.load(method);
    getConst(method, property.getName().toName());
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(target).getInternalName(), METHOD_CHECK_NON_NULL,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ValueOption.class), typeOf(Object.class),
                    typeOf(String.class)),
            false);
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static void defineCheckNull(ClassWriter writer, UserOperator operator, DataModelReference inputType) {

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, METHOD_CHECK_NON_NULL,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ValueOption.class), typeOf(Object.class),
                    typeOf(String.class)),
            null, null);/*from  ww w .ja va 2 s  .c  o  m*/

    LocalVarRef optionVar = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef objectVar = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef nameVar = new LocalVarRef(Opcodes.ALOAD, 2);

    // if (option.isNull()) {
    Label ifEnd = new Label();
    optionVar.load(method);
    getNullity(method, VALUE_DESC);
    method.visitJumpInsn(Opcodes.IFEQ, ifEnd);

    // new NullPointerException ...
    method.visitTypeInsn(Opcodes.NEW, typeOf(NullPointerException.class).getInternalName());
    method.visitInsn(Opcodes.DUP);

    // str = String.format("<type>.%s must not be null (in <operator>): %s", name, object)
    getConst(method,
            String.format("%s.%%s must not be null (in %s.%s): %%s", inputType.getDeclaration().getSimpleName(),
                    operator.getMethod().getDeclaringClass().getSimpleName(), operator.getMethod().getName()));

    getArray(method, typeOf(Object.class), new LocalVarRef[] { nameVar, objectVar });
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(String.class).getInternalName(), "format",
            Type.getMethodDescriptor(typeOf(String.class), typeOf(String.class), typeOf(Object[].class)),
            false);

    // throw new NullPointerException(str)
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(NullPointerException.class).getInternalName(),
            CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(String.class)), false);

    method.visitInsn(Opcodes.ATHROW);

    method.visitLabel(ifEnd);
    // }
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

static ClassDescription generateCombinerClass(Context context, UserOperator operator, ClassDescription outer) {
    ClassDescription target = getCombinerName(outer);
    OperatorInput input = operator.getInput(Summarize.ID_INPUT);
    OperatorOutput output = operator.getOutput(Summarize.ID_OUTPUT);

    ClassWriter writer = newWriter(target, Object.class, ObjectCombiner.class);
    writer.visitOuterClass(outer.getInternalName(), target.getSimpleName(), null);
    defineEmptyConstructor(writer, Object.class);
    defineBuildKey(context, writer, output.getDataType(), input.getGroup());

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "combine",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(Object.class)), null, null);

    LocalVarRef leftVar = cast(method, 1, output.getDataType());
    LocalVarRef rightVar = cast(method, 2, output.getDataType());

    List<PropertyFolding> foldings = Invariants
            .safe(() -> SummarizedModelUtil.getPropertyFoldings(context.getClassLoader(), operator));
    DataModelReference outputModel = context.getDataModelLoader().load(output.getDataType());
    for (PropertyFolding folding : foldings) {
        PropertyMapping mapping = folding.getMapping();
        Aggregation aggregation = folding.getAggregation();
        PropertyReference property = Invariants
                .requireNonNull(outputModel.findProperty(mapping.getDestinationProperty()));
        combine(method, aggregation, property, leftVar, rightVar);
    }//from   w w w.  jav  a  2s.  c  om

    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
    return context.addClassFile(new ClassData(target, writer::toByteArray));
}

From source file:com.asakusafw.dag.compiler.builtin.Util.java

License:Apache License

static void defineBuildKey(ClassGeneratorContext context, ClassWriter writer, TypeDescription dataType,
        Group group) {/*  w ww .j  a v  a 2s  . co 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.AsmUtil.java

License:Apache License

/**
 * Adds an empty constructor.//from  ww  w .jav a  2 s .c  o  m
 * @param writer the target class
 * @param block the constructor block
 */
public static void defineEmptyConstructor(ClassWriter writer, Consumer<MethodVisitor> block) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
    block.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 an empty constructor./*w  w w.  java 2 s. c  o m*/
 * @param writer the target class
 * @param superClass the super class
 * @param body the constructor body
 */
public static void defineEmptyConstructor(ClassWriter writer, Class<?> superClass,
        Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(superClass).getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), false);
    body.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}