Use AutoClosables with try-with-resources
Closes gh-33538
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user