org.pitest.bytecode.analysis.MethodTree Java Examples

The following examples show how to use org.pitest.bytecode.analysis.MethodTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StaticInitializerInterceptor.java    From pitest with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #2
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 6 votes vote down vote up
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");
  }
}
 
Example #3
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 6 votes vote down vote up
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");
  }
}
 
Example #4
Source File: InfiniteLoopFilter.java    From pitest with Apache License 2.0 6 votes vote down vote up
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;

}
 
Example #5
Source File: InfiniteLoopFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #6
Source File: KotlinInterceptor.java    From pitest-kotlin with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #7
Source File: MethodReferenceNullCheckFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #8
Source File: ForEachLoopFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #9
Source File: TryWithResourcesFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.lines = new HashSet<>();
  for (final MethodTree each : clazz.methods()) {
    checkMehod(each,this.lines);
  }
}
 
Example #10
Source File: ImplicitNullCheckFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #11
Source File: AvoidForLoopCounterFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #12
Source File: LoggingCallsFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.lines = new HashSet<>();
  for (final MethodTree each : clazz.methods()) {
    findLoggingLines(each,this.lines);
  }
}
 
Example #13
Source File: EquivalentReturnMutationFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
  };
}
 
Example #14
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #15
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #16
Source File: FilterTester.java    From pitest with Apache License 2.0 5 votes vote down vote up
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;
  };
}
 
Example #17
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #18
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #19
Source File: StopMethodInterceptor.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #20
Source File: SimpleGetterMatcherTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean criterion(MethodTree method) {
    return  method.rawNode().name.startsWith("get");
}
 
Example #21
Source File: AvoidNullInNotNullInterceptor.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean allows(MutationDetails details) {
    Optional<MethodTree> method = getMethod(details);
    return !details.getMutator().equals("null") || (method.isPresent() && canBeNull(method.get()));
}
 
Example #22
Source File: AvoidNullInNotNullInterceptor.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean canBeNull(MethodTree method) {
    return method.annotations()
            .stream()
            .noneMatch(anotation -> anotation.desc.equals("Lorg/jetbrains/annotations/NotNull;"));
}
 
Example #23
Source File: StopMethodMatcher.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
static boolean matchesNameDesc(MethodTree methodTree, String name, String desc) {
    MethodNode node = methodTree.rawNode();
    return node.name.equals(name) && node.desc.equals(desc);
}
 
Example #24
Source File: StopMethodMatcher.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
static boolean matchesAccess(MethodTree methodTree, int access) {
    return (methodTree.rawNode().access & access) != 0;
}
 
Example #25
Source File: ExcludedAnnotationInterceptor.java    From pitest with Apache License 2.0 4 votes vote down vote up
private Predicate<MethodTree> hasAvoidedAnnotation() {
  return a -> a.annotations().stream()
      .filter(avoidedAnnotation())
      .findFirst().isPresent();
}
 
Example #26
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
private boolean mutatesAConditionalJump(MethodTree tree, int index) {
  final AbstractInsnNode mutatedInsns = tree.instruction(index);
  return InstructionMatchers.aConditionalJump().test(null, mutatedInsns);
}
 
Example #27
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
private Predicate<MutationDetails> isShortcutEquals(final MethodTree tree, final Mutater m) {
  return a -> shortCutEquals(tree,a, m);
}
 
Example #28
Source File: MutationFilter.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Optional<MethodTree> getMethod(MutationDetails details) {
    return classTree.method(details.getId().getLocation());
}
 
Example #29
Source File: DelegateMethodMatcherTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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
}
 
Example #30
Source File: DeprecatedClassTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean criterion(MethodTree method) {
    return true;
}