OpenJDK / amber / amber
changeset 56799:80710dd15ca0 records-and-sealed
bug fixes plus updates to existing and addition of new regression tests
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Fri Jun 14 08:42:28 2019 +0200 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Jun 17 19:31:58 2019 -0400 @@ -3742,7 +3742,7 @@ List<JCTypeParameter> typarams = typeParametersOpt(); List<JCVariableDecl> headerFields = - headerFields((mods.flags & Flags.ABSTRACT) != 0); + headerFields(); List<JCExpression> implementing = List.nil(); if (token.kind == IMPLEMENTS) { @@ -3800,7 +3800,7 @@ return name; } - List<JCVariableDecl> headerFields(boolean abstractRecord) { + List<JCVariableDecl> headerFields() { ListBuffer<JCVariableDecl> fields = new ListBuffer<>(); if (token.kind == LPAREN) { nextToken(); @@ -3809,10 +3809,10 @@ nextToken(); return List.nil(); } - fields.add(headerField(abstractRecord)); + fields.add(headerField()); while (token.kind == COMMA) { nextToken(); - fields.add(headerField(abstractRecord)); + fields.add(headerField()); } accept(RPAREN); } else { @@ -3821,13 +3821,12 @@ return fields.toList(); } - JCVariableDecl headerField(boolean abstractRecord) { + JCVariableDecl headerField() { JCModifiers mods = modifiersOpt(); if (mods.flags != 0) { log.error(mods.pos, Errors.RecordCantDeclareFieldModifiers); } - mods.flags |= Flags.RECORD | Flags.FINAL; - mods.flags |= abstractRecord ? Flags.PROTECTED : 0; + mods.flags |= Flags.RECORD | Flags.FINAL | Flags.PRIVATE; JCExpression type = parseType(); int pos = token.pos; Name id = ident();
--- a/test/langtools/tools/javac/datum/AllowBodyLessInterfacesTest.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @summary allow for bodyless interfaces - * @compile AllowBodyLessInterfacesTest.java - */ -public class AllowBodyLessInterfacesTest { - interface Super; - - interface Child extends Super; -}
--- a/test/langtools/tools/javac/datum/AllowStaticFieldsInRecordsTest.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @summary smoke test to check that static fields are allowed in records - * @compile AllowStaticFieldsInRecordsTest.java - */ - -public record AllowStaticFieldsInRecordsTest(int x) { - static int S = 3; -}
--- a/test/langtools/tools/javac/datum/DataClassAsSuper.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -/* - * @test /nodynamiccopyright/ - * @summary check that a datum can inherit from DataClass or an abstract datum class - * @compile/fail/ref=DataClassAsSuper.out -XDrawDiagnostics DataClassAsSuper.java - */ - -class DataClassAsSuper { - - // should extend DataClass or an abstract datum - record D1(int x) extends Object { } - - record D2(int y) {} - - // D2 is datum but not abstract - record D3(int y, int x) extends D2(y) {} -}
--- a/test/langtools/tools/javac/datum/DataClassAsSuper.out Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -DataClassAsSuper.java:10:21: compiler.err.expected: ';' -DataClassAsSuper.java:10:36: compiler.err.expected: token.identifier -DataClassAsSuper.java:15:28: compiler.err.expected: ';' -DataClassAsSuper.java:15:37: compiler.err.invalid.meth.decl.ret.type.req -DataClassAsSuper.java:15:41: compiler.err.expected: token.identifier -5 errors
--- a/test/langtools/tools/javac/datum/PrimaryConstructorMustBePublic.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -/* - * @test /nodynamiccopyright/ - * @summary smoke negative test for record classes - * @compile/fail/ref=PrimaryConstructorMustBePublic.out -XDrawDiagnostics PrimaryConstructorMustBePublic.java - */ - -public record PrimaryConstructorMustBePublic(int i) { - PrimaryConstructorMustBePublic {} -}
--- a/test/langtools/tools/javac/datum/PrimaryConstructorMustBePublic.out Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -PrimaryConstructorMustBePublic.java:8:5: compiler.err.method.must.be.public: <init> -1 error
--- a/test/langtools/tools/javac/datum/RecordsCantBeAbstractTest.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -/* - * @test /nodynamiccopyright/ - * @summary smoke negative test for datum classes - * @compile/fail/ref=RecordsCantBeAbstractTest.out -XDrawDiagnostics RecordsCantBeAbstractTest.java - */ - -abstract record RecordsCantBeAbstractTest(int i);
--- a/test/langtools/tools/javac/datum/RecordsCantBeAbstractTest.out Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -RecordsCantBeAbstractTest.java:7:1: compiler.err.record.cant.be.abstract -1 error
--- a/test/langtools/tools/javac/datum/RecordsMustBeStaticTest.java Fri Jun 14 08:42:28 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @summary check that records are always static - * @library /tools/javac/lib - * @modules jdk.compiler/com.sun.source.util - * jdk.compiler/com.sun.tools.javac.api - * jdk.compiler/com.sun.tools.javac.code - * jdk.compiler/com.sun.tools.javac.file - * jdk.compiler/com.sun.tools.javac.tree - * jdk.compiler/com.sun.tools.javac.util - * @build DPrinter - * @run main RecordsMustBeStaticTest - */ - -import java.io.*; -import java.net.URI; -import java.util.Arrays; - -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; -import javax.tools.ToolProvider; - -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.util.JavacTask; -import com.sun.source.util.Trees; -import com.sun.tools.javac.api.JavacTrees; -import com.sun.tools.javac.file.JavacFileManager; -import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.util.Assert; -import com.sun.tools.javac.util.Context; - -public class RecordsMustBeStaticTest { - public static void main(String... args) throws Exception { - new RecordsMustBeStaticTest().run(); - } - - void run() throws Exception { - Context context = new Context(); - JavacFileManager.preRegister(context); - Trees trees = JavacTrees.instance(context); - strOut = new StringWriter(); - PrintWriter pw = new PrintWriter(strOut); - dprinter = new DPrinter(pw, trees); - tool = ToolProvider.getSystemJavaCompiler(); - test("Foo.java", source1); - test("Foo2.java", source11); - test("Foo3.java", source111); - test("Bar.java", source2); - test("Bar2.java", source3); - test("Baz.java", source4); - } - - StringWriter strOut; - DPrinter dprinter; - JavaCompiler tool; - - void test(String fileName, String source) throws Exception { - JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource(fileName, source))); - Iterable<? extends CompilationUnitTree> elements = ct.parse(); - Assert.check(elements.iterator().hasNext()); - dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next()); - String output = strOut.toString(); - Assert.check(output.contains("flags: [static, final, record]"), "nested records should be static and final"); - } - - static final String source1 = - "class Foo {\n" + - " record R (int x);\n" + - "}"; - - static final String source11 = - "class Foo2 {\n" + - " class Inner {\n" + - " record R (int x);\n" + - " }\n" + - "}"; - static final String source111 = - "class Foo3 {\n" + - " Runnable r = new Runnable() {\n" + - " record R(int i);\n" + - " public void run() {}\n" + - " };" + - "}"; - static final String source2 = - "class Bar {\n" + - " void m() {\n" + - " record R (int x);\n" + - " }\n" + - "}"; - - static final String source3 = - "class Bar2 {\n" + - " void m() {\n" + - " static record R (int x);\n" + - " }\n" + - "}"; - - static final String source4 = - "class Baz {\n" + - " void m() {\n" + - " Runnable r = () -> {" + - " record R (int x);\n" + - " };\n" + - " }\n" + - "}"; - - static class JavaSource extends SimpleJavaFileObject { - - String source; - - public JavaSource(String fileName, String source) { - super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE); - this.source = source; - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) { - return source; - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/allow_static_fields/AllowStaticFieldsInRecordsTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary smoke test to check that static fields are allowed in records + * @compile AllowStaticFieldsInRecordsTest.java + */ + +public record AllowStaticFieldsInRecordsTest(int x) { + static int I = 1; + static final String S = "Hello World!"; + static private Object O = null; + static protected Object O2 = null; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/bodyless_interfaces/AllowBodyLessInterfacesTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary allow for bodyless interfaces + * @compile AllowBodyLessInterfacesTest.java + */ +public class AllowBodyLessInterfacesTest { + interface Super; + + interface Child extends Super; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/canonical_constructor/BadCanonicalConstructorTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,19 @@ +/* + * @test /nodynamiccopyright/ + * @summary check that a datum can inherit from DataClass or an abstract datum class + * @compile/fail/ref=BadCanonicalConstructorTest.out -XDrawDiagnostics BadCanonicalConstructorTest.java + */ + +public class BadCanonicalConstructorTest { + record R1() { + private R1 {} + } + + record R2() { + protected R2 {} + } + + record R3() { + R3 {} + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/canonical_constructor/BadCanonicalConstructorTest.out Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,4 @@ +BadCanonicalConstructorTest.java:9:17: compiler.err.method.must.be.public: <init> +BadCanonicalConstructorTest.java:13:19: compiler.err.method.must.be.public: <init> +BadCanonicalConstructorTest.java:17:9: compiler.err.method.must.be.public: <init> +3 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/canonical_constructor/CanonicalConstructorTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary testing the canonical constructor for records + * @modules jdk.compiler/com.sun.tools.javac.util + */ + +import com.sun.tools.javac.util.Assert; + +public class CanonicalConstructorTest { + record R1(int i, int j); + record R2(int i, int j) { + public R2 {} + } + record R3(int i, int j) { + public R3 { + this.i = i; + } + } + record R4(int i, int j) { + public R4 { + this.i = i; + this.j = j; + } + } + + public static void main(String... args) { + R1 r1 = new R1(1, 2); + R2 r2 = new R2(1, 2); + R3 r3 = new R3(1, 2); + R4 r4 = new R4(1, 2); + + Assert.check(r1.i == r2.i && r2.i == r3.i && r3.i == r4.i && r4.i == 1 && r1.j == r2.j && r2.j == r3.j && r3.j == r4.j && r4.j == 2, "unexpected value of record component"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/fields/RecordFieldsTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary check the existence of private fields with same name and signature of the + * declared record components + * @modules jdk.compiler/com.sun.source.util + * jdk.compiler/com.sun.tools.javac.api + * jdk.compiler/com.sun.tools.javac.code + * jdk.compiler/com.sun.tools.javac.file + * jdk.compiler/com.sun.tools.javac.util + * @ignore + */ + +/* + this test is failing with a message saying that: Field "i" in class RecordFieldsTest has illegal signature "Ljava/util/List<Ljava/lang/String;>;" + */ + +import java.lang.reflect.*; +import java.util.*; + +//import com.sun.tools.javac.util.Assert; + +public record RecordFieldsTest(List<String> i) { + + public static void main(String... args) { + RecordFieldsTest r = new RecordFieldsTest(null); + Class<?> classOfRecord = r.getClass(); + Field[] fields = classOfRecord.getDeclaredFields(); + for (Field f: fields) { + System.out.println("field " + f.toString()); + //Assert.check(Modifier.isPrivate(f.getModifiers())); + System.out.println("type: " + f.getType()); + System.out.println("signature: " + f.toGenericString()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/nested_records_must_be_static_and_final/NestedRecordsMustBeStaticAndFinalTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary check that records are always static + * @library /tools/javac/lib + * @modules jdk.compiler/com.sun.source.util + * jdk.compiler/com.sun.tools.javac.api + * jdk.compiler/com.sun.tools.javac.code + * jdk.compiler/com.sun.tools.javac.file + * jdk.compiler/com.sun.tools.javac.tree + * jdk.compiler/com.sun.tools.javac.util + * @build DPrinter + * @run main NestedRecordsMustBeStaticAndFinalTest + */ + +import java.io.*; +import java.net.URI; +import java.util.Arrays; + +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.ToolProvider; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.util.JavacTask; +import com.sun.source.util.Trees; +import com.sun.tools.javac.api.JavacTrees; +import com.sun.tools.javac.file.JavacFileManager; +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.util.Assert; +import com.sun.tools.javac.util.Context; + +public class NestedRecordsMustBeStaticAndFinalTest { + public static void main(String... args) throws Exception { + new NestedRecordsMustBeStaticAndFinalTest().run(); + } + + void run() throws Exception { + Context context = new Context(); + JavacFileManager.preRegister(context); + Trees trees = JavacTrees.instance(context); + strOut = new StringWriter(); + PrintWriter pw = new PrintWriter(strOut); + dprinter = new DPrinter(pw, trees); + tool = ToolProvider.getSystemJavaCompiler(); + test("Foo.java", source1); + test("Foo2.java", source11); + test("Foo3.java", source111); + test("Bar.java", source2); + test("Bar2.java", source3); + test("Baz.java", source4); + } + + StringWriter strOut; + DPrinter dprinter; + JavaCompiler tool; + + void test(String fileName, String source) throws Exception { + JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource(fileName, source))); + Iterable<? extends CompilationUnitTree> elements = ct.parse(); + Assert.check(elements.iterator().hasNext()); + dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next()); + String output = strOut.toString(); + Assert.check(output.contains("flags: [static, final, record]"), "nested records should be static and final"); + } + + static final String source1 = + "class Foo {\n" + + " record R (int x);\n" + + "}"; + + static final String source11 = + "class Foo2 {\n" + + " class Inner {\n" + + " record R (int x);\n" + + " }\n" + + "}"; + static final String source111 = + "class Foo3 {\n" + + " Runnable r = new Runnable() {\n" + + " record R(int i);\n" + + " public void run() {}\n" + + " };" + + "}"; + static final String source2 = + "class Bar {\n" + + " void m() {\n" + + " record R (int x);\n" + + " }\n" + + "}"; + + static final String source3 = + "class Bar2 {\n" + + " void m() {\n" + + " static record R (int x);\n" + + " }\n" + + "}"; + + static final String source4 = + "class Baz {\n" + + " void m() {\n" + + " Runnable r = () -> {" + + " record R (int x);\n" + + " };\n" + + " }\n" + + "}"; + + static class JavaSource extends SimpleJavaFileObject { + + String source; + + public JavaSource(String fileName, String source) { + super(URI.create("myfo:/" + fileName), JavaFileObject.Kind.SOURCE); + this.source = source; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return source; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/records_cant_be_abstract/RecordsCantBeAbstractTest.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,7 @@ +/* + * @test /nodynamiccopyright/ + * @summary smoke negative test for datum classes + * @compile/fail/ref=RecordsCantBeAbstractTest.out -XDrawDiagnostics RecordsCantBeAbstractTest.java + */ + +abstract record RecordsCantBeAbstractTest(int i);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/records_cant_be_abstract/RecordsCantBeAbstractTest.out Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,2 @@ +RecordsCantBeAbstractTest.java:7:1: compiler.err.record.cant.be.abstract +1 error
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.java Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,23 @@ +/* + * @test /nodynamiccopyright/ + * @summary check that a datum can inherit from DataClass or an abstract datum class + * @compile/fail/ref=RecordsCantHaveExtendsClause.out -XDrawDiagnostics RecordsCantHaveExtendsClause.java + */ + +import java.io.Serializable; + +class RecordsCantHaveExtendsClause { + + // even Object which is the implicit super class for records + record R1(int x) extends Object {} + + record R2(int y); + + // can't extend other records either + record R3(int y, int x) extends R2(y); + + // records can implement interfaces + record R4() implements Serializable, Runnable { + public void run() {} + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/datum/records_cant_have_extends_clause/RecordsCantHaveExtendsClause.out Mon Jun 17 19:31:58 2019 -0400 @@ -0,0 +1,6 @@ +RecordsCantHaveExtendsClause.java:12:21: compiler.err.expected: ';' +RecordsCantHaveExtendsClause.java:12:36: compiler.err.expected: token.identifier +RecordsCantHaveExtendsClause.java:17:28: compiler.err.expected: ';' +RecordsCantHaveExtendsClause.java:17:37: compiler.err.invalid.meth.decl.ret.type.req +RecordsCantHaveExtendsClause.java:17:41: compiler.err.expected: token.identifier +5 errors