Skip to content

Commit

Permalink
Merge pull request #1381 from timtebeek/adopt-java-11
Browse files Browse the repository at this point in the history
Adopt features from Java 11
  • Loading branch information
hcoles authored Feb 5, 2025
2 parents 40fa0fb + a4cf5cf commit b50fc19
Show file tree
Hide file tree
Showing 20 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ParseResult(final ReportOptions options, final String errorMessage) {
}

public boolean isOk() {
return !this.errorMessage.isPresent();
return this.errorMessage.isEmpty();
}

public ReportOptions getOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -94,7 +94,7 @@ public void shouldCreatePredicateFromCommaSeparatedListOfTargetClassGlobs() {
public void shouldParseCommaSeparatedListOfSourceDirectories() {
final ReportOptions actual = parseAddingRequiredArgs("--sourceDirs",
"foo/bar,bar/far");
assertThat(actual.getSourcePaths()).containsExactly(Paths.get("foo/bar"), Paths.get(("bar/far")));
assertThat(actual.getSourcePaths()).containsExactly(Path.of("foo/bar"), Path.of(("bar/far")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private Optional<ClassInfo> getParent() {

private boolean descendsFrom(final ClassName clazz) {

if (!this.getSuperClass().isPresent()) {
if (this.getSuperClass().isEmpty()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void start() throws IOException, InterruptedException {
public ExitCode waitToDie() {
try {
Optional<ExitCode> maybeExit = this.crt.waitToFinish(5);
while (!maybeExit.isPresent() && this.process.isAlive()) {
while (maybeExit.isEmpty() && this.process.isAlive()) {
maybeExit = this.crt.waitToFinish(10);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Predicate<MutationDetails> buildPredicate() {
final int instruction = a.getInstructionIndex();
final Optional<MethodTree> method = this.currentClass.method(a.getId().getLocation());

if (!method.isPresent()) {
if (method.isEmpty()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ private void collectLambdaMethods(MethodTree method, ClassTree clazz,

List<MethodTree> recurse = lambdas.stream()
.map(clazz::method)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)
.collect(Collectors.toList());

methodsToProcess.addAll(recurse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private Predicate<MutationDetails> mutatesIteratorLoopPlumbing() {
return a -> {
final int instruction = a.getInstructionIndex();
final Optional<MethodTree> maybeMethod = currentClass.method(a.getId().getLocation());
if (!maybeMethod.isPresent()) {
if (maybeMethod.isEmpty()) {
return false;
}
MethodTree method = maybeMethod.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private void checkForInlinedCode(Collection<MutationDetails> mutantsToReturn,

private boolean isInFinallyBlock(MutationDetails m) {
Optional<MethodTree> maybeMethod = currentClass.method(m.getId().getLocation());
if (!maybeMethod.isPresent()) {
if (maybeMethod.isEmpty()) {
return false;
}
MethodTree method = maybeMethod.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private Predicate<MutationDetails> mutatesAForLoopCounter() {
return a -> {
final int instruction = a.getInstructionIndex();
Optional<MethodTree> maybeMethod = AvoidForLoopCounterFilter.this.currentClass.method(a.getId().getLocation());
if (!maybeMethod.isPresent()) {
if (maybeMethod.isEmpty()) {
return false;
}
MethodTree method = maybeMethod.get();
Expand All @@ -182,8 +182,7 @@ private Set<AbstractInsnNode> findLoopCounters(MethodTree method) {
Context context = Context.start(DEBUG);
return MUTATED_FOR_COUNTER.contextMatches(method.instructions(), context).stream()
.map(c -> c.retrieve(MUTATED_INSTRUCTION.read()))
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private Collection<MutationDetails> findTimeoutMutants(Location location,
Collection<MutationDetails> mutations, Mutater m) {

final Optional<MethodTree> maybeMethod = this.currentClass.method(location);
if (!maybeMethod.isPresent()) {
if (maybeMethod.isEmpty()) {
return Collections.emptyList();
}
MethodTree method = maybeMethod.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ExitCode waitToDie() {

// While the monitored process reports being alive, keep polling
// the monitoring thread to see if it has finished.
while (!maybeExit.isPresent() && this.process.isAlive()) {
while (maybeExit.isEmpty() && this.process.isAlive()) {
maybeExit = this.thread.waitToFinish(10);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private MutationResult prioritiseLastTest(MutationDetails mutation, String killi
.findFirst();

// last killing test is no longer available
if (!maybeKillingTest.isPresent()) {
if (maybeKillingTest.isEmpty()) {
return analyseFromScratch(mutation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void updateData(ReportOptions data, SettingsFactory settings) {

private History pickHistoryStore(CodeSource code, ReportOptions data, Optional<WriterFactory> historyWriter, HistoryFactory factory) {
final Optional<Reader> reader = data.createHistoryReader();
if (!reader.isPresent() && !historyWriter.isPresent()) {
if (reader.isEmpty() && historyWriter.isEmpty()) {
return new NullHistory();
}
FeatureParser parser = new FeatureParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public JarCreatingJarFinder() {

@Override
public Optional<String> getJarLocation() {
if (!this.location.isPresent()) {
if (this.location.isEmpty()) {
this.location = createJar();
}
return this.location;
Expand Down
3 changes: 2 additions & 1 deletion pitest-entry/src/main/java/org/pitest/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ThreadLocalRandom;

public class FileUtil {

Expand All @@ -24,7 +25,7 @@ public static String readToString(final InputStream is)

public static String randomFilename() {
return System.currentTimeMillis()
+ ("" + Math.random()).replaceAll("\\.", "");
+ ("" + ThreadLocalRandom.current().nextDouble()).replaceAll("\\.", "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public void describeTo(final Description description) {
protected boolean matchesSafely(final MutationResult item,
final Description mismatchDescription) {
Optional<String> itemKillingTest = item.getKillingTest();
if (!itemKillingTest.isPresent()) {
if (itemKillingTest.isEmpty()) {
mismatchDescription
.appendText("a mutation result with no killing test");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void autoAddJUnitPlatform(List<String> classPath) {
}

Optional<Artifact> maybeJUnitPlatform = findJUnitArtifact(junitDependencies);
if (!maybeJUnitPlatform.isPresent()) {
if (maybeJUnitPlatform.isEmpty()) {
this.log.debug("JUnit 5 not on classpath");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void testUsesSourceDirectoriesFromProject() {
when(this.project.getTestCompileSourceRoots()).thenReturn(
asList("tst"));
final ReportOptions actual = parseConfig("");
assertThat(actual.getSourcePaths()).containsExactly(Paths.get("src"), Paths.get("tst"));
assertThat(actual.getSourcePaths()).containsExactly(Path.of("src"), Path.of("tst"));
}

public void testParsesExcludedRunners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private boolean typeImplements(String type, ClassReader info, final String itf)
*/
private ClassReader typeInfo(final String type) {
final Optional<byte[]> maybeBytes = this.bytes.getBytes(type);
if (!maybeBytes.isPresent()) {
if (maybeBytes.isEmpty()) {
throw new PitError("Could not find class definition for " + type);
}
return new ClassReader(maybeBytes.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Optional<PitHelpError> verifyEnvironment() {

private static List<Configuration> pickChildren(List<Configuration> configs) {
List<Configuration> working = configs.stream()
.filter(c -> !c.verifyEnvironment().isPresent())
.filter(c -> c.verifyEnvironment().isEmpty())
.sorted(byPriority())
.collect(Collectors.toList());
// We don't have a working config, let it report errors later
Expand Down

0 comments on commit b50fc19

Please sign in to comment.