Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

From source file:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

private static InsnList throwExceptionsList(MethodNode methodNode, ClassNode classNode) {
    try {//from w w w . j  a v  a 2 s . c om
        String runtimeException = Type.getInternalName(RuntimeException.class);
        Constructor<RuntimeException> constructor = RuntimeException.class.getConstructor(String.class);

        InsnList instructions = new InsnList();
        instructions.add(new TypeInsnNode(Opcodes.NEW, runtimeException));
        instructions.add(new InsnNode(Opcodes.DUP));

        String className = classNode.name.replace('/', '.');
        instructions.add(new LdcInsnNode("Method " + methodNode.name + " in " + className + " not mocked. "
                + "See http://g.co/androidstudio/not-mocked for details."));
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, runtimeException, CONSTRUCTOR,
                Type.getType(constructor).getDescriptor(), false));
        instructions.add(new InsnNode(Opcodes.ATHROW));

        return instructions;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.android.mkstubs.stubber.MethodStubber.java

License:Apache License

@Override
public void visitCode() {
    Label l0 = new Label();
    mv.visitLabel(l0);//  w ww .j  av a2s . c om
    mv.visitLineNumber(36, l0);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("stub");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, // opcode
            "java/lang/RuntimeException", // owner
            "<init>", // name
            "(Ljava/lang/String;)V", // desc
            false);
    mv.visitInsn(Opcodes.ATHROW);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", // name
            "Lcom/android/mkstubs/stubber/MethodStubber;", // desc
            null, // signature
            l0, // label start
            l1, // label end
            0); // index
    mv.visitMaxs(3, 1); // maxStack, maxLocals
}

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.ja 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   w  ww  .  j av a 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.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an array on to the top of the stack.
 * @param method the current method visitor
 * @param elementType the element type//from w ww .  ja v a  2  s. c o m
 * @param values the array elements
 * @since 0.4.1
 */
public static void getArray(MethodVisitor method, Type elementType, Object[] values) {
    getInt(method, values.length);
    method.visitTypeInsn(Opcodes.ANEWARRAY, elementType.getInternalName());
    for (int index = 0; index < values.length; index++) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index);
        getConst(method, values[index]);
        method.visitInsn(Opcodes.AASTORE);
    }
}

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

License:Apache License

/**
 * Adds an array on to the top of the stack.
 * @param method the current method visitor
 * @param elementType the element type/*ww w. ja  v  a 2s  .  c  om*/
 * @param values the array elements
 * @since 0.4.1
 */
public static void getArray(MethodVisitor method, Type elementType, LocalVarRef[] values) {
    getInt(method, values.length);
    method.visitTypeInsn(Opcodes.ANEWARRAY, elementType.getInternalName());
    for (int index = 0; index < values.length; index++) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index);
        values[index].load(method);
        method.visitInsn(Opcodes.AASTORE);
    }
}

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

License:Apache License

/**
 * Adds a value list on to the top of the stack.
 * @param method the current method visitor
 * @param values the array elements//from w w w.j  a v  a 2 s .  c o  m
 */
public static void getList(MethodVisitor method, Collection<?> values) {
    getInt(method, values.size());
    method.visitTypeInsn(Opcodes.ANEWARRAY, typeOf(Object.class).getInternalName());
    int index = 0;
    for (Object value : values) {
        method.visitInsn(Opcodes.DUP);
        getInt(method, index++);
        getConst(method, value);
        method.visitInsn(Opcodes.AASTORE);
    }
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(Arrays.class).getInternalName(), "asList",
            Type.getMethodDescriptor(typeOf(List.class), typeOf(Object[].class)), false);
}

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

License:Apache License

/**
 * Adds an new instance of the target type on to the top of the stack.
 * @param method the current method visitor
 * @param type the target type/*w ww. ja  va2 s.c om*/
 */
public static void getNew(MethodVisitor method, TypeDescription type) {
    Type t = typeOf(type);
    method.visitTypeInsn(Opcodes.NEW, t.getInternalName());
    method.visitInsn(Opcodes.DUP);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, t.getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), false);
}

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

License:Apache License

private static void getClass(MethodVisitor method, ClassDescription target, ClassNode element,
        Function<VertexElement, String> ids) {
    method.visitTypeInsn(Opcodes.NEW, element.getImplementationType().getInternalName());
    method.visitInsn(Opcodes.DUP);
    List<Type> parameterTypes = new ArrayList<>();
    for (VertexElement dep : element.getDependencies()) {
        parameterTypes.add(typeOf(dep.getRuntimeType()));
        get(method, target, dep, ids);/* ww w. j  ava  2  s  .c o m*/
    }
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, element.getImplementationType().getInternalName(),
            CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, parameterTypes.stream().toArray(Type[]::new)), false);
}

From source file:com.axway.jmb.JMessageBuilderVisitorImpl.java

License:Open Source License

@Override
public Void visitVariableIdentifier(VariableIdentifierContext ctx) {
    super.visitVariableIdentifier(ctx);

    if (isStatementCall) {
        try {// w  w  w.ja v a  2s. c o  m
            debug("visitVariableIdentifier() for statementCallParameter");
            String varName = convertVariableName(ctx.getText());
            if (currentMethod != null) {
                currentMethod.visitInsn(Opcodes.DUP);
                currentMethod.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);

                if (currentMethod.isLocalVariableDefined(varName)) {
                    currentMethod.loadFromLocalVar(varName, false, 0);
                } else if (currentModule.isFieldDefined(varName)) {
                    currentMethod.loadFromField(currentModule, varName, false, 0);
                } else {
                    throw new CompileException("Variable " + varName + " used, but not defined.");
                }

                currentMethod.visitInsn(Opcodes.AASTORE);
            } else {
                currentConstructor.visitInsn(Opcodes.DUP);
                currentConstructor.visitInsn(Opcodes.ICONST_0 + statementCallCurrentParameterIndex);

                if (currentConstructor.isLocalVariableDefined(varName)) {
                    currentConstructor.loadFromLocalVar(varName, false, 0);
                } else if (currentModule.isFieldDefined(varName)) {
                    currentConstructor.loadFromField(currentModule, varName, false, 0);
                } else {
                    throw new CompileException("Variable " + varName + " used, but not defined.");
                }

                currentConstructor.visitInsn(Opcodes.AASTORE);
            }

            statementCallCurrentParameterIndex++;
        } catch (CompileException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    return null;
}