Example usage for org.objectweb.asm Opcodes ARETURN

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

Introduction

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

Prototype

int ARETURN

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

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./*from ww  w .java2 s.co  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.MasterJoinLikeOperatorGenerator.java

License:Apache License

private static void defineSelection(Context context, ClassWriter writer, UserOperator operator, FieldRef impl,
        Map<OperatorProperty, FieldRef> dependencies) {
    Method selector = Invariants.safe(() -> {
        return MasterJoinOperatorUtil.getSelection(context.getClassLoader(), operator);
    });//w w  w.  j  a v a 2 s  .co  m
    if (selector == null) {
        return;
    }
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "selectMaster",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(List.class), typeOf(Object.class)), null,
            null);
    cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType());

    List<ValueRef> arguments = new ArrayList<>();
    impl.load(method);
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 1));
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 2));
    arguments
            .addAll(Lang.project(getExtraViews(operator), v -> Invariants.requireNonNull(dependencies.get(v))));
    arguments
            .addAll(Lang.project(operator.getArguments(), v -> Invariants.requireNonNull(dependencies.get(v))));
    for (int i = 0, n = selector.getParameterCount(); i < n; i++) {
        arguments.get(i).load(method);
    }
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(selector.getDeclaringClass()).getInternalName(),
            selector.getName(), Type.getMethodDescriptor(selector), false);
    method.visitInsn(Opcodes.ARETURN);
    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 . j  a v  a2  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.codegen.AsmUtil.java

License:Apache License

/**
 * Adds constant {@code toString()} method.
 * @param writer the current writer/*from   w  w w  .  j a v  a2  s  .co m*/
 * @param value the constant value
 */
public static void defineToString(ClassVisitor writer, String value) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "toString",
            Type.getMethodDescriptor(typeOf(String.class)), null, null);
    getConst(method, value);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

private static void defineString(ClassWriter writer, String name, String value) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED, name,
            Type.getMethodDescriptor(typeOf(String.class)), null, new String[0]);
    getConst(method, value);/*from ww  w.  j  a  v a 2 s  . com*/
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

private static void putDeserialize(DataModelReference reference, List<PropertyReference> keys,
        List<PropertyReference> values, 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 keyInput = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef valueInput = new LocalVarRef(Opcodes.ALOAD, 2);
    self.load(v);/*w  ww  . j a  v  a 2 s.c  o  m*/
    getField(v, buffer);
    LocalVarRef object = putLocalVar(v, Type.OBJECT, 3);
    putDeserializeBody(v, keys, keyInput, object);
    putDeserializeBody(v, values, valueInput, object);
    object.load(v);
    v.visitInsn(Opcodes.ARETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

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

License:Apache License

private static void generateBody(MethodVisitor v, TypeDescription type, LocalVarRef target,
        LocalVarRef source) {//w  w  w .  ja v a2  s  . co  m
    target.load(v);
    source.load(v);
    copyDataModel(v, type);

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

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

License:Apache License

private static ClassData generate0(TypeDescription source, ClassDescription target) {
    ClassWriter writer = newWriter(target, Object.class, Supplier.class);
    defineEmptyConstructor(writer, Object.class);
    defineGetter(writer, typeOf(Object.class), "get", v -> {
        v.visitVarInsn(Opcodes.ALOAD, 0);
        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, target.getInternalName(), "get",
                Type.getMethodDescriptor(typeOf(source)), false);
        v.visitInsn(Opcodes.ARETURN);
    });//from w  w w  . j a va  2 s.co m
    defineGetter(writer, typeOf(source), "get", v -> {
        getNew(v, source);
        v.visitInsn(Opcodes.ARETURN);
    });
    writer.visitEnd();
    return new ClassData(target, writer::toByteArray);
}

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

License:Apache License

private static void putDeserialize(DataModelReference reference, FieldRef buffer, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(DataInput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    LocalVarRef self = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef input = new LocalVarRef(Opcodes.ALOAD, 1);

    self.load(v);//from  w  w  w  .jav a2s  .  c om
    getField(v, buffer);
    LocalVarRef object = putLocalVar(v, Type.OBJECT, 2);
    for (PropertyReference property : reference.getProperties()) {
        object.load(v);
        getOption(v, property);
        input.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.directio.OutputPatternSerDeGenerator.java

License:Apache License

private static void putGetProperty(ClassWriter writer, DataModelReference reference, OutputPattern pattern) {
    List<PropertyReference> properties = pattern.getResourcePattern().stream()
            .filter(s -> s.getKind() == OutputPattern.SourceKind.PROPERTY).map(CompiledSegment::getTarget)
            .collect(Collectors.toList());
    if (properties.isEmpty()) {
        return;//from w w  w  .j  a  va 2s .c om
    }
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "getProperty",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(Object.class), typeOf(int.class)), null,
            null);
    LocalVarRef object = cast(v, 1, reference.getDeclaration());
    LocalVarRef index = new LocalVarRef(Opcodes.ILOAD, 2);
    Label[] caseLabels = properties.stream().map(o -> new Label()).toArray(Label[]::new);
    Label defaultLabel = new Label();

    index.load(v);
    v.visitTableSwitchInsn(0, caseLabels.length - 1, defaultLabel, caseLabels);

    for (int i = 0, n = properties.size(); i < n; i++) {
        v.visitLabel(caseLabels[i]);
        PropertyReference property = properties.get(i);
        object.load(v);
        getOption(v, property);
        v.visitInsn(Opcodes.ARETURN);
    }
    v.visitLabel(defaultLabel);
    getNew(v, Descriptions.typeOf(AssertionError.class));
    v.visitInsn(Opcodes.ATHROW);

    v.visitMaxs(0, 0);
    v.visitEnd();
}