jdk/src/java.desktop/share/classes/java/beans/Encoder.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 11140
diff changeset
     2
 * Copyright (c) 2000, 2011, 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: 4846
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: 4846
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: 4846
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4846
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4846
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
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
    27
import com.sun.beans.finder.PersistenceDelegateFinder;
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.IdentityHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An <code>Encoder</code> is a class which can be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * files or streams that encode the state of a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * JavaBeans in terms of their public APIs. The <code>Encoder</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * in conjunction with its persistence delegates, is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * breaking the object graph down into a series of <code>Statements</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and <code>Expression</code>s which can be used to create it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A subclass typically provides a syntax for these expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * using some human readable form - like Java source code or XML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class Encoder {
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
    49
    private final PersistenceDelegateFinder finder = new PersistenceDelegateFinder();
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5959
diff changeset
    50
    private Map<Object, Expression> bindings = new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ExceptionListener exceptionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    boolean executeStatements = true;
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5959
diff changeset
    53
    private Map<Object, Object> attributes;
2
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.
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   115
     * The persistence delegate is calculated by applying
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   116
     * the following rules in order:
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   117
     * <ol>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   118
     * <li>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   119
     * If a persistence delegate is associated with the given type
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   120
     * by using the {@link #setPersistenceDelegate} method
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   121
     * it is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <li>
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   123
     * A persistence delegate is then looked up by the name
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   124
     * composed of the fully qualified name of the given type
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   125
     * and the "PersistenceDelegate" postfix.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   126
     * For example, a persistence delegate for the {@code Bean} class
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   127
     * should be named {@code BeanPersistenceDelegate}
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   128
     * and located in the same package.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   129
     * <pre>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   130
     * public class Bean { ... }
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   131
     * public class BeanPersistenceDelegate { ... }</pre>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   132
     * The instance of the {@code BeanPersistenceDelegate} class
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   133
     * is returned for the {@code Bean} class.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   134
     * <li>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   135
     * If the type is {@code null},
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   136
     * a shared internal persistence delegate is returned
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   137
     * that encodes {@code null} value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <li>
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   139
     * If the type is a {@code enum} declaration,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   140
     * a shared internal persistence delegate is returned
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   141
     * that encodes constants of this enumeration
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   142
     * by their names.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   143
     * <li>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   144
     * If the type is a primitive type or the corresponding wrapper,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   145
     * a shared internal persistence delegate is returned
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   146
     * that encodes values of the given type.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   147
     * <li>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   148
     * If the type is an array,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   149
     * a shared internal persistence delegate is returned
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   150
     * that encodes an array of the appropriate type and length,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   151
     * and each of its elements as if they are properties.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <li>
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   153
     * If the type is a proxy,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   154
     * a shared internal persistence delegate is returned
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   155
     * that encodes a proxy instance by using
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   156
     * the {@link java.lang.reflect.Proxy#newProxyInstance} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <li>
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   158
     * If the {@link BeanInfo} for this type has a {@link BeanDescriptor}
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   159
     * which defined a "persistenceDelegate" attribute,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   160
     * the value of this named attribute is returned.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   161
     * <li>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   162
     * In all other cases the default persistence delegate is returned.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   163
     * The default persistence delegate assumes the type is a <em>JavaBean</em>,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   164
     * implying that it has a default constructor and that its state
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   165
     * may be characterized by the matching pairs of "setter" and "getter"
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   166
     * methods returned by the {@link Introspector} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * The default constructor is the constructor with the greatest number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * of parameters that has the {@link ConstructorProperties} annotation.
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   169
     * If none of the constructors has the {@code ConstructorProperties} annotation,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * then the nullary constructor (constructor with no parameters) will be used.
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   171
     * For example, in the following code fragment, the nullary constructor
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   172
     * for the {@code Foo} class will be used,
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   173
     * while the two-parameter constructor
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   174
     * for the {@code Bar} class will be used.
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   175
     * <pre>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   176
     * public class Foo {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *     public Foo() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     public Foo(int x) { ... }
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   179
     * }
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   180
     * public class Bar {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *     public Bar() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *     &#64;ConstructorProperties({"x"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *     public Bar(int x) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *     &#64;ConstructorProperties({"x", "y"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *     public Bar(int x, int y) { ... }
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   186
     * }</pre>
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   187
     * </ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   189
     * @param type  the class of the objects
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   190
     * @return the persistence delegate for the given type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see #setPersistenceDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see java.beans.BeanInfo#getBeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public PersistenceDelegate getPersistenceDelegate(Class<?> type) {
5959
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5506
diff changeset
   197
        PersistenceDelegate pd = this.finder.find(type);
11085
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   198
        if (pd == null) {
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   199
            pd = MetaData.getPersistenceDelegate(type);
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   200
            if (pd != null) {
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   201
                this.finder.register(type, pd);
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   202
            }
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   203
        }
18d8201c795b 7057459: Regression: Performance degradation with java.beans.XMLEncoder
malenkov
parents: 5959
diff changeset
   204
        return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   208
     * Associates the specified persistence delegate with the given type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   210
     * @param type  the class of objects that the specified persistence delegate applies to
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   211
     * @param delegate  the persistence delegate for instances of the given type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see #getPersistenceDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see java.beans.Introspector#getBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see java.beans.BeanInfo#getBeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
4846
48f8e6d40dde 4968536: DOC: Javadoc for java.beans.Encoder.getPersistenceDelegate is incomplete
malenkov
parents: 3240
diff changeset
   217
    public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate) {
5959
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5506
diff changeset
   218
        this.finder.register(type, delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Removes the entry for this instance, returning the old entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param oldInstance The entry that should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return The entry that was removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public Object remove(Object oldInstance) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5959
diff changeset
   230
        Expression exp = bindings.remove(oldInstance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return getValue(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns a tentative value for <code>oldInstance</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the environment created by this stream. A persistence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * delegate can use its <code>mutatesTo</code> method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * determine whether this value may be initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * form the equivalent object at the output or whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * a new object must be instantiated afresh. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * stream has not yet seen this value, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param  oldInstance The instance to be looked up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return The object, null if the object has not been seen before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public Object get(Object oldInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (oldInstance == null || oldInstance == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            oldInstance.getClass() == String.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return oldInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5959
diff changeset
   251
        Expression exp = bindings.get(oldInstance);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return getValue(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private Object writeObject1(Object oldInstance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Object o = get(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            writeObject(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            o = get(oldInstance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private Statement cloneStatement(Statement oldExp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Object oldTarget = oldExp.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Object newTarget = writeObject1(oldTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Object[] oldArgs = oldExp.getArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Object[] newArgs = new Object[oldArgs.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        for (int i = 0; i < oldArgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            newArgs[i] = writeObject1(oldArgs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
3240
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 3239
diff changeset
   273
        Statement newExp = Statement.class.equals(oldExp.getClass())
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 3239
diff changeset
   274
                ? new Statement(newTarget, oldExp.getMethodName(), newArgs)
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 3239
diff changeset
   275
                : new Expression(newTarget, oldExp.getMethodName(), newArgs);
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 3239
diff changeset
   276
        newExp.loader = oldExp.loader;
2f79c1748c93 6329581: RFE: LTP: java.beans.XMLEncoder does not manage ClassLoader.
malenkov
parents: 3239
diff changeset
   277
        return newExp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Writes statement <code>oldStm</code> to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The <code>oldStm</code> should be written entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * in terms of the callers environment, i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * target and all arguments should be part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * object graph being written. These expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * represent a series of "what happened" expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * which tell the output stream how to produce an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * object graph like the original.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * The implementation of this method will produce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * a second expression to represent the same expression in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * an environment that will exist when the stream is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * This is achieved simply by calling <code>writeObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * on the target and all the arguments and building a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * expression with the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param oldStm The expression to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void writeStatement(Statement oldStm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // System.out.println("writeStatement: " + oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Statement newStm = cloneStatement(oldStm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (oldStm.getTarget() != this && executeStatements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                newStm.execute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                getExceptionListener().exceptionThrown(new Exception("Encoder: discarding statement "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                                     + newStm, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * The implementation first checks to see if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * expression with this value has already been written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * If not, the expression is cloned, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * the same procedure as <code>writeStatement</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * and the value of this expression is reconciled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * with the value of the cloned expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * by calling <code>writeObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param oldExp The expression to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void writeExpression(Expression oldExp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // System.out.println("Encoder::writeExpression: " + oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        Object oldValue = getValue(oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (get(oldValue) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        bindings.put(oldValue, (Expression)cloneStatement(oldExp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        writeObject(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        bindings.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Package private method for setting an attributes table for the encoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    void setAttribute(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (attributes == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5959
diff changeset
   340
            attributes = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        attributes.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    Object getAttribute(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return attributes.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
}