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.
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 Check that code with a lot named imports can compile.
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
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;
44
import java.net.URISyntaxException;
45
import java.nio.file.Paths;
46
import java.util.ArrayList;
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;
60
import java.lang.classfile.*;
62
import toolbox.JavacTask;
63
import toolbox.ToolBox;
65
public class T8193717 {
66
public static void main(String... args) throws IOException {
70
private static final int CLASSES = 50000;
72
private void run() throws IOException {
73
StringBuilder imports = new StringBuilder();
74
StringBuilder use = new StringBuilder();
76
for (int c = 0; c < CLASSES; c++) {
77
String simpleName = getSimpleName(c);
79
imports.append("import " + pack + "." + simpleName + ";\n");
80
use.append(simpleName + " " + simpleName + ";\n");
82
String source = imports.toString() + "public class T {\n" + use.toString() + "}";
83
ToolBox tb = new ToolBox();
84
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
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))
96
private static String getSimpleName(int c) {
97
return "T" + String.format("%0" + (int) Math.ceil(Math.log10(CLASSES)) + "d", c);
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"))
104
.withFlags(ClassFile.ACC_ABSTRACT | ClassFile.ACC_INTERFACE | ClassFile.ACC_PUBLIC);
106
ByteArrayOutputStream baos = new ByteArrayOutputStream();
108
return baos.toByteArray();
111
final class TestJFM extends ForwardingJavaFileManager<JavaFileManager> {
113
public TestJFM(JavaFileManager fileManager) {
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")) {
123
List<JavaFileObject> result = new ArrayList<>(CLASSES);
125
for (int c = 0; c < CLASSES; c++) {
126
result.add(new TestJFO("p." + getSimpleName(c)));
130
} catch (URISyntaxException ex) {
131
throw new IllegalStateException(ex);
135
return super.list(location, packageName, kinds, recurse);
139
public String inferBinaryName(Location location, JavaFileObject file) {
140
if (file instanceof TestJFO) {
141
return ((TestJFO) file).name;
143
return super.inferBinaryName(location, file);
146
private class TestJFO extends SimpleJavaFileObject {
148
private final String name;
150
public TestJFO(String name) throws URISyntaxException {
151
super(new URI("mem://" + name.replace(".", "/") + ".class"), Kind.CLASS);
156
public InputStream openInputStream() throws IOException {
157
return new ByteArrayInputStream(generateClassFile(name));