Java源码示例:org.pitest.bytecode.analysis.MethodTree
示例1
private void analyseClass(ClassTree tree) {
final Optional<MethodTree> clinit = tree.methods().stream().filter(nameEquals(CLINIT.name())).findFirst();
if (clinit.isPresent()) {
final List<Predicate<MethodTree>> selfCalls =
clinit.get().instructions().stream()
.flatMap(is(MethodInsnNode.class))
.filter(calls(tree.name()))
.map(toPredicate())
.collect(Collectors.toList());
final Predicate<MethodTree> matchingCalls = Prelude.or(selfCalls);
final Predicate<MutationDetails> initOnlyMethods = Prelude.or(tree.methods().stream()
.filter(isPrivateStatic())
.filter(matchingCalls)
.map(AnalysisFunctions.matchMutationsInMethod())
.collect(Collectors.toList())
);
this.isStaticInitCode = Prelude.or(isInStaticInitializer(), initOnlyMethods);
}
}
示例2
private Collection<MutationDetails> findTimeoutMutants(Location location,
Collection<MutationDetails> mutations, Mutater m) {
final MethodTree method = this.currentClass.methods().stream()
.filter(forLocation(location))
.findFirst()
.get();
// give up if our matcher thinks loop is already infinite
if (infiniteLoopMatcher().matches(method.instructions())) {
return Collections.emptyList();
}
final List<MutationDetails> timeouts = new ArrayList<>();
for ( final MutationDetails each : mutations ) {
// avoid cost of static analysis by first checking mutant is on
// on instruction that could affect looping
if (couldCauseInfiniteLoop(method, each) && isInfiniteLoop(each,m) ) {
timeouts.add(each);
}
}
return timeouts;
}
示例3
void checkNotFiltered(ClassName clazz, Predicate<MethodTree> method) {
boolean testedSomething = false;
for (final Compiler each : Compiler.values()) {
final Optional<MethodTree> mt = parseMethodFromCompiledResource(clazz, each,
method);
if (mt.isPresent()) {
assertThat(testee().infiniteLoopMatcher()
.matches(mt.get().instructions()))
.describedAs("With " + each
+ " compiler matched when it shouldn't " + toString(mt.get()))
.isFalse();
testedSomething = true;
}
}
if (!testedSomething) {
fail("No samples found for test");
}
}
示例4
void checkFiltered(ClassName clazz, Predicate<MethodTree> method) {
boolean testedSomething = false;
for (final Compiler each : Compiler.values()) {
final Optional<MethodTree> mt = parseMethodFromCompiledResource(clazz, each,
method);
if (mt.isPresent()) {
assertThat(testee().infiniteLoopMatcher()
.matches(mt.get().instructions()))
.describedAs("With " + each
+ " compiler did not match as expected " + toString(mt.get()))
.isTrue();
testedSomething = true;
}
}
if (!testedSomething) {
fail("No samples found for test");
}
}
示例5
private static Predicate<MutationDetails> isKotlinJunkMutation(final ClassTree currentClass) {
return a -> {
int instruction = a.getInstructionIndex();
MethodTree method = currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst()
.get();
AbstractInsnNode mutatedInstruction = method.instruction(instruction);
Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return KOTLIN_JUNK.matches(method.instructions(), context);
};
}
示例6
@Override
public Collection<MutationDetails> intercept(Collection<MutationDetails> collection, Mutater mutater) {
return collection.stream().filter(
details -> {
Optional<MethodTree> methodTree = getMethod(details);
return methodTree.isPresent() && !matcher.matches(getCurrentClass(), methodTree.get());
})
.collect(Collectors.toList());
}
示例7
@Override
public void begin(ClassTree clazz) {
this.lines = new HashSet<>();
for (final MethodTree each : clazz.methods()) {
findLoggingLines(each,this.lines);
}
}
示例8
private Predicate<MutationDetails> isAnImplicitNullCheck() {
return a -> {
final int instruction = a.getInstructionIndex();
final MethodTree method = MethodReferenceNullCheckFilter.this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst()
.get();
final AbstractInsnNode mutatedInstruction = method.instruction(instruction);
final Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return NULL_CHECK.matches(method.instructions(), context);
};
}
示例9
private Predicate<MutationDetails> mutatesIteratorLoopPlumbing() {
return a -> {
final int instruction = a.getInstructionIndex();
final MethodTree method = ForEachLoopFilter.this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst()
.get();
final AbstractInsnNode mutatedInstruction = method.instruction(instruction);
final Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return ITERATOR_LOOP.matches(method.instructions(), context);
};
}
示例10
@Override
public void begin(ClassTree clazz) {
this.lines = new HashSet<>();
for (final MethodTree each : clazz.methods()) {
checkMehod(each,this.lines);
}
}
示例11
private Predicate<MutationDetails> isAnImplicitNullCheck() {
return a -> {
final int instruction = a.getInstructionIndex();
final MethodTree method = ImplicitNullCheckFilter.this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst()
.get();
final AbstractInsnNode mutatedInstruction = method.instruction(instruction);
final Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return GET_CLASS_NULL_CHECK.matches(method.instructions(), context);
};
}
示例12
private Predicate<MutationDetails> mutatesAForLoopCounter() {
return a -> {
final int instruction = a.getInstructionIndex();
final MethodTree method = AvoidForLoopCounterFilter.this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst().get();
final AbstractInsnNode mutatedInstruction = method.instruction(instruction);
final Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
return MUTATED_FOR_COUNTER.matches(method.instructions(), context);
};
}
示例13
private boolean isInfiniteLoop(MutationDetails each, Mutater m) {
final ClassTree mutantClass = ClassTree.fromBytes(m.getMutation(each.getId()).getBytes());
final Optional<MethodTree> mutantMethod = mutantClass.methods().stream()
.filter(forLocation(each.getId().getLocation()))
.findFirst();
return infiniteLoopMatcher().matches(mutantMethod.get().instructions());
}
示例14
private Predicate<MutationDetails> isEquivalent(Mutater m) {
return a -> {
if (!MUTATOR_IDS.contains(a.getMutator())) {
return false;
}
final MethodTree method = PrimitiveEquivalentFilter.this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(a.getId().getLocation()))
.findFirst()
.get();
return ZERO_CONSTANTS.contains(method.realInstructionBefore(a.getInstructionIndex()).getOpcode());
};
}
示例15
private List<MutationDetails> filter(
List<MutationDetails> inEquals, Mutater m) {
final Location equalsMethod = inEquals.get(0).getId().getLocation();
final Optional<MethodTree> maybeEquals = this.currentClass.methods().stream()
.filter(MethodMatchers.forLocation(equalsMethod))
.findFirst();
return inEquals.stream()
.filter(isShortcutEquals(maybeEquals.get(), m).negate())
.collect(Collectors.toList());
}
示例16
private Boolean shortCutEquals(MethodTree tree, MutationDetails a, Mutater m) {
if (!mutatesAConditionalJump(tree, a.getInstructionIndex())) {
return false;
}
final ClassTree mutant = ClassTree.fromBytes(m.getMutation(a.getId()).getBytes());
final MethodTree mutantEquals = mutant.methods().stream()
.filter(MethodMatchers.forLocation(tree.asLocation()))
.findFirst()
.get();
return ALWAYS_FALSE.matches(mutantEquals.instructions());
}
示例17
private Function<MutationDetails, Loc> toLocation(final ClassTree tree) {
return a -> {
final MethodTree method = tree.method(a.getId().getLocation()).get();
final Loc l = new Loc();
l.index = a.getInstructionIndex();
l.node = method.instruction(a.getInstructionIndex());
return l;
};
}
示例18
private String toString(MethodTree mt) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final TraceMethodVisitor mv = new TraceMethodVisitor(new Textifier());
mt.rawNode().accept(mv);
try (PrintWriter pw = new PrintWriter(bos)) {
mv.p.print(pw);
}
return "Byte code is \n" + new String(bos.toByteArray());
}
示例19
private Optional<MethodTree> parseMethodFromCompiledResource(ClassName clazz,
Compiler compiler, Predicate<MethodTree> method) {
final ResourceFolderByteArraySource source = new ResourceFolderByteArraySource();
final Optional<byte[]> bs = source.getBytes("loops/" + compiler.name() + "/" + clazz.getNameWithoutPackage().asJavaName());
if (bs.isPresent()) {
final ClassTree tree = ClassTree.fromBytes(bs.get());
return tree.methods().stream().filter(method).findFirst();
}
return Optional.empty();
}
示例20
private boolean allows(MutationDetails details) {
Optional<MethodTree> method = getMethod(details);
return !details.getMutator().equals("null") || (method.isPresent() && canBeNull(method.get()));
}
示例21
private boolean canBeNull(MethodTree method) {
return method.annotations()
.stream()
.noneMatch(anotation -> anotation.desc.equals("Lorg/jetbrains/annotations/NotNull;"));
}
示例22
static boolean matchesNameDesc(MethodTree methodTree, String name, String desc) {
MethodNode node = methodTree.rawNode();
return node.name.equals(name) && node.desc.equals(desc);
}
示例23
static boolean matchesAccess(MethodTree methodTree, int access) {
return (methodTree.rawNode().access & access) != 0;
}
示例24
public Optional<MethodTree> getMethod(MutationDetails details) {
return classTree.method(details.getId().getLocation());
}
示例25
@Override
public boolean criterion(MethodTree method) {
String name = method.rawNode().name;
return name.startsWith("delegate") || name.equals("<init>"); //This class has a default constructor wich is in fact a delegation
}
示例26
@Override
public boolean criterion(MethodTree method) {
return true;
}
示例27
private MethodTree getMethod(String name) {
return classTree.methods().stream()
.filter( methodTree -> name.equals(methodTree.rawNode().name))
.findFirst()
.orElse(null);
}
示例28
@Override
public boolean criterion(MethodTree method) {
return method.rawNode().name.startsWith("get");
}
示例29
@Override
public boolean criterion(MethodTree method) {
return method.rawNode().name.equals("returnNull");
}
示例30
@Override
public boolean criterion(MethodTree method) {
return method.rawNode().name.startsWith("set");
}