Skip to content

Commit

Permalink
8290504: Close streams returned by ModuleReader::list
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, chegar
  • Loading branch information
rjernst authored and ChrisHegarty committed Jul 21, 2022
1 parent 15f4b30 commit 80bd8c3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Expand Up @@ -28,6 +28,7 @@
import java.lang.module.ResolvedModule;
import java.net.URL;
import java.util.Enumeration;
import java.util.stream.Stream;

/**
* Usage: Main $MODULE
Expand Down Expand Up @@ -79,8 +80,9 @@ public static void main(String[] args) throws Exception {
.map(ResolvedModule::reference)
.orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));

try (ModuleReader reader = mref.open()) {
reader.list().forEach(name -> {
try (ModuleReader reader = mref.open();
Stream<String> stream = reader.list()) {
stream.forEach(name -> {
testFindUnchecked(name);

// if the resource is a directory then find without trailing slash
Expand Down
Expand Up @@ -62,9 +62,10 @@ public class CallerSensitiveAccess {
*/
@DataProvider(name = "callerSensitiveMethods")
static Object[][] callerSensitiveMethods() {
return callerSensitiveMethods(Object.class.getModule())
.map(m -> new Object[] { m, shortDescription(m) })
.toArray(Object[][]::new);
try (Stream<Method> stream = callerSensitiveMethods(Object.class.getModule())) {
return stream.map(m -> new Object[]{m, shortDescription(m)})
.toArray(Object[][]::new);
}
}

/**
Expand Down Expand Up @@ -100,11 +101,13 @@ public void testPublicLookupUnreflect(@NoInjection Method method, String desc) t
*/
@DataProvider(name = "accessibleCallerSensitiveMethods")
static Object[][] accessibleCallerSensitiveMethods() {
return callerSensitiveMethods(Object.class.getModule())
try (Stream<Method> stream = callerSensitiveMethods(Object.class.getModule())) {
return stream
.filter(m -> Modifier.isPublic(m.getModifiers()))
.map(m -> { m.setAccessible(true); return m; })
.map(m -> new Object[] { m, shortDescription(m) })
.toArray(Object[][]::new);
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion test/jdk/java/lang/module/ModuleReader/ModuleReaderTest.java
Expand Up @@ -54,6 +54,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;

import jdk.internal.module.ModulePath;
import jdk.test.lib.compiler.CompilerUtils;
Expand Down Expand Up @@ -413,7 +414,10 @@ void testRead(ModuleReader reader, String name, byte[] expectedBytes)
* Test ModuleReader#list
*/
void testList(ModuleReader reader, String name) throws IOException {
List<String> list = reader.list().collect(Collectors.toList());
final List<String> list;
try (Stream<String> stream = reader.list()) {
list = stream.toList();
}
Set<String> names = new HashSet<>(list);
assertTrue(names.size() == list.size()); // no duplicates

Expand Down
Expand Up @@ -44,6 +44,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Stream;

import jdk.test.lib.util.JarUtils;

Expand Down Expand Up @@ -108,8 +109,9 @@ private void testZipFileSystem(Path zip) throws Exception {
private void listAllModules(ModuleFinder finder) throws Exception {
for (ModuleReference mref : finder.findAll()) {
System.out.println(mref.descriptor());
try (ModuleReader reader = mref.open()) {
reader.list().forEach(name -> System.out.format(" %s%n", name));
try (ModuleReader reader = mref.open();
Stream<String> stream = reader.list()) {
stream.forEach(name -> System.out.format(" %s%n", name));
}
}
}
Expand Down

0 comments on commit 80bd8c3

Please sign in to comment.