changeset 56991:10f3b9bd83b8 records-and-sealed

make records inherit from j.l.Record and check that no class can inherit from j.l.Record
author vromero
date Thu, 15 Aug 2019 17:45:52 -0400
parents 8e7456b980f4
children 64208d25926f 7f0d4f15e62b
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/langtools/tools/javac/records/record_superclass/RecordAsSuperclassTest.java
diffstat 4 files changed, 48 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Thu Aug 15 14:01:09 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Thu Aug 15 17:45:52 2019 -0400
@@ -218,6 +218,7 @@
     public final Type patternHandlesType;
     public final Type patternHandleType;
     public final Type typeDescriptorType;
+    public final Type recordType;
 
     /** The symbol representing the length field of an array.
      */
@@ -578,6 +579,7 @@
         patternHandlesType = enterClass("java.lang.runtime.PatternHandles");
         patternHandleType = enterClass("java.lang.runtime.PatternHandle");
         typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
+        recordType = enterClass("java.lang.Record");
 
         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
         synthesizeEmptyInterfaceIfMissing(cloneableType);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Aug 15 14:01:09 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Aug 15 17:45:52 2019 -0400
@@ -692,6 +692,9 @@
             if (tree.extending != null) {
                 extending = clearTypeParams(tree.extending);
                 supertype = attr.attribBase(extending, sym, baseEnv, true, false, true);
+                if (supertype == syms.recordType) {
+                    log.error(tree, Errors.InvalidSupertypeRecord);
+                }
             } else {
                 extending = null;
                 supertype = ((tree.mods.flags & Flags.ENUM) != 0)
@@ -699,7 +702,7 @@
                                   true, false, false)
                 : (sym.fullname == names.java_lang_Object)
                 ? Type.noType
-                : syms.objectType;
+                : sym.isRecord() ? syms.recordType : syms.objectType;
             }
             ct.supertype_field = modelMissingTypes(baseEnv, supertype, extending, false);
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Aug 15 14:01:09 2019 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Aug 15 17:45:52 2019 -0400
@@ -3473,6 +3473,9 @@
 compiler.err.illegal.record.component.name=\
     Illegal record component name: {0}
 
+compiler.err.invalid.supertype.record=\
+    no class can explicitly extend java.lang.Record
+
 ############################################
 # messages previouly at javac.properties
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/records/record_superclass/RecordAsSuperclassTest.java	Thu Aug 15 17:45:52 2019 -0400
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ *
+ * 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 the superclass is java.lang.Record
+ * @modules jdk.compiler/com.sun.tools.javac.util
+ */
+
+import com.sun.tools.javac.util.Assert;
+
+public class RecordAsSuperclassTest {
+    record R () {}
+
+    public static void main(String... args) {
+        Class<?> rClass = R.class;
+        Assert.check(rClass.getSuperclass() == Record.class);
+    }
+}