Example usage for org.objectweb.asm Opcodes RETURN

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

Introduction

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

Prototype

int RETURN

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

Click Source Link

Usage

From source file:com.android.tools.lint.checks.WakelockDetector.java

License:Apache License

/** Search from the given node towards the target; return false if we reach
 * an exit point such as a return or a call on the way there that is not within
 * a try/catch clause.//w  w w  .j  a  v a2 s.  c o  m
 *
 * @param node the current node
 * @return true if the target was reached
 *    XXX RETURN VALUES ARE WRONG AS OF RIGHT NOW
 */
protected int dfs(ControlFlowGraph.Node node) {
    AbstractInsnNode instruction = node.instruction;
    if (instruction.getType() == AbstractInsnNode.JUMP_INSN) {
        int opcode = instruction.getOpcode();
        if (opcode == Opcodes.RETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.LRETURN
                || opcode == Opcodes.IRETURN || opcode == Opcodes.DRETURN || opcode == Opcodes.FRETURN
                || opcode == Opcodes.ATHROW) {
            if (DEBUG) {
                System.out.println("Found exit via explicit return: " //$NON-NLS-1$
                        + node.toString(false));
            }
            return SEEN_RETURN;
        }
    }

    if (!DEBUG) {
        // There are no cycles, so no *NEED* for this, though it does avoid
        // researching shared labels. However, it makes debugging harder (no re-entry)
        // so this is only done when debugging is off
        if (node.visit != 0) {
            return 0;
        }
        node.visit = 1;
    }

    // Look for the target. This is any method call node which is a release on the
    // lock (later also check it's the same instance, though that's harder).
    // This is because finally blocks tend to be inlined so from a single try/catch/finally
    // with a release() in the finally, the bytecode can contain multiple repeated
    // (inlined) release() calls.
    if (instruction.getType() == AbstractInsnNode.METHOD_INSN) {
        MethodInsnNode method = (MethodInsnNode) instruction;
        if (method.name.equals(RELEASE_METHOD) && method.owner.equals(WAKELOCK_OWNER)) {
            return SEEN_TARGET;
        } else if (method.name.equals(ACQUIRE_METHOD) && method.owner.equals(WAKELOCK_OWNER)) {
            // OK
        } else if (method.name.equals(IS_HELD_METHOD) && method.owner.equals(WAKELOCK_OWNER)) {
            // OK
        } else {
            // Some non acquire/release method call: if this is not associated with a
            // try-catch block, it would mean the exception would exit the method,
            // which would be an error
            if (node.exceptions == null || node.exceptions.isEmpty()) {
                // Look up the corresponding frame, if any
                AbstractInsnNode curr = method.getPrevious();
                boolean foundFrame = false;
                while (curr != null) {
                    if (curr.getType() == AbstractInsnNode.FRAME) {
                        foundFrame = true;
                        break;
                    }
                    curr = curr.getPrevious();
                }

                if (!foundFrame) {
                    if (DEBUG) {
                        System.out.println("Found exit via unguarded method call: " //$NON-NLS-1$
                                + node.toString(false));
                    }
                    return SEEN_RETURN;
                }
            }
        }
    }

    // if (node.instruction is a call, and the call is not caught by
    // a try/catch block (provided the release is not inside the try/catch block)
    // then return false
    int status = 0;

    boolean implicitReturn = true;
    List<Node> successors = node.successors;
    List<Node> exceptions = node.exceptions;
    if (exceptions != null) {
        if (!exceptions.isEmpty()) {
            implicitReturn = false;
        }
        for (Node successor : exceptions) {
            status = dfs(successor) | status;
            if ((status & SEEN_RETURN) != 0) {
                if (DEBUG) {
                    System.out.println("Found exit via exception: " //$NON-NLS-1$
                            + node.toString(false));
                }
                return status;
            }
        }

        if (status != 0) {
            status |= SEEN_EXCEPTION;
        }
    }

    if (successors != null) {
        if (!successors.isEmpty()) {
            implicitReturn = false;
            if (successors.size() > 1) {
                status |= SEEN_BRANCH;
            }
        }
        for (Node successor : successors) {
            status = dfs(successor) | status;
            if ((status & SEEN_RETURN) != 0) {
                if (DEBUG) {
                    System.out.println("Found exit via branches: " //$NON-NLS-1$
                            + node.toString(false));
                }
                return status;
            }
        }
    }

    if (implicitReturn) {
        status |= SEEN_RETURN;
        if (DEBUG) {
            System.out.println("Found exit: via implicit return: " //$NON-NLS-1$
                    + node.toString(false));
        }
    }

    return status;
}

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

License:Apache License

private ClassDescription generateCombinerClass(Context context, UserOperator operator, ClassDescription outer) {
    ClassDescription target = getCombinerName(outer);
    OperatorInput input = operator.getInput(0);

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

    FieldRef impl = defineOperatorField(writer, operator, target);
    defineEmptyConstructor(writer, Object.class, method -> {
        setOperatorField(method, operator, impl);
    });//  www  .  j a  va 2  s.co m
    defineBuildKey(context, writer, input.getDataType(), input.getGroup());

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

    List<ValueRef> arguments = new ArrayList<>();
    arguments.add(impl);
    arguments.add(m -> {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitTypeInsn(Opcodes.CHECKCAST, typeOf(input.getDataType()).getInternalName());
    });
    arguments.add(m -> {
        m.visitVarInsn(Opcodes.ALOAD, 2);
        m.visitTypeInsn(Opcodes.CHECKCAST, typeOf(input.getDataType()).getInternalName());
    });
    for (VertexElement dep : context.getDependencies(operator.getArguments())) {
        Invariants.require(dep.getElementKind() == ElementKind.VALUE);
        ValueDescription value = ((ValueElement) dep).getValue();
        arguments.add(m -> {
            getConst(method, Invariants.safe(() -> value.resolve(context.getClassLoader())));
        });
    }
    invoke(method, context, operator, arguments);
    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.FoldOperatorGenerator.java

License:Apache License

private void defineSimpleStart(UserOperator operator, ClassWriter writer, FieldRef acc) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "start",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class)), null, null);

    TypeDescription dataType = operator.getInput(Fold.ID_INPUT).getDataType();

    acc.load(method);//from  w w  w.  j ava  2  s. com
    method.visitVarInsn(Opcodes.ALOAD, 1);
    method.visitTypeInsn(Opcodes.CHECKCAST, typeOf(dataType).getInternalName());
    copyDataModel(method, dataType);

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

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

License:Apache License

private void defineSimpleCombine(Context context, UserOperator operator, ClassWriter writer, FieldRef impl,
        FieldRef acc, Map<OperatorProperty, FieldRef> map) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "combine",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class)), null, null);

    TypeDescription dataType = operator.getInput(Fold.ID_INPUT).getDataType();
    LocalVarRef object = cast(method, 1, dataType);

    List<ValueRef> arguments = new ArrayList<>();
    arguments.add(impl);//  w  w  w.  j a v  a2 s  . co m
    arguments.add(acc);
    arguments.add(object);
    appendSecondaryInputs(arguments::add, operator, map::get);
    appendArguments(arguments::add, operator, map::get);
    invoke(method, context, operator, arguments);

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

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

License:Apache License

private void defineSimpleFinish(ClassWriter writer, FieldRef acc, ValueRef result) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "finish",
            Type.getMethodDescriptor(typeOf(void.class)), null, null);

    result.load(method);//  w  w  w  .  j  a  v a2 s  .  c om
    acc.load(method);
    invokeResultAdd(method);

    method.visitInsn(Opcodes.RETURN);
    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);//from   w w  w.  java2 s  . c  om
    method.visitEnd();
}

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. java 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);
    }/* w  ww .ja  va  2  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) {//from w  ww .j a  v  a2s  .  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.// w  w  w.j  a va2s  .  com
 * @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();
}