Example usage for org.objectweb.asm Opcodes INVOKESTATIC

List of usage examples for org.objectweb.asm Opcodes INVOKESTATIC

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes INVOKESTATIC.

Prototype

int INVOKESTATIC

To view the source code for org.objectweb.asm Opcodes INVOKESTATIC.

Click Source Link

Usage

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 www  .j ava2s.  co  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 .  j a  v  a2 s.c o m*/
    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

static ClassData generate(ClassDescription target, DataModelReference dataType,
        List<PropertyReference> properties) {
    ClassWriter writer = newWriter(target, Object.class, PreparedStatementAdapter.class);

    Optional<FieldRef> calendarBuf = properties.stream().map(PropertyReference::getType)
            .map(PropertyTypeKind::fromOptionType)
            .filter(Predicate.isEqual(PropertyTypeKind.DATE).or(Predicate.isEqual(PropertyTypeKind.DATE_TIME)))
            .findAny().map(k -> defineField(writer, target, "calendarBuf", typeOf(java.util.Calendar.class)));

    defineEmptyConstructor(writer, Object.class, v -> {
        LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
        calendarBuf.ifPresent(f -> {//from   w  w w  .  ja v  a 2  s  .  co  m
            self.load(v);
            v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(java.util.Calendar.class).getInternalName(),
                    "getInstance", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(java.util.Calendar.class)), false);
            putField(v, f);
        });
    });

    defineBody(writer, dataType, properties, calendarBuf);

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

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

License:Apache License

private static void doSetDateLike(MethodVisitor method, PropertyTypeKind kind, FieldRef calendarBuf) {
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "get", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(kind.getRawType())), false);

    calendarBuf.load(method);/*from w  ww .  ja  va 2 s .c o  m*/
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(JdbcUtil.class).getInternalName(), "setParameter",
            Type.getMethodDescriptor(typeOf(void.class), typeOf(PreparedStatement.class), typeOf(int.class),
                    typeOf(kind.getRawType()), typeOf(java.util.Calendar.class)),
            true);
}

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

License:Apache License

static ClassData generate(ClassDescription target, DataModelReference dataType,
        List<PropertyReference> properties) {
    ClassWriter writer = newWriter(target, Object.class, ResultSetAdapter.class);

    FieldRef buffer = defineField(writer, target, "buffer", typeOf(dataType.getDeclaration()));
    Optional<FieldRef> calendarBuf = properties.stream().map(PropertyReference::getType)
            .map(PropertyTypeKind::fromOptionType)
            .filter(Predicate.isEqual(PropertyTypeKind.DATE).or(Predicate.isEqual(PropertyTypeKind.DATE_TIME)))
            .findAny().map(k -> defineField(writer, target, "calendarBuf", typeOf(java.util.Calendar.class)));

    defineEmptyConstructor(writer, Object.class, method -> {
        LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
        self.load(method);/*from w w w  . j  a  v a2 s .c  om*/
        getNew(method, dataType.getDeclaration());
        putField(method, buffer);
        calendarBuf.ifPresent(f -> {
            self.load(method);
            method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(java.util.Calendar.class).getInternalName(),
                    "getInstance", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(java.util.Calendar.class)), false);
            putField(method, f);
        });
    });

    defineBody(writer, dataType, properties, buffer, calendarBuf);

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

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

License:Apache License

private static void doSetValue(MethodVisitor method, PropertyReference property) {
    PropertyTypeKind kind = PropertyTypeKind.fromOptionType(property.getType());
    switch (kind) {
    case BOOLEAN:
    case BYTE:/*w w w .  j a  v a  2  s.c  om*/
    case SHORT:
    case INT:
    case LONG:
    case FLOAT:
    case DOUBLE:
    case DECIMAL:
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(kind.getRawType())), false);
        method.visitInsn(Opcodes.POP);
        break;
    case STRING:
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(String.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    case DATE:
        method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(DateUtil.class).getInternalName(), "getDayFromDate", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(int.class), typeOf(java.util.Date.class)), false);
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(int.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    case DATE_TIME:
        method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(DateUtil.class).getInternalName(),
                "getSecondFromDate", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(long.class), typeOf(java.util.Date.class)), false);
        method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(kind.getOptionType()).getInternalName(), "modify", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(kind.getOptionType()), typeOf(long.class)), false);
        method.visitInsn(Opcodes.POP);
        break;
    default:
        throw new AssertionError(property);
    }
}

From source file:com.asakusafw.dag.compiler.jdbc.windgate.WindGateJdbcInputAdapterGenerator.java

License:Apache License

/**
 * Generates {@link JdbcInputAdapter} class.
 * @param context the current context/*from www . j  a  va  2  s.c o  m*/
 * @param specs the target input specs
 * @param target the target class
 * @return the generated class data
 */
public static ClassData generate(ClassGeneratorContext context, List<Spec> specs, ClassDescription target) {
    Arguments.requireNonNull(context);
    Arguments.requireNonNull(specs);
    ClassWriter writer = newWriter(target, JdbcInputAdapter.class);
    defineAdapterConstructor(writer, JdbcInputAdapter.class, v -> {
        LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
        for (Spec spec : specs) {
            self.load(v);
            getConst(v, spec.id);
            getConst(v, spec.model.getProfileName());

            getConst(v, spec.model.getProfileName());
            getConst(v, spec.model.getTableName());
            getList(v, Lang.project(spec.model.getColumnMappings(), Tuple::left));
            getNew(v, getAdapter(context, spec));
            v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(WindGateJdbcDirect.class).getInternalName(), "input",
                    Type.getMethodDescriptor(TYPE_BUILDER, typeOf(String.class), // profileName
                            typeOf(String.class), // tableName
                            typeOf(List.class), // columnNames
                            typeOf(Supplier.class)), // adapter
                    false);
            Lang.forEach(Optionals.of(spec.model.getCondition()), s -> {
                getConst(v, s);
                v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_BUILDER.getInternalName(), "withCondition", //$NON-NLS-1$
                        Type.getMethodDescriptor(TYPE_BUILDER, typeOf(String.class)), false);
            });
            Lang.forEach(spec.model.getOptions(), s -> {
                getConst(v, s);
                v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_BUILDER.getInternalName(), "withOption", //$NON-NLS-1$
                        Type.getMethodDescriptor(TYPE_BUILDER, typeOf(String.class)), false);
            });
            v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_BUILDER.getInternalName(), "build", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(Function.class)), false);
            v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, target.getInternalName(), "input", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(JdbcInputAdapter.class), typeOf(String.class),
                            typeOf(String.class), typeOf(Function.class)),
                    false);
            v.visitInsn(Opcodes.POP);
        }
    });
    writer.visitEnd();
    return new ClassData(target, writer::toByteArray);
}

From source file:com.asakusafw.dag.compiler.jdbc.windgate.WindGateJdbcOutputProcessorGenerator.java

License:Apache License

private static void truncate(ClassGeneratorContext context, MethodVisitor v, Spec spec) {
    if (spec.initialize == false) {
        return;/*  w w  w  .j a  va2s.com*/
    }
    getConst(v, spec.id);
    getConst(v, spec.model.getProfileName());

    getConst(v, spec.model.getProfileName());
    getConst(v, spec.model.getTableName());
    getList(v, Lang.project(spec.model.getColumnMappings(), Tuple::left));
    v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(WindGateJdbcDirect.class).getInternalName(), "truncate",
            Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class), // profileName
                    typeOf(String.class), // tableName
                    typeOf(List.class)), // columnNames
            false);
    Lang.forEach(Optionals.of(spec.model.getCustomTruncate()), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "withCustomTruncate", //$NON-NLS-1$
                Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class)), false);
    });
    Lang.forEach(spec.model.getOptions(), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "withOption", //$NON-NLS-1$
                Type.getMethodDescriptor(TRUNCATE_BUILDER, typeOf(String.class)), false);
    });
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TRUNCATE_BUILDER.getInternalName(), "build", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(Function.class)), false);
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(JdbcOutputProcessor.class).getInternalName(), "initialize", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(JdbcOutputProcessor.class), typeOf(String.class),
                    typeOf(String.class), typeOf(Function.class)),
            false);
}

From source file:com.asakusafw.dag.compiler.jdbc.windgate.WindGateJdbcOutputProcessorGenerator.java

License:Apache License

private static void output(ClassGeneratorContext context, MethodVisitor v, Spec spec) {
    if (spec.output == false) {
        return;//from   w w  w  .  j  ava 2  s.com
    }
    getConst(v, spec.id);
    getConst(v, spec.model.getProfileName());

    getConst(v, spec.model.getProfileName());
    getConst(v, spec.model.getTableName());
    getList(v, Lang.project(spec.model.getColumnMappings(), Tuple::left));
    getNew(v, getAdapter(context, spec));
    v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(WindGateJdbcDirect.class).getInternalName(), "output",
            Type.getMethodDescriptor(OUTPUT_BUILDER, typeOf(String.class), // profileName
                    typeOf(String.class), // tableName
                    typeOf(List.class), // columnNames
                    typeOf(Supplier.class)), // adapter
            false);
    Lang.forEach(spec.model.getOptions(), s -> {
        getConst(v, s);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, OUTPUT_BUILDER.getInternalName(), "withOption", //$NON-NLS-1$
                Type.getMethodDescriptor(OUTPUT_BUILDER, typeOf(String.class)), false);
    });
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, OUTPUT_BUILDER.getInternalName(), "build", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(Function.class)), false);
    v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(JdbcOutputProcessor.class).getInternalName(), "output", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(JdbcOutputProcessor.class), typeOf(String.class),
                    typeOf(String.class), typeOf(Function.class)),
            false);
}

From source file:com.builtbroken.profiler.asm.WorldTransformer.java

/** {@link World#setBlock(int, int, int, Block, int, int)} */
private void injectSetBlock(ClassNode cn) {
    MethodNode setBlockMethod = getMethod(cn, "setBlock", "(IIIL" + getName(CLASS_KEY_BLOCK) + ";II)Z");

    if (setBlockMethod != null) {
        //Create method call
        final InsnList nodeAdd = new InsnList();

        nodeAdd.add(new VarInsnNode(Opcodes.ALOAD, 0));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 1));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
        nodeAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onBlockChange",
                "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));

        //Inject method call at top of method
        setBlockMethod.instructions.insertBefore(setBlockMethod.instructions.get(0), nodeAdd);

        //Locate all return points from the method
        List<AbstractInsnNode> returnNodes = new ArrayList();
        for (int i = 0; i < setBlockMethod.instructions.size(); i++) {
            AbstractInsnNode ain = setBlockMethod.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNodes.add(ain);//from   w  ww .  ja va 2s .co  m
            }
        }

        //Inject calls in front of return points
        for (AbstractInsnNode node : returnNodes) {
            //Create method call
            final InsnList nodeAdd2 = new InsnList();
            nodeAdd2.add(new VarInsnNode(Opcodes.ALOAD, 0));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 1));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 2));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 3));
            nodeAdd2.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onPostBlockChange",
                    "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));
            //Inject method call before return node
            setBlockMethod.instructions.insertBefore(node, nodeAdd2);
        }
    }
}