RFR 8159751: ObjectStreamClass: hide 'final' flag for anonymous classes

Dan Smith daniel.smith at oracle.com
Wed Jun 22 23:05:31 UTC 2016


Hi, making a change to serialization's computation of UID in order to avoid taking ACC_FINAL into account for anonymous inner classes.  Will also change the Serialization Specification.

https://bugs.openjdk.java.net/browse/JDK-8159751

The patch is small, inline below.  Not sure about the best testing strategy, but I decided to add a test that compares to a precomputed magic number.  I verified that this test fails after modifying javac (per JDK-8129576), but then succeeds after applying the change to ObjectStreamClass.  (I won't actually push JDK-8129576 until this is pushed, though.)

—Dan

# HG changeset patch
# Parent 7e7f37ae1a6d76c0374b0dc46709d8fde25456b1
diff -r 7e7f37ae1a6d -r f468e80bcd51 src/java.base/share/classes/java/io/ObjectStreamClass.java
--- a/src/java.base/share/classes/java/io/ObjectStreamClass.java	Tue Jun 21 15:15:05 2016 -0700
+++ b/src/java.base/share/classes/java/io/ObjectStreamClass.java	Wed Jun 22 16:59:00 2016 -0600
@@ -1708,6 +1708,14 @@
                  Modifier.INTERFACE | Modifier.ABSTRACT);
 
             /*
+             * compensate for anonymous classes which have historically
+             * not been marked ACC_FINAL
+             */
+            if ((classMods & Modifier.FINAL) != 0 && cl.isAnonymousClass()) {
+                classMods = classMods & ~Modifier.FINAL;
+            }
+            
+            /*
              * compensate for javac bug in which ABSTRACT bit was set for an
              * interface only if the interface declared methods
              */
diff -r 7e7f37ae1a6d -r f468e80bcd51 test/java/io/ObjectInputStream/AnonymousClassUID.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/io/ObjectInputStream/AnonymousClassUID.java	Wed Jun 22 16:59:00 2016 -0600
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+
+import java.io.ObjectStreamClass;
+import java.io.Serializable;
+
+/*
+ * @test
+ * @bug 8159751
+ * @summary Ensure that an anonymous inner class has a stable UID
+ */
+public class AnonymousClassUID {
+
+    public static void main(String[] args) {
+        Class<?> c = new Serializable() {
+            int x = 0;
+            public String m(Object arg) { return null; }
+            public int m(Class<String> arg) { return 1; }
+        }.getClass();
+        long uid = ObjectStreamClass.lookup(c).getSerialVersionUID();
+        long expectedUID = -8213874395645447803L;
+        if (uid != expectedUID) throw new AssertionError("Unexpected UID: " + uid);
+    }
+
+}


More information about the core-libs-dev mailing list