changeset 51188:87909b67c156 datum

dont add readResolve if readObject is implemented, plus adding more cases to test CheckReadResolveMethodTest
author vromero
date Thu, 14 Jun 2018 14:59:06 -0700
parents 3532b9384a41
children d5d8ebbf38e0
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java test/langtools/tools/javac/datum/CheckReadResolveMethodTest.java
diffstat 3 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Jun 14 14:58:13 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Jun 14 14:59:06 2018 -0700
@@ -1157,7 +1157,8 @@
             }
 
             if (attr.isSerializable(tree.sym.type) && !defaultConstructorGenerated) {
-                if (lookupMethod(tree.sym, names.readResolve, List.nil()) == null) {
+                if (lookupMethod(tree.sym, names.readResolve, List.nil()) == null &&
+                    lookupMethod(tree.sym, names.readObject, List.nil()) == null) {
                     // private Object readResolve() { return ???; }
                     JCMethodDecl readResolve = make.
                         MethodDef(make.Modifiers(Flags.PRIVATE | Flags.RECORD | Flags.FINAL),
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java	Thu Jun 14 14:58:13 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java	Thu Jun 14 14:59:06 2018 -0700
@@ -115,6 +115,7 @@
     public final Name valueOf;
     public final Name values;
     public final Name readResolve;
+    public final Name readObject;
 
     // class names
     public final Name java_io_Serializable;
@@ -292,6 +293,7 @@
         valueOf = fromString("valueOf");
         values = fromString("values");
         readResolve = fromString("readResolve");
+        readObject = fromString("readObject");
         dollarThis = fromString("$this");
 
         // class names
--- a/test/langtools/tools/javac/datum/CheckReadResolveMethodTest.java	Thu Jun 14 14:58:13 2018 -0700
+++ b/test/langtools/tools/javac/datum/CheckReadResolveMethodTest.java	Thu Jun 14 14:59:06 2018 -0700
@@ -36,7 +36,7 @@
 
 public class CheckReadResolveMethodTest {
 
-    // readResolve should be generated as the user is providing a constructor
+    // readResolve should be generated as a constructor is provided
     record Point1(int i, int j) implements Serializable {
         Point1(int i, int j) {
             this.i = i;
@@ -44,9 +44,24 @@
         }
     }
 
-    // no readResolve should be generated as the user is not providing a constructor
+    // no readResolve should be generated as a constructor is not provided
     record Point2(int i, int j) implements Serializable;
 
+    // no readResolve should be generated as the record doesnt implement Serializable
+    record Point3(int i, int j);
+
+    // no readResolve should be generated as an implementation is already provided
+    record Point4(int i, int j) {
+        private Object readResolve() {
+            return new Point4(i, j);
+        }
+    }
+
+    // no readResolve should be generated as an implementation of readObject is already provided
+    record Point5(int i, int j) {
+        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {}
+    }
+
     public static void main(String args[]) throws Throwable {
         new CheckReadResolveMethodTest().run();
     }
@@ -54,6 +69,9 @@
     void run() throws Throwable {
         checkReadResolveMethod("Point1", true);
         checkReadResolveMethod("Point2", false);
+        checkReadResolveMethod("Point3", false);
+        checkReadResolveMethod("Point4", true);
+        checkReadResolveMethod("Point5", false);
     }
 
     void checkReadResolveMethod(String className, boolean shouldBeThere) throws Throwable {