Class Utils

java.lang.Object
com.blackrook.engine.struct.Utils

public final class Utils extends Object
Various utility functions.
Author:
Matthew Tropiano
  • Method Details

    • isEmpty

      public static boolean isEmpty(Object obj)
      Checks if a value is "empty." The following is considered "empty":
      Parameters:
      obj - the object to check.
      Returns:
      true if the provided object is considered "empty", false otherwise.
    • getClasses

      public static String[] getClasses(String prefix)
      Returns the fully-qualified names of all classes beginning with a certain string. This uses Thread.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

      public static String[] getClasses(String prefix, ClassLoader classLoader)
      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

      public static String[] getClassesFromClasspath(String prefix)
      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

      public 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. 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

      public static String urlUnescape(String inString)
      Decodes a URL-encoded string.
      Parameters:
      inString - the input string.
      Returns:
      the unescaped string.
    • getArrayType

      public static Class<?> getArrayType(Class<?> arrayType)
      Gets the class type of this array type, if this is an array type.
      Parameters:
      arrayType - the type to inspect.
      Returns:
      this array's type, or null if the provided type is not an array, or if the found class is not on the classpath.
    • getArrayType

      public static Class<?> getArrayType(Object object)
      Gets the class type of this array, if this is an array.
      Parameters:
      object - the object to inspect.
      Returns:
      this array's type, or null if the provided object is not an array, or if the found class is not on the classpath.
    • isArray

      public static boolean isArray(Class<?> clazz)
      Tests if a class is actually an array type.
      Parameters:
      clazz - the class to test.
      Returns:
      true if so, false if not.
    • isArray

      public static boolean isArray(Object object)
      Tests if an object is actually an array type.
      Parameters:
      object - the object to test.
      Returns:
      true if so, false if not.
    • getArrayDimensions

      public static int getArrayDimensions(Class<?> arrayType)
      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

      public static int getArrayDimensions(Object array)
      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

      public static boolean createPathForFile(File file)
      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

      public static boolean createPathForFile(String path)
      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

      public static boolean createPath(String path)
      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

      public static void close(AutoCloseable c)
      Attempts to close an AutoCloseable object. If the object is null, this does nothing.
      Parameters:
      c - the reference to the AutoCloseable object.
    • create

      public static <T> T create(Class<T> clazz)
      Creates a new instance of a class from a class type. This essentially calls Class.getDeclaredConstructor(Class...) with no arguments and Class.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

      public static <T> T construct(Constructor<T> constructor, Object... params)
      Creates a new instance of a class from a class type. This essentially calls Class.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

      public static Object invokeBlind(Method method, Object instance, Object... params)
      Blindly invokes a method, only throwing a RuntimeException if 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

      public static void setFieldValue(Object instance, Field field, Object value)
      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

      public static Object getFieldValue(Object instance, Field field)
      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

      public static <T> TypeProfileFactory.Profile<T> getProfile(Class<T> clazz)
      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

      public static <T> T createForType(Object object, Class<T> targetType)
      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 object
      targetType - 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

      public static <T> T createForType(String memberName, Object object, Class<T> targetType)
      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 object
      targetType - 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

      public static String getExceptionString(Throwable t)
      Gets a full String representation of a Throwable type, including a line-by-line breakdown of the stack trace.
      Parameters:
      t - the throwable to render into a string.
      Returns:
      a multi-line string of the exception, similar to the stack dump.
    • sleep

      public static void sleep(long millis)
      Calls Thread.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)
      Calls Thread.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: