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

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#arrayStore() . 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: RobustAsmUtils.java    From Robust with Apache License 2.0 6 votes vote down vote up
private static void createClassArray(GeneratorAdapter mv, List<Type> args) {
        // 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, arg);
//			 mv.visitLdcInsn(Type.getType(arg.getDescriptor()));
            // potentially box up intrinsic types.
//			 mv.box(arg);
            mv.arrayStore(Type.getType(Class.class));
            // stack index must progress according to the parameter type we just processed.
//			 stackIndex += arg.getSize();
        }
    }
 
Example 3
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 4
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Generates:
 * <pre>
 * Method method =
 *      method.getDeclaringClass().getMethod("methodName", new Class[] { args... });
 * </pre>
 * @param mg
 * @param method
 * @param methodType
 */
private static void loadCurrentMethod(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType)
{
    mg.push(Type.getType(method.getDeclaringClass()));
    mg.push(method.getName());

    // create the Class[]
    mg.push(methodType.getArgumentTypes().length);
    mg.newArray(TYPE_CLASS);

    // push parameters into array
    for (int i = 0; i < methodType.getArgumentTypes().length; i++)
    {
        // keep copy of array on stack
        mg.dup();

        // push index onto stack
        mg.push(i);
        mg.push(methodType.getArgumentTypes()[i]);
        mg.arrayStore(TYPE_CLASS);
    }

    // invoke getMethod() with the method name and the array of types
    mg.invokeVirtual(TYPE_CLASS, Method.getMethod("java.lang.reflect.Method getDeclaredMethod(String, Class[])"));
}
 
Example 5
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Defines a new Object[] and push all method argmuments into the array.
 *
 * @param mg
 * @param method
 * @param methodType
 */
private static void loadArguments(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType)
{
    // create the Object[]
    mg.push(methodType.getArgumentTypes().length);
    mg.newArray(TYPE_OBJECT);

    // push parameters into array
    for (int i = 0; i < methodType.getArgumentTypes().length; i++)
    {
        // keep copy of array on stack
        mg.dup();

        // push index onto stack
        mg.push(i);

        mg.loadArg(i);
        mg.valueOf(methodType.getArgumentTypes()[i]);
        mg.arrayStore(TYPE_OBJECT);
    }
}
 
Example 6
Source File: StringSwitch.java    From Stark with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a standard error exception with message similar to:
 *
 *    String switch could not find 'equals.(Ljava/lang/Object;)Z' with hashcode 0
 *    in com/example/basic/GrandChild
 *
 * @param mv The generator adaptor used to emit the lookup switch code.
 * @param visitedClassName The abstract string trie structure.
 */
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(STARK_RELOAD_EXCEPTION_TYPE);
    mv.dup();
    mv.push("String switch could not find '%s' with hashcode %s in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitString();
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(1);
    visitString();
    visitHashMethod(mv);
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/Integer",
            "valueOf",
            "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/String",
            "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(STARK_RELOAD_EXCEPTION_TYPE,
            Method.getMethod("void <init> (String)"));
    mv.throwException();
}
 
Example 7
Source File: IntSwitch.java    From Aceso with Apache License 2.0 5 votes vote down vote up
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
    mv.dup();
    mv.push("int switch could not find %d in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitInt();
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/Integer",
            "valueOf",
            "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/String",
            "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE,
            Method.getMethod("void <init> (String)"));
    mv.throwException();
}
 
Example 8
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 9
Source File: StringSwitch.java    From AnoleFix with MIT License 5 votes vote down vote up
/**
 * Generates a standard error exception with message similar to:
 *
 *    String switch could not find 'equals.(Ljava/lang/Object;)Z' with hashcode 0
 *    in com/example/basic/GrandChild
 *
 * @param mv The generator adaptor used to emit the lookup switch code.
 * @param visitedClassName The abstract string trie structure.
 */
void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) {
    mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE);
    mv.dup();
    mv.push("String switch could not find '%s' with hashcode %s in %s");
    mv.push(3);
    mv.newArray(OBJECT_TYPE);
    mv.dup();
    mv.push(0);
    visitString();
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(1);
    visitString();
    visitHashMethod(mv);
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/Integer",
            "valueOf",
            "(I)Ljava/lang/Integer;", false);
    mv.arrayStore(OBJECT_TYPE);
    mv.dup();
    mv.push(2);
    mv.push(visitedClassName);
    mv.arrayStore(OBJECT_TYPE);
    mv.visitMethodInsn(
            Opcodes.INVOKESTATIC,
            "java/lang/String",
            "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false);
    mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE,
            Method.getMethod("void <init> (String)"));
    mv.throwException();
}
 
Example 10
Source File: TestThreadExecutionGenerator.java    From lin-check with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void storeExceptionResultFromThrowable(GeneratorAdapter mv, int resLocal, int iLocal) {
    int eLocal = mv.newLocal(THROWABLE_TYPE);
    mv.storeLocal(eLocal);
    mv.loadLocal(resLocal);
    mv.loadLocal(iLocal);
    mv.loadLocal(eLocal);
    mv.invokeVirtual(OBJECT_TYPE, OBJECT_GET_CLASS);
    mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_EXCEPTION_RESULT);
    mv.arrayStore(RESULT_TYPE);
}
 
Example 11
Source File: ExpressionArraySet.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	Type arrayType = array.load(ctx);
	position.load(ctx);
	newElement.load(ctx);
	g.arrayStore(getType(arrayType.getDescriptor().substring(1)));
	return Type.VOID_TYPE;
}
 
Example 12
Source File: ConstructorRedirection.java    From Stark with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRedirect(GeneratorAdapter mv, int change) {
    mv.loadLocal(change);
    mv.push("init$args." + constructor.args.desc);

    Type arrayType = Type.getType("[Ljava/lang/Object;");
    // init$args args (including this) + locals
    mv.push(types.size() + 1);
    mv.newArray(Type.getType(Object.class));

    int array = mv.newLocal(arrayType);
    mv.dup();
    mv.storeLocal(array);

    // "this" is not ready yet, use null instead.
    mv.dup();
    mv.push(0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.arrayStore(Type.getType(Object.class));

    // Set the arguments in positions 1..(n-1);
    ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value

    // Add the locals array at the last position.
    mv.dup();
    // The index of the last position of the array.
    mv.push(types.size());
    // Create the array with all the local variables declared up to this point.
    ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis));
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(MonitorVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    //// At this point, init$args has been called and the result Object is on the stack.
    //// The value of that Object is Object[] with exactly n + 2 elements.
    //// The first element is the resulting local variables
    //// The second element is a string with the qualified name of the constructor to call.
    //// The remaining elements are the constructor arguments.

    // Keep a reference to the new locals array
    mv.dup();
    mv.push(0);
    mv.arrayLoad(Type.getType("[Ljava/lang/Object;"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    mv.storeLocal(array);

    // Call super constructor
    // Put this behind the returned array
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.swap();
    // Push a null for the marker parameter.
    mv.visitInsn(Opcodes.ACONST_NULL);
    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    // Dispatch to init$body
    mv.loadLocal(change);
    mv.push("init$body." + constructor.body.desc);
    mv.loadLocal(array);

    // Now "this" can be set
    mv.dup();
    mv.push(0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(MonitorVisitor.CHANGE_TYPE, Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.pop();
}
 
Example 13
Source File: TBConstructorRedirection.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRedirect(GeneratorAdapter mv, int change) {
    mv.loadLocal(change);
    mv.push("init$args." + constructor.args.desc);

    Type arrayType = Type.getType("[Ljava/lang/Object;");
    // init$args args (including this) + locals
    mv.push(types.size() + 1);
    mv.newArray(Type.getType(Object.class));

    int array = mv.newLocal(arrayType);
    mv.dup();
    mv.storeLocal(array);

    // "this" is not ready yet, use null instead.
    mv.dup();
    mv.push(0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.arrayStore(Type.getType(Object.class));

    // Set the arguments in positions 1..(n-1);
    ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value

    // Add the locals array at the last position.
    mv.dup();
    // The index of the last position of the array.
    mv.push(types.size());
    // Create the array with all the local variables declared up to this point.
    ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis));
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(TBIncrementalVisitor.ALI_CHANGE_TYPE, Method.getMethod("Object ipc$dispatch(String, Object[])"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    //// At this point, init$args has been called and the result Object is on the stack.
    //// The value of that Object is Object[] with exactly n + 2 elements.
    //// The first element is the resulting local variables
    //// The second element is a string with the qualified name of the constructor to call.
    //// The remaining elements are the constructor arguments.

    // Keep a reference to the new locals array
    mv.dup();
    mv.push(0);
    mv.arrayLoad(Type.getType("[Ljava/lang/Object;"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    mv.storeLocal(array);

    // Call super constructor
    // Put this behind the returned array
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.swap();
    // Push a null for the marker parameter.
    mv.visitInsn(Opcodes.ACONST_NULL);
    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    // Dispatch to init$body
    mv.loadLocal(change);
    mv.push("init$body." + constructor.body.desc);
    mv.loadLocal(array);

    // Now "this" can be set
    mv.dup();
    mv.push(0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(TBIncrementalVisitor.ALI_CHANGE_TYPE, Method.getMethod("Object ipc$dispatch(String, Object[])"));
    mv.pop();
}
 
Example 14
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();
}