Class Reflect

java.lang.Object
com.gmt2001.util.Reflect

public final class Reflect extends Object
Provides methods which perform common reflection operations
Author:
gmt2001
  • Method Details

    • instance

      public static Reflect instance()
      Returns an instance of Reflect
      Returns:
      an instance of Reflect
    • loadPackageRecursive

      public Reflect loadPackageRecursive(String pkg)
      Loads all classes visible to the default ClassLoader which have the specified package prefix into the local cache

      This process also triggers static initializers in matching classes

      pkg is matched using String.startsWith(String)

      The classes are enumerated from the main jar file

      Parameters:
      pkg - the package or package prefix to load
      Returns:
      this
    • loadPackageRecursive

      public Reflect loadPackageRecursive(String pkg, List<String> exclude)
      Loads all classes visible to the default ClassLoader which have the specified package prefix into the local cache

      If any paths return true on a String.contains(CharSequence) of any entry in exclude, the class will be excluded from loading

      This process also triggers static initializers in matching classes

      pkg is matched using String.startsWith(String). exclude is matched using String.contains(CharSequence)

      The classes are enumerated from the main jar file

      Parameters:
      pkg - the package or package prefix to load
      exclude - a list of partial path names to exclude
      Returns:
      this
    • loadPackageRecursive

      public Reflect loadPackageRecursive(URL jarFile, String pkg, List<String> exclude)
      Loads all classes visible to the default ClassLoader in the specified jar file which have the specified package prefix into the local cache

      If any paths return true on a String.contains(CharSequence) of any entry in exclude, the class will be excluded from loading

      This process also triggers static initializers in matching classes

      pkg is matched using String.startsWith(String). exclude is matched using String.contains(CharSequence)

      Parameters:
      jarFile - the URL representing the path to the jar file which will be searched; null to search the main jar file
      pkg - the package or package prefix to load; "" (empty string) to load the entire jar file
      exclude - a list of partial path names to exclude
      Returns:
      this
    • getClasses

      public List<Class<?>> getClasses()
      Gets a list of Class that are in the local cache as a result of calls to loadPackageRecursive(String, List)
      Returns:
      a list of Class
    • getSubTypesOf

      public <T> List<Class<? extends T>> getSubTypesOf(Class<T> type)
      Gets a list of non-abstract Class that are in the local cache as a result of calls to loadPackageRecursive(String, List) which are assignable from the specified type
      Type Parameters:
      T - the parent class or interface
      Parameters:
      type - the parent class or interface
      Returns:
      a list of sub-classes
    • dumpHeap

      public static void dumpHeap()
      Dumps the Java heap to an hprof file

      This method tries to generate a filename of java_pid##.TIMESTAMP.hprof where ## is the PID of the running process and TIMESTAMP is the timestamp when the method was called in yyyy-MM-dd_HH-mm-ss format

      This method calls dumpHeap(String, boolean) with the live parameter set to true

    • dumpHeap

      public static void dumpHeap(String filePath, boolean live) throws IOException, IllegalArgumentException
      Dumps the Java heap to an hprof file
      Parameters:
      filePath - the path to where the heap dump should be written
      live - true to only dump live objects (objects which are referenced by others)
      Throws:
      IOException - if filePath already exists, cannot be created, opened, or written to
      IllegalArgumentException - if filePath does not end with the .hprof extension
    • dumpThreads

      public static void dumpThreads()
      Dumps all threads to a file

      This method tries to generate a filename of java_pid##.TIMESTAMP.threads.txt where ## is the PID of the running process and TIMESTAMP is the timestamp when the method was called in yyyy-MM-dd_HH-mm-ss format

    • dumpThreads

      public static void dumpThreads(String filePath) throws IOException
      Dumps all threads to a file
      Parameters:
      filePath - the path to where the thread dump should be written
      Throws:
      IOException - if filePath already exists, cannot be created, opened, or written to
    • pid

      public static long pid() throws NumberFormatException
      Attempts to retrieve the PID of the running process from the RuntimeMXBean of the JVM process
      Returns:
      the PID
      Throws:
      NumberFormatException
    • getHeapMemoryUsage

      public static MemoryUsage getHeapMemoryUsage()
      Gets a MemoryUsage containing information about the current memory usage of the heap
      Returns:
      a MemoryUsage for the heap
    • getNonHeapMemoryUsage

      public static MemoryUsage getNonHeapMemoryUsage()
      Gets a MemoryUsage containing information about the current non-heap memory usage
      Returns:
      a MemoryUsage for non-heap memory
    • GetExecutionPath

      public static String GetExecutionPath()
      Attempts to retrieve the full, real, absolute path to the directory in which PhantomBot.jar is located
      Returns:
      . on failure; otherwise, the path