-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1268 from hcoles/feature/lombok_null_checks
filter lombok autogenerated null checks
- Loading branch information
Showing
7 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
pitest-entry/src/main/java/org/pitest/mutationtest/build/intercept/lombok/LombokFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.pitest.mutationtest.build.intercept.lombok; | ||
|
||
import org.pitest.mutationtest.build.InterceptorParameters; | ||
import org.pitest.mutationtest.build.MutationInterceptor; | ||
import org.pitest.mutationtest.build.MutationInterceptorFactory; | ||
import org.pitest.plugin.Feature; | ||
|
||
public class LombokFilter implements MutationInterceptorFactory { | ||
|
||
@Override | ||
public String description() { | ||
return "Lombok junk mutations filter"; | ||
} | ||
|
||
@Override | ||
public MutationInterceptor createInterceptor(InterceptorParameters params) { | ||
return new LombokNullFilter(); | ||
} | ||
|
||
@Override | ||
public Feature provides() { | ||
return Feature.named("lombok") | ||
.withDescription("Filters out junk mutations generated by lombok") | ||
.withOnByDefault(true); | ||
} | ||
|
||
} | ||
|
86 changes: 86 additions & 0 deletions
86
...-entry/src/main/java/org/pitest/mutationtest/build/intercept/lombok/LombokNullFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.pitest.mutationtest.build.intercept.lombok; | ||
|
||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.LabelNode; | ||
import org.pitest.bytecode.analysis.MethodTree; | ||
import org.pitest.classinfo.ClassName; | ||
import org.pitest.mutationtest.build.intercept.Region; | ||
import org.pitest.mutationtest.build.intercept.RegionInterceptor; | ||
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; | ||
import org.pitest.sequence.Slot; | ||
import org.pitest.sequence.SlotWrite; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import static org.pitest.bytecode.analysis.InstructionMatchers.anyInstruction; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.isA; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.ldcString; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.newCall; | ||
import static org.pitest.bytecode.analysis.InstructionMatchers.notAnInstruction; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.ALOAD; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.ATHROW; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.DUP; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.IFNONNULL; | ||
import static org.pitest.bytecode.analysis.OpcodeMatchers.INVOKESPECIAL; | ||
import static org.pitest.sequence.Result.result; | ||
|
||
public class LombokNullFilter extends RegionInterceptor { | ||
|
||
static final Slot<AbstractInsnNode> START = Slot.create(AbstractInsnNode.class); | ||
static final Slot<AbstractInsnNode> END = Slot.create(AbstractInsnNode.class); | ||
|
||
static final SequenceMatcher<AbstractInsnNode> NULL_CHECK = QueryStart | ||
.any(AbstractInsnNode.class) | ||
.then(ALOAD.and(store(START.write()))) | ||
.then(IFNONNULL) | ||
.then(newCall(ClassName.fromClass(NullPointerException.class))) | ||
.then(DUP) | ||
// perhaps requiring the string here is too brittle? But makes it less likely to pick up non lombok generated null checks | ||
.then(ldcString(s -> s.endsWith("is marked non-null but is null"))) | ||
.then(INVOKESPECIAL) | ||
.then(ATHROW.and(store(END.write()))) | ||
.zeroOrMore(QueryStart.match(anyInstruction())) | ||
.compile(QueryParams.params(AbstractInsnNode.class) | ||
.withIgnores(notAnInstruction().or(isA(LabelNode.class))) | ||
); | ||
|
||
private static Match<AbstractInsnNode> store(SlotWrite<AbstractInsnNode> slot) { | ||
return (c,n) -> result(true, c.store(slot, n)); | ||
} | ||
|
||
|
||
protected List<Region> computeRegions(MethodTree method) { | ||
// Should really be checking that the null check if for one | ||
// of the annotated parameters, but can likely get away with | ||
// this since hand rolled nulls checks are not commonly mixed with | ||
// lombok code | ||
if (!hasLombokNonNullAnnotation(method)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
Context context = Context.start(); | ||
return NULL_CHECK.contextMatches(method.instructions(), context).stream() | ||
.map(c -> new Region(c.retrieve(START.read()).get(), c.retrieve(END.read()).get())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean hasLombokNonNullAnnotation(MethodTree method) { | ||
if (method.rawNode().invisibleParameterAnnotations == null) { | ||
return false; | ||
} | ||
|
||
return Arrays.stream(method.rawNode().invisibleParameterAnnotations) | ||
.filter( l -> l != null) // asm is nasty | ||
.flatMap(l -> l.stream().filter(node -> node.desc.equals("Llombok/NonNull;"))) | ||
.findAny() | ||
.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ry/src/test/java/org/pitest/mutationtest/build/intercept/lombok/LombokNullFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.pitest.mutationtest.build.intercept.lombok; | ||
|
||
import org.junit.Test; | ||
import org.pitest.bytecode.analysis.OpcodeMatchers; | ||
import org.pitest.mutationtest.build.InterceptorType; | ||
import org.pitest.mutationtest.build.MutationInterceptorFactory; | ||
import org.pitest.mutationtest.engine.gregor.mutators.NullMutateEverything; | ||
import org.pitest.verifier.interceptors.FactoryVerifier; | ||
import org.pitest.verifier.interceptors.InterceptorVerifier; | ||
import org.pitest.verifier.interceptors.VerifierStart; | ||
|
||
public class LombokNullFilterTest { | ||
|
||
private MutationInterceptorFactory underTest = new LombokFilter(); | ||
InterceptorVerifier v = VerifierStart.forInterceptorFactory(underTest) | ||
.usingMutator(new NullMutateEverything()); | ||
|
||
@Test | ||
public void isOnChain() { | ||
FactoryVerifier.confirmFactory(underTest) | ||
.isOnChain(); | ||
} | ||
|
||
@Test | ||
public void isOnByDefault() { | ||
FactoryVerifier.confirmFactory(underTest) | ||
.isOnByDefault(); | ||
} | ||
|
||
@Test | ||
public void featureIsCalledLombok() { | ||
FactoryVerifier.confirmFactory(underTest) | ||
.featureName().isEqualTo("lombok"); | ||
} | ||
|
||
@Test | ||
public void createsFilters() { | ||
FactoryVerifier.confirmFactory(underTest) | ||
.createsInterceptorsOfType(InterceptorType.FILTER); | ||
} | ||
|
||
@Test | ||
public void filtersMutationsToLombokNotNullCode() { | ||
v.usingResourceFolder("lombok") | ||
.forClass("ExampleNotNull") | ||
.forCodeMatching(OpcodeMatchers.IFNONNULL.asPredicate()) | ||
.mutantsAreGenerated() | ||
.allMutantsAreFiltered() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void doesNotFilterHandRolledNullChecks() { | ||
v.forClass(HandRolledNullChecks.class) | ||
.forAnyCode() | ||
.mutantsAreGenerated() | ||
.noMutantsAreFiltered() | ||
.verify(); | ||
} | ||
} | ||
|
||
class HandRolledNullChecks { | ||
void mutateMe(String a, String b) { | ||
if (a == null) { | ||
throw new NullPointerException("a is marked non-null but is null"); | ||
} | ||
} | ||
} |
Binary file added
BIN
+722 Bytes
pitest-entry/src/test/resources/sampleClasses/lombok/ExampleNotNull.class.bin
Binary file not shown.