Package utilities.util.reflection
Class ReflectionUtilities
java.lang.Object
utilities.util.reflection.ReflectionUtilities
-
Method Summary
Modifier and TypeMethodDescriptionstatic Throwable
createFilteredThrowable
(String... patterns) A convenience method to create a throwable, filtering any lines that contain the given non-regex patterns.static Throwable
A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).static String
A convenience method to create a throwable, filtering boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).static String
Returns a string which is a printout of a stack trace for each thread running in the current JVMstatic 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.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.static Throwable
A convenience method to take a throwable, filter boiler-plate Java-related lines (e.g., AWT, Swing, Security, etc).static StackTraceElement[]
filterStackTrace
(StackTraceElement[] trace, String... patterns) Uses the givenpatterns
to remove elements from the given stack trace.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.static String
getClassNameOlderThan
(Class<?>... classes) Returns the class name of the entry in the stack that comes before all references to the given classes.static String
getClassNameOlderThan
(String... patterns) Returns the class name of the entry in the stack that comes before all references to the given patterns.static LinkedHashSet
<Class<?>> getSharedHierarchy
(List<?> list) Returns an ordered set of interfaces and classes that are shared amongst the items in the list.static LinkedHashSet
<Class<?>> getSharedParents
(List<?> list) Returns an ordered set of parent interfaces and classes that are shared amongst the items in the list.getTypeArguments
(Class<T> baseClass, Class<? extends T> childClass) Returns the type arguments for the given base class and extension.static Constructor
<?> locateConstructorOnClass
(Class<?> containingClass, Class<?>[] parameterTypes) static Field
locateFieldByTypeOnClass
(Class<?> classType, Class<?> containingClass) Get the first field specification contained within containingClass which has the type classType.static Field
locateFieldObjectOnClass
(String fieldName, Class<?> containingClass) Locates the field of the namefieldName
on the given class.static Method
locateMethodObjectOnClass
(String methodName, Class<?> containingClass, Class<?>[] parameterTypes) Locates the method of the namemethodName
on the given class.static Field
locateStaticFieldObjectOnClass
(String fieldName, Class<?> containingClass) Locates the field of the namefieldName
on the given class.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 patternstatic String
stackTraceToString
(String message, Throwable t) Turns the givenThrowable
into a String version of itsThrowable.printStackTrace()
method.static String
Turns the givenThrowable
into a String version of itsThrowable.printStackTrace()
method.
-
Method Details
-
locateStaticFieldObjectOnClass
Locates the field of the namefieldName
on the given class. If the given class does not contain the field, then this method will recursively call upcontainingClass
'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
Locates the field of the namefieldName
on the given class. If the given class does not contain the field, then this method will recursively call upcontainingClass
'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 namemethodName
on the given class. If the given class does not contain the method, then this method will recursively call upcontainingClass
'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
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 classcontainingClass
- the class that contains a field of the given type- Returns:
- field which corresponds to type classType or null
-
getClassNameOlderThan
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
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
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
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 updatepattern
- the non-regex patterns used to perform aString.contains(CharSequence)
on eachStackTraceElement
line- Returns:
- the updated trace
-
filterStackTrace
Uses the givenpatterns
to remove elements from the given stack trace. The current implementation will simply perform atoString()
on each element and then check to see if that string contains any of thepatterns
.- Parameters:
trace
- the trace to filterpatterns
- the non-regex patterns used to perform aString.contains(CharSequence)
on eachStackTraceElement
line.- Returns:
- the filtered trace
-
createFilteredThrowable
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 aString.contains(CharSequence)
on eachStackTraceElement
line.- Returns:
- the new 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
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
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
Returns a string which is a printout of a stack trace for each thread running in the current JVM- Returns:
- the stack trace string
-
stackTraceToString
Turns the givenThrowable
into a String version of itsThrowable.printStackTrace()
method.- Parameters:
t
- the throwable- Returns:
- the string
-
stackTraceToString
Turns the givenThrowable
into a String version of itsThrowable.printStackTrace()
method.- Parameters:
message
- the preferred message to use. If null, the throwable message will be usedt
- the throwable- Returns:
- the string
-
getAllParents
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 classchildClass
- the child class- Returns:
- the type arguments
-