Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.reflect.proxy
Class ReferenceHandler<T>

java.lang.Object
  extended by dk.rode.thesis.meta.reflect.proxy.ReferenceHandler<T>
Type Parameters:
T - The type of the referenced object.
All Implemented Interfaces:
Reference<T>

public class ReferenceHandler<T>
extends Object
implements Reference<T>

A reference handler is the standard implementation of the Reference interface.

Implementation notes:
The copy method cannot be a varargs method. The mutator methods are only matched on their name, regardless of declaring class.

There are potential issues with equals because the original class information is lost for proxies, i.e. the class becomes java.lang.reflect.Proxy.

Author:
Gunni Rode / rode.dk
See Also:
ReferenceInvocationHandler, ProxyFactory.getSharedObject(Object, String, Class[], String...)

Field Summary
private  Method copyMethod
          The copy method.
private  ProxyFactory factory
          The proxy factory used.
private  Class<?>[] interfaces
          All interfaces implemented by reference, including the adapted Reference interface at index zero.
private  Set<String> mutatorMethods
          The names of the mutator methods that will force a copy of reference before executed.
private  T reference
          The actual referenced object.
private  int referenceCount
          The number of references to reference.
 
Constructor Summary
  ReferenceHandler(ProxyFactory factory, T reference, String copyMethod, Class<?>[] parameterTypes, String... mutatorMethods)
          Constructor, which creates this reference to manage the referenced object supplied as reference.
  ReferenceHandler(ProxyFactory factory, T reference, String copyMethod, String... mutatorMethods)
          Constructor, which creates this reference to manage the referenced object supplied as reference.
private ReferenceHandler(ReferenceHandler<T> handler)
          Constructor, which creates this reference to manage a copy of the referenced object managed by handler.
 
Method Summary
 T addReference()
          Increases the reference count for the referenced object.
 Reference<T> copyIfMutator(Method method)
          Performs the actual copying of the referenced object if method is a mutator method and returns a new reference containing the copy with a reference count of one.
 boolean copyMethod(Method method)
          Returns true if the method supplied as method is the copy method used to copy the referenced object, false if not.
 T getReference()
          Returns the referenced object.
 T toProxy()
          Creates a new proxy instance using this reference handler.
 String toString()
          Returns the string representation of this handler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

copyMethod

private final Method copyMethod
The copy method.

Never null.


factory

private final ProxyFactory factory
The proxy factory used.

Never null.


interfaces

private final Class<?>[] interfaces
All interfaces implemented by reference, including the adapted Reference interface at index zero.

Never null.


mutatorMethods

private final Set<String> mutatorMethods
The names of the mutator methods that will force a copy of reference before executed.

The names do not include information about the declaring class!

Never null, and never empty.


reference

private T reference
The actual referenced object.

Never null.


referenceCount

private int referenceCount
The number of references to reference.

Constructor Detail

ReferenceHandler

public ReferenceHandler(ProxyFactory factory,
                        T reference,
                        String copyMethod,
                        Class<?>[] parameterTypes,
                        String... mutatorMethods)
                 throws Exception
Constructor, which creates this reference to manage the referenced object supplied as reference.

The reference count is set to 1.

Parameters:
factory - The proxy factory to create the proxies; cannot be null.
reference - The referenced object; cannot be null.
copyMethod - The name of the copy method to use; cannot be null. Must be declared in the class of reference, but will be made accessible if not already.
parameterTypes - The formal parameter types of copyMethod, if any, ignoring primitive types; can be null.
mutatorMethods - The names of the mutator methods that will force a copy of reference before executed; cannot be null or empty.
Throws:
NullPointerException - If factory, reference, or copyMethod are null.
IllegalArgumentException - If mutatorMethods is empty, or if the copy method is a vararg method.
Exception - If no such copy method exists.

ReferenceHandler

public ReferenceHandler(ProxyFactory factory,
                        T reference,
                        String copyMethod,
                        String... mutatorMethods)
                 throws Exception
Constructor, which creates this reference to manage the referenced object supplied as reference.

The reference count is set to 1.

Parameters:
factory - The proxy factory to create the proxies; cannot be null.
reference - The referenced object; cannot be null.
copyMethod - The name of the no-arg copy method to use; cannot be null. Must be declared in the class of reference, but will be made accessible if not already.
mutatorMethods - The names of the mutator methods that will force a copy of reference before executed; cannot be null or empty.
Throws:
NullPointerException - If either argument is null.
IllegalArgumentException - If mutatorMethods is empty.
Exception - If no such copy method exists.

ReferenceHandler

private ReferenceHandler(ReferenceHandler<T> handler)
                  throws Exception
Constructor, which creates this reference to manage a copy of the referenced object managed by handler.

The reference count is set to 1.

Parameters:
handler - The reference handler to copy; cannot be null.
Throws:
NullPointerException - If handler is null.
Exception - If the copying fails.
Method Detail

addReference

public T addReference()
Description copied from interface: Reference
Increases the reference count for the referenced object.

Specified by:
addReference in interface Reference<T>
Returns:
The referenced object; never null.

copyIfMutator

public Reference<T> copyIfMutator(Method method)
                           throws Exception
Description copied from interface: Reference
Performs the actual copying of the referenced object if method is a mutator method and returns a new reference containing the copy with a reference count of one. The reference count for this reference will at the same time be decreased by one.

If method is not a mutator method, this reference is simply returned.

Specified by:
copyIfMutator in interface Reference<T>
Parameters:
method - The method to test; cannot be null.
Returns:
The reference to use; never null.
Throws:
NullPointerException - If method is null.
Exception - If the copying fails.

copyMethod

public boolean copyMethod(Method method)
Description copied from interface: Reference
Returns true if the method supplied as method is the copy method used to copy the referenced object, false if not.

Specified by:
copyMethod in interface Reference<T>
Parameters:
method - The method to test; cannot be null.
Returns:
True if method is the method used to copy objects of the referenced object type, false if not.

getReference

public T getReference()
Description copied from interface: Reference
Returns the referenced object.

Specified by:
getReference in interface Reference<T>
Returns:
The referenced object; never null.

toProxy

public T toProxy()
Creates a new proxy instance using this reference handler.

This method does not alter the reference count of this reference.

Returns:
A new proxy instance; never null.

toString

public String toString()
Returns the string representation of this handler.

Overrides:
toString in class Object
Returns:
The string representation; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.