Example usage for org.objectweb.asm MethodVisitor visitEnd

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

Introduction

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

Prototype

public void visitEnd() 

Source Link

Document

Visits the end of the method.

Usage

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);//w  w w .  j ava 2 s. co m
    method.visitMaxs(0, 0);
    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  . c  o m*/
    });

    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 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 www . j  av  a 2s. com*/

    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);
    }/*w w  w  .  j  a  va2  s  .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  ava 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.AsmUtil.java

License:Apache License

/**
 * Adds an empty constructor./*from   w ww .  j a  va 2  s.c  om*/
 * @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 .  ja v  a  2s . 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();
}

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

License:Apache License

/**
 * Adds an adapter constructor.//from  w  w  w .  ja v a2  s.c o  m
 * @param writer the target class
 * @param superClass the super class
 * @param body the constructor body
 */
public static void defineAdapterConstructor(ClassWriter writer, Class<?> superClass,
        Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(superClass).getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), false);
    body.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 a getter-like method.//from ww  w .  j a  v  a2s  .co m
 * @param writer the target class
 * @param type the method result type
 * @param name the method name
 * @param body the method body
 */
public static void defineGetter(ClassWriter writer, Type type, String name, Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, name, Type.getMethodDescriptor(type), null,
            new String[0]);
    body.accept(method);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

/**
 * Adds {@link Result#add(Object)} method.
 * @param writer the current writer/*from w w w  . ja v  a  2  s.  c  o m*/
 * @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();
}