changeset 56986:c0de553f0b3f amber-demo-II

Automatic merge with records-and-sealed
author mcimadamore
date Wed, 14 Aug 2019 22:25:42 +0000
parents fa802f216fb2 36c88ca11314
children 416256f30eb9
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
diffstat 4 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Sat Aug 10 00:49:40 2019 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Aug 14 22:25:42 2019 +0000
@@ -3817,6 +3817,9 @@
         return fields.toList();
     }
 
+    static final Set<String> forbiddenRecordComponentNames = Set.of("toString", "hashCode", "getClass",
+            "readObjectNoData", "readResolve", "writeReplace", "serialPersistentFields");
+
     JCVariableDecl headerField() {
         JCModifiers mods = modifiersOpt();
         if (mods.flags != 0) {
@@ -3826,6 +3829,9 @@
         JCExpression type = parseType();
         int pos = token.pos;
         Name id = ident();
+        if (forbiddenRecordComponentNames.contains(id.toString())) {
+            log.error(pos, Errors.IllegalRecordComponentName(id));
+        }
         List<Pair<Accessors.Kind, Name>> accessors = List.of(new Pair<>(Accessors.Kind.GET, id));
         return toP(F.at(pos).VarDef(mods, id, type, null, accessors));
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Sat Aug 10 00:49:40 2019 +0000
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Aug 14 22:25:42 2019 +0000
@@ -3479,6 +3479,10 @@
     required: {0}\n\
     found:    {1}\n\
 
+# 0: name
+compiler.err.illegal.record.component.name=\
+    Illegal record component name: {0}
+
 ############################################
 # messages previouly at javac.properties
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/records/fields/IllegalRecordComponentNameTest.java	Wed Aug 14 22:25:42 2019 +0000
@@ -0,0 +1,15 @@
+/*
+ * @test /nodynamiccopyright/
+ * @summary dont accept illegal record component names
+ * @compile/fail/ref=IllegalRecordComponentNameTest.out -XDrawDiagnostics IllegalRecordComponentNameTest.java
+ */
+
+public class IllegalRecordComponentNameTest {
+    record R1(String toString) {}
+    record R2(String hashCode) {}
+    record R3(String getClass) {}
+    record R4(String readObjectNoData) {}
+    record R5(String readResolve) {}
+    record R6(String writeReplace) {}
+    record R7(String serialPersistentFields) {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/records/fields/IllegalRecordComponentNameTest.out	Wed Aug 14 22:25:42 2019 +0000
@@ -0,0 +1,8 @@
+IllegalRecordComponentNameTest.java:8:22: compiler.err.illegal.record.component.name: toString
+IllegalRecordComponentNameTest.java:9:22: compiler.err.illegal.record.component.name: hashCode
+IllegalRecordComponentNameTest.java:10:22: compiler.err.illegal.record.component.name: getClass
+IllegalRecordComponentNameTest.java:11:22: compiler.err.illegal.record.component.name: readObjectNoData
+IllegalRecordComponentNameTest.java:12:22: compiler.err.illegal.record.component.name: readResolve
+IllegalRecordComponentNameTest.java:13:22: compiler.err.illegal.record.component.name: writeReplace
+IllegalRecordComponentNameTest.java:14:22: compiler.err.illegal.record.component.name: serialPersistentFields
+7 errors