Class RuntimeUtility


  • public final class RuntimeUtility
    extends java.lang.Object
    Utility for acquiring runtime information on software systems, classes or objects.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static boolean containsDir​(java.io.File aDir, java.util.List<java.io.File>... aDirs)  
      static java.lang.StackTraceElement getCallerStackTraceElement()
      Returns the stack trace element belonging to the direct caller of this method.
      static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.Class<?> aCallee)
      Returns the stack trace element belonging to the caller of the callee.
      static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.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 java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.String aCalleeClassName)
      Returns the stack trace element belonging to the caller of the callee.
      static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.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 java.lang.Class<?> getCallerType()
      Returns the type of the (Class) belonging to the direct caller of this method.
      static java.lang.Class<?> getCallerType​(java.lang.Class<?> aCallee)
      Returns the type of the (Class) belonging to the caller of the callee.
      static java.lang.Class<?> getCallerType​(java.lang.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 java.lang.Class getMainClass()
      Returns the main class launching the application.
      static java.lang.Long getPid()
      Bad hack to get the JVM's (process ID) 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​(java.lang.Long aPid)
      Bad hack to kill an OS thread by PID.
      static java.lang.Process killProcess​(java.lang.Long aPid)
      Bad hack to kill an OS thread by PID.
      protected static void putProperty​(java.util.Map<java.lang.String,java.lang.String> aProperties, java.lang.String aKey, java.lang.String aValue, char aDelimiter)
      Puts a property and resolves the key in case of name collisions.
      static java.lang.Class<?> toClass​(java.lang.StackTraceElement aStackTraceElement)
      Retrieves the Class type to which the StackTraceElement belongs.
      static java.lang.String toClassName()
      Retrieves the class name of the caller of this method without the fully qualified package name part.
      static java.lang.String toClassName​(java.lang.StackTraceElement aStackTraceElement)
      Returns the class name part from a stack trace element.
      static java.lang.String toClassName​(java.lang.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)
      A Cloneable object cannot directly be cloned by casting it to be Cloneable :-( Thereforee this method does the job.
      static java.lang.String toFullyQualifiedClassName()
      Retrieves the fully qualified method name of the caller of this method.
      static java.lang.String toFullyQualifiedClassName​(java.lang.StackTraceElement aStackTraceElement)
      Retrieves the fully qualified class name from a stack trace element.
      static java.lang.String toFullyQualifiedMethodName()
      Retrieves the fully qualified method name of the caller of this method.
      static java.lang.String toFullyQualifiedMethodName​(java.lang.StackTraceElement aStackTraceElement)
      Retrieves the fully qualified method name from a stack trace element.
      static java.lang.String toFullyQualifiedPackageName()
      Retrieves the fully qualified package name of the caller of this method without the class name part.
      static java.lang.String toFullyQualifiedPackageName​(java.lang.StackTraceElement aStackTraceElement)
      Retrieves the fully qualified package name from a stack trace element.
      static java.lang.String toFullyQualifiedPackageName​(java.lang.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 java.io.File toLauncherDir()
      Generates the base path relative to the given class location.
      static java.lang.String toMethodName()
      Retrieves the fully qualified method name of the caller of this method.
      static java.lang.String toMethodName​(java.lang.StackTraceElement aStackTraceElement)
      Retrieves the method name from a stack trace element.
      protected static java.lang.String toNextKey​(java.util.Map<java.lang.String,java.lang.String> aProperties, java.lang.String aKey, char aDelimiter)
      Returns the next free key in the properties in case of key name collisions..
      protected static java.lang.String toOption​(java.lang.String aArg, java.lang.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 java.util.Map<java.lang.String,java.lang.String> toProperties​(java.lang.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 java.util.Map<java.lang.String,java.lang.String> toProperties​(java.lang.String[] aArgs, java.lang.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 java.lang.String toStackTrace()
      Gets the stack trace for the current thread.
      static java.lang.String toString​(java.lang.String aToString, java.lang.String aSuperToString)
      Creates a string of a super class's Object.toString() method and the provided "toString" text.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isUnderTest

        public static boolean isUnderTest()
        Determines whether this code is executed from inside a unit test or not.
        Returns:
        True in case if executed from within a unit test.
      • toStackTrace

        public static java.lang.String toStackTrace()
        Gets the stack trace for the current thread.
        Returns:
        The stack trace from the current thread.
      • getCallerStackTraceElement

        public static java.lang.StackTraceElement getCallerStackTraceElement()
        Returns the stack trace element 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).
        Returns:
        The stack element of the direct caller of this method.
      • getCallerStackTraceElement

        public static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.Class<?> aCallee)
        Returns the stack trace element 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!
        Parameters:
        aCallee - The callee class which wants to find out who called it.
        Returns:
        The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
      • getCallerStackTraceElement

        public static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.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.
        Parameters:
        aCallees - the callees
        Returns:
        the caller stack trace element
      • getCallerStackTraceElement

        public static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.String aCalleeClassName)
        Returns the stack trace element 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.
        Parameters:
        aCalleeClassName - The callee class name or package namespace which wants to find out who called it.
        Returns:
        The stack element of the caller of the callee or null if the callee is not present or if there is no caller of the given callee in the current stack trace.
      • getCallerStackTraceElement

        public static java.lang.StackTraceElement getCallerStackTraceElement​(java.lang.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.
        Parameters:
        aCalleeClassNames - the callee class names
        Returns:
        the caller stack trace element
      • getCallerType

        public static java.lang.Class<?> getCallerType()
        Returns the type of the (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).
        Returns:
        The type (Class) of the direct caller of this method.
      • getCallerType

        public static java.lang.Class<?> getCallerType​(java.lang.Class<?> aCallee)
        Returns the type of the (Class) belonging to the caller of the callee.
        Parameters:
        aCallee - The callee class which wants to find out who called it.
        Returns:
        The type (Class) of the caller of the caller of this method.
      • getCallerType

        public static java.lang.Class<?> getCallerType​(java.lang.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.
        Parameters:
        aCallees - the callees
        Returns:
        the caller type
      • toClass

        public static java.lang.Class<?> toClass​(java.lang.StackTraceElement aStackTraceElement)
        Retrieves the Class type to which the StackTraceElement belongs.
        Parameters:
        aStackTraceElement - The StackTraceElement for which to get the according Class.
        Returns:
        The type (Class) of the according StackTraceElement.
      • toMethodName

        public static java.lang.String toMethodName​(java.lang.StackTraceElement aStackTraceElement)
        Retrieves the method name from a stack trace element.
        Parameters:
        aStackTraceElement - The stack trace element from which to retrieve the method name.
        Returns:
        The method name or null in case the stack trace element was null.
      • toClassName

        public static java.lang.String toClassName​(java.lang.StackTraceElement aStackTraceElement)
        Returns the class name part from a stack trace element. Retrieves the fully qualified class name from a stack trace element.
        Parameters:
        aStackTraceElement - The stack trace element from which to retrieve the class name.
        Returns:
        The class name without the package declaration or null in case the stack trace element was null.
      • toFullyQualifiedClassName

        public static java.lang.String toFullyQualifiedClassName​(java.lang.StackTraceElement aStackTraceElement)
        Retrieves the fully qualified class name from a stack trace element.
        Parameters:
        aStackTraceElement - The stack trace element from which to retrieve the fully qualified class name.
        Returns:
        The fully qualified class name or null in case the stack trace element was null.
      • toFullyQualifiedMethodName

        public static java.lang.String toFullyQualifiedMethodName​(java.lang.StackTraceElement aStackTraceElement)
        Retrieves the fully qualified method name from a stack trace element. This adds the method name to the fully qualified path name separated by a hash "#".
        Parameters:
        aStackTraceElement - The stack trace element from which to retrieve the fully qualified method name.
        Returns:
        The fully qualified method name or null in case the stack trace element was null.
      • toFullyQualifiedClassName

        public static java.lang.String toFullyQualifiedClassName()
        Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".
        Returns:
        The fully qualified method name.
      • toFullyQualifiedMethodName

        public static java.lang.String toFullyQualifiedMethodName()
        Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".
        Returns:
        The fully qualified method name.
      • toMethodName

        public static java.lang.String toMethodName()
        Retrieves the fully qualified method name of the caller of this method. This adds the method name to the caller's fully qualified path name separated by a hash "#".
        Returns:
        The fully qualified method name.
      • toClassName

        public static java.lang.String toClassName()
        Retrieves the class name of the caller of this method without the fully qualified package name part.
        Returns:
        The class name.
      • toFullyQualifiedPackageName

        public static java.lang.String toFullyQualifiedPackageName()
        Retrieves the fully qualified package name of the caller of this method without the class name part.
        Returns:
        The fully qualified package name.
      • toFullyQualifiedPackageName

        public static java.lang.String toFullyQualifiedPackageName​(java.lang.StackTraceElement aStackTraceElement)
        Retrieves the fully qualified package name from a stack trace element.
        Parameters:
        aStackTraceElement - The stack trace element from which to retrieve the fully qualified package name.
        Returns:
        The fully qualified package name.
      • toClassName

        public static java.lang.String toClassName​(java.lang.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).
        Parameters:
        aFullyQualifiedClassName - The fully qualified class name.
        Returns:
        The class name without the package declaration.
      • toFullyQualifiedPackageName

        public static java.lang.String toFullyQualifiedPackageName​(java.lang.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).
        Parameters:
        aFullyQualifiedClassName - The fully qualified class name.
        Returns:
        The fully qualified package name without the class name.
      • toClone

        public static <T> T toClone​(T aObj)
                             throws java.lang.CloneNotSupportedException
        A 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"
        Type Parameters:
        T - the generic type
        Parameters:
        aObj - The object to be cloned.
        Returns:
        The cloned object.
        Throws:
        java.lang.CloneNotSupportedException - in case the object cannot be cloned.
        See Also:
        "http://stackoverflow.com/questions/1138769/why-is-the-clone-method-protected-in-java-lang-object"
      • toString

        public static java.lang.String toString​(java.lang.String aToString,
                                                java.lang.String aSuperToString)
        Creates a string of a super class's Object.toString() method and the provided "toString" text.
        Parameters:
        aToString - The provided "toString" text.
        aSuperToString - A super class's Object.toString() method's String.
        Returns:
        The "concatenated" and formatted new String to be returned by an implementing class's Object.toString() method.
      • getPid

        public static java.lang.Long getPid()
        Bad hack to get the JVM's (process ID) PID of the process running your JVM instance.
        Returns:
        The PID (process ID) of the JVM running your thread.
        See Also:
        "http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id"
      • killProcess

        public static java.lang.Process killProcess​(java.lang.Long aPid)
                                             throws java.io.IOException
        Bad hack to kill an OS thread by PID. The current threads does not wait till the operation finished.
        Parameters:
        aPid - The process ID (PID) of the process to kill.
        Returns:
        The 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()
        Throws:
        java.io.IOException - Thrown in case of failing to successfully execute the kill operation.
        See Also:
        "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java", "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
      • kill

        public static boolean kill​(java.lang.Long aPid)
                            throws java.io.IOException,
                                   java.lang.InterruptedException
        Bad hack to kill an OS thread by PID. The current threads does wait till the operation finished.
        Parameters:
        aPid - The process ID (PID) of the process to kill.
        Returns:
        True in case killing the process was successful (e.g. the kill operation returned an exit code 0), else false.
        Throws:
        java.io.IOException - Thrown in case of failing to successfully execute the kill operation.
        java.lang.InterruptedException - the interrupted exception
        See Also:
        "http://stackoverflow.com/questions/9573696/kill-a-process-based-on-pid-in-java", "http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte"
      • toLauncherDir

        public static java.io.File toLauncherDir()
        Generates the base path relative to the given class location. Depending on the runtime, the path is truncated till the required path is determined.
        Returns:
        The base path of this application.
      • getMainClass

        public static java.lang.Class getMainClass()
        Returns the main class launching the application.
        Returns:
        The main class.
      • toProperties

        public static java.util.Map<java.lang.String,java.lang.String> toProperties​(java.lang.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.
        Parameters:
        aArgs - The command line arguments to convert to properties.
        Returns:
        The Map containing the determined properties.
      • toProperties

        public static java.util.Map<java.lang.String,java.lang.String> toProperties​(java.lang.String[] aArgs,
                                                                                    java.lang.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.
        Parameters:
        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.
        Returns:
        The Map containing the determined properties.
      • putProperty

        protected static void putProperty​(java.util.Map<java.lang.String,java.lang.String> aProperties,
                                          java.lang.String aKey,
                                          java.lang.String aValue,
                                          char aDelimiter)
        Puts a property and resolves the key in case of name collisions.
        Parameters:
        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.
      • toNextKey

        protected static java.lang.String toNextKey​(java.util.Map<java.lang.String,java.lang.String> aProperties,
                                                    java.lang.String aKey,
                                                    char aDelimiter)
        Returns the next free key in the properties in case of key name collisions..
        Parameters:
        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.
        Returns:
        The next non colliding key or null if there are no collisions.
      • toOption

        protected static java.lang.String toOption​(java.lang.String aArg,
                                                   java.lang.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.
        Parameters:
        aArg - The argument for which to retrieve the option's name.
        aPrefixes - The prefixes to use when determining options.
        Returns:
        Either the option's name or null if the argument does not represent an option.
      • containsDir

        @SafeVarargs
        protected static boolean containsDir​(java.io.File aDir,
                                             java.util.List<java.io.File>... aDirs)