Class ReflectionUtilities

java.lang.Object
utilities.util.reflection.ReflectionUtilities

public class ReflectionUtilities extends Object
  • Method Details

    • locateStaticFieldObjectOnClass

      public static Field locateStaticFieldObjectOnClass(String fieldName, 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 Field locateFieldObjectOnClass(String fieldName, 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 Method locateMethodObjectOnClass(String methodName, Class<?> containingClass, 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 Constructor<?> locateConstructorOnClass(Class<?> containingClass, Class<?>[] parameterTypes)
    • locateFieldByTypeOnClass

      public static Field locateFieldByTypeOnClass(Class<?> classType, 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 String getClassNameOlderThan(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
    • getClassNameOlderThan

      public static String getClassNameOlderThan(String... patterns)
      Returns the class name of the entry in the stack that comes before all references to the given patterns. This is useful for figuring out at runtime who is calling a particular method.
      Parameters:
      patterns - the patterns to ignore
      Returns:
      the desired class name
    • createThrowableWithStackOlderThan

      public static Throwable createThrowableWithStackOlderThan(String... patterns)
      Creates a throwable whose stack trace is based upon the current call stack, with any information coming before, and including, the given patterns removed.
      Parameters:
      patterns - the strings to ignore (e.g., class or package names)
      Returns:
      the new throwable
      See Also:
    • createThrowableWithStackOlderThan

      public static Throwable createThrowableWithStackOlderThan(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 StackTraceElement[] movePastStackTracePattern(StackTraceElement[] trace, 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 StackTraceElement[] filterStackTrace(StackTraceElement[] trace, 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 Throwable createFilteredThrowable(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 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 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 Throwable filterJavaThrowable(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 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 LinkedHashSet<Class<?>> getSharedHierarchy(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 LinkedHashSet<Class<?>> getSharedParents(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 String stackTraceToString(Throwable t)
      Turns the given Throwable into a String version of its Throwable.printStackTrace() method.
      Parameters:
      t - the throwable
      Returns:
      the string
    • stackTraceToString

      public static String stackTraceToString(String message, Throwable t)
      Turns the given Throwable into a String version of its Throwable.printStackTrace() method.
      Parameters:
      message - the preferred message to use. If null, the throwable message will be used
      t - the throwable
      Returns:
      the string
    • getAllParents

      public static LinkedHashSet<Class<?>> getAllParents(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> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass)
      Returns the type arguments for the given base class and extension.

      Caveat: this lookup will only work if the given child class is a concrete class that has its type arguments specified. For example, these cases will work:

                      // anonymous class definition
                      List<String> myList = new ArrayList<String>() {
                              ...
                      };
      
                      // class definition
                      public class MyList implements List<String> {
       
      Whereas this case will not work:
                      // local variable with the type specified
                      List<String> myList = new ArrayList<String>();
       

      Note: a null entry in the result list will exist for any type that was unrecoverable

      Type Parameters:
      T - the type of the base and child class
      Parameters:
      baseClass - the base class
      childClass - the child class
      Returns:
      the type arguments