jdk/src/share/classes/java/beans/DefaultPersistenceDelegate.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 466 6acd5ec503a8
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #DefaultPersistenceDelegate(String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected Expression instantiate(Object oldInstance, Encoder out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int nArgs = constructor.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Class type = oldInstance.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Object[] constructorArgs = new Object[nArgs];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for(int i = 0; i < nArgs; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                Method method = findMethod(type, this.constructor[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                constructorArgs[i] = MethodUtil.invoke(method, oldInstance, new Object[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                out.getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return new Expression(oldInstance, oldInstance.getClass(), "new", constructorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private Method findMethod(Class type, String property) throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new IllegalArgumentException("Property name is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        BeanInfo info = Introspector.getBeanInfo(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (property.equals(pd.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                Method method = pd.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (method != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new IllegalStateException("Could not find getter for the property " + property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throw new IllegalStateException("Could not find property by the name " + property);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // This is a workaround for a bug in the introspector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // PropertyDescriptors are not shared amongst subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private boolean isTransient(Class type, PropertyDescriptor pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // This code was mistakenly deleted - it may be fine and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // is more efficient than the code below. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // all disappear anyway when property descriptors are shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // by the introspector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Method getter = pd.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Class declaringClass = getter.getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (declaringClass == type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return Boolean.TRUE.equals(pd.getValue("transient"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        String pName = pd.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        BeanInfo info = MetaData.getBeanInfo(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i = 0; i < propertyDescriptors.length; ++i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            PropertyDescriptor pd2 = propertyDescriptors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (pName.equals(pd2.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                Object value = pd2.getValue("transient");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    return Boolean.TRUE.equals(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return isTransient(type.getSuperclass(), pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static boolean equals(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return (o1 == null) ? (o2 == null) : o1.equals(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private void doProperty(Class type, PropertyDescriptor pd, Object oldInstance, Object newInstance, Encoder out) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Method getter = pd.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Method setter = pd.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (getter != null && setter != null && !isTransient(type, pd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Expression oldGetExp = new Expression(oldInstance, getter.getName(), new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            Expression newGetExp = new Expression(newInstance, getter.getName(), new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            Object oldValue = oldGetExp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            Object newValue = newGetExp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            out.writeExpression(oldGetExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (!equals(newValue, out.get(oldValue))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // Search for a static constant with this value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                Object e = (Object[])pd.getValue("enumerationValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (e instanceof Object[] && Array.getLength(e) % 3 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    Object[] a = (Object[])e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    for(int i = 0; i < a.length; i = i + 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                           Field f = type.getField((String)a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                           if (f.get(null).equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                               out.remove(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                               out.writeExpression(new Expression(oldValue, f, "get", new Object[]{null}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        catch (Exception ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                invokeStatement(oldInstance, setter.getName(), new Object[]{oldValue}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static void invokeStatement(Object instance, String methodName, Object[] args, Encoder out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        out.writeStatement(new Statement(instance, methodName, args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // Write out the properties of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private void initBean(Class type, Object oldInstance, Object newInstance, Encoder out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // System.out.println("initBean: " + oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        BeanInfo info = MetaData.getBeanInfo(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = 0; i < propertyDescriptors.length; ++i ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                doProperty(type, propertyDescriptors[i], oldInstance, newInstance, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                out.getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Pending(milne). There is a general problem with the archival of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        listeners which is unresolved as of 1.4. Many of the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        which install one object inside another (typically "add" methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        or setters) automatically install a listener on the "child" object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        so that its "parent" may respond to changes that are made to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        For example the JTable:setModel() method automatically adds a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        TableModelListener (the JTable itself in this case) to the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        table model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        We do not need to explictly add these listeners to the model in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        archive as they will be added automatically by, in the above case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        the JTable's "setModel" method. In some cases, we must specifically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        avoid trying to do this since the listener may be an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        that cannot be instantiated using public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        No general mechanism currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        exists for differentiating between these kind of listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        those which were added explicitly by the user. A mechanism must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        be created to provide a general means to differentiate these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        special cases so as to provide reliable persistence of listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for the general case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!java.awt.Component.class.isAssignableFrom(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return; // Just handle the listeners of Components for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        EventSetDescriptor[] eventSetDescriptors = info.getEventSetDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for (int e = 0; e < eventSetDescriptors.length; e++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            EventSetDescriptor d = eventSetDescriptors[e];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            Class listenerType = d.getListenerType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // The ComponentListener is added automatically, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // Contatiner:add is called on the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (listenerType == java.awt.event.ComponentListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // JMenuItems have a change listener added to them in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // their "add" methods to enable accessibility support -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // see the add method in JMenuItem for details. We cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // instantiate this instance as it is a private inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // and do not need to do this anyway since it will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // and installed by the "add" method. Special case this for now,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // ignoring all change listeners on JMenuItems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (listenerType == javax.swing.event.ChangeListener.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                type == javax.swing.JMenuItem.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            EventListener[] oldL = new EventListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            EventListener[] newL = new EventListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                Method m = d.getGetListenerMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            catch (Throwable e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    Method m = type.getMethod("getListeners", new Class[]{Class.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[]{listenerType});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[]{listenerType});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                catch (Exception e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // Asssume the listeners are in the same order and that there are no gaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // Eventually, this may need to do true differencing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            String addListenerMethodName = d.getAddListenerMethod().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            for (int i = newL.length; i < oldL.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                // System.out.println("Adding listener: " + addListenerMethodName + oldL[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                invokeStatement(oldInstance, addListenerMethodName, new Object[]{oldL[i]}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            String removeListenerMethodName = d.getRemoveListenerMethod().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            for (int i = oldL.length; i < newL.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                invokeStatement(oldInstance, removeListenerMethodName, new Object[]{newL[i]}, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * This default implementation of the <code>initialize</code> method assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * all state held in objects of this type is exposed via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * matching pairs of "setter" and "getter" methods in the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * they are returned by the Introspector. If a property descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * defines a "transient" attribute with a value equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>Boolean.TRUE</code> the property is ignored by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * default implementation. Note that this use of the word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * "transient" is quite independent of the field modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * that is used by the <code>ObjectOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * For each non-transient property, an expression is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * in which the nullary "getter" method is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * to the <code>oldInstance</code>. The value of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * expression is the value of the property in the instance that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * being serialized. If the value of this expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * in the cloned environment <code>mutatesTo</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * target value, the new value is initialized to make it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * equivalent to the old value. In this case, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * the property value has not changed there is no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * call the corresponding "setter" method and no statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * is emitted. If not however, the expression for this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * is replaced with another expression (normally a constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * and the corresponding "setter" method is called to install
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the new property value in the object. This scheme removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * default information from the output produced by streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * using this delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * In passing these statements to the output stream, where they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * will be executed, side effects are made to the <code>newInstance</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * In most cases this allows the problem of properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * whose values depend on each other to actually help the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * serialization process by making the number of statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * that need to be written to the output smaller. In general,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * the problem of handling interdependent properties is reduced to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * that of finding an order for the properties in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * a class such that no property value depends on the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * a subsequent property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param oldInstance The instance to be copied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param newInstance The instance that is to be modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param out The stream to which any initialization statements should be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @see java.beans.PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    protected void initialize(Class<?> type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                              Object oldInstance, Object newInstance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                              Encoder out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // System.out.println("DefulatPD:initialize" + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        super.initialize(type, oldInstance, newInstance, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (oldInstance.getClass() == type) { // !type.isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            initBean(type, oldInstance, newInstance, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
}