Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/4241573/T4241573.java
38813 views
1
/*
2
* Copyright (c) 2009, 2011, 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 4241573
27
* @summary SourceFile attribute includes full path
28
*/
29
30
import com.sun.tools.classfile.Attribute;
31
import com.sun.tools.classfile.ClassFile;
32
import com.sun.tools.classfile.SourceFile_attribute;
33
import java.io.*;
34
import java.util.*;
35
import java.util.jar.*;
36
37
public class T4241573 {
38
public static void main(String... args) throws Exception {
39
new T4241573().run();
40
}
41
42
public void run() throws Exception {
43
// Selection of files to be compiled
44
File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A");
45
File relJar = createJar(new File("rel.jar"), "j.R");
46
File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A");
47
File relDir = createDir(new File("rel.dir"), "d.R");
48
File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }");
49
File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }");
50
File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }");
51
// This next class references other classes that will be found on the source path
52
// and which will therefore need to be compiled as well.
53
File mainFile = writeFile(new File("Main.java"),
54
"class Main { j.A ja; j.R jr; d.A da; d.R dr; }" +
55
"");
56
57
String sourcePath = createPath(absJar, relJar, absDir, relDir);
58
File outDir = new File("classes");
59
outDir.mkdirs();
60
61
String[] args = {
62
"-sourcepath", sourcePath,
63
"-d", outDir.getPath(),
64
absTestFile.getPath(),
65
relTestFile.getPath(),
66
relTest2File.getPath(),
67
mainFile.getPath(),
68
};
69
System.err.println("compile: " + Arrays.asList(args));
70
StringWriter sw = new StringWriter();
71
PrintWriter pw = new PrintWriter(sw);
72
int rc = com.sun.tools.javac.Main.compile(args, pw);
73
pw.close();
74
if (rc != 0) {
75
System.err.println(sw.toString());
76
throw new Exception("unexpected exit from javac: " + rc);
77
}
78
79
Set<File> expect = getFiles(outDir,
80
"d/A.class", "d/A$Inner.class",
81
"d/R.class", "d/R$Inner.class",
82
"j/A.class", "j/A$Inner.class",
83
"j/R.class", "j/R$Inner.class",
84
"AbsTest.class", "AbsTest$Inner.class",
85
"RelTest.class", "RelTest$Inner.class",
86
"p/RelTest2.class", "p/RelTest2$Inner.class",
87
"Main.class" );
88
89
Set<File> found = findFiles(outDir);
90
91
if (!found.equals(expect)) {
92
if (found.containsAll(expect))
93
throw new Exception("unexpected files found: " + diff(found, expect));
94
else if (expect.containsAll(found))
95
throw new Exception("expected files not found: " + diff(expect, found));
96
}
97
98
for (File f: found)
99
verifySourceFileAttribute(f);
100
101
if (errors > 0)
102
throw new Exception(errors + " errors occurred");
103
}
104
105
/** Check the SourceFileAttribute is the simple name of the original source file. */
106
void verifySourceFileAttribute(File f) {
107
System.err.println("verify: " + f);
108
try {
109
ClassFile cf = ClassFile.read(f);
110
SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
111
String found = sfa.getSourceFile(cf.constant_pool);
112
String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
113
if (!expect.equals(found)) {
114
error("bad value found: " + found + ", expected: " + expect);
115
}
116
} catch (Exception e) {
117
error("error reading " + f +": " + e);
118
}
119
}
120
121
/** Create a directory containing one or more files. */
122
File createDir(File dir, String... entries) throws Exception {
123
if (!dir.mkdirs())
124
throw new Exception("cannot create directories " + dir);
125
for (String e: entries) {
126
writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e));
127
}
128
return dir;
129
}
130
131
/** Create a jar file containing one or more entries. */
132
File createJar(File jar, String... entries) throws IOException {
133
OutputStream out = new FileOutputStream(jar);
134
try {
135
JarOutputStream jos = new JarOutputStream(out);
136
for (String e: entries) {
137
jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));
138
jos.write(getBodyForEntry(e).getBytes());
139
}
140
jos.close();
141
} finally {
142
out.close();
143
}
144
return jar;
145
}
146
147
/** Return the path for an entry given to createDir */
148
String getPathForDirEntry(String e) {
149
return e.replace(".", File.separator) + ".java";
150
}
151
152
/** Return the path for an entry given to createJar. */
153
String getPathForZipEntry(String e) {
154
return e.replace(".", "/") + ".java";
155
}
156
157
/** Return the body text for an entry given to createDir or createJar. */
158
String getBodyForEntry(String e) {
159
int sep = e.lastIndexOf(".");
160
String pkgName = e.substring(0, sep);
161
String className = e.substring(sep + 1);
162
return "package " + pkgName + "; public class " + className + "{ class Inner { } }";
163
}
164
165
/** Write a file containing the given string. Parent directories are
166
* created as needed. */
167
File writeFile(File f, String s) throws IOException {
168
if (f.getParentFile() != null)
169
f.getParentFile().mkdirs();
170
FileWriter out = new FileWriter(f);
171
try {
172
out.write(s);
173
} finally {
174
out.close();
175
}
176
return f;
177
}
178
179
/** Create a path value from a list of directories and jar files. */
180
String createPath(File... files) {
181
StringBuilder sb = new StringBuilder();
182
for (File f: files) {
183
if (sb.length() > 0)
184
sb.append(File.pathSeparatorChar);
185
sb.append(f.getPath());
186
}
187
return sb.toString();
188
}
189
190
/** Create a set of files from a base directory and a set of relative paths. */
191
Set<File> getFiles(File dir, String... paths) {
192
Set<File> files = new LinkedHashSet<File>();
193
for (String p: paths)
194
files.add(new File(dir, p));
195
return files;
196
}
197
198
/** Find all the files in a directory and its subdirectories. */
199
Set<File> findFiles(File dir) {
200
Set<File> files = new LinkedHashSet<File>();
201
findFiles(dir, files);
202
return files;
203
}
204
// where
205
void findFiles(File dir, Set<File> files) {
206
for (File f: dir.listFiles()) {
207
if (f.isDirectory())
208
findFiles(f, files);
209
else
210
files.add(f);
211
}
212
}
213
214
/** Return the difference of two sets, a - b. */
215
<T> Set<T> diff(Set<T> a, Set<T> b) {
216
if (b.isEmpty())
217
return a;
218
Set<T> result = new LinkedHashSet<T>(a);
219
result.removeAll(b);
220
return result;
221
}
222
223
/** Report an error. */
224
void error(String msg) {
225
System.err.println(msg);
226
errors++;
227
}
228
229
int errors;
230
}
231
232