|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.atlassian.core.test.util.DuckTypeProxy
public class DuckTypeProxy
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 |
|---|
public static DuckTypeProxy.UnimplementedMethodHandler RETURN_NULL
public static DuckTypeProxy.UnimplementedMethodHandler THROW
| Constructor Detail |
|---|
public DuckTypeProxy()
| Method Detail |
|---|
public static java.lang.Object getProxy(java.lang.Class implementingClass,
java.util.List delegates)
throw an UnsupportedOperationException if the method is not implemented
public static java.lang.Object getProxy(java.lang.Class implementingClass,
java.util.List delegates,
DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)
public static java.lang.Object getProxy(java.lang.Class[] implementingClasses,
java.util.List delegates)
THROW DuckTypeProxy.UnimplementedMethodHandler
public static java.lang.Object getProxy(java.lang.Class[] implementingClasses,
java.util.List delegates,
DuckTypeProxy.UnimplementedMethodHandler unimplementedMethodHandler)
public static java.lang.Object getProxy(java.lang.Class implementingClass,
java.lang.Object delegate)
throw an UnsupportedOperationException if the method is not implemented
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||