public class StaticProxy
extends java.lang.Object
implements java.lang.reflect.InvocationHandler
To obtain a static proxy use
, specify the
interface and a list of classes from where the static methods will be
collected and mapped. If for an interface method an static method
equivalent can not be found, then the proxy creation will fail with a
RuntimeException
.
All proxies are saved for the life-time of the application. If you need to
remove a static proxy, use remove(Class)
. Else, the static proxy
will be built on the first obtain(Class, Class[])
call, and any
subsequent calls will return the cached proxy instance for that interface
(from the staticProxies
map).
Modifier and Type | Class and Description |
---|---|
static interface |
StaticProxy.Ai |
static class |
StaticProxy.Bi |
Modifier and Type | Field and Description |
---|---|
private java.lang.Class<?> |
ifc
The interface for this static proxy.
|
private java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> |
im2sm
Map of interface methods to static equivalent.
|
private static java.util.Map<java.lang.Class<?>,java.lang.Object> |
staticProxies
Cache of created static proxyies, by interface.
|
Modifier | Constructor and Description |
---|---|
private |
StaticProxy(java.lang.Class<?> ifc,
java.lang.Class<?>[] staticClasses)
Create and initialize a new static proxy for the given interface and
implementation classes.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
getSignature(java.lang.reflect.Method m)
Get a string representation of the given method, including the return
type, the method name and the type for all parameters.
|
static java.util.List<java.lang.reflect.Method> |
getStaticMethods(java.lang.Object instance)
Get the static methods implementing the static proxy represented by the given instance.
|
private void |
init(java.lang.Class<?>[] staticClasses)
Initialize this static proxy.
|
java.lang.Object |
invoke(java.lang.Object proxy,
java.lang.reflect.Method method,
java.lang.Object[] args)
Invokes the specified method by determining its associated static
implementation from the
im2sm map. |
static boolean |
isStaticProxy(java.lang.Object instance)
Check if the given instance is a static proxy.
|
static void |
main(java.lang.String[] args) |
static <T> T |
obtain(java.lang.Class<T> ifc)
Get the static proxy instance for the given interface.
|
static <T> T |
obtain(java.lang.Class<T> ifc,
java.lang.Class<?>[] staticClasses)
Get the static proxy instance for the given interface, if it was already
created by a previous call.
|
static void |
remove(java.lang.Class ifc)
Remove the static proxy for the given interface, if it exists.
|
private static java.util.Map<java.lang.Class<?>,java.lang.Object> staticProxies
private final java.util.Map<java.lang.reflect.Method,java.lang.reflect.Method> im2sm
private final java.lang.Class<?> ifc
private StaticProxy(java.lang.Class<?> ifc, java.lang.Class<?>[] staticClasses)
If not all interface methods are implemented by the specified classes,
a RuntimeException
is thrown.
ifc
- The proxy interface.staticClasses
- The classes which statically implement the proxy's methods.public static void remove(java.lang.Class ifc)
ifc
- The interface for which its static proxy needs to be removed.public static boolean isStaticProxy(java.lang.Object instance)
staticProxies
must have an entry for the implemented interface.instance
- The instance to check.true
if the instance is a static proxy.public static java.util.List<java.lang.reflect.Method> getStaticMethods(java.lang.Object instance)
instance
- The proxy instance for which the static methods are needed.public static <T> T obtain(java.lang.Class<T> ifc)
null
ifc
- The interface for which the proxy is needed.public static <T> T obtain(java.lang.Class<T> ifc, java.lang.Class<?>[] staticClasses)
ifc
- The proxy interface.staticClasses
- The classes which statically implement the proxy's methods.public java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args) throws java.lang.Throwable
im2sm
map. If the method is
Object.equals(Object)
, then the proxy reference is compared to
the references from args[0]
; if the method is
Object.hashCode()
, then the ifc
's hashcode is returned.
if The method is not found in the im2sm
map, a
RuntimeException
is generated.invoke
in interface java.lang.reflect.InvocationHandler
proxy
- The proxy instance that the method was invoked onmethod
- The Method
instance corresponding to
the interface method invoked on the proxy instance.args
- An array of objects containing the values of the arguments
passed in the method invocation on the proxy instance,
or null
if interface method takes no arguments.java.lang.Throwable
- If any exceptions are encounted during the static call.private void init(java.lang.Class<?>[] staticClasses)
RuntimeException
is thrown.staticClasses
- The classes which statically implement the proxy's methods.private java.lang.String getSignature(java.lang.reflect.Method m)
m
- The method for which the signature is neede.public static void main(java.lang.String[] args)