@Beta public abstract class AbstractInvocationHandler extends Object implements InvocationHandler
InvocationHandler
that handles Object.equals(java.lang.Object)
, Object.hashCode()
and Object.toString()
. For example:
class Unsupported extends AbstractInvocationHandler { protected Object handleInvocation(Object proxy, Method method, Object[] args) { throw new UnsupportedOperationException(); } } CharSequence unsupported = Reflection.newProxy(CharSequence.class, new Unsupported());
Constructor and Description |
---|
AbstractInvocationHandler() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
By default delegates to
Object.equals(java.lang.Object) so instances are only equal if they are
identical. |
protected abstract Object |
handleInvocation(Object proxy,
Method method,
Object[] args)
invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]) delegates to this method upon any method invocation on the proxy instance,
except Object.equals(java.lang.Object) , Object.hashCode() and Object.toString() . |
int |
hashCode()
By default delegates to
Object.hashCode() . |
Object |
invoke(Object proxy,
Method method,
Object[] args)
Processes a method invocation on a proxy instance and returns
the result.
|
String |
toString()
By default delegates to
Object.toString() . |
public AbstractInvocationHandler()
public final Object invoke(Object proxy, Method method, @NullableDecl Object[] args) throws Throwable
proxy.hashCode()
delegates to hashCode()
proxy.toString()
delegates to toString()
proxy.equals(argument)
returns true if:
proxy
and argument
are of the same type
equals(java.lang.Object)
returns true for the InvocationHandler
of argument
handleInvocation(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
.
invoke
in interface InvocationHandler
proxy
- the proxy instance that the method was invoked onmethod
- the Method
instance corresponding to
the interface method invoked on the proxy instance. The declaring
class of the Method
object will be the interface that
the method was declared in, which may be a superinterface of the
proxy interface that the proxy class inherits the method through.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.
Arguments of primitive types are wrapped in instances of the
appropriate primitive wrapper class, such as
java.lang.Integer
or java.lang.Boolean
.null
and the interface method's return type is
primitive, then a NullPointerException
will be
thrown by the method invocation on the proxy instance. If the
value returned by this method is otherwise not compatible with
the interface method's declared return type as described above,
a ClassCastException
will be thrown by the method
invocation on the proxy instance.Throwable
- the exception to throw from the method
invocation on the proxy instance. The exception's type must be
assignable either to any of the exception types declared in the
throws
clause of the interface method or to the
unchecked exception types java.lang.RuntimeException
or java.lang.Error
. If a checked exception is
thrown by this method that is not assignable to any of the
exception types declared in the throws
clause of
the interface method, then an
UndeclaredThrowableException
containing the
exception that was thrown by this method will be thrown by the
method invocation on the proxy instance.UndeclaredThrowableException
protected abstract Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable
invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
delegates to this method upon any method invocation on the proxy instance,
except Object.equals(java.lang.Object)
, Object.hashCode()
and Object.toString()
. The result
will be returned as the proxied method's return value.
Unlike invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
, args
will never be null. When the method has no parameter,
an empty array is passed in.
Throwable
public boolean equals(Object obj)
Object.equals(java.lang.Object)
so instances are only equal if they are
identical. proxy.equals(argument)
returns true if:
proxy
and argument
are of the same type
InvocationHandler
of argument
Subclasses can override this method to provide custom equality.
equals
in class Object
obj
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
Object.hashCode()
. The dynamic proxies' hashCode()
will
delegate to this method. Subclasses can override this method to provide custom equality.hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
Object.toString()
. The dynamic proxies' toString()
will
delegate to this method. Subclasses can override this method to provide custom string
representation for the proxies.Copyright © 2010–2017. All rights reserved.