Class Encoder
java.lang.Object
java.beans.Encoder
- Direct Known Subclasses:
XMLEncoder
An
Encoder is a class which can be used to create
files or streams that encode the state of a collection of
JavaBeans in terms of their public APIs. The Encoder,
in conjunction with its persistence delegates, is responsible for
breaking the object graph down into a series of Statements
and Expressions which can be used to create it.
A subclass typically provides a syntax for these expressions
using some human readable form - like Java source code or XML.- Since:
- 1.4
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a tentative value foroldInstancein the environment created by this stream.Gets the exception handler for this stream.getPersistenceDelegate(Class<?> type) Returns the persistence delegate for the given type.Removes the entry for this instance, returning the old entry.voidsetExceptionListener(ExceptionListener exceptionListener) Sets the exception handler for this stream toexceptionListener.voidsetPersistenceDelegate(Class<?> type, PersistenceDelegate delegate) Associates the specified persistence delegate with the given type.voidwriteExpression(Expression oldExp) The implementation first checks to see if an expression with this value has already been written.protected voidWrite the specified object to the output stream.voidwriteStatement(Statement oldStm) Writes statementoldStmto the stream.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
Encoder
public Encoder()Constructs anEncoder.
-
-
Method Details
-
writeObject
Write the specified object to the output stream. The serialized form will denote a series of expressions, the combined effect of which will create an equivalent object when the input stream is read. By default, the object is assumed to be a JavaBean with a nullary constructor, whose state is defined by the matching pairs of "setter" and "getter" methods returned by the Introspector.- Parameters:
o- The object to be written to the stream.- See Also:
-
setExceptionListener
Sets the exception handler for this stream toexceptionListener. The exception handler is notified when this stream catches recoverable exceptions.- Parameters:
exceptionListener- The exception handler for this stream; ifnullthe default exception listener will be used.- See Also:
-
getExceptionListener
Gets the exception handler for this stream.- Returns:
- The exception handler for this stream; Will return the default exception listener if this has not explicitly been set.
- See Also:
-
getPersistenceDelegate
Returns the persistence delegate for the given type. The persistence delegate is calculated by applying the following rules in order:-
If a persistence delegate is associated with the given type
by using the
setPersistenceDelegate(Class, PersistenceDelegate)method it is returned. -
A persistence delegate is then looked up by the name
composed of the fully qualified name of the given type
and the "PersistenceDelegate" postfix.
For example, a persistence delegate for the
Beanclass should be namedBeanPersistenceDelegateand located in the same package.public class Bean { ... } public class BeanPersistenceDelegate { ... }The instance of theBeanPersistenceDelegateclass is returned for theBeanclass. -
If the type is
null, a shared internal persistence delegate is returned that encodesnullvalue. -
If the type is an
enumdeclaration, a shared internal persistence delegate is returned that encodes constants of this enumeration by their names. - If the type is a primitive type or the corresponding wrapper, a shared internal persistence delegate is returned that encodes values of the given type.
- If the type is an array, a shared internal persistence delegate is returned that encodes an array of the appropriate type and length, and each of its elements as if they are properties.
-
If the type is a proxy,
a shared internal persistence delegate is returned
that encodes a proxy instance by using
the
Proxy.newProxyInstance(ClassLoader, Class[], InvocationHandler)method. -
If the
BeanInfofor this type has aBeanDescriptorwhich defined a "persistenceDelegate" attribute, the value of this named attribute is returned. -
In all other cases the default persistence delegate is returned.
The default persistence delegate assumes the type is a JavaBean,
implying that it has a default constructor and that its state
may be characterized by the matching pairs of "setter" and "getter"
methods returned by the
Introspectorclass. The default constructor is the constructor with the greatest number of parameters that has theConstructorPropertiesannotation. If none of the constructors has theConstructorPropertiesannotation, then the nullary constructor (constructor with no parameters) will be used. For example, in the following code fragment, the nullary constructor for theFooclass will be used, while the two-parameter constructor for theBarclass will be used.public class Foo { public Foo() { ... } public Foo(int x) { ... } } public class Bar { public Bar() { ... } @ConstructorProperties({"x"}) public Bar(int x) { ... } @ConstructorProperties({"x", "y"}) public Bar(int x, int y) { ... } }
- Parameters:
type- the class of the objects- Returns:
- the persistence delegate for the given type
- See Also:
-
If a persistence delegate is associated with the given type
by using the
-
setPersistenceDelegate
Associates the specified persistence delegate with the given type.- Parameters:
type- the class of objects that the specified persistence delegate applies todelegate- the persistence delegate for instances of the given type- See Also:
-
remove
-
get
Returns a tentative value foroldInstancein the environment created by this stream. A persistence delegate can use itsmutatesTomethod to determine whether this value may be initialized to form the equivalent object at the output or whether a new object must be instantiated afresh. If the stream has not yet seen this value, null is returned.- Parameters:
oldInstance- The instance to be looked up.- Returns:
- The object, null if the object has not been seen before.
-
writeStatement
Writes statementoldStmto the stream. TheoldStmshould be written entirely in terms of the callers environment, i.e. the target and all arguments should be part of the object graph being written. These expressions represent a series of "what happened" expressions which tell the output stream how to produce an object graph like the original.The implementation of this method will produce a second expression to represent the same expression in an environment that will exist when the stream is read. This is achieved simply by calling
writeObjecton the target and all the arguments and building a new expression with the results.- Parameters:
oldStm- The expression to be written to the stream.
-
writeExpression
The implementation first checks to see if an expression with this value has already been written. If not, the expression is cloned, using the same procedure aswriteStatement, and the value of this expression is reconciled with the value of the cloned expression by callingwriteObject.- Parameters:
oldExp- The expression to be written to the stream.
-