8013416: Java Bean Persistence with XMLEncoder
authormalenkov
Fri, 24 May 2013 19:41:09 +0400
changeset 17683 1b4209f788d2
parent 17682 72c03a9bfe0c
child 17684 fc7f8dbe87b4
8013416: Java Bean Persistence with XMLEncoder Reviewed-by: alexsch
jdk/src/share/classes/com/sun/beans/finder/AbstractFinder.java
jdk/src/share/classes/com/sun/beans/finder/ConstructorFinder.java
jdk/src/share/classes/com/sun/beans/finder/MethodFinder.java
jdk/test/java/beans/XMLEncoder/Test8013416.java
--- a/jdk/src/share/classes/com/sun/beans/finder/AbstractFinder.java	Fri May 24 18:01:22 2013 +0400
+++ b/jdk/src/share/classes/com/sun/beans/finder/AbstractFinder.java	Fri May 24 19:41:09 2013 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2013, 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
@@ -24,6 +24,9 @@
  */
 package com.sun.beans.finder;
 
+import java.lang.reflect.Executable;
+import java.lang.reflect.Modifier;
+
 import java.util.HashMap;
 import java.util.Map;
 
@@ -37,7 +40,7 @@
  *
  * @author Sergey A. Malenkov
  */
-abstract class AbstractFinder<T> {
+abstract class AbstractFinder<T extends Executable> {
     private final Class<?>[] args;
 
     /**
@@ -53,27 +56,6 @@
     }
 
     /**
-     * Returns an array of {@code Class} objects
-     * that represent the formal parameter types of the method.
-     * Returns an empty array if the method takes no parameters.
-     *
-     * @param method  the object that represents method
-     * @return the parameter types of the method
-     */
-    protected abstract Class<?>[] getParameters(T method);
-
-    /**
-     * Returns {@code true} if and only if the method
-     * was declared to take a variable number of arguments.
-     *
-     * @param method  the object that represents method
-     * @return {@code true} if the method was declared
-     *         to take a variable number of arguments;
-     *         {@code false} otherwise
-     */
-    protected abstract boolean isVarArgs(T method);
-
-    /**
      * Checks validness of the method.
      * At least the valid method should be public.
      *
@@ -81,7 +63,9 @@
      * @return {@code true} if the method is valid,
      *         {@code false} otherwise
      */
-    protected abstract boolean isValid(T method);
+    protected boolean isValid(T method) {
+        return Modifier.isPublic(method.getModifiers());
+    }
 
     /**
      * Performs a search in the {@code methods} array.
@@ -109,7 +93,7 @@
 
         for (T newMethod : methods) {
             if (isValid(newMethod)) {
-                Class<?>[] newParams = getParameters(newMethod);
+                Class<?>[] newParams = newMethod.getParameterTypes();
                 if (newParams.length == this.args.length) {
                     PrimitiveWrapperMap.replacePrimitivesWithWrappers(newParams);
                     if (isAssignable(newParams, this.args)) {
@@ -120,6 +104,11 @@
                             boolean useNew = isAssignable(oldParams, newParams);
                             boolean useOld = isAssignable(newParams, oldParams);
 
+                            if (useOld && useNew) {
+                                // only if parameters are equal
+                                useNew = !newMethod.isSynthetic();
+                                useOld = !oldMethod.isSynthetic();
+                            }
                             if (useOld == useNew) {
                                 ambiguous = true;
                             } else if (useNew) {
@@ -130,7 +119,7 @@
                         }
                     }
                 }
-                if (isVarArgs(newMethod)) {
+                if (newMethod.isVarArgs()) {
                     int length = newParams.length - 1;
                     if (length <= this.args.length) {
                         Class<?>[] array = new Class<?>[this.args.length];
@@ -160,6 +149,11 @@
                         boolean useNew = isAssignable(oldParams, newParams);
                         boolean useOld = isAssignable(newParams, oldParams);
 
+                        if (useOld && useNew) {
+                            // only if parameters are equal
+                            useNew = !newMethod.isSynthetic();
+                            useOld = !oldMethod.isSynthetic();
+                        }
                         if (useOld == useNew) {
                             if (oldParams == map.get(oldMethod)) {
                                 ambiguous = true;
--- a/jdk/src/share/classes/com/sun/beans/finder/ConstructorFinder.java	Fri May 24 18:01:22 2013 +0400
+++ b/jdk/src/share/classes/com/sun/beans/finder/ConstructorFinder.java	Fri May 24 19:41:09 2013 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2013, 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
@@ -86,44 +86,4 @@
     private ConstructorFinder(Class<?>[] args) {
         super(args);
     }
-
-    /**
-     * Returns an array of {@code Class} objects
-     * that represent the formal parameter types of the constructor.
-     * Returns an empty array if the constructor takes no parameters.
-     *
-     * @param constructor  the object that represents constructor
-     * @return the parameter types of the constructor
-     */
-    @Override
-    protected Class<?>[] getParameters(Constructor<?> constructor) {
-        return constructor.getParameterTypes();
-    }
-
-    /**
-     * Returns {@code true} if and only if the constructor
-     * was declared to take a variable number of arguments.
-     *
-     * @param constructor  the object that represents constructor
-     * @return {@code true} if the constructor was declared
-     *         to take a variable number of arguments;
-     *         {@code false} otherwise
-     */
-    @Override
-    protected boolean isVarArgs(Constructor<?> constructor) {
-        return constructor.isVarArgs();
-    }
-
-    /**
-     * Checks validness of the constructor.
-     * The valid constructor should be public.
-     *
-     * @param constructor  the object that represents constructor
-     * @return {@code true} if the constructor is valid,
-     *         {@code false} otherwise
-     */
-    @Override
-    protected boolean isValid(Constructor<?> constructor) {
-        return Modifier.isPublic(constructor.getModifiers());
-    }
 }
--- a/jdk/src/share/classes/com/sun/beans/finder/MethodFinder.java	Fri May 24 18:01:22 2013 +0400
+++ b/jdk/src/share/classes/com/sun/beans/finder/MethodFinder.java	Fri May 24 19:41:09 2013 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2013, 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
@@ -196,33 +196,6 @@
     }
 
     /**
-     * Returns an array of {@code Class} objects
-     * that represent the formal parameter types of the method.
-     * Returns an empty array if the method takes no parameters.
-     *
-     * @param method  the object that represents method
-     * @return the parameter types of the method
-     */
-    @Override
-    protected Class<?>[] getParameters(Method method) {
-        return method.getParameterTypes();
-    }
-
-    /**
-     * Returns {@code true} if and only if the method
-     * was declared to take a variable number of arguments.
-     *
-     * @param method  the object that represents method
-     * @return {@code true} if the method was declared
-     *         to take a variable number of arguments;
-     *         {@code false} otherwise
-     */
-    @Override
-    protected boolean isVarArgs(Method method) {
-        return method.isVarArgs();
-    }
-
-    /**
      * Checks validness of the method.
      * The valid method should be public and
      * should have the specified name.
@@ -233,6 +206,6 @@
      */
     @Override
     protected boolean isValid(Method method) {
-        return !method.isBridge() && Modifier.isPublic(method.getModifiers()) && method.getName().equals(this.name);
+        return super.isValid(method) && method.getName().equals(this.name);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/XMLEncoder/Test8013416.java	Fri May 24 19:41:09 2013 +0400
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2013, 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
+ * @bug 8013416
+ * @summary Tests public synthetic methods
+ * @author Sergey Malenkov
+ */
+
+import java.beans.DefaultPersistenceDelegate;
+import java.beans.Encoder;
+import java.beans.Expression;
+import java.beans.Statement;
+import java.beans.XMLEncoder;
+import java.util.HashMap;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class Test8013416 extends AbstractTest {
+    public static void main(String[] args) {
+        new Test8013416().test(true);
+    }
+
+    protected Object getObject() {
+        Public<String, String> map = new Public<String, String>();
+        map.put(" pz1 ", " pz2 ");
+        map.put(" pz3 ", " pz4 ");
+        return map;
+    }
+
+    @Override
+    protected void initialize(XMLEncoder encoder) {
+        super.initialize(encoder);
+        encoder.setPersistenceDelegate(Public.class, new PublicPersistenceDelegate());
+    }
+
+    private static final class PublicPersistenceDelegate extends DefaultPersistenceDelegate {
+        @Override
+        protected Expression instantiate(Object oldInstance, Encoder out) {
+            return new Expression(oldInstance, oldInstance.getClass(), "new", null);
+        }
+
+        @Override
+        protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
+            super.initialize(type, oldInstance, newInstance, out);
+
+            Public<String, String> map = (Public) oldInstance;
+            for (Entry<String, String> entry : map.getAll()) {
+                String[] args = {entry.getKey(), entry.getValue()};
+                out.writeStatement(new Statement(oldInstance, "put", args));
+            }
+        }
+    }
+
+    public static final class Public<K, V> extends Private<K, V> {
+    }
+
+    private static class Private<K, V> {
+        private HashMap<K, V> map = new HashMap<K, V>();
+
+        public void put(K key, V value) {
+            this.map.put(key, value);
+        }
+
+        public Set<Entry<K, V>> getAll() {
+            return this.map.entrySet();
+        }
+    }
+}