Class ReflectiveCall<T>

  • Type Parameters:
    T - - the return type of the method being called

    public class ReflectiveCall<T>
    extends java.lang.Object
    Everything 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
      T call​(java.lang.Object... args)
      Invoke the given method with the given args.
      void dispatchException​(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 ReflectiveCall
      java.lang.String getClassName()
      Get the class name specified in this ReflectiveCall
      java.lang.String getMethodName()
      Get the name of the method specified in this ReflectiveCall
      java.lang.Class getReturnType()
      Get the class return type of the method specified in this ReflectiveCall
      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.
      static boolean isAgentPresent()
      Test if a DiSCo agent is present
      boolean methodFound()
      Test if the method about to be called exists or not
      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.
      ReflectiveCall<T> ofMethod​(java.lang.String methodName)
      Set the name of the method to be called
      ReflectiveCall<T> onInstance​(java.lang.Object thiz)
      For non-static methods, set the instance object on which to invoke the method
      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
      static ReflectiveCall returningVoid()
      Factory method to create a new ReflectiveCall which is on a void method
      java.lang.String toString()
      ReflectiveCall<T> withArgTypes​(java.lang.Class... argTypes)
      Declare the expected method signature by its arguments
      ReflectiveCall<T> withDefaultValue​(T defaultValue)
      Set the default value to return in case no DiSCo agent is present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 UncaughtExceptionHandler
        args - any arguments to notify the UncaughtExceptionHandler of
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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