jdk

Форк
0
162 строки · 5.9 Кб
1
/*
2
 * Copyright (c) 2018, 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 8193717
27
 * @summary Check that code with a lot named imports can compile.
28
 * @library /tools/lib
29
 * @enablePreview
30
 * @modules java.base/jdk.internal.classfile.impl
31
 *          jdk.compiler/com.sun.tools.javac.api
32
 *          jdk.compiler/com.sun.tools.javac.main
33
 *          jdk.jdeps/com.sun.tools.javap
34
 * @build toolbox.ToolBox toolbox.JavapTask
35
 * @run main T8193717
36
 */
37

38
import java.io.ByteArrayInputStream;
39
import java.io.ByteArrayOutputStream;
40
import java.io.IOException;
41
import java.io.InputStream;
42
import java.lang.constant.ClassDesc;
43
import java.net.URI;
44
import java.net.URISyntaxException;
45
import java.nio.file.Paths;
46
import java.util.ArrayList;
47
import java.util.List;
48
import java.util.Set;
49

50
import javax.tools.ForwardingJavaFileManager;
51
import javax.tools.JavaCompiler;
52
import javax.tools.JavaFileManager;
53
import javax.tools.JavaFileObject;
54
import javax.tools.JavaFileObject.Kind;
55
import javax.tools.SimpleJavaFileObject;
56
import javax.tools.StandardJavaFileManager;
57
import javax.tools.StandardLocation;
58
import javax.tools.ToolProvider;
59

60
import java.lang.classfile.*;
61

62
import toolbox.JavacTask;
63
import toolbox.ToolBox;
64

65
public class T8193717 {
66
    public static void main(String... args) throws IOException {
67
        new T8193717().run();
68
    }
69

70
    private static final int CLASSES = 50000;
71

72
    private void run() throws IOException {
73
        StringBuilder imports = new StringBuilder();
74
        StringBuilder use = new StringBuilder();
75

76
        for (int c = 0; c < CLASSES; c++) {
77
            String simpleName = getSimpleName(c);
78
            String pack = "p";
79
            imports.append("import " + pack + "." + simpleName + ";\n");
80
            use.append(simpleName + " " + simpleName + ";\n");
81
        }
82
        String source = imports.toString() + "public class T {\n" + use.toString() + "}";
83
        ToolBox tb = new ToolBox();
84
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
85

86
        try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
87
            fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, List.of(Paths.get(".")));
88
            new JavacTask(tb).sources(source)
89
                             .options("-XDshould-stop.ifError=ATTR",
90
                                      "-XDshould-stop.ifNoError=ATTR") //the source is too big for a classfile
91
                             .fileManager(new TestJFM(fm))
92
                             .run();
93
        }
94
    }
95

96
    private static String getSimpleName(int c) {
97
        return "T" + String.format("%0" + (int) Math.ceil(Math.log10(CLASSES)) + "d", c);
98
    }
99

100
    private byte[] generateClassFile(String name) throws IOException {
101
        byte[] bytes = ClassFile.of().build(ClassDesc.of(name), classBuilder -> {
102
            classBuilder.withSuperclass(ClassDesc.ofInternalName("java/lang/Object"))
103
                    .withVersion(51, 0)
104
                    .withFlags(ClassFile.ACC_ABSTRACT | ClassFile.ACC_INTERFACE | ClassFile.ACC_PUBLIC);
105
        });
106
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
107
        baos.write(bytes);
108
        return baos.toByteArray();
109
    }
110

111
    final class TestJFM extends ForwardingJavaFileManager<JavaFileManager> {
112

113
        public TestJFM(JavaFileManager fileManager) {
114
            super(fileManager);
115
        }
116

117
        @Override
118
        public Iterable<JavaFileObject> list(Location location, String packageName,
119
                                             Set<Kind> kinds, boolean recurse) throws IOException {
120
            if (location == StandardLocation.CLASS_PATH) {
121
                if (packageName.equals("p")) {
122
                    try {
123
                        List<JavaFileObject> result = new ArrayList<>(CLASSES);
124

125
                        for (int c = 0; c < CLASSES; c++) {
126
                            result.add(new TestJFO("p." + getSimpleName(c)));
127
                        }
128

129
                        return result;
130
                    } catch (URISyntaxException ex) {
131
                        throw new IllegalStateException(ex);
132
                    }
133
                }
134
            }
135
            return super.list(location, packageName, kinds, recurse);
136
        }
137

138
        @Override
139
        public String inferBinaryName(Location location, JavaFileObject file) {
140
            if (file instanceof TestJFO) {
141
                return ((TestJFO) file).name;
142
            }
143
            return super.inferBinaryName(location, file);
144
        }
145

146
        private class TestJFO extends SimpleJavaFileObject {
147

148
            private final String name;
149

150
            public TestJFO(String name) throws URISyntaxException {
151
                super(new URI("mem://" + name.replace(".", "/") + ".class"), Kind.CLASS);
152
                this.name = name;
153
            }
154

155
            @Override
156
            public InputStream openInputStream() throws IOException {
157
                return new ByteArrayInputStream(generateClassFile(name));
158
            }
159
        }
160

161
    }
162
}
163

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

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

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

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