Skip to content

Commit

Permalink
only run implicit null check when call present
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Mar 15, 2023
1 parent 6fd04a7 commit 81d2a5e
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.pitest.bytecode.analysis.ClassTree;
import org.pitest.bytecode.analysis.MethodTree;
import org.pitest.classinfo.ClassName;
Expand All @@ -23,6 +22,7 @@
import org.pitest.mutationtest.engine.Mutater;
import org.pitest.mutationtest.engine.MutationDetails;
import org.pitest.sequence.Context;
import org.pitest.sequence.Match;
import org.pitest.sequence.QueryParams;
import org.pitest.sequence.QueryStart;
import org.pitest.sequence.SequenceMatcher;
Expand All @@ -36,7 +36,7 @@ public class ImplicitNullCheckFilter implements MutationInterceptor {

static final SequenceMatcher<AbstractInsnNode> GET_CLASS_NULL_CHECK = QueryStart
.any(AbstractInsnNode.class)
.then(methodCallTo(ClassName.fromClass(Object.class), "getClass").and(isInstruction(MUTATED_INSTRUCTION.read())))
.then(aGetClassCall().and(isInstruction(MUTATED_INSTRUCTION.read())))
.then(POP) // immediate discard
.then(isA(LabelNode.class).negate()) // use presence of a label to indicate this was a programmer call to getClass
.zeroOrMore(QueryStart.match(anyInstruction()))
Expand All @@ -45,6 +45,10 @@ public class ImplicitNullCheckFilter implements MutationInterceptor {
.withDebug(DEBUG)
);

private static Match<AbstractInsnNode> aGetClassCall() {
return methodCallTo(ClassName.fromClass(Object.class), "getClass");
}


private ClassTree currentClass;

Expand Down Expand Up @@ -73,10 +77,11 @@ private Predicate<MutationDetails> isAnImplicitNullCheck() {
final AbstractInsnNode mutatedInstruction = method.instruction(instruction);

// performance hack
if (!(mutatedInstruction instanceof MethodInsnNode)) {
return false;
if (!aGetClassCall().asPredicate().test(mutatedInstruction)) {
return false;
}


Context context = Context.start(DEBUG);
context = context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return GET_CLASS_NULL_CHECK.matches(method.instructions(), context);
Expand Down

0 comments on commit 81d2a5e

Please sign in to comment.