From 7d01ce81f0ca32273bc6140a3c42f13a0d48c151 Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Thu, 17 Aug 2023 10:31:26 +0100 Subject: [PATCH 1/2] remove calls to legacy map method --- .../pitest/aggregate/CodeSourceAggregator.java | 10 +++++++--- .../mutationtest/commandline/OptionsParser.java | 11 +++++++---- .../org/pitest/bytecode/analysis/ClassTree.java | 7 +++++-- .../java/org/pitest/coverage/CoverageData.java | 11 +++++++---- .../pitest/mutationtest/MutationStatusMap.java | 5 +++-- .../mutationtest/build/MutationTestBuilder.java | 5 +++-- .../pitest/mutationtest/config/ReportOptions.java | 7 ++++--- .../statistics/MutationStatisticsPrecursor.java | 5 ++++- .../mutationtest/tooling/SmartSourceLocator.java | 4 +++- .../org/pitest/coverage/CoverageDataTest.java | 5 ++++- .../org/pitest/mutationtest/NullAnalyser.java | 5 ++++- .../build/DefaultTestPrioritiserTest.java | 12 +++++------- .../report/html/AnnotatedLineFactory.java | 5 ++++- .../report/html/MutationHtmlReportListener.java | 9 +++++---- .../report/html/InputStreamLineIterableTest.java | 3 +-- .../maven/MojoToReportOptionsConverter.java | 10 ++++++++-- .../pitest/maven/PathToJavaClassConverter.java | 12 ++++++------ .../src/main/java/org/pitest/maven/ScmMojo.java | 10 +++++++--- .../report/AbstractPitAggregationReportMojo.java | 5 ++++- .../java/org/pitest/maven/BasePitMojoTest.java | 7 ++++--- .../pitest/classpath/CompoundClassPathRoot.java | 7 ++++--- .../main/java/org/pitest/functional/Streams.java | 15 +++++++++++++++ .../junit/JUnitCustomRunnerTestUnitFinder.java | 5 ++++- .../engine/gregor/GregorMutationEngine.java | 6 ++++-- .../engine/gregor/config/Mutator.java | 8 +++++--- .../java/org/pitest/plugin/FeatureParser.java | 9 ++++++--- pitest/src/main/java/org/pitest/util/Glob.java | 10 ++++++---- 27 files changed, 139 insertions(+), 69 deletions(-) diff --git a/pitest-aggregator/src/main/java/org/pitest/aggregate/CodeSourceAggregator.java b/pitest-aggregator/src/main/java/org/pitest/aggregate/CodeSourceAggregator.java index 244e0f350..7b82be33e 100644 --- a/pitest-aggregator/src/main/java/org/pitest/aggregate/CodeSourceAggregator.java +++ b/pitest-aggregator/src/main/java/org/pitest/aggregate/CodeSourceAggregator.java @@ -8,7 +8,6 @@ import org.pitest.classpath.DirectoryClassPathRoot; import org.pitest.classpath.PathFilter; import org.pitest.classpath.ProjectClassPaths; -import org.pitest.functional.FCollection; import org.pitest.functional.prelude.Prelude; import org.pitest.mutationtest.config.DefaultCodePathPredicate; import org.pitest.mutationtest.config.DefaultDependencyPathPredicate; @@ -21,6 +20,7 @@ import java.util.HashSet; import java.util.function.Function; import java.util.function.Predicate; +import java.util.stream.Collectors; class CodeSourceAggregator { @@ -49,10 +49,14 @@ private Predicate createClassPredicate() { for (final File buildOutputDirectory : this.compiledCodeDirectories) { if (buildOutputDirectory.exists()) { final DirectoryClassPathRoot dcRoot = new DirectoryClassPathRoot(buildOutputDirectory); - classes.addAll(FCollection.map(dcRoot.classNames(), toPredicate())); + classes.addAll(dcRoot.classNames().stream() + .map(toPredicate()) + .collect(Collectors.toList())); } } - return Prelude.or(FCollection.map(classes, Glob.toGlobPredicate())); + return Prelude.or(classes.stream() + .map(Glob.toGlobPredicate()) + .collect(Collectors.toList())); } private Function toPredicate() { diff --git a/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/OptionsParser.java b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/OptionsParser.java index 3f8dcef9a..5719e1f7f 100644 --- a/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/OptionsParser.java +++ b/pitest-command-line/src/main/java/org/pitest/mutationtest/commandline/OptionsParser.java @@ -46,6 +46,7 @@ import java.util.logging.Logger; import java.util.stream.Collectors; +import static org.pitest.functional.Streams.asStream; import static org.pitest.mutationtest.config.ConfigOption.ARG_LINE; import static org.pitest.mutationtest.config.ConfigOption.AVOID_CALLS; import static org.pitest.mutationtest.config.ConfigOption.CHILD_JVM; @@ -437,8 +438,9 @@ private ParseResult parseCommandLine(final ReportOptions data, final OptionSet userArgs) { data.setReportDir(this.reportDirSpec.value(userArgs)); data.setTargetClasses(this.targetClassesSpec.values(userArgs)); - data.setTargetTests(FCollection.map(this.targetTestsSpec.values(userArgs), - Glob.toGlobPredicate())); + data.setTargetTests(asStream(this.targetTestsSpec.values(userArgs)) + .map(Glob.toGlobPredicate()) + .collect(Collectors.toList())); data.setSourceDirs(asPaths(this.sourceDirSpec.values(userArgs))); data.setMutators(this.mutators.values(userArgs)); data.setFeatures(this.features.values(userArgs)); @@ -467,8 +469,9 @@ private ParseResult parseCommandLine(final ReportOptions data, data.setLoggingClasses(this.avoidCallsSpec.values(userArgs)); data.setExcludedMethods(this.excludedMethodsSpec.values(userArgs)); data.setExcludedClasses(this.excludedClassesSpec.values(userArgs)); - data.setExcludedTestClasses(FCollection.map( - this.excludedTestClassesSpec.values(userArgs), Glob.toGlobPredicate())); + data.setExcludedTestClasses(asStream(this.excludedTestClassesSpec.values(userArgs)). + map(Glob.toGlobPredicate()) + .collect(Collectors.toList())); configureVerbosity(data, userArgs); data.addOutputFormats(this.outputFormatSpec.values(userArgs)); diff --git a/pitest-entry/src/main/java/org/pitest/bytecode/analysis/ClassTree.java b/pitest-entry/src/main/java/org/pitest/bytecode/analysis/ClassTree.java index 677b75384..4b15f26f9 100644 --- a/pitest-entry/src/main/java/org/pitest/bytecode/analysis/ClassTree.java +++ b/pitest-entry/src/main/java/org/pitest/bytecode/analysis/ClassTree.java @@ -21,9 +21,10 @@ import org.objectweb.asm.util.Textifier; import org.objectweb.asm.util.TraceClassVisitor; import org.pitest.classinfo.ClassName; -import org.pitest.functional.FCollection; import org.pitest.mutationtest.engine.Location; +import static org.pitest.functional.Streams.asStream; + public class ClassTree { private final ClassNode rawNode; @@ -44,7 +45,9 @@ public List methods() { if (this.lazyMethods != null) { return this.lazyMethods; } - this.lazyMethods = FCollection.map(this.rawNode.methods, toTree(name())); + this.lazyMethods = asStream(this.rawNode.methods) + .map(toTree(name())) + .collect(Collectors.toList()); return this.lazyMethods; } diff --git a/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java b/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java index fde7b4778..6c974a96d 100644 --- a/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java +++ b/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java @@ -18,7 +18,6 @@ import org.pitest.classinfo.ClassInfo; import org.pitest.classinfo.ClassName; import org.pitest.classpath.CodeSource; -import org.pitest.functional.FCollection; import org.pitest.testapi.Description; import org.pitest.util.Log; @@ -124,12 +123,16 @@ public BigInteger getCoverageIdForClass(final ClassName clazz) { } public List createCoverage() { - return FCollection.map(this.blockCoverage.entrySet(), toBlockCoverage()); + return this.blockCoverage.entrySet().stream() + .map(toBlockCoverage()) + .collect(Collectors.toList()); } private static Function>, BlockCoverage> toBlockCoverage() { - return a -> new BlockCoverage(a.getKey(), FCollection.map(a.getValue(), - TestInfo.toName())); + return a -> new BlockCoverage(a.getKey(), + a.getValue().stream() + .map(TestInfo.toName()) + .collect(Collectors.toList())); } @Override diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java b/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java index 1ba51f740..e086342b2 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java @@ -50,8 +50,9 @@ public void setStatusForMutations( } public List createMutationResults() { - return FCollection.map(this.mutationMap.entrySet(), - detailsToMutationResults()); + return this.mutationMap.entrySet().stream() + .map(detailsToMutationResults()) + .collect(Collectors.toList()); } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java index f955211a4..690ae4a27 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java @@ -107,8 +107,9 @@ private static Predicate statusNotKnown() { } private static Function> mutationDetailsToTestClass() { - return a -> FCollection.map(a.getTestsInOrder(), - TestInfo.toDefiningClassName()); + return a -> a.getTestsInOrder().stream() + .map(TestInfo.toDefiningClassName()) + .collect(Collectors.toList()); } } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java b/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java index 007a8ae89..216e01247 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/config/ReportOptions.java @@ -19,7 +19,6 @@ import org.pitest.classpath.ClassPathRoot; import org.pitest.classpath.PathFilter; import org.pitest.classpath.ProjectClassPaths; -import org.pitest.functional.FCollection; import org.pitest.functional.prelude.Prelude; import org.pitest.help.Help; import org.pitest.help.PitHelpError; @@ -53,6 +52,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; +import static org.pitest.functional.Streams.asStream; import static org.pitest.functional.prelude.Prelude.not; import static org.pitest.functional.prelude.Prelude.or; @@ -246,8 +246,9 @@ public ClassPath getClassPath() { } private ClassPath createClassPathFromElements() { - return new ClassPath( - FCollection.map(this.classPathElements, File::new)); + return new ClassPath(asStream(this.classPathElements) + .map(File::new) + .collect(Collectors.toList())); } public Collection getTargetClasses() { diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java index e3cde2864..d1b5eea41 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java @@ -7,6 +7,7 @@ import java.util.Set; import java.util.function.BiFunction; import java.util.function.Consumer; +import java.util.stream.Collectors; import org.pitest.classinfo.ClassName; import org.pitest.functional.FCollection; @@ -55,7 +56,9 @@ public MutationStatistics toStatistics() { } Iterable getScores() { - return FCollection.map(this.mutatorTotalMap.values(), ScorePrecursor::toScore); + return this.mutatorTotalMap.values().stream() + .map(ScorePrecursor::toScore) + .collect(Collectors.toList()); } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java index 45333b3cc..37eca388b 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java @@ -45,7 +45,9 @@ public SmartSourceLocator(final Collection roots, Charset inputCharset) { final Collection childDirs = FCollection.flatMap(roots, collectChildren(MAX_DEPTH)); childDirs.addAll(roots); - this.children = FCollection.map(childDirs, f -> new DirectorySourceLocator(f, this.inputCharset)); + this.children = childDirs.stream() + .map(f -> new DirectorySourceLocator(f, this.inputCharset)) + .collect(Collectors.toList()); } private Function> collectChildren(final int depth) { diff --git a/pitest-entry/src/test/java/org/pitest/coverage/CoverageDataTest.java b/pitest-entry/src/test/java/org/pitest/coverage/CoverageDataTest.java index f39a703a1..5a2beb71e 100644 --- a/pitest-entry/src/test/java/org/pitest/coverage/CoverageDataTest.java +++ b/pitest-entry/src/test/java/org/pitest/coverage/CoverageDataTest.java @@ -40,6 +40,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -116,7 +117,9 @@ public void shouldReturnUniqueTestsForClassWhenSomeTestsCoverClass() { this.testee.calculateClassCoverage(makeCoverageResult("foo", "fooTest2", 0, 2)); - List actual = FCollection.map(this.testee.getTestsForClass(this.foo), TestInfo::getName); + List actual = this.testee.getTestsForClass(this.foo).stream() + .map(TestInfo::getName) + .collect(Collectors.toList()); assertThat(actual).containsExactlyInAnyOrder("fooTest", "fooTest2"); } diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/NullAnalyser.java b/pitest-entry/src/test/java/org/pitest/mutationtest/NullAnalyser.java index eff133df7..d8fc4dd31 100644 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/NullAnalyser.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/NullAnalyser.java @@ -2,6 +2,7 @@ import java.util.Collection; import java.util.function.Function; +import java.util.stream.Collectors; import org.pitest.functional.FCollection; import org.pitest.mutationtest.engine.MutationDetails; @@ -15,7 +16,9 @@ public class NullAnalyser implements MutationAnalyser { @Override public Collection analyse( final Collection mutationsForClasses) { - return FCollection.map(mutationsForClasses, mutationToResult()); + return mutationsForClasses.stream() + .map(mutationToResult()) + .collect(Collectors.toList()); } private Function mutationToResult() { diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/build/DefaultTestPrioritiserTest.java b/pitest-entry/src/test/java/org/pitest/mutationtest/build/DefaultTestPrioritiserTest.java index 8476b13dd..aeca51235 100644 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/build/DefaultTestPrioritiserTest.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/build/DefaultTestPrioritiserTest.java @@ -6,7 +6,6 @@ import static org.mockito.Mockito.when; import static org.pitest.mutationtest.LocationMother.aLocation; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; @@ -20,8 +19,9 @@ import org.pitest.coverage.BlockLocation; import org.pitest.coverage.CoverageDatabase; import org.pitest.coverage.TestInfo; -import org.pitest.functional.FCollection; import java.util.Optional; +import java.util.stream.Collectors; + import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.MutationIdentifier; @@ -60,9 +60,6 @@ public void shouldPrioritiseTestsByExecutionTime() { final List actual = this.testee.assignTests(makeMutation("foo")); assertThat(actual.stream().map(toTime())).contains(1, 100, 1000, 10000); - - assertEquals(Arrays.asList(1, 100, 1000, 10000), - FCollection.map(actual, toTime())); } private Function toTime() { @@ -70,8 +67,9 @@ private Function toTime() { } private List makeTestInfos(final Integer... times) { - return new ArrayList<>(FCollection.map(Arrays.asList(times), - timeToTestInfo())); + return Arrays.stream(times) + .map(timeToTestInfo()) + .collect(Collectors.toList()); } private Function timeToTestInfo() { diff --git a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/AnnotatedLineFactory.java b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/AnnotatedLineFactory.java index bcd0fe38b..0f5afbf59 100644 --- a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/AnnotatedLineFactory.java +++ b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/AnnotatedLineFactory.java @@ -28,6 +28,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.StreamSupport; public class AnnotatedLineFactory { @@ -55,7 +56,9 @@ private Set findCoveredLines(Collection classes, ReportCove public List convert(final Reader source) throws IOException { try { final InputStreamLineIterable lines = new InputStreamLineIterable(source); - return FCollection.map(lines, stringToAnnotatedLine()); + return StreamSupport.stream(lines.spliterator(), false) + .map(stringToAnnotatedLine()) + .collect(Collectors.toList()); } finally { source.close(); } diff --git a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationHtmlReportListener.java b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationHtmlReportListener.java index 51654ac42..bd0db514d 100644 --- a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationHtmlReportListener.java +++ b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationHtmlReportListener.java @@ -18,7 +18,6 @@ import org.antlr.stringtemplate.StringTemplateGroup; import org.pitest.coverage.ClassLines; import org.pitest.coverage.ReportCoverage; -import org.pitest.functional.FCollection; import org.pitest.mutationtest.ClassMutationResults; import org.pitest.mutationtest.MutationResultListener; import org.pitest.mutationtest.SourceLocator; @@ -41,6 +40,7 @@ import java.util.Set; import java.util.function.Function; import java.util.logging.Level; +import java.util.stream.Collectors; import static java.util.Arrays.asList; @@ -159,9 +159,10 @@ private List createAnnotatedSourceCodeLines(final String sourceFile, return Collections.emptyList(); } - private Collection classInfoToNames( - final Collection classes) { - return FCollection.map(classes, classInfoToJavaName()); + private Collection classInfoToNames(Collection classes) { + return classes.stream() + .map(classInfoToJavaName()) + .collect(Collectors.toList()); } private Function classInfoToJavaName() { diff --git a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/InputStreamLineIterableTest.java b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/InputStreamLineIterableTest.java index ed41193ac..7fa7a2ecb 100644 --- a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/InputStreamLineIterableTest.java +++ b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/InputStreamLineIterableTest.java @@ -20,7 +20,6 @@ import org.junit.Before; import org.junit.Test; -import org.pitest.functional.FCollection; public class InputStreamLineIterableTest { @@ -34,7 +33,7 @@ public void setUp() { @Test public void shouldReadAllInput() { - assertThat(FCollection.map(testee, s -> s)).containsExactly("1", "2", "3"); + assertThat(testee).containsExactly("1", "2", "3"); } } diff --git a/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java b/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java index fc77fb78e..f40561324 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java +++ b/pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java @@ -52,6 +52,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.pitest.functional.Streams.asStream; + public class MojoToReportOptionsConverter { private final AbstractPitMojo mojo; @@ -350,11 +352,15 @@ private void addOwnDependenciesToClassPath(final List classPath) { private Collection> globStringsToPredicates( final List excludedMethods) { - return FCollection.map(excludedMethods, Glob.toGlobPredicate()); + return asStream(excludedMethods) + .map(Glob.toGlobPredicate()) + .collect(Collectors.toList()); } private Collection> determineTargetTests() { - return FCollection.map(useConfiguredTargetTestsOrFindOccupiedPackages(this.mojo.getTargetTests()), Glob.toGlobPredicate()); + return useConfiguredTargetTestsOrFindOccupiedPackages(this.mojo.getTargetTests()).stream() + .map(Glob.toGlobPredicate()) + .collect(Collectors.toList()); } private Collection useConfiguredTargetTestsOrFindOccupiedPackages( diff --git a/pitest-maven/src/main/java/org/pitest/maven/PathToJavaClassConverter.java b/pitest-maven/src/main/java/org/pitest/maven/PathToJavaClassConverter.java index 158a0768f..bd3a4944c 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/PathToJavaClassConverter.java +++ b/pitest-maven/src/main/java/org/pitest/maven/PathToJavaClassConverter.java @@ -1,9 +1,9 @@ package org.pitest.maven; import java.io.File; -import java.util.Collections; import java.util.function.Function; +import java.util.stream.Stream; /** * Converts paths to java class names globs if they are within the supplied @@ -15,7 +15,7 @@ * with the supplied filenames. * */ -class PathToJavaClassConverter implements Function> { +class PathToJavaClassConverter implements Function> { private final String sourceRoot; @@ -24,7 +24,7 @@ class PathToJavaClassConverter implements Function> { } @Override - public Iterable apply(final String a) { + public Stream apply(final String a) { final File f = new File(a); final String modifiedFilePath = f.getAbsolutePath(); final String fileName = f.getName(); @@ -33,17 +33,17 @@ public Iterable apply(final String a) { && (fileName.indexOf('.') != -1)) { return createClassGlobFromFilePath(this.sourceRoot, modifiedFilePath); } - return Collections.emptyList(); + return Stream.empty(); } - private Iterable createClassGlobFromFilePath(final String sourceRoot, + private Stream createClassGlobFromFilePath(final String sourceRoot, final String modifiedFilePath) { final String rootedPath = modifiedFilePath.substring(sourceRoot.length() + 1); // some scms report paths in portable format, some in os specific format (i // think) // replace both possibilities regardless of host os - return Collections.singleton(stripFileExtension(rootedPath).replace('/', + return Stream.of(stripFileExtension(rootedPath).replace('/', '.').replace('\\', '.') + "*"); } diff --git a/pitest-maven/src/main/java/org/pitest/maven/ScmMojo.java b/pitest-maven/src/main/java/org/pitest/maven/ScmMojo.java index 206f70af6..5912dc63b 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/ScmMojo.java +++ b/pitest-maven/src/main/java/org/pitest/maven/ScmMojo.java @@ -11,6 +11,8 @@ import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; @@ -161,9 +163,11 @@ private List findModifiedClassNames() throws MojoExecutionException { final File sourceRoot = new File(this.getProject().getBuild() .getSourceDirectory()); - final List modifiedPaths = FCollection.map(findModifiedPaths(), pathByScmDir()); - return FCollection.flatMap(modifiedPaths, new PathToJavaClassConverter( - sourceRoot.getAbsolutePath())); + final Stream modifiedPaths = findModifiedPaths().stream() + .map(pathByScmDir()); + return modifiedPaths.flatMap(new PathToJavaClassConverter( + sourceRoot.getAbsolutePath())) + .collect(Collectors.toList()); } diff --git a/pitest-maven/src/main/java/org/pitest/maven/report/AbstractPitAggregationReportMojo.java b/pitest-maven/src/main/java/org/pitest/maven/report/AbstractPitAggregationReportMojo.java index 9597dacf7..63a9c4036 100644 --- a/pitest-maven/src/main/java/org/pitest/maven/report/AbstractPitAggregationReportMojo.java +++ b/pitest-maven/src/main/java/org/pitest/maven/report/AbstractPitAggregationReportMojo.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.List; import java.util.Locale; +import java.util.stream.Collectors; /** * Common code for report aggregation mojo. @@ -136,7 +137,9 @@ private List convertToRootDirs(final List... directoryLists) { for (final List directoryList : directoryLists) { roots.addAll(directoryList); } - return FCollection.map(roots, File::new); + return roots.stream() + .map(File::new) + .collect(Collectors.toList()); } private List getProjectFilesByFilter(final File projectBaseDir, diff --git a/pitest-maven/src/test/java/org/pitest/maven/BasePitMojoTest.java b/pitest-maven/src/test/java/org/pitest/maven/BasePitMojoTest.java index 51526fe2a..21419f6de 100644 --- a/pitest-maven/src/test/java/org/pitest/maven/BasePitMojoTest.java +++ b/pitest-maven/src/test/java/org/pitest/maven/BasePitMojoTest.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.Properties; import java.util.function.Predicate; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Build; @@ -35,7 +36,6 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.pitest.classpath.ClassPath; -import org.pitest.functional.FCollection; import org.pitest.mutationtest.config.PluginServices; import edu.emory.mathcs.backport.java.util.Collections; @@ -62,8 +62,9 @@ public abstract class BasePitMojoTest extends AbstractMojoTestCase { protected void setUp() throws Exception { super.setUp(); MockitoAnnotations.openMocks(this); - this.classPath = new ArrayList<>(FCollection.map( - ClassPath.getClassPathElementsAsFiles(), File::getAbsolutePath)); + this.classPath = ClassPath.getClassPathElementsAsFiles().stream() + .map(File::getAbsolutePath) + .collect(Collectors.toList()); when(this.project.getProperties()).thenReturn(properties); when(this.project.getTestClasspathElements()).thenReturn(this.classPath); when(this.project.getPackaging()).thenReturn("jar"); diff --git a/pitest/src/main/java/org/pitest/classpath/CompoundClassPathRoot.java b/pitest/src/main/java/org/pitest/classpath/CompoundClassPathRoot.java index 8e9d1c8c7..768ec25a9 100644 --- a/pitest/src/main/java/org/pitest/classpath/CompoundClassPathRoot.java +++ b/pitest/src/main/java/org/pitest/classpath/CompoundClassPathRoot.java @@ -1,7 +1,5 @@ package org.pitest.classpath; -import org.pitest.functional.FCollection; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -11,6 +9,7 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public class CompoundClassPathRoot implements ClassPathRoot, Iterable { @@ -83,7 +82,9 @@ public Iterator iterator() { private static List wrapToAvoidIOOperations( List roots) { - return FCollection.map(roots, NameCachingRoot.toCachingRoot()); + return roots.stream() + .map(NameCachingRoot.toCachingRoot()) + .collect(Collectors.toList()); } diff --git a/pitest/src/main/java/org/pitest/functional/Streams.java b/pitest/src/main/java/org/pitest/functional/Streams.java index 69c8115f8..be533b50b 100644 --- a/pitest/src/main/java/org/pitest/functional/Streams.java +++ b/pitest/src/main/java/org/pitest/functional/Streams.java @@ -1,9 +1,24 @@ package org.pitest.functional; +import java.util.Collection; import java.util.stream.Stream; public class Streams { public static Stream fromOptional(java.util.Optional opt) { return opt.map(Stream::of).orElseGet(Stream::empty); } + + /** + * Convert to stream with a null check. Added for easy migration + * from legacy FCollection class. + * + * @param c a collection + * @return a stream + */ + public static Stream asStream(Collection c) { + if (c == null) { + return Stream.empty(); + } + return c.stream(); + } } diff --git a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java index dd04af72c..736596d40 100644 --- a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java +++ b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java @@ -27,6 +27,7 @@ import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -127,7 +128,9 @@ private Function> toCategoryNames() { if (a == null) { return Collections.emptyList(); } - return FCollection.map(Arrays.asList(a.value()), Class::getName); + return Stream.of(a.value()) + .map(Class::getName) + .collect(Collectors.toList()); }; } diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/GregorMutationEngine.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/GregorMutationEngine.java index 2eb940df5..7f217b4cd 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/GregorMutationEngine.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/GregorMutationEngine.java @@ -18,9 +18,9 @@ import java.util.LinkedHashSet; import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; import org.pitest.classinfo.ClassByteArraySource; -import org.pitest.functional.FCollection; import org.pitest.mutationtest.engine.Mutater; import org.pitest.mutationtest.engine.MutationEngine; @@ -54,7 +54,9 @@ public String toString() { @Override public Collection getMutatorNames() { - return FCollection.map(this.mutationOperators, MethodMutatorFactory::getName); + return this.mutationOperators.stream() + .map(MethodMutatorFactory::getName) + .collect(Collectors.toList()); } @Override diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java index 48b6eec39..f54375f32 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java @@ -15,7 +15,6 @@ package org.pitest.mutationtest.engine.gregor.config; import org.pitest.functional.FCollection; -import org.pitest.functional.prelude.Prelude; import org.pitest.help.Help; import org.pitest.help.PitHelpError; import org.pitest.mutationtest.engine.gregor.MethodMutatorFactory; @@ -44,6 +43,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import static org.pitest.functional.Streams.asStream; + public final class Mutator { private static final Map> MUTATORS; @@ -102,8 +103,9 @@ private static Collection group( } public static Collection byName(final String name) { - return FCollection.map(MUTATORS.get(name), - Prelude.id(MethodMutatorFactory.class)); + // prevent null collection returns? + return asStream(MUTATORS.get(name)) + .collect(Collectors.toList()); } public static Collection fromStrings( diff --git a/pitest/src/main/java/org/pitest/plugin/FeatureParser.java b/pitest/src/main/java/org/pitest/plugin/FeatureParser.java index de9872a8d..4c3f4e358 100644 --- a/pitest/src/main/java/org/pitest/plugin/FeatureParser.java +++ b/pitest/src/main/java/org/pitest/plugin/FeatureParser.java @@ -1,13 +1,14 @@ package org.pitest.plugin; -import org.pitest.functional.FCollection; - import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.pitest.functional.Streams.asStream; /** * Cheap and cheerful parser for simple feature config language. @@ -18,7 +19,9 @@ public class FeatureParser { public List parseFeatures(Collection config) { - return FCollection.map(config, stringToSettings()); + return asStream(config) + .map(stringToSettings()) + .collect(Collectors.toList()); } private Function stringToSettings() { diff --git a/pitest/src/main/java/org/pitest/util/Glob.java b/pitest/src/main/java/org/pitest/util/Glob.java index ec67c6368..00789d92d 100644 --- a/pitest/src/main/java/org/pitest/util/Glob.java +++ b/pitest/src/main/java/org/pitest/util/Glob.java @@ -18,8 +18,9 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.regex.Pattern; +import java.util.stream.Collectors; -import org.pitest.functional.FCollection; +import static org.pitest.functional.Streams.asStream; public class Glob implements Predicate { @@ -47,9 +48,10 @@ public static Function> toGlobPredicate() { return Glob::new; } - public static Collection> toGlobPredicates( - final Collection globs) { - return FCollection.map(globs, Glob.toGlobPredicate()); + public static Collection> toGlobPredicates(Collection globs) { + return asStream(globs) + .map(Glob.toGlobPredicate()) + .collect(Collectors.toList()); } private static String convertGlobToRegex(final String glob) { From 0e2bfd28ac4ee0c646ad840ddf2f9e55ae75ba8d Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Thu, 17 Aug 2023 11:14:15 +0100 Subject: [PATCH 2/2] deprecate methods and remove calls to flatmap --- .../mutationtest/build/MutationTestBuilder.java | 4 +++- .../mutationtest/tooling/SmartSourceLocator.java | 11 ++++------- .../mutationtest/build/MutationSourceTest.java | 6 ++++-- .../java/org/pitest/functional/FCollection.java | 15 +++++++++++---- .../junit/JUnitCustomRunnerTestUnitFinder.java | 12 ++++++------ 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java index 690ae4a27..260a18607 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java @@ -56,7 +56,9 @@ public List createMutationTestUnits( final Collection codeClasses) { final List tus = new ArrayList<>(); - final List mutations = FCollection.flatMap(codeClasses, mutationSource::createMutations); + final List mutations = codeClasses.stream() + .flatMap(c -> mutationSource.createMutations(c).stream()) + .collect(Collectors.toList()); mutations.sort(comparing(MutationDetails::getId)); diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java index 37eca388b..4eb5ddb44 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/SmartSourceLocator.java @@ -23,9 +23,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.function.Function; -import org.pitest.functional.FCollection; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -42,7 +40,10 @@ public class SmartSourceLocator implements SourceLocator { public SmartSourceLocator(final Collection roots, Charset inputCharset) { this.inputCharset = inputCharset; - final Collection childDirs = FCollection.flatMap(roots, collectChildren(MAX_DEPTH)); + final Collection childDirs = roots.stream() + .flatMap(r -> collectDirectories(r, MAX_DEPTH).stream()) + .collect(Collectors.toList()); + childDirs.addAll(roots); this.children = childDirs.stream() @@ -50,10 +51,6 @@ public SmartSourceLocator(final Collection roots, Charset inputCharset) { .collect(Collectors.toList()); } - private Function> collectChildren(final int depth) { - return a -> collectDirectories(a, depth); - } - private Collection collectDirectories(Path root, int depth) { try { if (!Files.exists(root)) { diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationSourceTest.java b/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationSourceTest.java index 1e739357d..a826d6142 100644 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationSourceTest.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/build/MutationSourceTest.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import java.util.function.Function; +import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -77,8 +78,9 @@ public void shouldAssignTestsFromPrioritiserToMutant() { } private List makeTestInfos(final Integer... times) { - return new ArrayList<>(FCollection.map(Arrays.asList(times), - timeToTestInfo())); + return Arrays.stream(times) + .map(timeToTestInfo()) + .collect(Collectors.toList()); } private Function timeToTestInfo() { diff --git a/pitest/src/main/java/org/pitest/functional/FCollection.java b/pitest/src/main/java/org/pitest/functional/FCollection.java index 8e7397e20..da6d16742 100644 --- a/pitest/src/main/java/org/pitest/functional/FCollection.java +++ b/pitest/src/main/java/org/pitest/functional/FCollection.java @@ -28,6 +28,7 @@ */ public abstract class FCollection { + @Deprecated public static void mapTo(final Iterable as, final Function f, final Collection bs) { if (as != null) { @@ -37,6 +38,7 @@ public static void mapTo(final Iterable as, } } + @Deprecated public static List map(final Iterable as, final Function f) { final List bs = emptyList(); @@ -55,6 +57,7 @@ public static void flatMapTo(final Iterable as, } } + @Deprecated public static List flatMap( final Iterable as, final Function> f) { final List bs = emptyList(); @@ -62,10 +65,8 @@ public static List flatMap( return bs; } - private static List emptyList() { - return new ArrayList<>(); - } + @Deprecated public static List filter(final Iterable xs, final Predicate predicate) { final List dest = emptyList(); @@ -73,7 +74,7 @@ public static List filter(final Iterable xs, return dest; } - public static void filter(final Iterable xs, + private static void filter(final Iterable xs, final Predicate predicate, final Collection dest) { for (final T x : xs) { if (predicate.test(x)) { @@ -82,6 +83,7 @@ public static void filter(final Iterable xs, } } + @Deprecated public static boolean contains(final Iterable xs, final Predicate predicate) { for (final T x : xs) { @@ -102,6 +104,7 @@ public static A fold(final BiFunction f, final A z, return p; } + @Deprecated public static Collection flatten( final Iterable> ts) { final List list = new ArrayList<>(); @@ -144,4 +147,8 @@ public static Map> bucket(final Iterable bs, return bucketed; } + private static List emptyList() { + return new ArrayList<>(); + } + } diff --git a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java index 736596d40..5546956c8 100644 --- a/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java +++ b/pitest/src/main/java/org/pitest/junit/JUnitCustomRunnerTestUnitFinder.java @@ -18,7 +18,6 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -120,17 +119,18 @@ private boolean isExcludedCategory(final Class a) { private List getCategories(final Class a) { final Category c = a.getAnnotation(Category.class); - return FCollection.flatMap(Arrays.asList(c), toCategoryNames()); + return Stream.of(c) + .flatMap(toCategoryNames()) + .collect(Collectors.toList()); } - private Function> toCategoryNames() { + private Function> toCategoryNames() { return a -> { if (a == null) { - return Collections.emptyList(); + return Stream.empty(); } return Stream.of(a.value()) - .map(Class::getName) - .collect(Collectors.toList()); + .map(Class::getName); }; }