Class ReflectionUtilities


  • public class ReflectionUtilities
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Throwable createFilteredThrowable​(java.lang.String... patterns)
      A convenience method to create a throwable, filtering any lines that contain the given non-regex patterns.
      static java.lang.Throwable createJavaFilteredThrowable()
      A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).
      static java.lang.String createJavaFilteredThrowableString()
      A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).
      static java.lang.String createStackTraceForAllThreads()
      Returns a string which is a printout of a stack trace for each thread running in the current JVM
      static java.lang.Throwable createThrowableWithStackOlderThan​(java.lang.Class<?>... classes)
      Creates a throwable whose stack trace is based upon the current call stack, with any information coming before, and including, the given classes removed.
      static java.lang.Throwable filterJavaThrowable​(java.lang.Throwable t)
      A convenience method to take a throwable, filter boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).
      static java.lang.StackTraceElement[] filterStackTrace​(java.lang.StackTraceElement[] trace, java.lang.String... patterns)
      Uses the given patterns to remove elements from the given stack trace.
      static java.util.LinkedHashSet<java.lang.Class<?>> getAllParents​(java.lang.Class<?> c)
      Returns an order set of all interfaces implemented and classes extended for the entire type structure of the given class.
      static java.lang.String getClassNameOlderThan​(java.lang.Class<?>... classes)
      Returns the class name of the entry in the stack that comes before all references to the given classes.
      static java.util.LinkedHashSet<java.lang.Class<?>> getSharedHierarchy​(java.util.List<?> list)
      Returns an ordered set of interfaces and classes that are shared amongst the items in the list.
      static java.util.LinkedHashSet<java.lang.Class<?>> getSharedParents​(java.util.List<?> list)
      Returns an ordered set of parent interfaces and classes that are shared amongst the items in the list.
      static <T> java.util.List<java.lang.Class<?>> getTypeArguments​(java.lang.Class<T> baseClass, java.lang.Class<? extends T> childClass)  
      static java.lang.reflect.Constructor<?> locateConstructorOnClass​(java.lang.Class<?> containingClass, java.lang.Class<?>[] parameterTypes)  
      static java.lang.reflect.Field locateFieldByTypeOnClass​(java.lang.Class<?> classType, java.lang.Class<?> containingClass)
      Get the first field specification contained within containingClass which has the type classType.
      static java.lang.reflect.Field locateFieldObjectOnClass​(java.lang.String fieldName, java.lang.Class<?> containingClass)
      Locates the field of the name fieldName on the given class.
      static java.lang.reflect.Method locateMethodObjectOnClass​(java.lang.String methodName, java.lang.Class<?> containingClass, java.lang.Class<?>[] parameterTypes)
      Locates the method of the name methodName on the given class.
      static java.lang.reflect.Field locateStaticFieldObjectOnClass​(java.lang.String fieldName, java.lang.Class<?> containingClass)
      Locates the field of the name fieldName on the given class.
      static java.lang.StackTraceElement[] movePastStackTracePattern​(java.lang.StackTraceElement[] trace, java.lang.String pattern)
      Finds the first occurrence of the given pattern and then stops filtering when it finds something that is not that pattern
      static java.lang.String stackTraceToString​(java.lang.Throwable t)
      Turns the given Throwable into a String version of its Throwable.printStackTrace() method.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • locateStaticFieldObjectOnClass

        public static java.lang.reflect.Field locateStaticFieldObjectOnClass​(java.lang.String fieldName,
                                                                             java.lang.Class<?> containingClass)
        Locates the field of the name fieldName on the given class. If the given class does not contain the field, then this method will recursively call up containingClass's implementation tree looking for a parent implementation of the requested field.
        Parameters:
        fieldName - The name of the field to locate.
        containingClass - The class that contains the desired field.
        Returns:
        The Field object that matches the given name, or null if not suitable field could be found.
      • locateFieldObjectOnClass

        public static java.lang.reflect.Field locateFieldObjectOnClass​(java.lang.String fieldName,
                                                                       java.lang.Class<?> containingClass)
        Locates the field of the name fieldName on the given class. If the given class does not contain the field, then this method will recursively call up containingClass's implementation tree looking for a parent implementation of the requested field.
        Parameters:
        fieldName - The name of the field to locate.
        containingClass - The class that contains the desired field.
        Returns:
        The Field object that matches the given name, or null if not suitable field could be found.
      • locateMethodObjectOnClass

        public static java.lang.reflect.Method locateMethodObjectOnClass​(java.lang.String methodName,
                                                                         java.lang.Class<?> containingClass,
                                                                         java.lang.Class<?>[] parameterTypes)
        Locates the method of the name methodName on the given class. If the given class does not contain the method, then this method will recursively call up containingClass's implementation tree looking for a parent implementation of the requested method.
        Parameters:
        methodName - The name of the method to locate.
        containingClass - The class that contains the desired method.
        parameterTypes - The parameters of the desired method (may be null).
        Returns:
        The Method object that matches the given name, or null if not suitable method could be found.
      • locateConstructorOnClass

        public static java.lang.reflect.Constructor<?> locateConstructorOnClass​(java.lang.Class<?> containingClass,
                                                                                java.lang.Class<?>[] parameterTypes)
      • locateFieldByTypeOnClass

        public static java.lang.reflect.Field locateFieldByTypeOnClass​(java.lang.Class<?> classType,
                                                                       java.lang.Class<?> containingClass)
        Get the first field specification contained within containingClass which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the containingClass hierarchy.
        Parameters:
        classType - the class
        containingClass - the class that contains a field of the given type
        Returns:
        field which corresponds to type classType or null
      • getClassNameOlderThan

        public static java.lang.String getClassNameOlderThan​(java.lang.Class<?>... classes)
        Returns the class name of the entry in the stack that comes before all references to the given classes. This is useful for figuring out at runtime who is calling a particular method.

        This method can take multiple classes, but you really only need to pass the oldest class of disinterest.

        Parameters:
        classes - the classes to ignore
        Returns:
        the desired class name
      • createThrowableWithStackOlderThan

        public static java.lang.Throwable createThrowableWithStackOlderThan​(java.lang.Class<?>... classes)
        Creates a throwable whose stack trace is based upon the current call stack, with any information coming before, and including, the given classes removed.

        This method can take multiple classes, but you really only need to pass the oldest class of disinterest.

        Parameters:
        classes - the classes to ignore
        Returns:
        the new throwable
      • movePastStackTracePattern

        public static java.lang.StackTraceElement[] movePastStackTracePattern​(java.lang.StackTraceElement[] trace,
                                                                              java.lang.String pattern)
        Finds the first occurrence of the given pattern and then stops filtering when it finds something that is not that pattern
        Parameters:
        trace - the trace to update
        pattern - the non-regex patterns used to perform a String.contains(CharSequence) on each StackTraceElement line
        Returns:
        the updated trace
      • filterStackTrace

        public static java.lang.StackTraceElement[] filterStackTrace​(java.lang.StackTraceElement[] trace,
                                                                     java.lang.String... patterns)
        Uses the given patterns to remove elements from the given stack trace. The current implementation will simply perform a toString() on each element and then check to see if that string contains any of the patterns.
        Parameters:
        trace - the trace to filter
        patterns - the non-regex patterns used to perform a String.contains(CharSequence) on each StackTraceElement line.
        Returns:
        the filtered trace
      • createFilteredThrowable

        public static java.lang.Throwable createFilteredThrowable​(java.lang.String... patterns)
        A convenience method to create a throwable, filtering any lines that contain the given non-regex patterns. This can be useful for emitting diagnostic stack traces.
        Parameters:
        patterns - the non-regex patterns used to perform a String.contains(CharSequence) on each StackTraceElement line.
        Returns:
        the new throwable
      • createJavaFilteredThrowable

        public static java.lang.Throwable createJavaFilteredThrowable()
        A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc). This can be useful for emitting diagnostic stack traces with reduced noise.
        Returns:
        the new throwable
      • createJavaFilteredThrowableString

        public static java.lang.String createJavaFilteredThrowableString()
        A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc). This can be useful for emitting diagnostic stack traces with reduced noise.

        This method differs from createJavaFilteredThrowable() in that this method returns a String, which is useful when printing log messages without having to directly print the stack trace.

        Returns:
        the new throwable
      • filterJavaThrowable

        public static java.lang.Throwable filterJavaThrowable​(java.lang.Throwable t)
        A convenience method to take a throwable, filter boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc). This can be useful for emitting diagnostic stack traces with reduced noise.
        Parameters:
        t - the throwable to filter
        Returns:
        the throwable
      • createStackTraceForAllThreads

        public static java.lang.String createStackTraceForAllThreads()
        Returns a string which is a printout of a stack trace for each thread running in the current JVM
        Returns:
        the stack trace string
      • getSharedHierarchy

        public static java.util.LinkedHashSet<java.lang.Class<?>> getSharedHierarchy​(java.util.List<?> list)
        Returns an ordered set of interfaces and classes that are shared amongst the items in the list.

        The order of the items is as they are first encountered, favoring interfaces before classes. Further, interface hierarchies are examined before concrete parent extensions.

        If the given items have no parents in common, then the result will be a list with only Object.class.

        Parameters:
        list - the items to examine
        Returns:
        the set of items
      • getSharedParents

        public static java.util.LinkedHashSet<java.lang.Class<?>> getSharedParents​(java.util.List<?> list)
        Returns an ordered set of parent interfaces and classes that are shared amongst the items in the list.

        The order of the items is as they are first encountered, favoring interfaces before classes. Further, interface hierarchies are examined before concrete parent extensions.

        If the given items have no parents in common, then the result will be a list with only Object.class.

        Parameters:
        list - the items to examine
        Returns:
        the set of items
      • stackTraceToString

        public static java.lang.String stackTraceToString​(java.lang.Throwable t)
        Turns the given Throwable into a String version of its Throwable.printStackTrace() method.
        Parameters:
        t - the throwable
        Returns:
        the string
      • getAllParents

        public static java.util.LinkedHashSet<java.lang.Class<?>> getAllParents​(java.lang.Class<?> c)
        Returns an order set of all interfaces implemented and classes extended for the entire type structure of the given class.

        If Object.class is passed to this method, then it will be returned in the result of this method.

        Parameters:
        c - the class to introspect
        Returns:
        the set of parents
      • getTypeArguments

        public static <T> java.util.List<java.lang.Class<?>> getTypeArguments​(java.lang.Class<T> baseClass,
                                                                              java.lang.Class<? extends T> childClass)