Search in sources :

Example 1 with ConstantDynamic

use of org.objectweb.asm.ConstantDynamic in project jacoco by jacoco.

the class CondyProbeArrayStrategyTest method should_store_instance_using_condy_and_checkcast.

@Test
public void should_store_instance_using_condy_and_checkcast() {
    final MethodNode m = new MethodNode();
    final int maxStack = strategy.storeInstance(m, false, 1);
    assertEquals(1, maxStack);
    final ConstantDynamic constantDynamic = (ConstantDynamic) ((LdcInsnNode) m.instructions.get(0)).cst;
    assertEquals("$jacocoData", constantDynamic.getName());
    assertEquals("Ljava/lang/Object;", constantDynamic.getDescriptor());
    final Handle bootstrapMethod = constantDynamic.getBootstrapMethod();
    assertEquals(Opcodes.H_INVOKESTATIC, bootstrapMethod.getTag());
    assertEquals("ClassName", bootstrapMethod.getOwner());
    assertEquals("$jacocoInit", bootstrapMethod.getName());
    assertEquals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", bootstrapMethod.getDesc());
    assertTrue(bootstrapMethod.isInterface());
    final TypeInsnNode castInstruction = (TypeInsnNode) m.instructions.get(1);
    assertEquals(Opcodes.CHECKCAST, castInstruction.getOpcode());
    assertEquals("[Z", castInstruction.desc);
    final VarInsnNode storeInstruction = (VarInsnNode) m.instructions.get(2);
    assertEquals(Opcodes.ASTORE, storeInstruction.getOpcode());
    assertEquals(1, storeInstruction.var);
    assertEquals(3, m.instructions.size());
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) ConstantDynamic(org.objectweb.asm.ConstantDynamic) Handle(org.objectweb.asm.Handle) Test(org.junit.Test)

Example 2 with ConstantDynamic

use of org.objectweb.asm.ConstantDynamic in project jacoco by jacoco.

the class CondyProbeArrayStrategy method storeInstance.

public int storeInstance(final MethodVisitor mv, final boolean clinit, final int variable) {
    final Handle bootstrapMethod = new Handle(Opcodes.H_INVOKESTATIC, className, InstrSupport.INITMETHOD_NAME, B_DESC, isInterface);
    // As a workaround for https://bugs.openjdk.java.net/browse/JDK-8216970
    // constant should have type Object
    mv.visitLdcInsn(new ConstantDynamic(InstrSupport.DATAFIELD_NAME, "Ljava/lang/Object;", bootstrapMethod));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Z");
    mv.visitVarInsn(Opcodes.ASTORE, variable);
    return 1;
}
Also used : Handle(org.objectweb.asm.Handle) ConstantDynamic(org.objectweb.asm.ConstantDynamic)

Aggregations

ConstantDynamic (org.objectweb.asm.ConstantDynamic)2 Handle (org.objectweb.asm.Handle)2 Test (org.junit.Test)1 MethodNode (org.objectweb.asm.tree.MethodNode)1 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)1 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)1