2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
27
* @summary JavaFileObject#toUri in MR-JAR returns real path
28
* @modules java.compiler
35
import java.nio.charset.StandardCharsets;
36
import java.nio.file.*;
38
import java.util.jar.JarEntry;
41
public class T8271079 {
43
public static void main(String[] args) throws Exception {
47
final PrintStream out;
50
this.out = System.out;
53
void run() throws Exception {
54
Path mr = generateMultiReleaseJar();
58
Files.deleteIfExists(mr);
62
// $ echo 'module hello {}' > module-info.java
63
// $ javac -d classes --release 9 module-info.java
64
// $ jar --create --file mr.jar --release 9 -C classes .
65
Path generateMultiReleaseJar() throws Exception {
66
Files.writeString(Path.of("module-info.java"), "module hello {}");
67
java.util.spi.ToolProvider.findFirst("javac").orElseThrow()
68
.run(out, System.err, "-d", "classes", "--release", "9", "module-info.java");
69
Path mr = Path.of("mr.jar");
70
java.util.spi.ToolProvider.findFirst("jar").orElseThrow()
71
.run(out, System.err, "--create", "--file", mr.toString(), "--release", "9", "-C", "classes", ".");
72
out.println("Created: " + mr.toUri());
73
out.println(" Exists: " + Files.exists(mr));
77
void testT8271079(Path path) throws Exception {
78
StandardJavaFileManager fileManager =
79
ToolProvider.getSystemJavaCompiler()
80
.getStandardFileManager(null, Locale.ENGLISH, StandardCharsets.UTF_8);
81
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(path));
82
Iterator<String> options = Arrays.asList("--multi-release", "9").iterator();
83
fileManager.handleOption(options.next(), options);
85
Iterable<JavaFileObject> list =
87
StandardLocation.CLASS_PATH, "", EnumSet.allOf(JavaFileObject.Kind.class), false);
89
for (JavaFileObject f : list) {
90
out.println("JavaFileObject#getName: " + f.getName());
91
out.println("JavaFileObject#toUri: " + f.toUri());
92
openUsingUri(f.toUri());
94
System.gc(); // JDK-8224794
97
void openUsingUri(URI uri) throws IOException {
98
URLConnection connection = uri.toURL().openConnection();
99
connection.setUseCaches(false); // JDK-8224794
100
if (connection instanceof JarURLConnection jar) {
102
JarEntry entry = jar.getJarEntry();
103
out.println("JarEntry#getName: " + entry.getName());
104
connection.getInputStream().close(); // JDK-8224794
105
} catch (FileNotFoundException e) {