Class Reflect
- Author:
- gmt2001
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
dumpHeap()
Dumps the Java heap to an hprof filestatic void
Dumps the Java heap to an hprof filestatic void
Dumps all threads to a filestatic void
dumpThreads
(String filePath) Dumps all threads to a fileGets a list ofClass
that are in the local cache as a result of calls toloadPackageRecursive(String, List)
static String
Attempts to retrieve the full, real, absolute path to the directory in which PhantomBot.jar is locatedstatic MemoryUsage
Gets aMemoryUsage
containing information about the current memory usage of the heapstatic MemoryUsage
Gets aMemoryUsage
containing information about the current non-heap memory usagegetSubTypesOf
(Class<T> type) Gets a list of non-abstractClass
that are in the local cache as a result of calls toloadPackageRecursive(String, List)
which are assignable from the specified typestatic Reflect
instance()
Returns an instance ofReflect
Loads all classes visible to the defaultClassLoader
which have the specified package prefix into the local cacheloadPackageRecursive
(String pkg, List<String> exclude) Loads all classes visible to the defaultClassLoader
which have the specified package prefix into the local cacheloadPackageRecursive
(URL jarFile, String pkg, List<String> exclude) Loads all classes visible to the defaultClassLoader
in the specified jar file which have the specified package prefix into the local cachestatic long
pid()
Attempts to retrieve the PID of the running process from theRuntimeMXBean
of the JVM process
-
Method Details
-
instance
Returns an instance ofReflect
- Returns:
- an instance of
Reflect
-
loadPackageRecursive
Loads all classes visible to the defaultClassLoader
which have the specified package prefix into the local cacheThis process also triggers static initializers in matching classes
pkg
is matched usingString.startsWith(String)
The classes are enumerated from the main jar file
- Parameters:
pkg
- the package or package prefix to load- Returns:
this
-
loadPackageRecursive
Loads all classes visible to the defaultClassLoader
which have the specified package prefix into the local cacheIf any paths return
true
on aString.contains(CharSequence)
of any entry inexclude
, the class will be excluded from loadingThis process also triggers static initializers in matching classes
pkg
is matched usingString.startsWith(String)
.exclude
is matched usingString.contains(CharSequence)
The classes are enumerated from the main jar file
- Parameters:
pkg
- the package or package prefix to loadexclude
- a list of partial path names to exclude- Returns:
this
-
loadPackageRecursive
Loads all classes visible to the defaultClassLoader
in the specified jar file which have the specified package prefix into the local cacheIf any paths return
true
on aString.contains(CharSequence)
of any entry inexclude
, the class will be excluded from loadingThis process also triggers static initializers in matching classes
pkg
is matched usingString.startsWith(String)
.exclude
is matched usingString.contains(CharSequence)
- Parameters:
jarFile
- theURL
representing the path to the jar file which will be searched;null
to search the main jar filepkg
- the package or package prefix to load;""
(empty string) to load the entire jar fileexclude
- a list of partial path names to exclude- Returns:
this
-
getClasses
Gets a list ofClass
that are in the local cache as a result of calls toloadPackageRecursive(String, List)
- Returns:
- a list of
Class
-
getSubTypesOf
Gets a list of non-abstractClass
that are in the local cache as a result of calls toloadPackageRecursive(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 fileThis method tries to generate a filename of
java_pid##.TIMESTAMP.hprof
where##
is the PID of the running process andTIMESTAMP
is the timestamp when the method was called inyyyy-MM-dd_HH-mm-ss
formatThis method calls
dumpHeap(String, boolean)
with thelive
parameter set totrue
-
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 writtenlive
-true
to only dump live objects (objects which are referenced by others)- Throws:
IOException
- iffilePath
already exists, cannot be created, opened, or written toIllegalArgumentException
- iffilePath
does not end with the.hprof
extension
-
dumpThreads
public static void dumpThreads()Dumps all threads to a fileThis method tries to generate a filename of
java_pid##.TIMESTAMP.threads.txt
where##
is the PID of the running process andTIMESTAMP
is the timestamp when the method was called inyyyy-MM-dd_HH-mm-ss
format -
dumpThreads
Dumps all threads to a file- Parameters:
filePath
- the path to where the thread dump should be written- Throws:
IOException
- iffilePath
already exists, cannot be created, opened, or written to
-
pid
Attempts to retrieve the PID of the running process from theRuntimeMXBean
of the JVM process- Returns:
- the PID
- Throws:
NumberFormatException
-
getHeapMemoryUsage
Gets aMemoryUsage
containing information about the current memory usage of the heap- Returns:
- a
MemoryUsage
for the heap
-
getNonHeapMemoryUsage
Gets aMemoryUsage
containing information about the current non-heap memory usage- Returns:
- a
MemoryUsage
for non-heap memory
-
GetExecutionPath
Attempts to retrieve the full, real, absolute path to the directory in which PhantomBot.jar is located- Returns:
.
on failure; otherwise, the path
-