Search in sources :

Example 11 with TargetLoader

use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.

the class RuntimeDataTest method testGenerateArgumentArray.

@Test
public void testGenerateArgumentArray() throws Exception {
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) });
    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    RuntimeData.generateArgumentArray(1000, "Sample", 15, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(5, 1);
    mv.visitEnd();
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    Callable<?> callable = (Callable<?>) loader.add("Sample", writer.toByteArray()).newInstance();
    final Object[] args = (Object[]) callable.call();
    assertEquals(3, args.length, 0.0);
    assertEquals(Long.valueOf(1000), args[0]);
    assertEquals("Sample", args[1]);
    assertEquals(Integer.valueOf(15), args[2]);
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassWriter(org.objectweb.asm.ClassWriter) Callable(java.util.concurrent.Callable) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 12 with TargetLoader

use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.

the class RuntimeDataTest method testGenerateAccessCall.

@Test
public void testGenerateAccessCall() throws Exception {
    final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes();
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) });
    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;");
    RuntimeData.generateAccessCall(1234, "Sample", 5, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(6, 1);
    mv.visitEnd();
    writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null);
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    Callable<?> callable = (Callable<?>) loader.add("Sample", writer.toByteArray()).getConstructor(Object.class).newInstance(data);
    assertSame(probes, callable.call());
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassWriter(org.objectweb.asm.ClassWriter) Callable(java.util.concurrent.Callable) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 13 with TargetLoader

use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.

the class ResizeInstructionsTest method should_not_require_computation_of_common_superclass.

/**
 * Test of ASM bug
 * <a href= "https://gitlab.ow2.org/asm/asm/issues/317630">#317630</a> that
 * caused {@code java.lang.ClassNotFoundException}.
 */
@Test
public void should_not_require_computation_of_common_superclass() throws Exception {
    final String className = "Example";
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {

        @Override
        protected String getCommonSuperClass(final String type1, final String type2) {
            computedCommonSuperClass |= className.equals(type1) || className.equals(type2);
            return "java/lang/Object";
        }
    };
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "m", "()V", null, null);
    mv.visitCode();
    addCauseOfResizeInstructions(mv);
    addCauseOfGetCommonSuperClass(mv);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] original = cw.toByteArray();
    assertTrue(computedCommonSuperClass);
    new TargetLoader().add(className, original);
    final byte[] instrumented = instrumenter.instrument(original, className);
    new TargetLoader().add(className, instrumented);
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 14 with TargetLoader

use of org.jacoco.core.test.TargetLoader in project jacoco by jacoco.

the class ResizeInstructionsTest method should_not_loose_InnerClasses_attribute.

/**
 * Test of ASM bug
 * <a href="https://gitlab.ow2.org/asm/asm/issues/317792">#317792</a>.
 */
@Test
public void should_not_loose_InnerClasses_attribute() throws Exception {
    byte[] source = TargetLoader.getClassDataAsBytes(Inner.class);
    final ClassReader cr = InstrSupport.classReaderFor(source);
    final ClassWriter cw = new ClassWriter(0);
    cr.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION, cw) {

        @Override
        public void visitEnd() {
            final MethodVisitor mv = cv.visitMethod(0, "m", "()V", null, null);
            mv.visitCode();
            addCauseOfResizeInstructions(mv);
            mv.visitInsn(Opcodes.NOP);
            mv.visitMaxs(2, 1);
            mv.visitEnd();
            super.visitEnd();
        }
    }, 0);
    source = cw.toByteArray();
    final byte[] bytes = instrumenter.instrument(source, "");
    final TargetLoader targetLoader = new TargetLoader();
    final Class<?> outer = targetLoader.add(ResizeInstructionsTest.class, TargetLoader.getClassDataAsBytes(ResizeInstructionsTest.class));
    final Class<?> inner = targetLoader.add(Inner.class, bytes);
    assertSame(outer, inner.getEnclosingClass());
    assertNotNull(inner.getEnclosingClass());
    assertSame(outer, inner.getDeclaringClass());
    assertNotNull(inner.getDeclaringClass());
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Aggregations

TargetLoader (org.jacoco.core.test.TargetLoader)14 Test (org.junit.Test)8 ClassWriter (org.objectweb.asm.ClassWriter)8 MethodVisitor (org.objectweb.asm.MethodVisitor)6 Callable (java.util.concurrent.Callable)3 Instrumenter (org.jacoco.core.instr.Instrumenter)3 AnalyzerTest (org.jacoco.core.analysis.AnalyzerTest)2 ClassReader (org.objectweb.asm.ClassReader)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 Type (org.objectweb.asm.Type)2 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)2 Method (org.objectweb.asm.commons.Method)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Field (java.lang.reflect.Field)1 IRuntime (org.jacoco.core.runtime.IRuntime)1 LoggerRuntime (org.jacoco.core.runtime.LoggerRuntime)1 RuntimeData (org.jacoco.core.runtime.RuntimeData)1