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