Example usage for org.objectweb.asm Opcodes ALOAD

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

Introduction

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

Prototype

int ALOAD

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

Click Source Link

Usage

From source file:com.asakusafw.dag.compiler.internalio.InternalOutputAdapterGenerator.java

License:Apache License

/**
 * Generates {@link InternalOutputAdapter} class.
 * @param context the current context//from   ww  w  .  j  a v a  2 s.com
 * @param outputs the target output specs
 * @param target the target class
 * @return the generated class data
 */
public ClassData generate(ClassGeneratorContext context, List<Spec> outputs, ClassDescription target) {
    ClassWriter writer = newWriter(target, InternalOutputAdapter.class);
    defineAdapterConstructor(writer, InternalOutputAdapter.class, v -> {
        for (Spec spec : outputs) {
            v.visitVarInsn(Opcodes.ALOAD, 0);
            getConst(v, spec.id);
            getConst(v, spec.path);
            getConst(v, spec.dataType);
            v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, target.getInternalName(), "bind", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(InternalOutputAdapter.class), typeOf(String.class),
                            typeOf(String.class), typeOf(Class.class)),
                    false);
            v.visitInsn(Opcodes.POP);
        }
    });
    return new ClassData(target, writer::toByteArray);
}

From source file:com.asakusafw.dag.compiler.internalio.InternalOutputPrepareGenerator.java

License:Apache License

/**
 * Generates {@link InternalOutputPrepare} class.
 * @param context the current context/*from www .  j  ava2  s  .  c  om*/
 * @param specs the target output specs
 * @param target the target class
 * @return the generated class data
 */
public ClassData generate(ClassGeneratorContext context, List<Spec> specs, ClassDescription target) {
    ClassWriter writer = newWriter(target, InternalOutputPrepare.class);
    defineEmptyConstructor(writer, InternalOutputPrepare.class, v -> {
        LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
        for (Spec spec : specs) {
            self.load(v);
            getConst(v, spec.id);
            getConst(v, spec.path);
            getConst(v, spec.dataType);
            v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, target.getInternalName(), "bind", //$NON-NLS-1$
                    Type.getMethodDescriptor(typeOf(InternalOutputPrepare.class), typeOf(String.class),
                            typeOf(String.class), typeOf(Class.class)),
                    false);
            v.visitInsn(Opcodes.POP);
        }
    });
    return new ClassData(target, writer::toByteArray);
}

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  ww  w.jav 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 defineBody(ClassWriter writer, DataModelReference dataType,
        List<PropertyReference> properties, Optional<FieldRef> dateBuf) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "drive", //$NON-NLS-1$
            Type.getMethodDescriptor(typeOf(void.class), typeOf(PreparedStatement.class), typeOf(Object.class)),
            null, new String[] { typeOf(SQLException.class).getInternalName(), });
    LocalVarRef row = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef object = cast(v, 2, dataType.getDeclaration());

    int columnIndex = 0;
    for (PropertyReference property : properties) {
        columnIndex++;/*from w  ww. j ava  2 s  .c  o  m*/

        object.load(v);
        getOption(v, property);
        LocalVarRef option = putLocalVar(v, Type.OBJECT, 3);

        Label elseIf = new Label();
        Label endIf = new Label();

        // if (option.isNull()) {
        option.load(v);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(ValueOption.class).getInternalName(), "isNull", //$NON-NLS-1$
                Type.getMethodDescriptor(typeOf(boolean.class)), false);
        v.visitJumpInsn(Opcodes.IFEQ, elseIf);

        row.load(v);
        getConst(v, columnIndex);
        doSetNull(v, property);

        v.visitJumpInsn(Opcodes.GOTO, endIf);

        // } else { @elseIf
        v.visitLabel(elseIf);

        row.load(v);
        getConst(v, columnIndex);
        option.load(v);
        doSetValue(v, property, dateBuf);

        // } @endIf
        v.visitLabel(endIf);
    }

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

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);/*  w w w.j  av  a2s.com*/
        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 defineBody(ClassWriter writer, DataModelReference dataType,
        List<PropertyReference> properties, FieldRef valueBuffer, Optional<FieldRef> calendarBuf) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "extract",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(ResultSet.class)), null,
            new String[] { typeOf(SQLException.class).getInternalName(), });
    LocalVarRef rs = new LocalVarRef(Opcodes.ALOAD, 1);
    valueBuffer.load(v);//from w w  w .j  a  va  2 s .  c om
    LocalVarRef buf = putLocalVar(v, Type.OBJECT, 2);

    int columnIndex = 0;
    Set<PropertyName> sawSet = new HashSet<>();
    for (PropertyReference property : properties) {
        columnIndex++;
        Label elseIf = new Label();
        Label endIf = new Label();

        buf.load(v);
        getOption(v, property);

        rs.load(v);
        getConst(v, columnIndex);
        doGetValue(v, property, calendarBuf);

        // if (rs.wasNull()) {
        rs.load(v);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "wasNull",
                Type.getMethodDescriptor(typeOf(boolean.class)), true);
        v.visitJumpInsn(Opcodes.IFEQ, elseIf);
        if (isWideType(property)) {
            v.visitInsn(Opcodes.POP2);
        } else {
            v.visitInsn(Opcodes.POP);
        }
        doSetNull(v);
        v.visitJumpInsn(Opcodes.GOTO, endIf);

        // } else { @elseIf
        v.visitLabel(elseIf);
        doSetValue(v, property);

        // } @endIf
        v.visitLabel(endIf);

        sawSet.add(property.getName());
    }

    // resets other properties
    for (PropertyReference property : dataType.getProperties()) {
        if (sawSet.contains(property.getName())) {
            continue;
        }
        buf.load(v);
        getOption(v, property);
        doSetNull(v);
    }

    buf.load(v);
    v.visitInsn(Opcodes.ARETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

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

License:Apache License

/**
 * Generates {@link JdbcInputAdapter} class.
 * @param context the current context/*w w w  . j a v  a2s .  co 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

/**
 * Generates {@link JdbcOutputProcessor} class.
 * @param context the current context//from   w  w w.  j a  va2s  .c o m
 * @param specs the target operation 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, JdbcOutputProcessor.class);
    defineEmptyConstructor(writer, JdbcOutputProcessor.class, v -> {
        LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
        self.load(v);
        specs.forEach(spec -> {
            truncate(context, v, spec);
            output(context, v, spec);
        });
        v.visitInsn(Opcodes.POP);
    });
    writer.visitEnd();
    return new ClassData(target, writer::toByteArray);
}

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  ww w. ja  v a 2  s .  c  o  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);
        }
    }
}

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

/** {@link World#setBlockMetadataWithNotify(int, int, int, int, int)} */
private void injectSetBlockWithMeta(ClassNode cn) {
    MethodNode setBlockMetaMethod = getMethod(cn, "setBlockMetadataWithNotify", "(IIIII)Z");

    if (setBlockMetaMethod != null) {
        final InsnList nodeAdd = new InsnList();

        nodeAdd.add(new VarInsnNode(Opcodes.ALOAD, 0)); //this
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 1)); //x
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 2)); //y
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 3)); //z
        nodeAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onBlockMetaChange",
                "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));

        setBlockMetaMethod.instructions.insertBefore(setBlockMetaMethod.instructions.get(0), nodeAdd);

        //Locate all return points from the method
        List<AbstractInsnNode> returnNodes = new ArrayList();
        for (int i = 0; i < setBlockMetaMethod.instructions.size(); i++) {
            AbstractInsnNode ain = setBlockMetaMethod.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNodes.add(ain);/*from  w  ww  .  j  ava2  s  .  c o 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, "onPostBlockMetaChange",
                    "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));
            //Inject method call before return node
            setBlockMetaMethod.instructions.insertBefore(node, nodeAdd2);
        }
    }
}