changeset 58505:38d92ac3b281 records

fix bug related to serialization members
author vromero
date Thu, 24 Oct 2019 11:56:37 -0400
parents 02fb3c24d5db
children c265707c9eb1
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/records/RecordCompilationTests.java
diffstat 3 files changed, 8 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 24 16:45:55 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Oct 24 11:56:37 2019 -0400
@@ -27,7 +27,6 @@
 
 import java.util.*;
 import java.util.function.BiConsumer;
-import java.util.stream.Collectors;
 
 import javax.lang.model.element.ElementKind;
 import javax.tools.JavaFileObject;
@@ -77,8 +76,6 @@
 import com.sun.tools.javac.comp.Analyzer.AnalyzerMode;
 import static com.sun.tools.javac.tree.JCTree.Tag.*;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
-import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
-import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
 
 /** This is the main context-dependent analysis phase in GJC. It
  *  encompasses name resolution, type checking and constant folding as
@@ -1041,7 +1038,7 @@
             }
 
             if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP) {
-                chk.checkForSerializationMembers(env, tree, true);
+                chk.checkForSerializationMethods(env, tree);
             }
 
             // annotation method checks
@@ -1224,11 +1221,10 @@
                 }
             }
             result = tree.type = v.type;
-            if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP) {
+            if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
                 if (forbiddenRecordComponentNames.contains(v.name.toString())) {
                     log.error(env.enclClass, Errors.IllegalRecordComponentName(env.enclClass.sym, v));
                 }
-                chk.checkForSerializationMembers(env, tree, false);
             }
         }
         finally {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Oct 24 16:45:55 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Oct 24 11:56:37 2019 -0400
@@ -2860,20 +2860,11 @@
             validateTypeAnnotation(a, isTypeParameter);
     }
 
-    public void checkForSerializationMembers(Env<AttrContext> env, JCTree tree, boolean isMethod) {
-        if (isMethod) {
-            JCMethodDecl methodDecl = (JCMethodDecl)tree;
-            if (methodDecl.sym.name == names.writeObject && methodDecl.params.size() == 1 && methodDecl.params.head.sym.type == syms.objectOutputStreamType ||
-                    methodDecl.sym.name == names.readObjectNoData && methodDecl.params.isEmpty() ||
-                    methodDecl.sym.name == names.readObject && methodDecl.params.size() == 1 && methodDecl.params.head.sym.type == syms.objectInputStreamType) {
-                errorOnSerializationMember(env.enclClass, methodDecl.name, methodDecl.sym, syms.voidType, false);
-            }
-        } else {
-            JCVariableDecl variableDecl = (JCVariableDecl)tree;
-            Type objectStreamFieldArr = new ArrayType(syms.objectStreamFieldType, syms.arrayClass);
-            if (variableDecl.sym.name == names.serialPersistentFields && types.isSameType(variableDecl.type, objectStreamFieldArr)) {
-                errorOnSerializationMember(env.enclClass, variableDecl.name, variableDecl.sym, objectStreamFieldArr, true);
-            }
+    public void checkForSerializationMethods(Env<AttrContext> env, JCMethodDecl methodDecl) {
+        if (methodDecl.sym.name == names.writeObject && methodDecl.params.size() == 1 && methodDecl.params.head.sym.type == syms.objectOutputStreamType ||
+                methodDecl.sym.name == names.readObjectNoData && methodDecl.params.isEmpty() ||
+                methodDecl.sym.name == names.readObject && methodDecl.params.size() == 1 && methodDecl.params.head.sym.type == syms.objectInputStreamType) {
+            errorOnSerializationMember(env.enclClass, methodDecl.name, methodDecl.sym, syms.voidType, false);
         }
     }
 
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Oct 24 16:45:55 2019 +0100
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Oct 24 11:56:37 2019 -0400
@@ -299,8 +299,7 @@
 
     public void testIllegalSerializationMembers() {
         String template = "record R(int x) { # }";
-        for (String s : List.of("private static final java.io.ObjectStreamField[] serialPersistentFields = {};",
-                                "private void writeObject(java.io.ObjectOutputStream stream) { }",
+        for (String s : List.of("private void writeObject(java.io.ObjectOutputStream stream) { }",
                                 "private void readObject(java.io.ObjectInputStream stream) { }",
                                 "private void readObjectNoData() { }"))
             assertFail("compiler.err.illegal.record.member", template, s);