Search in sources :

Example 1 with TargetLoader

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

the class CyclomaticComplexityTest method instrument.

private void instrument(final Class<? extends Target> clazz) throws Exception {
    bytes = TargetLoader.getClassDataAsBytes(clazz);
    final byte[] instrumented = new Instrumenter(runtime).instrument(bytes, "TestTarget");
    final TargetLoader loader = new TargetLoader();
    target = (Target) loader.add(clazz, instrumented).newInstance();
}
Also used : TargetLoader(org.jacoco.core.test.TargetLoader) Instrumenter(org.jacoco.core.instr.Instrumenter)

Example 2 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 3 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 {
    // FIXME fails without COMPUTE_FRAMES because of
    // https://gitlab.ow2.org/asm/asm/issues/317800
    byte[] source = TargetLoader.getClassDataAsBytes(Inner.class);
    final int version = BytecodeVersion.get(source);
    source = BytecodeVersion.downgradeIfNeeded(version, source);
    final ClassReader cr = new ClassReader(source);
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    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();
    BytecodeVersion.set(source, version);
    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)

Example 4 with TargetLoader

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

the class RuntimeTestBase method generateAndInstantiateClass.

/**
 * Creates a new class with the given id, loads this class and instantiates
 * it. The constructor of the generated class will request the probe array
 * from the runtime under test.
 */
private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException {
    final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid;
    Type classType = Type.getObjectType(className);
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) });
    writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null);
    // Constructor
    GeneratorAdapter gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V");
    gen.visitCode();
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V"));
    gen.loadThis();
    final int size = runtime.generateDataAccessor(classid, className, 2, gen);
    gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(size + 1, 0);
    gen.visitEnd();
    // get()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(1, 0);
    gen.visitEnd();
    // a()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "a", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "a", "()V");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.push(0);
    gen.push(1);
    gen.arrayStore(Type.BOOLEAN_TYPE);
    gen.returnValue();
    gen.visitMaxs(3, 0);
    gen.visitEnd();
    // b()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "b", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "b", "()V");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.push(1);
    gen.push(1);
    gen.arrayStore(Type.BOOLEAN_TYPE);
    gen.returnValue();
    gen.visitMaxs(3, 0);
    gen.visitEnd();
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    return (ITarget) loader.add(className.replace('/', '.'), writer.toByteArray()).newInstance();
}
Also used : Type(org.objectweb.asm.Type) TargetLoader(org.jacoco.core.test.TargetLoader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Example 5 with TargetLoader

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

the class OfflineInstrumentationAccessGeneratorTest method generateAndInstantiateClass.

/**
 * Creates a new class with the given id, loads this class and instantiates
 * it. The constructor of the generated class will request the probe array
 * from the access generator under test.
 */
private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException {
    final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid;
    Type classType = Type.getObjectType(className);
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) });
    writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null);
    // Constructor
    GeneratorAdapter gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V");
    gen.visitCode();
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V"));
    gen.loadThis();
    final int size = generator.generateDataAccessor(classid, className, 2, gen);
    gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(size + 1, 0);
    gen.visitEnd();
    // get()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(1, 0);
    gen.visitEnd();
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    return (ITarget) loader.add(className.replace('/', '.'), writer.toByteArray()).newInstance();
}
Also used : Type(org.objectweb.asm.Type) TargetLoader(org.jacoco.core.test.TargetLoader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter)

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