8271079: JavaFileObject#toUri and multi-release jars
Reviewed-by: jjg, lancea, alanb
This commit is contained in:
parent
ccdb9f1b16
commit
23fd9f15da
src
jdk.compiler/share/classes/com/sun/tools/javac/file
jdk.zipfs/share/classes/jdk/nio/zipfs
test
@ -178,13 +178,6 @@ public abstract class PathFileObject implements JavaFileObject {
|
||||
return toBinaryName(root.relativize(path));
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.COMPILER)
|
||||
public URI toUri() {
|
||||
// Work around bug JDK-8134451:
|
||||
// path.toUri() returns double-encoded URIs, that cannot be opened by URLConnection
|
||||
return createJarUri(userJarPath, path.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JarFileObject[" + userJarPath + ":" + path + "]";
|
||||
@ -197,17 +190,6 @@ public abstract class PathFileObject implements JavaFileObject {
|
||||
userJarPath
|
||||
);
|
||||
}
|
||||
|
||||
private static URI createJarUri(Path jarFile, String entryName) {
|
||||
URI jarURI = jarFile.toUri().normalize();
|
||||
String separator = entryName.startsWith("/") ? "!" : "!/";
|
||||
try {
|
||||
// The jar URI convention appears to be not to re-encode the jarURI
|
||||
return new URI("jar:" + jarURI + separator + entryName);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new CannotCreateUriError(jarURI + separator + entryName, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1471,6 +1471,13 @@ class ZipFileSystem extends FileSystem {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Package-private accessor to entry alias map used by ZipPath.
|
||||
*/
|
||||
byte[] lookupPath(byte[] resolvedPath) {
|
||||
return entryLookup.apply(resolvedPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a sorted version map of version -> inode, for inodes <= max version.
|
||||
* 9 -> META-INF/versions/9
|
||||
|
@ -200,13 +200,19 @@ final class ZipPath implements Path {
|
||||
return new URI("jar",
|
||||
decodeUri(zfs.getZipFile().toUri().toString()) +
|
||||
"!" +
|
||||
zfs.getString(toAbsolutePath().path),
|
||||
getRealPath(),
|
||||
null);
|
||||
} catch (Exception ex) {
|
||||
throw new AssertionError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private String getRealPath() {
|
||||
byte[] resolvedPath = getResolvedPath();
|
||||
byte[] realPath = zfs.lookupPath(resolvedPath);
|
||||
return zfs.getString(realPath != null ? realPath : resolvedPath);
|
||||
}
|
||||
|
||||
private boolean equalsNameAt(ZipPath other, int index) {
|
||||
int mbegin = offsets[index];
|
||||
int mlen;
|
||||
|
@ -73,7 +73,7 @@ import static java.nio.file.StandardCopyOption.*;
|
||||
* @test
|
||||
* @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
|
||||
* 7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
|
||||
* 8131067 8034802 8210899 8273961
|
||||
* 8131067 8034802 8210899 8273961 8271079
|
||||
* @summary Test Zip filesystem provider
|
||||
* @modules jdk.zipfs
|
||||
* @run main ZipFSTester
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8164389 8222440
|
||||
* @bug 8164389 8222440 8271079
|
||||
* @summary walk entries in a multi-release jar file via jdk.zipfs
|
||||
* @modules jdk.jartool
|
||||
* jdk.zipfs
|
||||
@ -146,4 +146,58 @@ public class JFSTester {
|
||||
throw new UncheckedIOException(x);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToUri() throws IOException {
|
||||
// treat multi-release jar as unversioned
|
||||
Map<String, String> env = new HashMap<>();
|
||||
Set<String> contents = doTestUri(env);
|
||||
Set<String> expectedContents = Set.of(
|
||||
"!/root/dir1/leaf1.txt",
|
||||
"!/root/dir1/leaf2.txt",
|
||||
"!/root/dir2/leaf3.txt",
|
||||
"!/root/dir2/leaf4.txt"
|
||||
);
|
||||
Assert.assertEquals(contents, expectedContents);
|
||||
|
||||
// open file as multi-release for version 9
|
||||
env.put("multi-release", "9");
|
||||
contents = doTestUri(env);
|
||||
expectedContents = Set.of(
|
||||
"!/root/dir1/leaf1.txt",
|
||||
"!/root/dir1/leaf2.txt",
|
||||
"!/META-INF/versions/9/root/dir2/leaf3.txt",
|
||||
"!/META-INF/versions/9/root/dir2/leaf4.txt",
|
||||
"!/META-INF/versions/9/root/dir3/leaf5.txt",
|
||||
"!/META-INF/versions/9/root/dir3/leaf6.txt"
|
||||
);
|
||||
Assert.assertEquals(contents, expectedContents);
|
||||
|
||||
// open file as multi-release for version 10
|
||||
env.put("multi-release", "10");
|
||||
contents = doTestUri(env);
|
||||
expectedContents = Set.of(
|
||||
"!/root/dir1/leaf1.txt",
|
||||
"!/root/dir1/leaf2.txt",
|
||||
"!/META-INF/versions/9/root/dir2/leaf3.txt",
|
||||
"!/META-INF/versions/9/root/dir2/leaf4.txt",
|
||||
"!/META-INF/versions/10/root/dir3/leaf5.txt",
|
||||
"!/META-INF/versions/10/root/dir3/leaf6.txt"
|
||||
);
|
||||
Assert.assertEquals(contents, expectedContents);
|
||||
}
|
||||
|
||||
private Set<String> doTestUri(Map<String,String> env) throws IOException {
|
||||
Set<String> contents;
|
||||
try (FileSystem fs = FileSystems.newFileSystem(jarURI, env)) {
|
||||
Path root = fs.getPath("root");
|
||||
int prefix = root.toUri().toString().indexOf('!');
|
||||
contents = Files.walk(root)
|
||||
.filter(p -> !Files.isDirectory(p))
|
||||
.map(p -> p.toUri().toString().substring(prefix))
|
||||
.sorted()
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
|
110
test/langtools/tools/javac/T8271079.java
Normal file
110
test/langtools/tools/javac/T8271079.java
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8271079
|
||||
* @summary JavaFileObject#toUri in MR-JAR returns real path
|
||||
* @modules java.compiler
|
||||
* jdk.compiler
|
||||
* @run main T8271079
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.jar.JarEntry;
|
||||
import javax.tools.*;
|
||||
|
||||
public class T8271079 {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new T8271079().run();
|
||||
}
|
||||
|
||||
final PrintStream out;
|
||||
|
||||
T8271079() {
|
||||
this.out = System.out;
|
||||
}
|
||||
|
||||
void run() throws Exception {
|
||||
Path mr = generateMultiReleaseJar();
|
||||
try {
|
||||
testT8271079(mr);
|
||||
} finally {
|
||||
Files.deleteIfExists(mr);
|
||||
}
|
||||
}
|
||||
|
||||
// $ echo 'module hello {}' > module-info.java
|
||||
// $ javac -d classes --release 9 module-info.java
|
||||
// $ jar --create --file mr.jar --release 9 -C classes .
|
||||
Path generateMultiReleaseJar() throws Exception {
|
||||
Files.writeString(Path.of("module-info.java"), "module hello {}");
|
||||
java.util.spi.ToolProvider.findFirst("javac").orElseThrow()
|
||||
.run(out, System.err, "-d", "classes", "--release", "9", "module-info.java");
|
||||
Path mr = Path.of("mr.jar");
|
||||
java.util.spi.ToolProvider.findFirst("jar").orElseThrow()
|
||||
.run(out, System.err, "--create", "--file", mr.toString(), "--release", "9", "-C", "classes", ".");
|
||||
out.println("Created: " + mr.toUri());
|
||||
out.println(" Exists: " + Files.exists(mr));
|
||||
return mr;
|
||||
}
|
||||
|
||||
void testT8271079(Path path) throws Exception {
|
||||
StandardJavaFileManager fileManager =
|
||||
ToolProvider.getSystemJavaCompiler()
|
||||
.getStandardFileManager(null, Locale.ENGLISH, StandardCharsets.UTF_8);
|
||||
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, List.of(path));
|
||||
Iterator<String> options = Arrays.asList("--multi-release", "9").iterator();
|
||||
fileManager.handleOption(options.next(), options);
|
||||
|
||||
Iterable<JavaFileObject> list =
|
||||
fileManager.list(
|
||||
StandardLocation.CLASS_PATH, "", EnumSet.allOf(JavaFileObject.Kind.class), false);
|
||||
|
||||
for (JavaFileObject f : list) {
|
||||
out.println("JavaFileObject#getName: " + f.getName());
|
||||
out.println("JavaFileObject#toUri: " + f.toUri());
|
||||
openUsingUri(f.toUri());
|
||||
}
|
||||
System.gc(); // JDK-8224794
|
||||
}
|
||||
|
||||
void openUsingUri(URI uri) throws IOException {
|
||||
URLConnection connection = uri.toURL().openConnection();
|
||||
connection.setUseCaches(false); // JDK-8224794
|
||||
if (connection instanceof JarURLConnection jar) {
|
||||
try {
|
||||
JarEntry entry = jar.getJarEntry();
|
||||
out.println("JarEntry#getName: " + entry.getName());
|
||||
connection.getInputStream().close(); // JDK-8224794
|
||||
} catch (FileNotFoundException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user