1 /*
2 * Copyright (c) 2020, 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 * @summary test the records can be read by javac properly
27 * @library /tools/lib
28 * @bug 8273018
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 * jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask
32 * @run main RecordReading
33 */
34
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.List;
40 import java.util.Objects;
41 import toolbox.TestRunner;
42 import toolbox.ToolBox;
43 import toolbox.JavacTask;
44 import toolbox.Task;
45
46 public class RecordReading extends TestRunner {
47 ToolBox tb;
48
49 RecordReading() {
50 super(System.err);
51 tb = new ToolBox();
52 }
53
54 protected void runTests() throws Exception {
55 runTests(m -> new Object[]{Paths.get(m.getName())});
56 }
57
58 public static void main(String... args) throws Exception {
59 RecordReading t = new RecordReading();
60 t.runTests();
61 }
62
63 Path[] findJavaFiles(Path... paths) throws IOException {
64 return tb.findJavaFiles(paths);
65 }
66
67 @Test
68 public void testRecordClassFileReading(Path base) throws Exception {
69 Path src = base.resolve("src");
70
71 tb.writeJavaFiles(src,
72 """
73 public record R(int i, @A long j, java.util.List<String> l) {}
74 """,
75 """
76 public @interface A {}
77 """);
78
79 Path out = base.resolve("out");
80 Files.createDirectories(out);
81
82 new JavacTask(tb)
83 .outdir(out)
84 .files(findJavaFiles(src))
85 .run();
86
87 //read the class file back, to verify javac's ClassReader
88 //reads the Record attribute properly:
89 String output = new JavacTask(tb)
90 .options("-Xprint")
91 .classpath(out.toString())
92 .classes("R")
93 .run()
94 .writeAll()
95 .getOutput(Task.OutputKind.STDOUT)
96 .replaceAll("\\R", "\n");
97
98 String expected =
99 """
100 \n\
101 public record R(int i, @A long j, java.util.List<java.lang.String> l) {
102 private final int i;
103 @A
104 private final long j;
105 private final java.util.List<java.lang.String> l;
106 \n\
107 public R(int i,
108 @A long j,
109 java.util.List<java.lang.String> l);
110 \n\
111 public final java.lang.String toString();
112 \n\
113 public final int hashCode();
114 \n\
115 public final boolean equals(java.lang.Object arg0);
116 \n\
117 public int i();
118 \n\
119 @A
120 public long j();
121 \n\
122 public java.util.List<java.lang.String> l();
123 }
124 """;
125 if (!Objects.equals(expected, output)) {
126 throw new AssertionError("Unexpected output: " + output);
127 }
128 }
129 }