jdk

Форк
0
/
T8271079.java 
110 строк · 3.9 Кб
1
/*
2
 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
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.
8
 *
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).
14
 *
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.
18
 *
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
21
 * questions.
22
 */
23

24
/*
25
 * @test
26
 * @bug 8271079
27
 * @summary JavaFileObject#toUri in MR-JAR returns real path
28
 * @modules java.compiler
29
 *          jdk.compiler
30
 * @run main T8271079
31
 */
32

33
import java.io.*;
34
import java.net.*;
35
import java.nio.charset.StandardCharsets;
36
import java.nio.file.*;
37
import java.util.*;
38
import java.util.jar.JarEntry;
39
import javax.tools.*;
40

41
public class T8271079 {
42

43
    public static void main(String[] args) throws Exception {
44
        new T8271079().run();
45
    }
46

47
    final PrintStream out;
48

49
    T8271079() {
50
        this.out = System.out;
51
    }
52

53
    void run() throws Exception {
54
        Path mr = generateMultiReleaseJar();
55
        try {
56
            testT8271079(mr);
57
        } finally {
58
            Files.deleteIfExists(mr);
59
        }
60
    }
61

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));
74
        return mr;
75
    }
76

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);
84

85
        Iterable<JavaFileObject> list =
86
            fileManager.list(
87
                StandardLocation.CLASS_PATH, "", EnumSet.allOf(JavaFileObject.Kind.class), false);
88

89
        for (JavaFileObject f : list) {
90
            out.println("JavaFileObject#getName: " + f.getName());
91
            out.println("JavaFileObject#toUri: " + f.toUri());
92
            openUsingUri(f.toUri());
93
        }
94
        System.gc(); // JDK-8224794
95
    }
96

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) {
101
            try {
102
                JarEntry entry = jar.getJarEntry();
103
                out.println("JarEntry#getName: " + entry.getName());
104
                connection.getInputStream().close(); // JDK-8224794
105
            } catch (FileNotFoundException e) {
106
                throw e;
107
            }
108
        }
109
    }
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.