Example usage for org.objectweb.asm.tree MethodNode MethodNode

List of usage examples for org.objectweb.asm.tree MethodNode MethodNode

Introduction

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

Prototype

public MethodNode(final int api, final int access, final String name, final String descriptor,
        final String signature, final String[] exceptions) 

Source Link

Document

Constructs a new MethodNode .

Usage

From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenStringFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label h1 = new Label();
    final Label sameHash = new Label();
    final Label h2 = new Label();
    final Label case1 = new Label();
    final Label case2 = new Label();
    final Label case3 = new Label();
    final Label defaultCase = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);// w ww  .java 2 s. c o m
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitTableSwitchInsn(97, 98, defaultCase, h1, h2);

    // case "a"
    m.visitLabel(h1);
    final AbstractInsnNode expectedFromInclusive = m.instructions.getLast();

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFEQ, sameHash);
    m.visitJumpInsn(Opcodes.GOTO, case1);

    // case "\u0000a"
    m.visitLabel(sameHash);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("\u0000a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFEQ, defaultCase);
    m.visitJumpInsn(Opcodes.GOTO, case2);

    // case "b"
    m.visitLabel(h2);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("\u0000a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    m.visitJumpInsn(Opcodes.IFEQ, defaultCase);
    m.visitJumpInsn(Opcodes.GOTO, case3);
    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case2);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case3);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(defaultCase);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(expectedFromInclusive.getPrevious(), expectedNewTargets);
    assertIgnored(new Range(expectedFromInclusive, expectedToInclusive));
}

From source file:org.jacoco.core.internal.analysis.filter.KotlinWhenStringFilterTest.java

License:Open Source License

@Test
public void should_not_filter_empty_lookup_switch() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "(Ljava/lang/String;)V", null,
            null);/*from  www .  j  a v  a 2 s .  co  m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    final Label defaultCase = new Label();
    m.visitLookupSwitchInsn(defaultCase, null, new Label[] {});
    m.visitLabel(defaultCase);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.LombokGeneratedFilterTest.java

License:Open Source License

@Test
public void testOtherAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Lother/Annotation;", false);

    m.visitInsn(Opcodes.ICONST_0);//from   w ww  .  j  ava  2  s .  co m
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertNull(fromInclusive);
    assertNull(toInclusive);
}

From source file:org.jacoco.core.internal.analysis.filter.LombokGeneratedFilterTest.java

License:Open Source License

@Test
public void testLombokGeneratedAnnotation() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null);
    m.visitAnnotation("Llombok/Generated;", false);

    m.visitInsn(Opcodes.ICONST_0);/*  w w w.ja  va  2  s. co m*/
    m.visitInsn(Opcodes.IRETURN);

    filter.filter("Foo", "java/lang/Object", m, this);

    assertEquals(m.instructions.getFirst(), fromInclusive);
    assertEquals(m.instructions.getLast(), toInclusive);
}

From source file:org.jacoco.core.internal.analysis.filter.PrivateEmptyNoArgConstructorFilterTest.java

License:Open Source License

@Test
public void test() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_PRIVATE, "<init>", "()V",
            null, null);//from w ww.  j a  v  a  2 s.c  o  m

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertMethodIgnored(m);
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_filter() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label case2 = new Label();
    final Label case3 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();
    final Label h2 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);// ww  w  . jav a2s.c o  m
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitTableSwitchInsn(97, 98, caseDefault, h1, h2);
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("\0a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "\0a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case2);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);

    m.visitLabel(h2);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "b", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case3);

    // goto default case
    m.visitJumpInsn(Opcodes.GOTO, caseDefault);
    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case2);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case3);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_filter_when_default_is_first() {
    final Set<AbstractInsnNode> expectedNewTargets = new HashSet<AbstractInsnNode>();

    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);

    final Label case1 = new Label();
    final Label caseDefault = new Label();
    final Label h1 = new Label();

    // filter should not remember this unrelated slot
    m.visitLdcInsn("");
    m.visitVarInsn(Opcodes.ASTORE, 1);/* w  w w.  j a  va 2 s.  c  o m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);

    // switch (...)
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitLookupSwitchInsn(caseDefault, new int[] { 97 }, new Label[] { h1 });
    final AbstractInsnNode switchNode = m.instructions.getLast();

    m.visitLabel(h1);

    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if equal "a", then goto its case
    m.visitJumpInsn(Opcodes.IFNE, case1);

    final AbstractInsnNode expectedToInclusive = m.instructions.getLast();

    m.visitLabel(caseDefault);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());
    m.visitLabel(case1);
    m.visitInsn(Opcodes.RETURN);
    expectedNewTargets.add(m.instructions.getLast());

    filter.filter(m, context, output);

    assertReplacedBranches(switchNode, expectedNewTargets);
    assertIgnored(new Range(switchNode.getNext(), expectedToInclusive));
}

From source file:org.jacoco.core.internal.analysis.filter.StringSwitchEcjFilterTest.java

License:Open Source License

@Test
public void should_not_filter_empty_lookup_switch() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "(Ljava/lang/String;)V", null,
            null);/*from w w  w . j a v a 2s . c  o  m*/
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    final Label defaultCase = new Label();
    m.visitLookupSwitchInsn(defaultCase, null, new Label[] {});
    m.visitLabel(defaultCase);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.SyntheticFilterTest.java

License:Open Source License

@Test
public void testNonSynthetic() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "name", "()V", null, null);
    m.visitInsn(Opcodes.NOP);/*w  ww  . j a va 2 s  . c o m*/

    filter.filter(m, context, output);

    assertIgnored();
}

From source file:org.jacoco.core.internal.analysis.filter.SyntheticFilterTest.java

License:Open Source License

@Test
public void testSynthetic() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_SYNTHETIC, "name", "()V",
            null, null);//from w  w  w .j  av  a2  s. com
    m.visitInsn(Opcodes.NOP);

    filter.filter(m, context, output);

    assertMethodIgnored(m);
}