jdk/src/share/classes/java/beans/Encoder.java
author malenkov
Thu, 29 Jan 2009 15:34:50 +0300
changeset 1851 8203c7eb8caf
parent 2 90ce3da70b43
child 3239 f675984c2349
permissions -rw-r--r--
6788531: java.beans.Statement imposes excessive access control Reviewed-by: peterz, rupashka
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-2007 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.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.IdentityHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * An <code>Encoder</code> is a class which can be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * files or streams that encode the state of a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * JavaBeans in terms of their public APIs. The <code>Encoder</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * in conjunction with its persistence delegates, is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * breaking the object graph down into a series of <code>Statements</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and <code>Expression</code>s which can be used to create it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A subclass typically provides a syntax for these expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * using some human readable form - like Java source code or XML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class Encoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final Map<Class<?>, PersistenceDelegate> delegates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            = Collections.synchronizedMap(new HashMap<Class<?>, PersistenceDelegate>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private Map bindings = new IdentityHashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ExceptionListener exceptionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    boolean executeStatements = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Map attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Write the specified object to the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The serialized form will denote a series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * expressions, the combined effect of which will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * an equivalent object when the input stream is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * By default, the object is assumed to be a <em>JavaBean</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * with a nullary constructor, whose state is defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * the matching pairs of "setter" and "getter" methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * returned by the Introspector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param o The object to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see XMLDecoder#readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected void writeObject(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        PersistenceDelegate info = getPersistenceDelegate(o == null ? null : o.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        info.writeObject(o, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Sets the exception handler for this stream to <code>exceptionListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The exception handler is notified when this stream catches recoverable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param exceptionListener The exception handler for this stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *       if <code>null</code> the default exception listener will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #getExceptionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void setExceptionListener(ExceptionListener exceptionListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.exceptionListener = exceptionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Gets the exception handler for this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return The exception handler for this stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *    Will return the default exception listener if this has not explicitly been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see #setExceptionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public ExceptionListener getExceptionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return (exceptionListener != null) ? exceptionListener : Statement.defaultExceptionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    Object getValue(Expression exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return (exp == null) ? null : exp.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new RuntimeException("failed to evaluate: " + exp.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the persistence delegate for the given type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The persistence delegate is calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * by applying the following of rules in order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If the type is an array, an internal persistence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * delegate is returned which will instantiate an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * array of the appropriate type and length, initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * each of its elements as if they are properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * If the type is a proxy, an internal persistence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * delegate is returned which will instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * new proxy instance using the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * "newProxyInstance" method defined in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * If the BeanInfo for this type has a <code>BeanDescriptor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * which defined a "persistenceDelegate" property, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * In all other cases the default persistence delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * is returned. The default persistence delegate assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * the type is a <em>JavaBean</em>, implying that it has a default constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * and that its state may be characterized by the matching pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * of "setter" and "getter" methods returned by the Introspector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The default constructor is the constructor with the greatest number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * of parameters that has the {@link ConstructorProperties} annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * If none of the constructors have the {@code ConstructorProperties} annotation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * then the nullary constructor (constructor with no parameters) will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * For example, in the following the nullary constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * for {@code Foo} will be used, while the two parameter constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * for {@code Bar} will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *   public class Foo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *     public Foo() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *     public Foo(int x) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *   public class Bar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *     public Bar() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *     &#64;ConstructorProperties({"x"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *     public Bar(int x) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *     &#64;ConstructorProperties({"x", "y"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *     public Bar(int x, int y) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param  type The type of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return The persistence delegate for this type of object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see #setPersistenceDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see java.beans.BeanInfo#getBeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        PersistenceDelegate pd = this.delegates.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return (pd != null) ? pd : MetaData.getPersistenceDelegate(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Sets the persistence delegate associated with this <code>type</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>persistenceDelegate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param  type The class of objects that <code>persistenceDelegate</code> applies to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param  persistenceDelegate The persistence delegate for instances of <code>type</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see #getPersistenceDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see java.beans.BeanInfo#getBeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void setPersistenceDelegate(Class<?> type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                       PersistenceDelegate persistenceDelegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (persistenceDelegate != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            this.delegates.put(type, persistenceDelegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            this.delegates.remove(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Removes the entry for this instance, returning the old entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param oldInstance The entry that should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return The entry that was removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see #get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Object remove(Object oldInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Expression exp = (Expression)bindings.remove(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return getValue(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns a tentative value for <code>oldInstance</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the environment created by this stream. A persistence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * delegate can use its <code>mutatesTo</code> method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * determine whether this value may be initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * form the equivalent object at the output or whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * a new object must be instantiated afresh. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * stream has not yet seen this value, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param  oldInstance The instance to be looked up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return The object, null if the object has not been seen before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public Object get(Object oldInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (oldInstance == null || oldInstance == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            oldInstance.getClass() == String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return oldInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Expression exp = (Expression)bindings.get(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return getValue(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private Object writeObject1(Object oldInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Object o = get(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            writeObject(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            o = get(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private Statement cloneStatement(Statement oldExp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Object oldTarget = oldExp.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Object newTarget = writeObject1(oldTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Object[] oldArgs = oldExp.getArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        Object[] newArgs = new Object[oldArgs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        for (int i = 0; i < oldArgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            newArgs[i] = writeObject1(oldArgs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (oldExp.getClass() == Statement.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return new Statement(newTarget, oldExp.getMethodName(), newArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return new Expression(newTarget, oldExp.getMethodName(), newArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Writes statement <code>oldStm</code> to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * The <code>oldStm</code> should be written entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * in terms of the callers environment, i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * target and all arguments should be part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * object graph being written. These expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * represent a series of "what happened" expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * which tell the output stream how to produce an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * object graph like the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * The implementation of this method will produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * a second expression to represent the same expression in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * an environment that will exist when the stream is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * This is achieved simply by calling <code>writeObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * on the target and all the arguments and building a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * expression with the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param oldStm The expression to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void writeStatement(Statement oldStm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // System.out.println("writeStatement: " + oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Statement newStm = cloneStatement(oldStm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (oldStm.getTarget() != this && executeStatements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                newStm.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                getExceptionListener().exceptionThrown(new Exception("Encoder: discarding statement "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                                     + newStm, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * The implementation first checks to see if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * expression with this value has already been written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * If not, the expression is cloned, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * the same procedure as <code>writeStatement</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * and the value of this expression is reconciled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * with the value of the cloned expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * by calling <code>writeObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param oldExp The expression to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public void writeExpression(Expression oldExp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // System.out.println("Encoder::writeExpression: " + oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Object oldValue = getValue(oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (get(oldValue) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        bindings.put(oldValue, (Expression)cloneStatement(oldExp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        writeObject(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        bindings.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    // Package private method for setting an attributes table for the encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    void setAttribute(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            attributes = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        attributes.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return attributes.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}