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();
}
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);
}
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());
}
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();
}
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();
}
Aggregations