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