APIdia Launch Partner
Top Description Fields Constructors Methods
java.beans

public Class Encoder

extends Object
Class Inheritance
Known Direct Subclasses
java.beans.XMLEncoder
Imports
com.sun.beans.finder.PersistenceDelegateFinder, java.util.HashMap, .IdentityHashMap, .Map

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.
Author
Philip Milne
Since
1.4

Field Summary

Modifier and TypeField and Description
private Map<Object, Object>
private Map<Object, Expression>
private ExceptionListener
pack-priv boolean
private final PersistenceDelegateFinder

Constructor Summary

AccessConstructor and Description
public
Encoder()

Constructs an Encoder.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
private Statement
public Object

Returns:

The object, null if the object has not been seen before.
get
(Object
The instance to be looked up.
oldInstance
)

Returns a tentative value for oldInstance in the environment created by this stream.

pack-priv Object
public ExceptionListener

Returns:

The exception handler for this stream; Will return the default exception listener if this has not explicitly been set.
getExceptionListener
()

Gets the exception handler for this stream.

public PersistenceDelegate

Returns:

the persistence delegate for the given type
getPersistenceDelegate
(Class<?>
the class of the objects
type
)

Returns the persistence delegate for the given type.

pack-priv Object
public Object

Returns:

The entry that was removed.
remove
(Object
The entry that should be removed.
oldInstance
)

Removes the entry for this instance, returning the old entry.

pack-priv void
public void
setExceptionListener(ExceptionListener
The exception handler for this stream; if null the default exception listener will be used.
exceptionListener
)

Sets the exception handler for this stream to exceptionListener.

public void
setPersistenceDelegate(Class<?>
the class of objects that the specified persistence delegate applies to
type
,
PersistenceDelegate
the persistence delegate for instances of the given type
delegate
)

Associates the specified persistence delegate with the given type.

public void
writeExpression(Expression
The expression to be written to the stream.
oldExp
)

The implementation first checks to see if an expression with this value has already been written.

protected void
writeObject(Object
The object to be written to the stream.
o
)

Write the specified object to the output stream.

private Object
writeObject1(Object oldInstance)

public void
writeStatement(Statement
The expression to be written to the stream.
oldStm
)

Writes statement oldStm to the stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributesback to summary
private Map<Object, Object> attributes
bindingsback to summary
private Map<Object, Expression> bindings
exceptionListenerback to summary
private ExceptionListener exceptionListener
executeStatementsback to summary
pack-priv boolean executeStatements
finderback to summary
private final PersistenceDelegateFinder finder

Constructor Detail

Encoderback to summary
public Encoder()

Constructs an Encoder.

Method Detail

clearback to summary
pack-priv void clear()
cloneStatementback to summary
private Statement cloneStatement(Statement oldExp)
getback to summary
public Object get(Object oldInstance)

Returns a tentative value for oldInstance in the environment created by this stream. A persistence delegate can use its mutatesTo method 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:Object

The instance to be looked up.

Returns:Object

The object, null if the object has not been seen before.

getAttributeback to summary
pack-priv Object getAttribute(Object key)
getExceptionListenerback to summary
public ExceptionListener getExceptionListener()

Gets the exception handler for this stream.

Returns:ExceptionListener

The exception handler for this stream; Will return the default exception listener if this has not explicitly been set.

See Also
setExceptionListener
getPersistenceDelegateback to summary
public PersistenceDelegate getPersistenceDelegate(Class<?> type)

Returns the persistence delegate for the given type. The persistence delegate is calculated by applying the following rules in order:

  1. If a persistence delegate is associated with the given type by using the setPersistenceDelegate method it is returned.
  2. 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 Bean class should be named BeanPersistenceDelegate and located in the same package.
    public class Bean { ... }
    public class BeanPersistenceDelegate { ... }
    The instance of the BeanPersistenceDelegate class is returned for the Bean class.
  3. If the type is null, a shared internal persistence delegate is returned that encodes null value.
  4. If the type is an enum declaration, a shared internal persistence delegate is returned that encodes constants of this enumeration by their names.
  5. 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.
  6. 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.
  7. If the type is a proxy, a shared internal persistence delegate is returned that encodes a proxy instance by using the java.lang.reflect.Proxy#newProxyInstance method.
  8. If the BeanInfo for this type has a BeanDescriptor which defined a "persistenceDelegate" attribute, the value of this named attribute is returned.
  9. 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 Introspector class. The default constructor is the constructor with the greatest number of parameters that has the ConstructorProperties annotation. If none of the constructors has the ConstructorProperties annotation, then the nullary constructor (constructor with no parameters) will be used. For example, in the following code fragment, the nullary constructor for the Foo class will be used, while the two-parameter constructor for the Bar class 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:Class<?>

the class of the objects

Returns:PersistenceDelegate

the persistence delegate for the given type

See Also
setPersistenceDelegate, java.beans.Introspector#getBeanInfo, java.beans.BeanInfo#getBeanDescriptor
getValueback to summary
pack-priv Object getValue(Expression exp)
removeback to summary
public Object remove(Object oldInstance)

Removes the entry for this instance, returning the old entry.

Parameters
oldInstance:Object

The entry that should be removed.

Returns:Object

The entry that was removed.

See Also
get
setAttributeback to summary
pack-priv void setAttribute(Object key, Object value)
setExceptionListenerback to summary
public void setExceptionListener(ExceptionListener exceptionListener)

Sets the exception handler for this stream to exceptionListener. The exception handler is notified when this stream catches recoverable exceptions.

Parameters
exceptionListener:ExceptionListener

The exception handler for this stream; if null the default exception listener will be used.

See Also
getExceptionListener
setPersistenceDelegateback to summary
public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate)

Associates the specified persistence delegate with the given type.

Parameters
type:Class<?>

the class of objects that the specified persistence delegate applies to

delegate:PersistenceDelegate

the persistence delegate for instances of the given type

See Also
getPersistenceDelegate, java.beans.Introspector#getBeanInfo, java.beans.BeanInfo#getBeanDescriptor
writeExpressionback to summary
public void writeExpression(Expression oldExp)

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 as writeStatement, and the value of this expression is reconciled with the value of the cloned expression by calling writeObject.

Parameters
oldExp:Expression

The expression to be written to the stream.

writeObjectback to summary
protected void writeObject(Object o)

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:Object

The object to be written to the stream.

See Also
XMLDecoder#readObject
writeObject1back to summary
private Object writeObject1(Object oldInstance)
writeStatementback to summary
public void writeStatement(Statement oldStm)

Writes statement oldStm to the stream. The oldStm should 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 writeObject on the target and all the arguments and building a new expression with the results.

Parameters
oldStm:Statement

The expression to be written to the stream.