Example usage for org.objectweb.asm.tree InsnList size

List of usage examples for org.objectweb.asm.tree InsnList size

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList size.

Prototype

int size

To view the source code for org.objectweb.asm.tree InsnList size.

Click Source Link

Document

The number of instructions in this list.

Usage

From source file:de.tuberlin.uebb.jbop.optimizer.var.FinalFieldInlinerTest.java

License:Open Source License

/**
 * Tests that FinalFieldInliner is working correctly for boolean-Fields.
 * //from  w  w  w . j a  v a2  s. c  o  m
 * @throws Exception
 *           the exception
 */
@Test
public void testFinalFieldInlinerCharField() throws Exception {
    // INIT
    initTestMethod("C", 'C', 8);

    // RUN
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT
    assertEquals(2, optimized.size());
    assertEquals('C', NodeHelper.getCharValue(optimized.get(0)));
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.FinalFieldInlinerTest.java

License:Open Source License

/**
 * Tests that finalFieldInliner is is working correctly for field-Chains with mutli-arrays.
 * //from w w w. j a va 2  s.  com
 * @throws Exception
 *           the exception
 */
@Test
public void testFinalFieldInlinerFieldChainMultiArray() throws Exception {
    // INIT
    final ClassNodeBuilder builderC = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.CM")
            .addField("d", "[[D").withModifiers(ACC_PRIVATE, ACC_FINAL).withAnnotation(ImmutableArray.class)
            .addToConstructor(new VarInsnNode(ALOAD, 0)).//
            addToConstructor(new InsnNode(ICONST_1)).//
            addToConstructor(new TypeInsnNode(ANEWARRAY, "[D")).//
            addToConstructor(new InsnNode(DUP)).//
            addToConstructor(new InsnNode(ICONST_0)).//
            addToConstructor(new InsnNode(ICONST_1)).//
            addToConstructor(new IntInsnNode(NEWARRAY, Opcodes.T_DOUBLE)).//
            addToConstructor(new InsnNode(DUP)).//
            addToConstructor(new InsnNode(ICONST_0)).//
            addToConstructor(new InsnNode(DCONST_1)).//
            addToConstructor(new InsnNode(DASTORE)).//
            addToConstructor(new InsnNode(AASTORE)).//
            addToConstructor(new FieldInsnNode(PUTFIELD, "de/tuberlin/uebb/jbop/optimizer/var/CM", "d", "[[D")).//
            toClass();
    final ClassNodeBuilder builderB = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.BM")
            .addField("c", Type.getDescriptor(builderC.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderA = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.AM")
            .addField("b", Type.getDescriptor(builderB.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderTestClass = ClassNodeBuilder
            .createClass("de.tuberlin.uebb.jbop.optimizer.var.ChainedTestClassM")
            .addField("a", Type.getDescriptor(builderA.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).//
            addMethod("get", "()D").//
            addGetClassField("a").// ;
            addGetField(builderA, "b").//
            addGetField(builderB, "c").//
            addGetField(builderC, "d").//
            addInsn(new InsnNode(ICONST_0)).//
            addInsn(new InsnNode(AALOAD)).//
            addInsn(new InsnNode(ICONST_0)).//
            addInsn(new InsnNode(DALOAD)).//
            addInsn(new InsnNode(DRETURN));//

    // RUN
    final Object instance = builderTestClass.instance();

    inliner.setInputObject(instance);
    method = builderTestClass.getMethod("get");
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT
    assertEquals(2, optimized.size());
    assertEquals(1.0, NodeHelper.getNumberValue(optimized.get(0)).doubleValue(), .00001);
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.FinalFieldInlinerTest.java

License:Open Source License

/**
 * Tests that finalFieldInliner is is working correctly for field-Chains with single-arrays.
 * /*ww w.  j a  va  2s.co m*/
 * @throws Exception
 *           the exception
 */
@Test
public void testFinalFieldInlinerFieldChainArray() throws Exception {
    // INIT
    final ClassNodeBuilder builderC = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.C")
            .addField("d", "[D").withModifiers(ACC_PRIVATE, ACC_FINAL).withAnnotation(ImmutableArray.class)
            .initArrayWith(1.0).//
            toClass();
    final ClassNodeBuilder builderB = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.B")
            .addField("c", Type.getDescriptor(builderC.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderA = ClassNodeBuilder.createClass("de.tuberlin.uebb.jbop.optimizer.var.A")
            .addField("b", Type.getDescriptor(builderB.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).toClass();
    final ClassNodeBuilder builderTestClass = ClassNodeBuilder
            .createClass("de.tuberlin.uebb.jbop.optimizer.var.ChainedTestClass")
            .addField("a", Type.getDescriptor(builderA.getBuildedClass())).withModifiers(ACC_PRIVATE, ACC_FINAL)
            .initWith(null).//
            addMethod("get", "()D").//
            addGetClassField("a").// ;
            addGetField(builderA, "b").//
            addGetField(builderB, "c").//
            addGetField(builderC, "d").//
            addInsn(new InsnNode(ICONST_0)).//
            addInsn(new InsnNode(DALOAD)).//
            addInsn(new InsnNode(DRETURN));//

    // RUN
    final Object instance = builderTestClass.instance();

    inliner.setInputObject(instance);
    method = builderTestClass.getMethod("get");
    final InsnList optimized = inliner.optimize(method.instructions, method);

    // ASSERT
    assertEquals(2, optimized.size());
    assertEquals(1.0, NodeHelper.getNumberValue(optimized.get(0)).doubleValue(), .00001);
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case:/*from w w  w  . j  av  a2  s  . co  m*/
 * there is a single store of a known value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInliner() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_1)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_1, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that LocalVarInliner is working correctly.
 * There is more then one store to the var, therefore
 * the load of the var is not replaced./*from   w  w  w.j a  v  a  2  s.c  om*/
 */
@Test
public void testLocalVarInlinerDoubleStore() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_1)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new InsnNode(ICONST_2)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(6, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(6, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_2, optimized.get(4).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that LocalVarInliner is working correctly.
 * There is a single (direct) store to the var, but
 * the var is altered via a iinc instruction. Therefore
 * the load of the variable is not replaced.
 *///from  w  w w  . jav a 2  s.  co  m
@Test
public void testLocalVarInlinerIInc() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_0)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new IincInsnNode(1, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(5, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(5, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ICONST_1, optimized.get(3).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that LocalVarInliner is working correctly.
 * There is is a single store to the var but the value is not known (yet).
 * Therefore the load of the var is not replaced.
 *///from w w w  .  ja  v a  2s. co m
@Test
public void testLocalVarInlinerUnknownStore() {
    // INIT
    builder.addInsn(new InsnNode(ICONST_2)).//
            addInsn(new InsnNode(ICONST_2)).//
            addInsn(new InsnNode(IADD)).//
            addInsn(new VarInsnNode(ISTORE, 1)).//
            addInsn(new VarInsnNode(ILOAD, 1)).//
            addInsn(new InsnNode(IRETURN));//

    // RUN
    assertEquals(6, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(6, optimized.size());
    assertFalse(optimizer.isOptimized());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case:/*from   ww  w . j a  va2  s .  c  o m*/
 * there is a single store of a known 'String' value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInlinerStringValue() {
    // INIT
    builder.addInsn(new LdcInsnNode("Hallo")).//
            addInsn(new VarInsnNode(ASTORE, 1)).//
            addInsn(new VarInsnNode(ALOAD, 1)).//
            addInsn(new InsnNode(ARETURN));//
    methodNode.desc = "()Ljava/lang/String;";

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(LDC, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.LocalVarInlinerTest.java

License:Open Source License

/**
 * Tests that localVarInliner is working correctly.
 * This is the simplest case://from w w w .  j a v a2 s  .c  om
 * there is a single store of a known 'null' value.
 * The value is pushed directly to the stack,
 * instead of loading the var.
 */
@Test
public void testLocalVarInlinerNullValue() {
    // INIT
    builder.addInsn(new InsnNode(ACONST_NULL)).//
            addInsn(new VarInsnNode(ASTORE, 1)).//
            addInsn(new VarInsnNode(ALOAD, 1)).//
            addInsn(new InsnNode(ARETURN));//
    methodNode.desc = "()Ljava/lang/Object;";

    // RUN
    assertEquals(4, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertTrue(optimizer.isOptimized());
    assertEquals(ACONST_NULL, optimized.get(2).getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVarsTest.java

License:Open Source License

/**
 * Tests that RemoveUnusedLocalVars is working correctly.
 *//*from  w ww  . j a  va 2  s .  co m*/
@Test
public void testRemoveUnusedLocalVars() {
    // INIT
    builder.add(ICONST_1).//
            add(ISTORE, 1).//
            add(ICONST_2).//
            add(ISTORE, 2).//
            add(ICONST_3).//
            add(ISTORE, 3).//
            add(ICONST_4).//
            add(ISTORE, 4).//
            add(ICONST_5).//
            add(ISTORE, 5).//
            add(DCONST_1).//
            add(DSTORE, 6).//
            add(ACONST_NULL).//
            add(ASTORE, 7).//
            add(FCONST_1).//
            add(FSTORE, 8).//
            add(LCONST_1).//
            add(LSTORE, 9).//
            add(ILOAD, 1).//
            addReturn();

    // RUN
    assertEquals(20, methodNode.instructions.size());
    final InsnList optimized = optimizer.optimize(methodNode.instructions, methodNode);

    // ASSERT
    assertEquals(4, optimized.size());
    assertEquals(ICONST_1, optimized.get(0).getOpcode());
    assertEquals(ISTORE, optimized.get(1).getOpcode());
    assertEquals(1, ((VarInsnNode) optimized.get(1)).var);
    assertEquals(ILOAD, optimized.get(2).getOpcode());
    assertEquals(1, ((VarInsnNode) optimized.get(2)).var);
    assertEquals(IRETURN, optimized.get(3).getOpcode());
}