jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4849 c83eca4dbb8f
child 5597 ab490f66d2cf
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4849
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.reflect.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>DefaultPersistenceDelegate</code> is a concrete implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the abstract <code>PersistenceDelegate</code> class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * is the delegate used by default for classes about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * which no information is available. The <code>DefaultPersistenceDelegate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * provides, version resilient, public API-based persistence for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * classes that follow the JavaBeans conventions without any class specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The key assumptions are that the class has a nullary constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and that its state is accurately represented by matching pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of "setter" and "getter" methods in the order they are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by the Introspector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * In addition to providing code-free persistence for JavaBeans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the <code>DefaultPersistenceDelegate</code> provides a convenient means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to effect persistent storage for classes that have a constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * that, while not nullary, simply requires some property values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * as arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see #DefaultPersistenceDelegate(String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see java.beans.Introspector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class DefaultPersistenceDelegate extends PersistenceDelegate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private String[] constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Boolean definesEquals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a persistence delegate for a class with a nullary constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @see #DefaultPersistenceDelegate(java.lang.String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public DefaultPersistenceDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a default persistence delegate for a class with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * constructor whose arguments are the values of the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * names as specified by <code>constructorPropertyNames</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The constructor arguments are created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * evaluating the property names in the order they are supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * To use this class to specify a single preferred constructor for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * in the serialization of a particular type, we state the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * names of the properties that make up the constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * arguments. For example, the <code>Font</code> class which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * does not define a nullary constructor can be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * with the following persistence delegate:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *     new DefaultPersistenceDelegate(new String[]{"name", "style", "size"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param  constructorPropertyNames The property names for the arguments of this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @see #instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public DefaultPersistenceDelegate(String[] constructorPropertyNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.constructor = constructorPropertyNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static boolean definesEquals(Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return type == type.getMethod("equals", Object.class).getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        catch(NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private boolean definesEquals(Object instance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (definesEquals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return (definesEquals == Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            boolean result = definesEquals(instance.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            definesEquals = result ? Boolean.TRUE : Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * If the number of arguments in the specified constructor is non-zero and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * the class of <code>oldInstance</code> explicitly declares an "equals" method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * this method returns the value of <code>oldInstance.equals(newInstance)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Otherwise, this method uses the superclass's definition which returns true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * classes of the two instances are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param oldInstance The instance to be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param newInstance The instance that is to be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return True if an equivalent copy of <code>newInstance</code> may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         created by applying a series of mutations to <code>oldInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see #DefaultPersistenceDelegate(String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected boolean mutatesTo(Object oldInstance, Object newInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Assume the instance is either mutable or a singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // if it has a nullary constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return (constructor.length == 0) || !definesEquals(oldInstance) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            super.mutatesTo(oldInstance, newInstance) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            oldInstance.equals(newInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This default implementation of the <code>instantiate</code> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * an expression containing the predefined method name "new" which denotes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * call to a constructor with the arguments as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the <code>DefaultPersistenceDelegate</code>'s constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param  oldInstance The instance to be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param  out The code output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return An expression whose value is <code>oldInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
4849
c83eca4dbb8f 6412286: DOC: LTP: Unspecified NPE in java.beans.DefaultPersistenceDelegate.instantiate method
malenkov
parents: 1280
diff changeset
   149
     * @throws NullPointerException if {@code out} is {@code null}
c83eca4dbb8f 6412286: DOC: LTP: Unspecified NPE in java.beans.DefaultPersistenceDelegate.instantiate method
malenkov
parents: 1280
diff changeset
   150
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #DefaultPersistenceDelegate(String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected Expression instantiate(Object oldInstance, Encoder out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int nArgs = constructor.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Class type = oldInstance.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Object[] constructorArgs = new Object[nArgs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        for(int i = 0; i < nArgs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                Method method = findMethod(type, this.constructor[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                constructorArgs[i] = MethodUtil.invoke(method, oldInstance, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                out.getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return new Expression(oldInstance, oldInstance.getClass(), "new", constructorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   169
    private Method findMethod(Class type, String property) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new IllegalArgumentException("Property name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   173
        PropertyDescriptor pd = getPropertyDescriptor(type, property);
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   174
        if (pd == null) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   175
            throw new IllegalStateException("Could not find property by the name " + property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   177
        Method method = pd.getReadMethod();
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   178
        if (method == null) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   179
            throw new IllegalStateException("Could not find getter for the property " + property);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   181
        return method;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static boolean equals(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return (o1 == null) ? (o2 == null) : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private void doProperty(Class type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Method getter = pd.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Method setter = pd.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   192
        if (getter != null && setter != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            Expression oldGetExp = new Expression(oldInstance, getter.getName(), new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Expression newGetExp = new Expression(newInstance, getter.getName(), new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            Object oldValue = oldGetExp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            Object newValue = newGetExp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            out.writeExpression(oldGetExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (!equals(newValue, out.get(oldValue))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // Search for a static constant with this value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                Object e = (Object[])pd.getValue("enumerationValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (e instanceof Object[] && Array.getLength(e) % 3 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    Object[] a = (Object[])e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    for(int i = 0; i < a.length; i = i + 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                           Field f = type.getField((String)a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                           if (f.get(null).equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                               out.remove(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                               out.writeExpression(new Expression(oldValue, f, "get", new Object[]{null}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        catch (Exception ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                invokeStatement(oldInstance, setter.getName(), new Object[]{oldValue}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static void invokeStatement(Object instance, String methodName, Object[] args, Encoder out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        out.writeStatement(new Statement(instance, methodName, args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Write out the properties of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private void initBean(Class type, Object oldInstance, Object newInstance, Encoder out) {
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   225
        BeanInfo info;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   226
        try {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   227
            info = Introspector.getBeanInfo(type);
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   228
        } catch (IntrospectionException exception) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   229
            return;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   230
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // Properties
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   232
        for (PropertyDescriptor d : info.getPropertyDescriptors()) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   233
            if (d.isTransient()) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   234
                continue;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   235
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            try {
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   237
                doProperty(type, d, oldInstance, newInstance, out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                out.getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Pending(milne). There is a general problem with the archival of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        listeners which is unresolved as of 1.4. Many of the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        which install one object inside another (typically "add" methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        or setters) automatically install a listener on the "child" object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        so that its "parent" may respond to changes that are made to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        For example the JTable:setModel() method automatically adds a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        TableModelListener (the JTable itself in this case) to the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        table model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        We do not need to explictly add these listeners to the model in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        archive as they will be added automatically by, in the above case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        the JTable's "setModel" method. In some cases, we must specifically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        avoid trying to do this since the listener may be an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        that cannot be instantiated using public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        No general mechanism currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        exists for differentiating between these kind of listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        those which were added explicitly by the user. A mechanism must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        be created to provide a general means to differentiate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        special cases so as to provide reliable persistence of listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        for the general case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (!java.awt.Component.class.isAssignableFrom(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return; // Just handle the listeners of Components for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   271
        for (EventSetDescriptor d : info.getEventSetDescriptors()) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   272
            if (d.isTransient()) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   273
                continue;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   274
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            Class listenerType = d.getListenerType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            // The ComponentListener is added automatically, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Contatiner:add is called on the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (listenerType == java.awt.event.ComponentListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // JMenuItems have a change listener added to them in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // their "add" methods to enable accessibility support -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // see the add method in JMenuItem for details. We cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // instantiate this instance as it is a private inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // and do not need to do this anyway since it will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // and installed by the "add" method. Special case this for now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // ignoring all change listeners on JMenuItems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (listenerType == javax.swing.event.ChangeListener.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                type == javax.swing.JMenuItem.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            EventListener[] oldL = new EventListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            EventListener[] newL = new EventListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                Method m = d.getGetListenerMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
1280
f58fc9f575e3 6351692: catch(Throwable) in java.beans.MetaData preventing thread shutdown
malenkov
parents: 466
diff changeset
   303
            catch (Exception e2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    Method m = type.getMethod("getListeners", new Class[]{Class.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{listenerType});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{listenerType});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                catch (Exception e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // Asssume the listeners are in the same order and that there are no gaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // Eventually, this may need to do true differencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            String addListenerMethodName = d.getAddListenerMethod().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            for (int i = newL.length; i < oldL.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // System.out.println("Adding listener: " + addListenerMethodName + oldL[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                invokeStatement(oldInstance, addListenerMethodName, new Object[]{oldL[i]}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            String removeListenerMethodName = d.getRemoveListenerMethod().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            for (int i = oldL.length; i < newL.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                invokeStatement(oldInstance, removeListenerMethodName, new Object[]{newL[i]}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * This default implementation of the <code>initialize</code> method assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * all state held in objects of this type is exposed via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * matching pairs of "setter" and "getter" methods in the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * they are returned by the Introspector. If a property descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * defines a "transient" attribute with a value equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>Boolean.TRUE</code> the property is ignored by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * default implementation. Note that this use of the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * "transient" is quite independent of the field modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * that is used by the <code>ObjectOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * For each non-transient property, an expression is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * in which the nullary "getter" method is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * to the <code>oldInstance</code>. The value of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * expression is the value of the property in the instance that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * being serialized. If the value of this expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * in the cloned environment <code>mutatesTo</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * target value, the new value is initialized to make it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * equivalent to the old value. In this case, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * the property value has not changed there is no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * call the corresponding "setter" method and no statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * is emitted. If not however, the expression for this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * is replaced with another expression (normally a constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * and the corresponding "setter" method is called to install
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * the new property value in the object. This scheme removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * default information from the output produced by streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * using this delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * In passing these statements to the output stream, where they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * will be executed, side effects are made to the <code>newInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * In most cases this allows the problem of properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * whose values depend on each other to actually help the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * serialization process by making the number of statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * that need to be written to the output smaller. In general,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * the problem of handling interdependent properties is reduced to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * that of finding an order for the properties in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * a class such that no property value depends on the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * a subsequent property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param oldInstance The instance to be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param newInstance The instance that is to be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param out The stream to which any initialization statements should be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
4849
c83eca4dbb8f 6412286: DOC: LTP: Unspecified NPE in java.beans.DefaultPersistenceDelegate.instantiate method
malenkov
parents: 1280
diff changeset
   372
     * @throws NullPointerException if {@code out} is {@code null}
c83eca4dbb8f 6412286: DOC: LTP: Unspecified NPE in java.beans.DefaultPersistenceDelegate.instantiate method
malenkov
parents: 1280
diff changeset
   373
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @see java.beans.PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    protected void initialize(Class<?> type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                              Object oldInstance, Object newInstance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                              Encoder out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // System.out.println("DefulatPD:initialize" + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        super.initialize(type, oldInstance, newInstance, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (oldInstance.getClass() == type) { // !type.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            initBean(type, oldInstance, newInstance, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   387
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   388
    private static PropertyDescriptor getPropertyDescriptor(Class type, String property) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   389
        try {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   390
            for (PropertyDescriptor pd : Introspector.getBeanInfo(type).getPropertyDescriptors()) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   391
                if (property.equals(pd.getName()))
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   392
                    return pd;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   393
            }
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   394
        } catch (IntrospectionException exception) {
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   395
        }
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   396
        return null;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   397
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}