Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#box()

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#box() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteCodeUtils.java    From Stark with Apache License 2.0 7 votes vote down vote up
/**
 * Given an array on the stack, it loads it with the values of the given variables stating at
 * offset.
 */
static void loadVariableArray(
        @NonNull GeneratorAdapter mv,
        @NonNull List<LocalVariable> variables, int offset) {
    // we need to maintain the stack index when loading parameters from, as for long and double
    // values, it uses 2 stack elements, all others use only 1 stack element.
    for (int i = offset; i < variables.size(); i++) {
        LocalVariable variable = variables.get(i);
        // duplicate the array of objects reference, it will be used to store the value in.
        mv.dup();
        // index in the array of objects to store the boxed parameter.
        mv.push(i);
        // Pushes the appropriate local variable on the stack
        mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var);
        // potentially box up intrinsic types.
        mv.box(variable.type);
        // store it in the array
        mv.arrayStore(Type.getType(Object.class));
    }
}
 
Example 2
Source File: ByteCodeUtils.java    From Aceso with Apache License 2.0 6 votes vote down vote up
/**
 * Given an array on the stack, it loads it with the values of the given variables stating at
 * offset.
 */
static void loadVariableArray(
         GeneratorAdapter mv,
         List<LocalVariable> variables, int offset) {
    // we need to maintain the stack index when loading parameters from, as for long and double
    // values, it uses 2 stack elements, all others use only 1 stack element.
    for (int i = offset; i < variables.size(); i++) {
        LocalVariable variable = variables.get(i);
        // duplicate the array of objects reference, it will be used to store the value in.
        mv.dup();
        // index in the array of objects to store the boxed parameter.
        mv.push(i);
        // Pushes the appropriate local variable on the stack
        mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var);
        // potentially box up intrinsic types.
        mv.box(variable.type);
        // store it in the array
        mv.arrayStore(Type.getType(Object.class));
    }
}
 
Example 3
Source File: Redirection.java    From AnoleFix with MIT License 5 votes vote down vote up
/**
 * Adds the instructions to do a generic redirection.
 * <p/>
 * Note that the generated bytecode does not have a direct translation to code, but as an
 * example, the following code block gets inserted.
 * <code>
 * if ($change != null) {
 * $change.access$dispatch($name, new object[] { arg0, ... argsN })
 * $anyCodeInsertedbyRestore
 * }
 * $originalMethodBody
 * </code>
 *
 * @param mv     the method visitor to add the instructions to.
 * @param change the local variable containing the alternate implementation.
 * @param args   the type of the local variable that need to be forwarded.
 */
void redirect(GeneratorAdapter mv, int change, List<Type> args) {
    // code to check if a new implementation of the current class is available.
    Label l0 = new Label();
    mv.loadLocal(change);
    mv.visitJumpInsn(Opcodes.IFNULL, l0);
    mv.loadLocal(change);
    mv.push(name);

    // create an array of objects capable of containing all the parameters and optionally the "this"
    createLocals(mv, args);

    // we need to maintain the stack index when loading parameters from, as for long and double
    // values, it uses 2 stack elements, all others use only 1 stack element.
    int stackIndex = 0;
    for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) {
        Type arg = args.get(arrayIndex);
        // duplicate the array of objects reference, it will be used to store the value in.
        mv.dup();
        // index in the array of objects to store the boxed parameter.
        mv.push(arrayIndex);
        // Pushes the appropriate local variable on the stack
        redirectLocal(mv, stackIndex, arg);
        // potentially box up intrinsic types.
        mv.box(arg);
        mv.arrayStore(Type.getType(Object.class));
        // stack index must progress according to the parameter type we just processed.
        stackIndex += arg.getSize();
    }

    // now invoke the generic dispatch method.
    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])"));

    // Restore the state after the redirection
    restore(mv, args);
    // jump label for classes without any new implementation, just invoke the original
    // method implementation.
    mv.visitLabel(l0);
}
 
Example 4
Source File: TestThreadExecutionGenerator.java    From lin-check with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void generateRun(ClassVisitor cv, Type testType, int iThread, List<Actor> actors, List<Object> objArgs, boolean waitsEnabled) {
    int access = ACC_PUBLIC;
    Method m = new Method("call", RESULT_ARRAY_TYPE, NO_ARGS);
    GeneratorAdapter mv = new GeneratorAdapter(access, m,
        // Try-catch blocks sorting is required
        new TryCatchBlockSorter(cv.visitMethod(access, m.getName(), m.getDescriptor(), null, null),
            access, m.getName(), m.getDescriptor(), null, null)
    );
    mv.visitCode();
    // Create Result[] array and store it to a local variable
    int resLocal = createResultArray(mv, actors.size());
    // Call runner's onStart(iThread) method
    mv.loadThis();
    mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE);
    mv.push(iThread);
    mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_START_METHOD);
    // Number of current operation (starts with 0)
    int iLocal = mv.newLocal(Type.INT_TYPE);
    mv.push(0);
    mv.storeLocal(iLocal);
    // Invoke actors
    for (int i = 0; i < actors.size(); i++) {
        Actor actor = actors.get(i);
        // Add busy-wait before operation execution (for non-first operations only)
        if (waitsEnabled && i > 0) {
            mv.loadThis();
            mv.getField(TEST_THREAD_EXECUTION_TYPE, "waits", INT_ARRAY_TYPE);
            mv.push(i - 1);
            mv.arrayLoad(Type.INT_TYPE);
            mv.invokeStatic(UTILS_TYPE, UTILS_CONSUME_CPU);
        }
        // Start of try-catch block for exceptions which this actor should handle
        Label start, end = null, handler = null, handlerEnd = null;
        if (actor.handlesExceptions()) {
            start = mv.newLabel();
            end = mv.newLabel();
            handler = mv.newLabel();
            handlerEnd = mv.newLabel();
            for (Class<? extends Throwable> ec : actor.handledExceptions)
                mv.visitTryCatchBlock(start, end, handler, Type.getType(ec).getInternalName());
            mv.visitLabel(start);
        }
        // Load result array and index to store the current result
        mv.loadLocal(resLocal);
        mv.push(i);
        // Load test instance
        mv.loadThis();
        mv.getField(TEST_THREAD_EXECUTION_TYPE, "testInstance", OBJECT_TYPE);
        mv.checkCast(testType);
        // Load arguments for operation
        for (int j = 0; j < actor.arguments.length; j++) {
            pushArgumentOnStack(mv, objArgs, actor.arguments[j], actor.method.getParameterTypes()[j]);
        }
        // Invoke operation
        Method actorMethod = Method.getMethod(actor.method);
        mv.invokeVirtual(testType, actorMethod);
        // Create result
        mv.box(actorMethod.getReturnType()); // box if needed
        if (actor.method.getReturnType() == void.class) {
            mv.pop();
            mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VOID_RESULT);
        } else {
            mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VALUE_RESULT);
        }
        // Store result to array
        mv.arrayStore(RESULT_TYPE);
        // End of try-catch block
        if (actor.handlesExceptions()) {
            mv.visitLabel(end);
            mv.goTo(handlerEnd);
            mv.visitLabel(handler);
            storeExceptionResultFromThrowable(mv, resLocal, iLocal);
            mv.visitLabel(handlerEnd);
        }
        // Increment number of current operation
        mv.iinc(iLocal, 1);
    }
    // Call runner's onFinish(iThread) method
    mv.loadThis();
    mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE);
    mv.push(iThread);
    mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_FINISH_METHOD);
    // Return results
    mv.loadThis();
    mv.loadLocal(resLocal);
    mv.returnValue();
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}