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

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

Introduction

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

Prototype

@Override
    public void visitMethodInsn(final int opcodeAndSource, final String owner, final String name,
            final String descriptor, final boolean isInterface) 

Source Link

Usage

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);/*  w w w.ja  va  2  s  .com*/
    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.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);//  w  w w  .ja  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);/*from www.j  a v a 2s  .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);//from w  ww. 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);/* w  w w  . ja  v a2s .  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.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateGetterConstructor() {
    MethodNode cn = new MethodNode();
    cn.name = "<init>";
    cn.desc = "()V";
    cn.access = ACC_PUBLIC;/*from w  ww  . j  av a 2s .  c  o  m*/
    cn.exceptions = Collections.emptyList();
    cn.maxStack = 1;
    cn.maxLocals = 1;
    // load the instance
    cn.visitVarInsn(ALOAD, 0);
    // call superclass constructor
    cn.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(BeanAttributeGetter.class), "<init>", "()V", false);
    cn.visitInsn(RETURN);
    return cn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "get";
    gn.desc = Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;/*from  www . ja v a2s .  c  om*/
    gn.exceptions = Collections.emptyList();
    Type rt = Type.getReturnType(getter);
    gn.maxLocals = 2;
    gn.maxStack = 1 + rt.getSize();
    gn.visitCode();
    // load the target object from parameter
    gn.visitVarInsn(ALOAD, 1);
    // cast to target object type
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    // call target object method
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    // convert from primitive to object if necessary
    CGUtils.adaptFromType(gn, getter.getReturnType());
    gn.visitInsn(ARETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateLongGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "getLong";
    gn.desc = Type.getMethodDescriptor(Type.LONG_TYPE, Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;/*from   w  w w .  j av a 2s . c  o m*/
    gn.exceptions = Collections.emptyList();
    gn.maxLocals = 2;
    gn.maxStack = 2;
    gn.visitCode();
    gn.visitVarInsn(ALOAD, 1);
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    gn.visitInsn(LRETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntity.java

License:Open Source License

private static MethodNode generateDoubleGetterMethod(Class<? extends AbstractBeanEntity> type, Method getter) {
    MethodNode gn = new MethodNode();
    gn.name = "getDouble";
    gn.desc = Type.getMethodDescriptor(Type.DOUBLE_TYPE, Type.getType(AbstractBeanEntity.class));
    gn.access = ACC_PUBLIC;/*  ww  w.  jav  a 2 s .  co  m*/
    gn.exceptions = Collections.emptyList();
    gn.maxLocals = 2;
    gn.maxStack = 2;
    gn.visitCode();
    gn.visitVarInsn(ALOAD, 1);
    gn.visitTypeInsn(CHECKCAST, Type.getInternalName(type));
    gn.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(type), getter.getName(),
            Type.getMethodDescriptor(getter), false);
    gn.visitInsn(DRETURN);
    return gn;
}

From source file:org.lenskit.data.entities.AbstractBeanEntityBuilder.java

License:Open Source License

private static MethodNode generateBeanConstructor(Class<? extends AttrMethod> superclass) {
    MethodNode ctor = new MethodNode();
    ctor.access = ACC_PUBLIC;/*from w  w w.j  ava  2  s.  c om*/
    ctor.desc = getMethodDescriptor(VOID_TYPE, getType(TypedName.class));
    ctor.name = "<init>";
    ctor.exceptions = Collections.emptyList();
    ctor.maxLocals = 2;
    ctor.maxStack = 2;
    // load instance ('this')
    ctor.visitVarInsn(ALOAD, 0);
    // load the attribute
    ctor.visitVarInsn(ALOAD, 1);
    // invoke superclass constructor with attribute
    ctor.visitMethodInsn(INVOKESPECIAL, getInternalName(superclass), "<init>", ctor.desc, false);
    ctor.visitInsn(RETURN);
    return ctor;
}