public final class RuntimeUtility extends Object
Modifier and Type | Method | Description |
---|---|---|
protected static boolean |
containsDir(File aDir,
List<File>... aDirs) |
|
static StackTraceElement |
getCallerStackTraceElement() |
Returns the
StackTraceElement belonging to the direct caller of
this method. |
static StackTraceElement |
getCallerStackTraceElement(Class<?> aCallee) |
Returns the
StackTraceElement belonging to the caller of the
callee. |
static StackTraceElement |
getCallerStackTraceElement(Class<?>... aCallees) |
Same as
getCallerStackTraceElement(Class) with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned. |
static StackTraceElement |
getCallerStackTraceElement(StackTraceElement aCallee) |
Returns the
StackTraceElement belonging to the caller of the
callee. |
static StackTraceElement |
getCallerStackTraceElement(StackTraceElement... aCallees) |
Same as
getCallerStackTraceElement(StackTraceElement) with the
difference that the passed callees are tried out one after the other
until the first caller determined for a callee is returned. |
static StackTraceElement |
getCallerStackTraceElement(String aCalleeClassName) |
Returns the
StackTraceElement belonging to the caller of the
callee. |
static StackTraceElement |
getCallerStackTraceElement(String... aCalleeClassNames) |
Same as
getCallerStackTraceElement(String) with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned. |
static Class<?> |
getCallerType() |
Returns the type of the (
Class ) belonging to the direct caller of
this method. |
static Class<?> |
getCallerType(Class<?> aCallee) |
Returns the type of the (
Class ) belonging to the caller of the
callee. |
static Class<?> |
getCallerType(Class<?>... aCallees) |
Same as
getCallerType(Class) with the difference that the passed
callees are tried out one after the other until the first caller
determined for a callee is returned. |
static Class<?> |
getCallerType(StackTraceElement aCallee) |
Returns the type of the (
Class ) belonging to the caller of the
callee. |
static Class<?> |
getCallerType(StackTraceElement... aCallees) |
Same as
getCallerType(StackTraceElement) with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned. |
static Class<?> |
getCallerType(String aCallee) |
Returns the type of the (
Class ) belonging to the caller of the
callee. |
static Class<?> |
getCallerType(String... aCallees) |
Same as
getCallerType(String) with the difference that the
passed callees are tried out one after the other until the first caller
determined for a callee is returned. |
static Class |
getMainClass() |
Returns the main class launching the application.
|
static Long |
getPid() |
Bad hack to get the JVM's (process TID) PID of the process running your
JVM instance.
|
static boolean |
isUnderTest() |
Determines whether this code is executed from inside a unit test or not.
|
static boolean |
kill(Long aPid) |
Bad hack to kill an OS thread by PID.
|
static Process |
killProcess(Long aPid) |
Bad hack to kill an OS thread by PID.
|
protected static void |
putProperty(Map<String,String> aProperties,
String aKey,
String aValue,
char aDelimiter) |
Puts a property and resolves the key in case of name collisions.
|
static StackTraceElement |
toBestCallerStackTraceElement(Class<?> aCallee) |
Returns the best matching
StackTraceElement belonging to the
caller of the callee.The "best matching" StackTraceElement is
considered to be the one not belonging to any "internal" API in a package
namespace such as "com.sun.*", "java.*" or "javax.*". |
static StackTraceElement |
toBestCallerStackTraceElement(Class<?>[] aCallees) |
Returns the best matching
StackTraceElement belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*". |
static StackTraceElement |
toBestCallerStackTraceElement(StackTraceElement aCallee) |
Returns the best matching
StackTraceElement belonging to the
caller of the callee.The "best matching" StackTraceElement is
considered to be the one not belonging to any "internal" API in a package
namespace such as "com.sun.*", "java.*" or "javax.*". |
static StackTraceElement |
toBestCallerStackTraceElement(StackTraceElement[] aCallees) |
Returns the best matching
StackTraceElement belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*". |
static StackTraceElement |
toBestCallerStackTraceElement(String aCalleeClassName) |
Returns the best matching
StackTraceElement belonging to the
caller of the callee. |
static StackTraceElement |
toBestCallerStackTraceElement(String[] aCallees) |
Returns the best matching
StackTraceElement belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*". |
static Class<?> |
toClass(StackTraceElement aStackTraceElement) |
Retrieves the
Class type to which the StackTraceElement
belongs. |
static String |
toClassName() |
Retrieves the class name of the caller of this method without the fully
qualified package name part.
|
static String |
toClassName(StackTraceElement aStackTraceElement) |
Returns the class name part from a
StackTraceElement . |
static String |
toClassName(String aFullyQualifiedClassName) |
Returns the class name part from a fully qualified class name (which has
the fully qualified package name as part of its name).
|
static <T> T |
toClone(T aObj) |
|
static String |
toFullyQualifiedClassName() |
Retrieves the fully qualified method name of the caller of this method.
|
static String |
toFullyQualifiedClassName(StackTraceElement aStackTraceElement) |
Retrieves the fully qualified class name from a
StackTraceElement . |
static String |
toFullyQualifiedMethodName() |
Retrieves the fully qualified method name of the caller of this method.
|
static String |
toFullyQualifiedMethodName(StackTraceElement aStackTraceElement) |
Retrieves the fully qualified method name from a
StackTraceElement . |
static String |
toFullyQualifiedPackageName() |
Retrieves the fully qualified package name of the caller of this method
without the class name part.
|
static String |
toFullyQualifiedPackageName(StackTraceElement aStackTraceElement) |
Retrieves the fully qualified package name from a
StackTraceElement . |
static String |
toFullyQualifiedPackageName(String aFullyQualifiedClassName) |
Returns the fully qualified package name part from a fully qualified
class name (which has the fully qualified package name as part of its
name).
|
static File |
toLauncherDir() |
Generates the base path relative to the given class location.
|
static String |
toMethodName() |
Retrieves the fully qualified method name of the caller of this method.
|
static String |
toMethodName(StackTraceElement aStackTraceElement) |
Retrieves the method name from a
StackTraceElement . |
protected static String |
toNextKey(Map<String,String> aProperties,
String aKey,
char aDelimiter) |
Returns the next free key in the properties in case of key name
collisions..
|
protected static String |
toOption(String aArg,
String... aPrefixes) |
When the provided argument starts with one of the provided prefixes, then
the portion of the argument without the prefix is returned, which
represents the option in question.
|
static Map<String,String> |
toProperties(String[] aArgs) |
Creates a
Map from the provided command line arguments using the
CommandArgPrefix.toPrefixes() prefixes to identify the keys (and
the values) from the provided arguments. |
static Map<String,String> |
toProperties(String[] aArgs,
String[] aPrefixes,
char aDelimiter) |
Creates a
Map from the provided command line arguments using the
provided prefixes to identify the keys (and the values) from the provided
arguments. |
static String |
toStackTrace() |
Gets the stack trace for the current thread.
|
static String |
toString(String aToString,
String aSuperToString) |
Creates a string of a super class's
Object.toString() method and
the provided "toString" text. |
static File |
toUserWorkingDir() |
Tries to determine the current directory from which the Java programm was
started for example as of bash's
pwd command. |
public static boolean isUnderTest()
public static String toStackTrace()
public static StackTraceElement getCallerStackTraceElement()
StackTraceElement
belonging to the direct caller of
this method. When you use this method in your code, you get the stack
trace element of your method (invoking this method).public static StackTraceElement getCallerStackTraceElement(Class<?> aCallee)
StackTraceElement
belonging to the caller of the
callee. Best you do not use the Object.getClass()
method, instead
use YourClass.class as as of inheritance, Object.getClass()
returns the wrong type not being the actual callee!aCallee
- The callee class which wants to find out who called it.public static StackTraceElement getCallerStackTraceElement(StackTraceElement aCallee)
StackTraceElement
belonging to the caller of the
callee. Best you do not use the Object.getClass()
method, instead
use YourClass.class as as of inheritance, Object.getClass()
returns the wrong type not being the actual callee!aCallee
- The callee StackTraceElement
which wants to find
out who called it.public static StackTraceElement getCallerStackTraceElement(Class<?>... aCallees)
getCallerStackTraceElement(Class)
with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned.aCallees
- the calleesStackTraceElement
public static StackTraceElement getCallerStackTraceElement(StackTraceElement... aCallees)
getCallerStackTraceElement(StackTraceElement)
with the
difference that the passed callees are tried out one after the other
until the first caller determined for a callee is returned.aCallees
- the calleesStackTraceElement
public static StackTraceElement getCallerStackTraceElement(String... aCalleeClassNames)
getCallerStackTraceElement(String)
with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned.aCalleeClassNames
- the callee class namesStackTraceElement
public static StackTraceElement getCallerStackTraceElement(String aCalleeClassName)
StackTraceElement
belonging to the caller of the
callee. The callee can also be a package namespace where the matchee's
must begin with the given package namespace.aCalleeClassName
- The callee class name or package namespace which
wants to find out who called it.public static StackTraceElement toBestCallerStackTraceElement(String aCalleeClassName)
StackTraceElement
belonging to the
caller of the callee. The "best matching" StackTraceElement
is
considered to be the one not belonging to any "internal" API in a package
namespace such as "com.sun.*", "java.*" or "javax.*". The callee can also
be a package namespace where the matchee's must begin with the given
package namespace.aCalleeClassName
- The callee class name or package namespace which
wants to find out who called it.public static StackTraceElement toBestCallerStackTraceElement(StackTraceElement aCallee)
StackTraceElement
belonging to the
caller of the callee.The "best matching" StackTraceElement
is
considered to be the one not belonging to any "internal" API in a package
namespace such as "com.sun.*", "java.*" or "javax.*".aCallee
- The callee StackTraceElement
which wants to find
out who called it.public static StackTraceElement toBestCallerStackTraceElement(Class<?> aCallee)
StackTraceElement
belonging to the
caller of the callee.The "best matching" StackTraceElement
is
considered to be the one not belonging to any "internal" API in a package
namespace such as "com.sun.*", "java.*" or "javax.*".aCallee
- The callee StackTraceElement
which wants to find
out who called it.public static StackTraceElement toBestCallerStackTraceElement(String[] aCallees)
StackTraceElement
belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement
is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*". The callees can also be a package
namespace where the matchee's must begin with the given package
namespace.aCallees
- The callees names for which to find out who called it.public static StackTraceElement toBestCallerStackTraceElement(StackTraceElement[] aCallees)
StackTraceElement
belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement
is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*".aCallees
- The callees StackTraceElement
instances for which
to find out who called it.public static StackTraceElement toBestCallerStackTraceElement(Class<?>[] aCallees)
StackTraceElement
belonging to the
caller of one of the callees (in the order passed to the method).The
"best matching" StackTraceElement
is considered to be the one not
belonging to any "internal" API in a package namespace such as
"com.sun.*", "java.*" or "javax.*".aCallees
- The callees Class
instances for which to find out
who called it.public static Class<?> getCallerType()
Class
) belonging to the direct caller of
this method. When you use this method in your code, you get the
Class
of your method (invoking this method).Class
) of the direct caller of this method.public static Class<?> getCallerType(Class<?> aCallee)
Class
) belonging to the caller of the
callee.aCallee
- The callee class which wants to find out who called it.Class
) of the caller of the caller of this
method.public static Class<?> getCallerType(Class<?>... aCallees)
getCallerType(Class)
with the difference that the passed
callees are tried out one after the other until the first caller
determined for a callee is returned.aCallees
- the calleespublic static Class<?> getCallerType(StackTraceElement aCallee)
Class
) belonging to the caller of the
callee.aCallee
- The callee class which wants to find out who called it.Class
) of the caller of the caller of this
method.public static Class<?> getCallerType(String... aCallees)
getCallerType(String)
with the difference that the
passed callees are tried out one after the other until the first caller
determined for a callee is returned.aCallees
- the calleespublic static Class<?> getCallerType(String aCallee)
Class
) belonging to the caller of the
callee. The callee can also be a package namespace where the matchee's
must begin with the given package namespace.aCallee
- The callee class which wants to find out who called it.Class
) of the caller of the caller of this
method.public static Class<?> getCallerType(StackTraceElement... aCallees)
getCallerType(StackTraceElement)
with the difference
that the passed callees are tried out one after the other until the first
caller determined for a callee is returned.aCallees
- the calleespublic static Class<?> toClass(StackTraceElement aStackTraceElement)
Class
type to which the StackTraceElement
belongs.aStackTraceElement
- The StackTraceElement
for which to get
the according Class
.Class
) of the according
StackTraceElement
.public static String toMethodName(StackTraceElement aStackTraceElement)
StackTraceElement
.aStackTraceElement
- The StackTraceElement
from which to
retrieve the method name.StackTraceElement
was
null.public static String toClassName(StackTraceElement aStackTraceElement)
StackTraceElement
. Retrieves
the fully qualified class name from a StackTraceElement
.aStackTraceElement
- The StackTraceElement
from which to
retrieve the class name.StackTraceElement
was null.public static String toFullyQualifiedClassName(StackTraceElement aStackTraceElement)
StackTraceElement
.aStackTraceElement
- The StackTraceElement
from which to
retrieve the fully qualified class name.public static String toFullyQualifiedMethodName(StackTraceElement aStackTraceElement)
StackTraceElement
. This adds the method name to the fully
qualified path name separated by a hash "#".aStackTraceElement
- The StackTraceElement
from which to
retrieve the fully qualified method name.public static String toFullyQualifiedClassName()
public static String toFullyQualifiedMethodName()
public static String toMethodName()
public static String toClassName()
public static String toFullyQualifiedPackageName()
public static String toFullyQualifiedPackageName(StackTraceElement aStackTraceElement)
StackTraceElement
.aStackTraceElement
- The StackTraceElement
from which to
retrieve the fully qualified package name.public static String toClassName(String aFullyQualifiedClassName)
aFullyQualifiedClassName
- The fully qualified class name.public static String toFullyQualifiedPackageName(String aFullyQualifiedClassName)
aFullyQualifiedClassName
- The fully qualified class name.public static <T> T toClone(T aObj) throws CloneNotSupportedException
Cloneable
object cannot directly be cloned by casting it to be
Cloneable
:-( Thereforee this method does the job. Citation From
Josh Bloch's Effective Java: "The Cloneable
interface was
intended as a mixin interface for objects to advertise that they permit
cloning. Unfortunately it fails to serve this purpose ... This is a
highly atypical use of interfaces and not one to be emulated ... In order
for implementing the interface to have any effect on a class, it and all
of its superclasses must obey a fairly complex, unenforceable and largely
undocumented protocol"T
- the generic typeaObj
- The object to be cloned.CloneNotSupportedException
- in case the object cannot be cloned.public static String toString(String aToString, String aSuperToString)
Object.toString()
method and
the provided "toString" text.aToString
- The provided "toString" text.aSuperToString
- A super class's Object.toString()
method's
String
.String
to be
returned by an implementing class's Object.toString()
method.public static Long getPid()
public static Process killProcess(Long aPid) throws IOException
aPid
- The process TID (PID) of the process to kill.Process
object representing the kill operation. This
instance will let you wait till the operation finished
Process.waitFor()
and provides access to the
Process.exitValue()
IOException
- Thrown in case of failing to successfully execute the
kill operation.public static boolean kill(Long aPid) throws IOException, InterruptedException
aPid
- The process TID (PID) of the process to kill.IOException
- Thrown in case of failing to successfully execute the
kill operation.InterruptedException
- the interrupted exceptionpublic static File toUserWorkingDir()
pwd
command. Pass as JVM
argument via "-Dcurrent.dir=/path/to/current/dir".public static File toLauncherDir()
SystemProperty.LAUNCHER_DIR
(e.g. when
providing the following argument
-Dlauncher.dir=/path/to/launcher/dir
when invoking your
java's executable) is set and in case its value points to a directory,
then this directory is returned.public static Class getMainClass()
public static Map<String,String> toProperties(String[] aArgs)
Map
from the provided command line arguments using the
CommandArgPrefix.toPrefixes()
prefixes to identify the keys (and
the values) from the provided arguments.aArgs
- The command line arguments to convert to properties.Map
containing the determined properties.public static Map<String,String> toProperties(String[] aArgs, String[] aPrefixes, char aDelimiter)
Map
from the provided command line arguments using the
provided prefixes to identify the keys (and the values) from the provided
arguments.aArgs
- The command line arguments to convert to properties.aPrefixes
- The prefixes to be used to identify options.aDelimiter
- The delimiter to use when generating non colliding
keys.Map
containing the determined properties.protected static void putProperty(Map<String,String> aProperties, String aKey, String aValue, char aDelimiter)
aProperties
- The properties which to modify.aKey
- The key which to put.aValue
- The value to put.aDelimiter
- The delimiter to use when generating non colliding
keys.protected static String toNextKey(Map<String,String> aProperties, String aKey, char aDelimiter)
aProperties
- The properties for which to determine the next key.aKey
- The key for which to test for name collisions.aDelimiter
- The delimiter to use when creating non colliding keys.protected static String toOption(String aArg, String... aPrefixes)
aArg
- The argument for which to retrieve the option's name.aPrefixes
- The prefixes to use when determining options.@SafeVarargs protected static boolean containsDir(File aDir, List<File>... aDirs)
Copyright © 2021. All rights reserved.