Example usage for org.objectweb.asm Opcodes ALOAD

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

Introduction

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

Prototype

int ALOAD

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

Click Source Link

Usage

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilder.java

License:Open Source License

private static int createInstructions(final int param, final FieldNode field, final ClassNode node,
        final InsnList instructions) {
    final AbstractInsnNode nThis = new VarInsnNode(Opcodes.ALOAD, 0);
    final int opcode = Opcodes.ALOAD;
    final int nextParam = param + 1;
    final AbstractInsnNode unboxing = getUnboxingNode(field);

    instructions.add(nThis);/*from ww w  .j a va2s  . c o m*/
    final AbstractInsnNode nParam = new VarInsnNode(opcode, param);
    instructions.add(nParam);

    if (unboxing != null) {
        instructions.add(unboxing);
    }

    final AbstractInsnNode nPut = new FieldInsnNode(Opcodes.PUTFIELD, node.name, field.name, field.desc);
    instructions.add(nPut);

    return nextParam;
}

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilder.java

License:Open Source License

private static MethodNode createMethodNode(final ClassNode node) {
    final MethodNode constructor = new MethodNode();
    constructor.access = Opcodes.ACC_PUBLIC;
    constructor.name = "<init>";
    constructor.exceptions = Collections.emptyList();
    final InsnList list = new InsnList();
    // currently only call to noarg super constructor is supported
    final AbstractInsnNode nThis = new VarInsnNode(Opcodes.ALOAD, 0);
    final AbstractInsnNode nSuperConstructor = new MethodInsnNode(Opcodes.INVOKESPECIAL, node.superName,
            "<init>", "()V");
    list.add(nThis);//from w  w w  .j  a v a 2s.c  om
    list.add(nSuperConstructor);
    constructor.instructions = list;
    return constructor;
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayLengthInliner is working correctly.
 * //  w ww.j  a  v a 2  s  . c o  m
 * @throws Exception
 *           the exception
 */
@Test
public void test_newarrayAndSubarray() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayLengthTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").initArray(15, 42)
            .withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            addMethod("getArrayLength", "()I").withAnnotation(Optimizable.class).//
            addArray("[D", 15).// 3 -> 3
            addInsn(new VarInsnNode(Opcodes.ALOAD, 1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ARRAYLENGTH)).// 1 -> 0|1
            addInsn(new InsnNode(Opcodes.IADD)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.IRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayLengthInliner inliner = new LocalArrayLengthInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayLength");
    assertEquals(14, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);
    method.instructions = optimized;

    // ASSERT STEP 1
    assertEquals(12, optimized.size());
    assertEquals(15, NodeHelper.getNumberValue(optimized.get(3)).intValue());
    assertEquals(42, NodeHelper.getNumberValue(optimized.get(9)).intValue());

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(12, optimized2.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueInlinerTest.java

License:Open Source License

/**
 * Tests that LocalArrayValueInliner is working correctly.
 * /*  w w  w  .  j a  va2  s  .  co m*/
 * @throws Exception
 *           the exception
 */
@Test
public void testLocalArrayValueInliner() throws Exception {
    // INIT
    final String owner = "de.tuberlin.uebb.jbop.optimizer.array.LocalArrayValueTestClass";
    final ClassNodeBuilder builder = ClassNodeBuilder.createClass(owner).//
            addField("doubleArray", "[[D").//
            withAnnotation(ImmutableArray.class).//
            withModifiers(Opcodes.ACC_PRIVATE, Opcodes.ACC_FINAL).//
            initArray(2, 2).//
            initMultiArrayWith(1.0, 0, 0).//
            initMultiArrayWith(2.0, 0, 1).//
            initMultiArrayWith(3.0, 1, 0).//
            initMultiArrayWith(4.0, 1, 1).//
            addMethod("getArrayValue", "()D").withAnnotation(Optimizable.class).//
            addGetClassField("doubleArray").// 2 -> 2
            addInsn(new InsnNode(Opcodes.ICONST_0)).// 1 -> 1
            addInsn(new InsnNode(Opcodes.AALOAD)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ASTORE, 2)).// 1 -> 1
            addInsn(new VarInsnNode(Opcodes.ALOAD, 2)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.ICONST_1)).// 1 -> 0|
            addInsn(new InsnNode(Opcodes.DALOAD)).// 1 -> 0| 1
            addInsn(new InsnNode(Opcodes.DRETURN));// 1 -> 1
    // 14 -> 12
    final LocalArrayValueInliner inliner = new LocalArrayValueInliner();
    inliner.setInputObject(builder.toClass().instance());

    // RUN STEP 1
    final MethodNode method = builder.getMethod("getArrayValue");
    assertEquals(9, method.instructions.size());
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT STEP 1

    assertEquals(7, optimized.size());
    assertEquals(2.0, NodeHelper.getNumberValue(optimized.get(5)).doubleValue(), .0001);

    // RUN STEP 2
    final InsnList optimized2 = inliner.optimize(method.instructions, method);

    // ASSERT STEP 2
    assertEquals(7, optimized2.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Appends a Constructor with the given descriptors.
 * /*  w  ww .  java  2s . co  m*/
 * @param constructorDesc
 *          the constructor desc
 * @param superConstructorDesc
 *          the super constructor desc
 * @return the class node builder
 */
public ClassNodeBuilder addConstructor(final String constructorDesc, final String superConstructorDesc) {
    if (isInterface) {
        return this;
    }
    addMethod("<init>", constructorDesc);//
    lastConstructorVarIndex = 1;
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type methodType = Type.getMethodType(superConstructorDesc);
    for (final Type parameterType : methodType.getArgumentTypes()) {
        list.add(new VarInsnNode(parameterType.getOpcode(ILOAD), lastConstructorVarIndex));
        lastConstructorVarIndex += parameterType.getSize();
    }
    list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.superName, "<init>", superConstructorDesc));
    list.add(new InsnNode(Opcodes.RETURN));
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Creates a getter for the last added field.
 * //from   w ww.jav  a2  s  .co m
 * @return the class node builder
 */
public ClassNodeBuilder withGetter() {
    if (isInterface) {
        return this;
    }
    final String name = lastField.name;
    final String desc = lastField.desc;
    addMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1), "()" + desc);
    final Type type = Type.getType(desc);
    addInsn(new VarInsnNode(Opcodes.ALOAD, 0));
    addInsn(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, name, desc));
    addInsn(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Creates a setter for the last added field.
 * //from   w w  w .j ava  2 s.  com
 * @return the class node builder
 */
public ClassNodeBuilder withSetter() {
    if (isInterface) {
        return this;
    }
    final String name = lastField.name;
    final String desc = lastField.desc;
    addMethod("set" + Character.toUpperCase(name.charAt(0)) + name.substring(1), "(" + desc + ")V");
    final Type type = Type.getType(desc);
    addInsn(new VarInsnNode(Opcodes.ALOAD, 0));
    addInsn(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
    addInsn(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, name, desc));
    addInsn(new InsnNode(Opcodes.RETURN));
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Initializes a classField in the default lastConstructor
 * (eg: if fieldType of field "field" is "TestObject", field = new TestObject() is called).
 * /*from  ww  w  .ja  va 2 s.  co m*/
 * Numbers and Strings are used to assign "field", every other value leads to new Object.
 * 
 * @param object
 *          the object to Use.
 * @return the abstract optimizer test
 */
public ClassNodeBuilder initWith(final Object object) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    if (object instanceof String || object instanceof Number || object instanceof Boolean
            || object instanceof Character) {
        list.add(new VarInsnNode(Opcodes.ALOAD, 0));
        final AbstractInsnNode numberNode = NodeHelper.getInsnNodeFor(object);
        list.add(numberNode);
        if (lastField.desc.startsWith("L") && object instanceof Number) {
            list.add(ConstructorBuilder.getBoxingNode(lastField));
        }
        list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));
    } else {
        list.add(new VarInsnNode(ALOAD, 0));
        list.add(newObject(lastField.desc));
        list.add(new MethodInsnNode(INVOKESPECIAL,
                StringUtils.removeEnd(StringUtils.removeStart(lastField.desc, "L"), ";"), "<init>", "()V"));
        list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));

    }
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Inits the multi array with./*from www . j  av a2  s  .c  om*/
 * 
 * @param value
 *          the value
 * @param indexes
 *          the indexes
 * @return the class node builder
 */
public ClassNodeBuilder initMultiArrayWith(final Object value, final int... indexes) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    list.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, lastField.name, lastField.desc));
    for (int i = 0; i < indexes.length - 1; ++i) {
        list.add(NodeHelper.getInsnNodeFor(indexes[i]));
        list.add(new InsnNode(Opcodes.AALOAD));
    }
    list.add(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1]));
    list.add(NodeHelper.getInsnNodeFor(value));
    final Type elementType = Type.getType(lastField.desc).getElementType();
    if (elementType.getDescriptor().startsWith("L")) {
        list.add(new InsnNode(Opcodes.AASTORE));
    } else {
        list.add(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE)));

    }
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private void initArrayInternal(final int opcode, final Object... values) {
    final InsnList list = new InsnList();
    final int length = values.length;
    initArray(length);// w  ww.  j a  va 2s.  co m
    int index = 0;
    for (final Object number : values) {
        list.add(new VarInsnNode(Opcodes.ALOAD, lastConstructorVarIndex));
        final AbstractInsnNode indexNode = NodeHelper.getInsnNodeFor(index++);
        list.add(indexNode);
        final AbstractInsnNode numberNode = NodeHelper.getInsnNodeFor(number);
        list.add(numberNode);
        if (number instanceof Number && opcode == Opcodes.ASTORE) {
            list.add(ConstructorBuilder.getBoxingNode(lastField));
        }
        list.add(new InsnNode(opcode));
    }
    addToConstructor(list);
}