mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-31 07:48:49 +00:00
8013416: Java Bean Persistence with XMLEncoder
Reviewed-by: alexsch
This commit is contained in:
parent
7ff57078e3
commit
7dc93ab854
@ -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 @@ import java.util.Map;
|
||||
*
|
||||
* @author Sergey A. Malenkov
|
||||
*/
|
||||
abstract class AbstractFinder<T> {
|
||||
abstract class AbstractFinder<T extends Executable> {
|
||||
private final Class<?>[] args;
|
||||
|
||||
/**
|
||||
@ -52,27 +55,6 @@ abstract class AbstractFinder<T> {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ abstract class AbstractFinder<T> {
|
||||
* @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 @@ abstract class AbstractFinder<T> {
|
||||
|
||||
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 @@ abstract class AbstractFinder<T> {
|
||||
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 @@ abstract class AbstractFinder<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
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 @@ abstract class AbstractFinder<T> {
|
||||
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;
|
||||
|
||||
@ -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 @@ public final class ConstructorFinder extends AbstractFinder<Constructor<?>> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
@ -195,33 +195,6 @@ public final class MethodFinder extends AbstractFinder<Method> {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -233,6 +206,6 @@ public final class MethodFinder extends AbstractFinder<Method> {
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
90
jdk/test/java/beans/XMLEncoder/Test8013416.java
Normal file
90
jdk/test/java/beans/XMLEncoder/Test8013416.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user