Class Reflection


  • public class Reflection
    extends Object
    Utilities for working with Java reflection.
    • Constructor Detail

      • Reflection

        public Reflection()
    • Method Detail

      • defaultInvoker

        public static MethodHandle defaultInvoker​(Method method)
        Returns a method handle that invoke a default method of an interface.
        Parameters:
        method - the (default) method to get a method handle for.
        Returns:
        a method handle that invokes the specific default method.
      • invoke

        public static Object invoke​(MethodHandle method,
                                    Object target)
        Invoke the given method handle with the specified single argument. Handles the exceptions declared by MethodHandle allowing this method to be used in a context where no checked exceptions may be thrown.
        Parameters:
        method - the method handle to invoke.
        target - the argument to pass to the method.
        Returns:
        the result of invoking the method.
      • lambdaClass

        public static Class<?> lambdaClass​(Serializable lambda)
        Get the class where the method of a (serializable) lambda is implemented.
        Parameters:
        lambda - the (serializable) lambda to get the implementing class of.
        Returns:
        the class where the (serializable) lambda is implemented.
      • pathOf

        public static String pathOf​(Class<?> cls)
        Get the location in the classpath from where the given class is loaded.
        Parameters:
        cls - the class to find the classpath location of.
        Returns:
        the classpath location of the given class.
      • lambdaImplMethodName

        public static String lambdaImplMethodName​(Serializable lambda)
        Get the name of the method used to implement a (serializable) lambda. This is mostly useful for lambdas that are implemented by a method reference.
        Parameters:
        lambda - the lambda to get the implementing method name of.
        Returns:
        the name of the method that implements the given (serializable) lambda.
      • methodHandle

        public static MethodHandle methodHandle​(MethodHandles.Lookup lookup,
                                                Serializable lambda)
        Get a method handle to the underlying method that implements a given (serializable) lambda.
        Parameters:
        lookup - a context that has access to looking up the target method.
        lambda - the (serializable) lambda to find the implementing method of.
        Returns:
        a method handle to the implementing method of the given (serializable) lambda.
      • implementation

        public static <T> Method implementation​(T instance,
                                                Class<T> base,
                                                String methodName)
        Find that method of a given instance that implements the specified abstract method of a given base class.

        This is useful for finding the actual type that implements a type parameter of the base class.

        Type Parameters:
        T - the base type of the instance (the type of the base class).
        Parameters:
        instance - the instance to find the implementation method of.
        base - the base class that declares the abstract method in question.
        methodName - the name of the abstract method.
        Returns:
        the method that implements the abstract method of the base class.