org.pitest.bytecode.analysis.ClassTree Java Examples

The following examples show how to use org.pitest.bytecode.analysis.ClassTree. 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: FilterTester.java    From pitest with Apache License 2.0 6 votes vote down vote up
private List<Sample> samples(final String sample) {
  final Function<String, Stream<Sample>> toPair = compiler -> {
    final String clazz = makeClassName(sample, compiler);
    final Optional<byte[]> bs = FilterTester.this.source.getBytes(clazz);
    if (bs.isPresent()) {
      final Sample p = new Sample();
      p.className = ClassName.fromString(clazz);
      p.clazz = ClassTree.fromBytes(bs.get());
      p.compiler = compiler;
      return Stream.of(p);
    }
    return Stream.empty();

  };
  return COMPILERS.stream().flatMap(toPair).collect(Collectors.toList());
}
 
Example #3
Source File: MutationSource.java    From pitest with Apache License 2.0 6 votes vote down vote up
public Collection<MutationDetails> createMutations(final ClassName clazz) {

    final Mutater m = this.mutationConfig.createMutator(this.source);

    final Collection<MutationDetails> availableMutations = m
        .findMutations(clazz);

    if (availableMutations.isEmpty()) {
      return availableMutations;
    } else {
      final ClassTree tree = ClassTree
          .fromBytes(this.source.getBytes(clazz.asJavaName()).get());

      this.interceptor.begin(tree);
      final Collection<MutationDetails> updatedMutations = this.interceptor
          .intercept(availableMutations, m);
      this.interceptor.end();

      assignTestsToMutations(updatedMutations);

      return updatedMutations;
    }
  }
 
Example #4
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 #5
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 #6
Source File: ExcludedAnnotationInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.skipClass = clazz.annotations().stream()
      .filter(avoidedAnnotation())
      .findFirst().isPresent();
  if (!this.skipClass) {
    final List<Predicate<MutationDetails>> methods = clazz.methods().stream()
        .filter(hasAvoidedAnnotation())
        .map(AnalysisFunctions.matchMutationsInMethod())
        .collect(Collectors.toList());
    this.annotatedMethodMatcher = Prelude.or(methods);
  }
}
 
Example #7
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 #8
Source File: CompoundMutationInterceptorTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotifyAllChildrenOfNewClass() {
  this.testee = new CompoundMutationInterceptor(Arrays.asList(this.modifyChild,this.filterChild));
  final ClassTree aClass = new ClassTree(null);

  this.testee.begin(aClass);
  verify(this.modifyChild).begin(aClass);
  verify(this.filterChild).begin(aClass);
}
 
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: 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 #11
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 #12
Source File: FilterTester.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Sample makeSampleForCurrentCompiler(Class<?> clazz) {
  final ClassloaderByteArraySource source = ClassloaderByteArraySource.fromContext();
  final Sample s = new Sample();
  s.className = ClassName.fromClass(clazz);
  s.clazz = ClassTree.fromBytes(source.getBytes(clazz.getName()).get());
  s.compiler = "current";
  return s;
}
 
Example #13
Source File: FilterTester.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Collection<MutationDetails> filter(ClassTree clazz,
    List<MutationDetails> mutations, Mutater mutator) {
  this.testee.begin(clazz);
  final Collection<MutationDetails> actual = this.testee.intercept(mutations, mutator);
  this.testee.end();
  return actual;
}
 
Example #14
Source File: BaseMethodMatcherTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void shouldNotMatch() throws IOException {
    ClassTree classTree = getClassTree(getTargetClass());
    StopMethodMatcher matcher = getMatcher();
    classTree.methods().stream()
            .filter( method -> !criterion(method))
            .forEach(
                    method -> assertFalse("Incorrectly matched: " + method.rawNode().name, matcher.matches(classTree, method)));
}
 
Example #15
Source File: BaseMethodMatcherTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void shouldMatchMethods() throws IOException {

    ClassTree classTree = getClassTree(getTargetClass());
    StopMethodMatcher matcher = getMatcher();
    classTree.methods().stream()
            .filter(this::criterion)
            .forEach(
                    method -> assertTrue("Could not match method: " + method.rawNode().name, matcher.matches(classTree, method)));
}
 
Example #16
Source File: StopMethodMatcherTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void getClassTree() throws IOException {
    ClassReader reader = new ClassReader(StopMethods.class.getName());
    ClassNode classNode = new ClassNode();
    classTree = new ClassTree(classNode);
    reader.accept(classNode, 0);
}
 
Example #17
Source File: TestUtils.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ClassTree getClassTree(Class<?> target) throws IOException {
    org.objectweb.asm.ClassReader reader = new org.objectweb.asm.ClassReader(target.getName());
    org.objectweb.asm.tree.ClassNode classNode = new org.objectweb.asm.tree.ClassNode();
    ClassTree classTree = new ClassTree(classNode);
    reader.accept(classNode, 0);
    return classTree;
}
 
Example #18
Source File: FilterTester.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Sample sampleForClass(Class<?> clazz) {
  final ClassloaderByteArraySource source = ClassloaderByteArraySource.fromContext();
  final Sample s = new Sample();
  s.className = ClassName.fromClass(clazz);
  s.clazz = ClassTree.fromBytes(source.getBytes(clazz.getName()).get());
  s.compiler = "current";
  return s;
}
 
Example #19
Source File: MutantExportInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.currentClass = clazz.name();
  final String[] classLocation = ("export." + clazz.name().asJavaName())
      .split("\\.");
  final Path classDir = this.fileSystem.getPath(this.outDir, classLocation);
  this.mutantsDir = classDir.resolve("mutants");
  try {
    Files.createDirectories(this.mutantsDir);
    writeBytecodeToDisk(this.source.getBytes(clazz.name().asJavaName()).get(), classDir);
  } catch (final IOException e) {
    throw new RuntimeException("Couldn't create direectory for " + clazz, e);
  }
}
 
Example #20
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 #21
Source File: KotlinInterceptor.java    From pitest-kotlin with Apache License 2.0 5 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  currentClass = clazz;
  isKotlinClass = clazz.annotations().stream()
      .filter(annotationNode -> annotationNode.desc.equals("Lkotlin/Metadata;"))
      .findFirst()
      .isPresent();
}
 
Example #22
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.currentClass = clazz;
}
 
Example #23
Source File: ExcludedAnnotationInterceptorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
ClassTree treeFor(Class<?> clazz) {
  final ClassloaderByteArraySource source = ClassloaderByteArraySource.fromContext();
  return ClassTree.fromBytes(source.getBytes(clazz.getName()).get());
}
 
Example #24
Source File: KotlinFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  // noop
}
 
Example #25
Source File: EqualsPerformanceShortcutFilterTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
ClassTree forClass(Class<?> clazz) {
  final byte[] bs = this.source.getBytes(clazz.getName()).get();
  return ClassTree.fromBytes(bs);
}
 
Example #26
Source File: CompoundMutationInterceptor.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  this.children.forEach(each -> each.begin(clazz));
}
 
Example #27
Source File: LimitNumberOfMutationPerClassFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public void begin(ClassTree clazz) {
  // noop
}
 
Example #28
Source File: MutantExportInterceptorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
private ClassTree tree(Class<?> clazz) {
  return ClassTree.fromBytes(this.source.getBytes(clazz.getName()).get());
}
 
Example #29
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
ClassTree forClass(Class<?> clazz) {
  final byte[] bs = this.source.getBytes(clazz.getName()).get();
  return ClassTree.fromBytes(bs);
}
 
Example #30
Source File: StaticInitializerInterceptorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
private ClassTree treeFor(Class<?> clazz) {
  final ClassloaderByteArraySource source = ClassloaderByteArraySource.fromContext();
  return ClassTree.fromBytes(source.getBytes(clazz.getName()).get());
}