Class ReflectiveCall<T>
- java.lang.Object
-
- software.amazon.disco.agent.reflect.ReflectiveCall<T>
-
- Type Parameters:
T- - the return type of the method being called
public class ReflectiveCall<T> extends java.lang.ObjectEverything in disco.agent.reflect basically works the same way - attempting to reflectively invoke methods, usually static ones, that reside within the Agent. Any failures are degraded gracefully to no-ops in the case that the Agent is not loaded, making it safe for clients to use the reflect library in their Prod code.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tcall(java.lang.Object... args)Invoke the given method with the given args.voiddispatchException(java.lang.Throwable t, java.lang.Object... args)Tries to dispatch an exception to the installed UncaughtExceptionHandler.java.lang.Class[]getArgTypes()Get the array of argument types specified in this ReflectiveCalljava.lang.StringgetClassName()Get the class name specified in this ReflectiveCalljava.lang.StringgetMethodName()Get the name of the method specified in this ReflectiveCalljava.lang.ClassgetReturnType()Get the class return type of the method specified in this ReflectiveCallstatic voidinstallUncaughtExceptionHandler(UncaughtExceptionHandler handler)Install an UncaughtExceptionHandler, to receive a notification whe any exception is thrown by any method in the DiSCo reflect APIs.static booleanisAgentPresent()Test if a DiSCo agent is presentbooleanmethodFound()Test if the method about to be called exists or notReflectiveCall<T>ofClass(java.lang.String className)Set the partial class name which declares the method to be called as it appears after software.amazon.disco.agent e.g.ReflectiveCall<T>ofMethod(java.lang.String methodName)Set the name of the method to be calledReflectiveCall<T>onInstance(java.lang.Object thiz)For non-static methods, set the instance object on which to invoke the methodstatic <T> ReflectiveCall<T>returning(java.lang.Class<T> clazz)Factory method to create a new ReflectiveCall which is on a method returning the given typestatic ReflectiveCallreturningVoid()Factory method to create a new ReflectiveCall which is on a void methodjava.lang.StringtoString()ReflectiveCall<T>withArgTypes(java.lang.Class... argTypes)Declare the expected method signature by its argumentsReflectiveCall<T>withDefaultValue(T defaultValue)Set the default value to return in case no DiSCo agent is present.
-
-
-
Method Detail
-
returningVoid
public static ReflectiveCall returningVoid()
Factory method to create a new ReflectiveCall which is on a void method- Returns:
- a new ReflectiveCall to continue building/calling
-
returning
public static <T> ReflectiveCall<T> returning(java.lang.Class<T> clazz)
Factory method to create a new ReflectiveCall which is on a method returning the given type- Type Parameters:
T- the class of the method's return type- Parameters:
clazz- the class of the method's return type- Returns:
- a new ReflectiveCall to continue building/calling
-
withDefaultValue
public ReflectiveCall<T> withDefaultValue(T defaultValue)
Set the default value to return in case no DiSCo agent is present. If none is provided, then null it returns null.- Parameters:
defaultValue- The default value- Returns:
- the ReflectiveCall to continue building/calling
-
ofClass
public ReflectiveCall<T> ofClass(java.lang.String className)
Set the partial class name which declares the method to be called as it appears after software.amazon.disco.agent e.g. '.config.Config' including the prefixing '.'- Parameters:
className- the class name which declares the method- Returns:
- the ReflectiveCall to continue building/calling
-
ofMethod
public ReflectiveCall<T> ofMethod(java.lang.String methodName)
Set the name of the method to be called- Parameters:
methodName- the name of the method to be called- Returns:
- the ReflectiveCall to continue building/calling
-
onInstance
public ReflectiveCall<T> onInstance(java.lang.Object thiz)
For non-static methods, set the instance object on which to invoke the method- Parameters:
thiz- the instance object on which to invoke the method- Returns:
- the ReflectiveCall to continue building/calling
-
withArgTypes
public ReflectiveCall<T> withArgTypes(java.lang.Class... argTypes)
Declare the expected method signature by its arguments- Parameters:
argTypes- array of types for method lookup- Returns:
- the ReflectiveCall to continue building/calling
-
methodFound
public boolean methodFound()
Test if the method about to be called exists or not- Returns:
- true if the method configured exists
-
call
public T call(java.lang.Object... args)
Invoke the given method with the given args. If a DiSCo agent is present the call will proceed. In the case that building the method was not possible, due to the agent being absent, this method will have no side effects.- Parameters:
args- any arguments to pass to the method invocation.- Returns:
- any value which the method returns, or null if the call could not be made
-
isAgentPresent
public static boolean isAgentPresent()
Test if a DiSCo agent is present- Returns:
- true if a DiSCo agent is present
-
installUncaughtExceptionHandler
public static void installUncaughtExceptionHandler(UncaughtExceptionHandler handler)
Install an UncaughtExceptionHandler, to receive a notification whe any exception is thrown by any method in the DiSCo reflect APIs. Without such a handler installed, all exceptions - even unchecked ones - will be suppressed- Parameters:
handler- a callback to receive any unhandled exceptions in client code
-
dispatchException
public void dispatchException(java.lang.Throwable t, java.lang.Object... args)Tries to dispatch an exception to the installed UncaughtExceptionHandler. Does nothing if no such handler is installed- Parameters:
t- the throwable to notify the calling code, via any installed UncaughtExceptionHandlerargs- any arguments to notify the UncaughtExceptionHandler of
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getClassName
public java.lang.String getClassName()
Get the class name specified in this ReflectiveCall- Returns:
- the class name
-
getReturnType
public java.lang.Class getReturnType()
Get the class return type of the method specified in this ReflectiveCall- Returns:
- the return type of this method
-
getMethodName
public java.lang.String getMethodName()
Get the name of the method specified in this ReflectiveCall- Returns:
- the name of this method
-
getArgTypes
public java.lang.Class[] getArgTypes()
Get the array of argument types specified in this ReflectiveCall- Returns:
- the argument types of this method
-
-