Lambda Mutations: "removed call to java/lang/Object::getClass → SURVIVED"

2,264 views
Skip to first unread message

Matthew Cory

unread,
Aug 5, 2017, 2:25:26 PM8/5/17
to PIT Users
I've come across this a few times, and for the life of me can't figure out any way to kill this. A quick sample (this is using classes from vavr.io; the List is regular java.util.List though):

public static <T> Try<List<T>> flattenTry(final List<Try<T>> listOfTries) {
    final Try<Seq<T>> zero = Try.success(Stream.empty());
    final BiFunction<Try<Seq<T>>, Try<? extends T>, Try<Seq<T>>> f =
            (result, future) -> result.flatMap(seq -> future.map(seq::append)); // this is the line with the problem.
    return Iterator.ofAll(listOfTries).foldLeft(zero, f).map(Seq::toJavaList);

There are a total of 5 mutations created for the problem line; I have tests for it that are killing all of them except for the "removed call to java/lang/Object::getClass → SURVIVED".

I've happened across this a few times while working with lambdas - this is the only sample I can find that's generic enough I'm comfortable sharing it from work.

I'm using the gradle plugin, but have it configured to use pitest 1.2.2. I've got it configured to use the following mutators:

mutators = [
'CONDITIONALS_BOUNDARY',
'NEGATE_CONDITIONALS',
'INCREMENTS',
'INVERT_NEGS',
'MATH',
'RETURN_VALS',
'VOID_METHOD_CALLS',
'NON_VOID_METHOD_CALLS',
'REMOVE_CONDITIONALS'
]

I'm not a stickler for 100% coverage (of course, it'd be nice), but it would be nice to look at the report and know that what's red is something I can address. Any suggestions? Is there some way to test for a call to Object::getClass, or some way to avoid calls to it?

Thanks!

henry

unread,
Aug 23, 2017, 8:28:29 AM8/23/17
to PIT Users
Sorry for the slow response - I missed this when it was posted.

I've just taken a look and spent a while puzzling over what the compiler is producing. The bytecode shows a call to getClass followed by an immediate pop of the returned value.

A bit of googling suggests that this is meant as a cheap null check

https://stackoverflow.com/questions/43115645/in-java-lambdas-why-is-getclass-called-on-a-captured-variable/43116722

It should be simple enough to filter them out.

No tracked here -> https://github.com/hcoles/pitest/issues/389

Thanks

Henry

Matthew Cory

unread,
Aug 28, 2017, 2:30:37 PM8/28/17
to PIT Users
Awesome - thanks henry! 
Reply all
Reply to author
Forward
0 new messages