Class Utils
java.lang.Object
com.blackrook.engine.struct.Utils
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAttempts to close anAutoCloseableobject.static <T> Tconstruct(Constructor<T> constructor, Object... params) Creates a new instance of a class from a class type.static <T> TCreates a new instance of a class from a class type.static <T> TcreateForType(Object object, Class<T> targetType) Creates a new instance of an object for placement in a POJO or elsewhere.static <T> TcreateForType(String memberName, Object object, Class<T> targetType) Creates a new instance of an object for placement in a POJO or elsewhere.static booleancreatePath(String path) Creates the necessary directories for a file path.static booleancreatePathForFile(File file) Creates the necessary directories for a file path.static booleancreatePathForFile(String path) Creates the necessary directories for a file path.static File[]explodeFiles(File... files) Explodes a list of files into a larger list of files, such that all of the files in the resultant list are not directories, by traversing directory paths.static intgetArrayDimensions(Class<?> arrayType) Gets how many dimensions that this array, represented by the provided type, has.static intgetArrayDimensions(Object array) Gets how many array dimensions that an object (presumably an array) has.static Class<?> getArrayType(Class<?> arrayType) Gets the class type of this array type, if this is an array type.static Class<?> getArrayType(Object object) Gets the class type of this array, if this is an array.static String[]getClasses(String prefix) Returns the fully-qualified names of all classes beginning with a certain string.static String[]getClasses(String prefix, ClassLoader classLoader) Returns the fully-qualified names of all classes beginning with a certain string.static String[]getClassesFromClasspath(String prefix) Returns the fully-qualified names of all classes beginning with a certain string.static StringGets a full String representation of a Throwable type, including a line-by-line breakdown of the stack trace.static ObjectgetFieldValue(Object instance, Field field) Gets the value of a field on an object.static <T> TypeProfileFactory.Profile<T> getProfile(Class<T> clazz) Creates a new profile for a provided type.static ObjectinvokeBlind(Method method, Object instance, Object... params) Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong.static booleanTests if a class is actually an array type.static booleanTests if an object is actually an array type.static booleanChecks if a value is "empty."static <T> T[]joinArrays(T[]... arrays) Concatenates a set of arrays together, such that the contents of each array are joined into one array.static voidsetFieldValue(Object instance, Field field, Object value) Sets the value of a field on an object.static voidsleep(long millis) CallsThread.sleep()but in an encapsulated try to avoid catching InterruptedException.static voidsleep(long millis, int nanos) CallsThread.sleep()but in an encapsulated try to avoid catching InterruptedException.static StringurlUnescape(String inString) Decodes a URL-encoded string.
-
Method Details
-
isEmpty
Checks if a value is "empty." The following is considered "empty":- Null references.
Arrayobjects that have a length of 0.Booleanobjects that are false.Characterobjects that are the null character ('\0', ' ').Numberobjects that are zero.Stringobjects that are the empty string, or areString.trim()'ed down to the empty string.Collectionobjects whereCollection.isEmpty()returns true.
- Parameters:
obj- the object to check.- Returns:
- true if the provided object is considered "empty", false otherwise.
-
getClasses
Returns the fully-qualified names of all classes beginning with a certain string. This usesThread.getContextClassLoader()on the current thread to find them. None of the classes are "forName"-ed into PermGen space.This scan can be expensive, as this searches the contents of the entire classpath.
- Parameters:
prefix- the String to use for lookup. Can be null.- Returns:
- the list of class names.
- Throws:
RuntimeException- if a JAR file could not be read for some reason.
-
getClasses
Returns the fully-qualified names of all classes beginning with a certain string. None of the classes are "forName"-ed into PermGen/Metaspace.This scan can be expensive, as this searches the contents of the entire
ClassLoader.- Parameters:
prefix- the String to use for lookup. Can be null.classLoader- the ClassLoader to look into.- Returns:
- the list of class names.
- Throws:
RuntimeException- if a JAR file could not be read for some reason.
-
getClassesFromClasspath
Returns the fully-qualified names of all classes beginning with a certain string. None of the classes are "forName"-ed into PermGen/Metaspace.This scan can be expensive, as this searches the contents of the entire
ClassLoader.- Parameters:
prefix- the String to use for lookup. Can be null.- Returns:
- the list of class names.
- Throws:
RuntimeException- if a JAR file could not be read for some reason.
-
joinArrays
public static <T> T[] joinArrays(T[]... arrays) Concatenates a set of arrays together, such that the contents of each array are joined into one array. Null arrays are skipped.- Type Parameters:
T- the object type stored in the arrays.- Parameters:
arrays- the list of arrays.- Returns:
- a new array with all objects in each provided array added to the resultant one in the order in which they appear.
-
explodeFiles
Explodes a list of files into a larger list of files, such that all of the files in the resultant list are not directories, by traversing directory paths. The returned list is not guaranteed to be in any order related to the input list, and may contain files that are in the input list if they are not directories.- Parameters:
files- the list of files to expand.- Returns:
- a list of all files found in the subdirectory search.
- Throws:
NullPointerException- if files is null.
-
urlUnescape
-
getArrayType
-
getArrayType
-
isArray
Tests if a class is actually an array type.- Parameters:
clazz- the class to test.- Returns:
- true if so, false if not.
-
isArray
Tests if an object is actually an array type.- Parameters:
object- the object to test.- Returns:
- true if so, false if not.
-
getArrayDimensions
Gets how many dimensions that this array, represented by the provided type, has.- Parameters:
arrayType- the type to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
getArrayDimensions
Gets how many array dimensions that an object (presumably an array) has.- Parameters:
array- the object to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
createPathForFile
Creates the necessary directories for a file path.- Parameters:
file- the abstract file path.- Returns:
- true if the paths were made (or exists), false otherwise.
-
createPathForFile
Creates the necessary directories for a file path.- Parameters:
path- the abstract path.- Returns:
- true if the paths were made (or exists), false otherwise.
-
createPath
Creates the necessary directories for a file path.- Parameters:
path- the abstract path.- Returns:
- true if the paths were made (or exists), false otherwise.
-
close
Attempts to close anAutoCloseableobject. If the object is null, this does nothing.- Parameters:
c- the reference to the AutoCloseable object.
-
create
Creates a new instance of a class from a class type. This essentially callsClass.getDeclaredConstructor(Class...)with no arguments andClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
clazz- the class type to instantiate.- Returns:
- a new instance of an object.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
construct
Creates a new instance of a class from a class type. This essentially callsClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
constructor- the constructor to call.params- the constructor parameters.- Returns:
- a new instance of an object created via the provided constructor.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
invokeBlind
Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong. Here for the convenience of not making a billion try/catch clauses for a method invocation.- Parameters:
method- the method to invoke.instance- the object instance that is the method target.params- the parameters to pass to the method.- Returns:
- the return value from the method invocation. If void, this is null.
- Throws:
ClassCastException- if one of the parameters could not be cast to the proper type.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the method).- See Also:
-
setFieldValue
Sets the value of a field on an object.- Parameters:
instance- the object instance to set the field on.field- the field to set.value- the value to set.- Throws:
NullPointerException- if the field or object provided is null.ClassCastException- if the value could not be cast to the proper type.RuntimeException- if anything goes wrong (bad field name, bad target, bad argument, or can't access the field).- See Also:
-
getFieldValue
Gets the value of a field on an object.- Parameters:
instance- the object instance to get the field value of.field- the field to get the value of.- Returns:
- the current value of the field.
- Throws:
NullPointerException- if the field or object provided is null.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the field).- See Also:
-
getProfile
Creates a new profile for a provided type. Generated profiles are stored in memory, and retrieved again by class type.This method is thread-safe.
- Type Parameters:
T- the class type.- Parameters:
clazz- the class.- Returns:
- a new profile.
-
createForType
Creates a new instance of an object for placement in a POJO or elsewhere.- Type Parameters:
T- the return object type.- Parameters:
object- the object to convert to another objecttargetType- the target class type to convert to, if the types differ.- Returns:
- a suitable object of type
targetType. - Throws:
ClassCastException- if the incoming type cannot be converted.
-
createForType
Creates a new instance of an object for placement in a POJO or elsewhere.- Type Parameters:
T- the return object type.- Parameters:
memberName- the name of the member that is being converted (for reporting).object- the object to convert to another objecttargetType- the target class type to convert to, if the types differ.- Returns:
- a suitable object of type
targetType. - Throws:
ClassCastException- if the incoming type cannot be converted.
-
getExceptionString
-
sleep
public static void sleep(long millis) CallsThread.sleep()but in an encapsulated try to avoid catching InterruptedException. Convenience method for making the current thread sleep when you don't care if it's interrupted or not and want to keep code neat.- Parameters:
millis- the amount of milliseconds to sleep.- See Also:
-
sleep
public static void sleep(long millis, int nanos) CallsThread.sleep()but in an encapsulated try to avoid catching InterruptedException. Convenience method for making the current thread sleep when you don't care if it's interrupted or not and want to keep code neat.- Parameters:
millis- the amount of milliseconds to sleep.nanos- the amount of additional nanoseconds to sleep.- See Also:
-