Use AutoClosables with try-with-resources

Closes gh-33538
This commit is contained in:
Moritz Halbritter
2022-12-16 15:43:15 +01:00
parent 725337f976
commit f36e2ecb7b
16 changed files with 121 additions and 77 deletions

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.TestTemplate;
@@ -173,10 +174,10 @@ public class AotTests {
});
}
Stream<Path> collectRelativePaths(Path sourceDirectory) {
try {
return Files.walk(sourceDirectory).filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount()));
List<Path> collectRelativePaths(Path sourceDirectory) {
try (Stream<Path> pathStream = Files.walk(sourceDirectory)) {
return pathStream.filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount())).toList();
}
catch (IOException ex) {
throw new IllegalStateException(ex);

View File

@@ -47,6 +47,8 @@ class MavenBuildExtension implements TestTemplateInvocationContextProvider {
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
try {
// Returning a stream which must be closed here is fine, as JUnit will take
// care of closing it
return Files.list(Paths.get("build/maven-binaries")).map(MavenVersionTestTemplateInvocationContext::new);
}
catch (IOException ex) {

View File

@@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
@@ -124,7 +125,10 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo {
protected final void compileSourceFiles(URL[] classPath, File sourcesDirectory, File outputDirectory)
throws Exception {
List<Path> sourceFiles = Files.walk(sourcesDirectory.toPath()).filter(Files::isRegularFile).toList();
List<Path> sourceFiles;
try (Stream<Path> pathStream = Files.walk(sourcesDirectory.toPath())) {
sourceFiles = pathStream.filter(Files::isRegularFile).toList();
}
if (sourceFiles.isEmpty()) {
return;
}
@@ -167,8 +171,13 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo {
}
protected final void copyAll(Path from, Path to) throws IOException {
List<Path> files = (Files.exists(from)) ? Files.walk(from).filter(Files::isRegularFile).toList()
: Collections.emptyList();
if (!Files.exists(from)) {
return;
}
List<Path> files;
try (Stream<Path> pathStream = Files.walk(from)) {
files = pathStream.filter(Files::isRegularFile).toList();
}
for (Path file : files) {
String relativeFileName = file.subpath(from.getNameCount(), file.getNameCount()).toString();
getLog().debug("Copying '" + relativeFileName + "' to " + to);