Encoder.java revision 2144
2144N/A * Copyright 2000-2010 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * An <code>Encoder</code> is a class which can be used to create 0N/A * files or streams that encode the state of a collection of 0N/A * JavaBeans in terms of their public APIs. The <code>Encoder</code>, 0N/A * in conjunction with its persistence delegates, is responsible for 0N/A * breaking the object graph down into a series of <code>Statements</code>s 0N/A * and <code>Expression</code>s which can be used to create it. 0N/A * A subclass typically provides a syntax for these expressions 0N/A * using some human readable form - like Java source code or XML. 0N/A * @author Philip Milne 0N/A * Write the specified object to the output stream. 0N/A * The serialized form will denote a series of 0N/A * expressions, the combined effect of which will create 0N/A * an equivalent object when the input stream is read. 0N/A * By default, the object is assumed to be a <em>JavaBean</em> 0N/A * with a nullary constructor, whose state is defined by 0N/A * the matching pairs of "setter" and "getter" methods 0N/A * returned by the Introspector. 0N/A * @param o The object to be written to the stream. 0N/A * @see XMLDecoder#readObject 0N/A * Sets the exception handler for this stream to <code>exceptionListener</code>. 0N/A * The exception handler is notified when this stream catches recoverable 0N/A * @param exceptionListener The exception handler for this stream; 0N/A * if <code>null</code> the default exception listener will be used. 0N/A * @see #getExceptionListener 0N/A * Gets the exception handler for this stream. 0N/A * @return The exception handler for this stream; 0N/A * Will return the default exception listener if this has not explicitly been set. 0N/A * @see #setExceptionListener 0N/A * Returns the persistence delegate for the given type. 2144N/A * The persistence delegate is calculated by applying 2144N/A * the following rules in order: 2144N/A * If a persistence delegate is associated with the given type 2144N/A * by using the {@link #setPersistenceDelegate} method 2144N/A * A persistence delegate is then looked up by the name 2144N/A * composed of the the fully qualified name of the given type 2144N/A * and the "PersistenceDelegate" postfix. 2144N/A * For example, a persistence delegate for the {@code Bean} class 2144N/A * should be named {@code BeanPersistenceDelegate} 2144N/A * and located in the same package. 2144N/A * public class Bean { ... } 2144N/A * public class BeanPersistenceDelegate { ... }</pre> 2144N/A * The instance of the {@code BeanPersistenceDelegate} class 2144N/A * is returned for the {@code Bean} class. 2144N/A * If the type is {@code null}, 2144N/A * a shared internal persistence delegate is returned 2144N/A * that encodes {@code null} value. 2144N/A * If the type is a {@code enum} declaration, 2144N/A * a shared internal persistence delegate is returned 2144N/A * that encodes constants of this enumeration 2144N/A * If the type is a primitive type or the corresponding wrapper, 2144N/A * a shared internal persistence delegate is returned 2144N/A * that encodes values of the given type. 2144N/A * a shared internal persistence delegate is returned 2144N/A * that encodes an array of the appropriate type and length, 2144N/A * and each of its elements as if they are properties. 2144N/A * a shared internal persistence delegate is returned 2144N/A * that encodes a proxy instance by using 2144N/A * the {@link java.lang.reflect.Proxy#newProxyInstance} method. 2144N/A * If the {@link BeanInfo} for this type has a {@link BeanDescriptor} 2144N/A * which defined a "persistenceDelegate" attribute, 2144N/A * the value of this named attribute is returned. 2144N/A * In all other cases the default persistence delegate is returned. 2144N/A * The default persistence delegate assumes the type is a <em>JavaBean</em>, 2144N/A * implying that it has a default constructor and that its state 2144N/A * may be characterized by the matching pairs of "setter" and "getter" 2144N/A * methods returned by the {@link Introspector} class. 0N/A * The default constructor is the constructor with the greatest number 0N/A * of parameters that has the {@link ConstructorProperties} annotation. 2144N/A * If none of the constructors has the {@code ConstructorProperties} annotation, 0N/A * then the nullary constructor (constructor with no parameters) will be used. 2144N/A * For example, in the following code fragment, the nullary constructor 2144N/A * for the {@code Foo} class will be used, 2144N/A * while the two-parameter constructor 2144N/A * for the {@code Bar} class will be used. 0N/A * public Foo() { ... } 0N/A * public Foo(int x) { ... } 0N/A * public Bar() { ... } 0N/A * @ConstructorProperties({"x"}) 0N/A * public Bar(int x) { ... } 0N/A * @ConstructorProperties({"x", "y"}) 0N/A * public Bar(int x, int y) { ... } 2144N/A * @param type the class of the objects 2144N/A * @return the persistence delegate for the given type 0N/A * @see #setPersistenceDelegate 0N/A * @see java.beans.Introspector#getBeanInfo 0N/A * @see java.beans.BeanInfo#getBeanDescriptor 2144N/A * Associates the specified persistence delegate with the given type. 2144N/A * @param type the class of objects that the specified persistence delegate applies to 2144N/A * @param delegate the persistence delegate for instances of the given type 0N/A * @see #getPersistenceDelegate 0N/A * @see java.beans.Introspector#getBeanInfo 0N/A * @see java.beans.BeanInfo#getBeanDescriptor 0N/A * Removes the entry for this instance, returning the old entry. 0N/A * @param oldInstance The entry that should be removed. 0N/A * @return The entry that was removed. 0N/A * Returns a tentative value for <code>oldInstance</code> in 0N/A * the environment created by this stream. A persistence 0N/A * delegate can use its <code>mutatesTo</code> method to 0N/A * determine whether this value may be initialized to 0N/A * form the equivalent object at the output or whether 0N/A * a new object must be instantiated afresh. If the 0N/A * stream has not yet seen this value, null is returned. 0N/A * @param oldInstance The instance to be looked up. 0N/A * @return The object, null if the object has not been seen before. 0N/A * Writes statement <code>oldStm</code> to the stream. 0N/A * The <code>oldStm</code> should be written entirely 0N/A * in terms of the callers environment, i.e. the 0N/A * target and all arguments should be part of the 0N/A * object graph being written. These expressions 0N/A * represent a series of "what happened" expressions 0N/A * which tell the output stream how to produce an 0N/A * object graph like the original. 0N/A * The implementation of this method will produce 0N/A * a second expression to represent the same expression in 0N/A * an environment that will exist when the stream is read. 0N/A * This is achieved simply by calling <code>writeObject</code> 0N/A * on the target and all the arguments and building a new 0N/A * expression with the results. 0N/A * @param oldStm The expression to be written to the stream. 0N/A // System.out.println("writeStatement: " + oldExp); 0N/A * The implementation first checks to see if an 0N/A * expression with this value has already been written. 0N/A * If not, the expression is cloned, using 0N/A * the same procedure as <code>writeStatement</code>, 0N/A * and the value of this expression is reconciled 0N/A * with the value of the cloned expression 0N/A * by calling <code>writeObject</code>. 0N/A * @param oldExp The expression to be written to the stream. 0N/A // System.out.println("Encoder::writeExpression: " + oldExp); 0N/A // Package private method for setting an attributes table for the encoder