com.atlassian.core.test.util
Class DuckTypeProxy

java.lang.Object
  extended by com.atlassian.core.test.util.DuckTypeProxy

public class DuckTypeProxy
extends java.lang.Object

Utility for getting a proxy that delegates to a list of delegates in order. The delegates are queried whether they implement a particular method and if so, it is called and the result returned. If they don't, the next handler is queried and so forth. If none of the objects implement the interface then the UnimplementedMethodhandler is invoked. None of the delegates need to actually implement the proxied interface, only have a method with the same signature.

So for instance, given:

 interface MyInterface
 {
     String getString();

     Long getLong();

     Integer getInteger();
 }
 
you can create a proxy for this interface without needing to implement the whole thing:
      Object mocker = new Object()
      {
          public String myMethod()
          {
              return "proxied";
          }
      }
      MyInterface mock = (MyInterface) DuckTypeProxy.getProxy(MyInterface.class, mocker);
      System.out.println(mock.getString());
 
prints "proxied" to the console.

There are facilities for handling unimplemented methods either by returning null or throwing exceptions by default.


Nested Class Summary
static interface DuckTypeProxy.UnimplementedMethodHandler
           
 
Field Summary
static DuckTypeProxy.UnimplementedMethodHandler RETURN_NULL
          Return null if the method cannot be found.
static DuckTypeProxy.UnimplementedMethodHandler THROW
          Throw an exception if the method cannot be found.
 
Constructor Summary
DuckTypeProxy()
           
 
Method Summary
static java.lang.Object getProxy(java.lang.Class[] implementingClasses, java.util.List delegates)
          Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature.
static java.lang.Object getProxy(java.lang.Class[] implementingClasses, java.util.List delegates, DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)
           
static java.lang.Object getProxy(java.lang.Class implementingClass, java.util.List delegates)
          Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature.
static java.lang.Object getProxy(java.lang.Class implementingClass, java.util.List delegates, DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)
          Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature.
static java.lang.Object getProxy(java.lang.Class implementingClass, java.lang.Object delegate)
          Get a Proxy that checks the enclosed object and calls it if it has a Method of the same signature.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RETURN_NULL

public static DuckTypeProxy.UnimplementedMethodHandler RETURN_NULL
Return null if the method cannot be found.


THROW

public static DuckTypeProxy.UnimplementedMethodHandler THROW
Throw an exception if the method cannot be found.

Constructor Detail

DuckTypeProxy

public DuckTypeProxy()
Method Detail

getProxy

public static java.lang.Object getProxy(java.lang.Class implementingClass,
                                        java.util.List delegates)
Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature. By default this will throw an UnsupportedOperationException if the method is not implemented


getProxy

public static java.lang.Object getProxy(java.lang.Class implementingClass,
                                        java.util.List delegates,
                                        DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)
Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature.


getProxy

public static java.lang.Object getProxy(java.lang.Class[] implementingClasses,
                                        java.util.List delegates)
Get a Proxy that checks each of the enclosed objects and calls them if they have a Method of the same signature. Uses the THROW DuckTypeProxy.UnimplementedMethodHandler


getProxy

public static java.lang.Object getProxy(java.lang.Class[] implementingClasses,
                                        java.util.List delegates,
                                        DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)

getProxy

public static java.lang.Object getProxy(java.lang.Class implementingClass,
                                        java.lang.Object delegate)
Get a Proxy that checks the enclosed object and calls it if it has a Method of the same signature. By default this will throw an UnsupportedOperationException if the method is not implemented



Copyright © 2009 Atlassian Pty Ltd. All Rights Reserved.