Class Execution

java.lang.Object
org.refcodes.runtime.Execution

public final class Execution extends Object
Utility for acquiring runtime information on software systems, classes or objects.
  • Field Details

  • Method Details

    • toLauncherDir

      public static 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. In case the 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.
      Returns:
      The base path of this application.
    • getMainClass

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

      public static void addShutdownHook(Thread hook)
      Registers a new virtual-machine shutdown hook. In case of running as a native image (GraalVM AOT), then a Signal handler for capturing CTRL+C is registered exiting the application regularirly so that shutdown hooks registered with addShutdownHook(Thread) are also called when inside a native image. Exiting vie CTRL+C then will exit with status code 130 as of ExitCode.CONTROL_C.
      Parameters:
      hook - An initialized but not started Thread object
    • addShutdownHook

      public static void addShutdownHook(Runnable hook)
      Registers a new virtual-machine shutdown hook. In case of running as a native image (GraalVM AOT), then a Signal handler for capturing CTRL+C is registered exiting the application properly so that shutdown hooks registered with addShutdownHook(Thread) are also called when inside a native image. Exiting vie CTRL+C then will exit with status code 130 as of ExitCode.CONTROL_C.
      Parameters:
      hook - A Runnable object (convenience method for addShutdownHook(Thread)).
    • setLoggingHandler

      public static void setLoggingHandler(Handler aHandler)
      Redirects the Logger configuration and sets the provided handler.
      Parameters:
      aHandler - The Handler to be set as the one and only handler.
    • setLoggingStreams

      public static void setLoggingStreams(OutputStream aStdStream, OutputStream aErrStream, Level aLevel)
      Redirects the Logger configuration to the provided streams.
      Parameters:
      aStdStream - The OutputStream to write all unproblematic logs with levels such as Level.INFO or Level.CONFIG to.
      aErrStream - The OutputStream to write all problematic logs with levels such as Level.WARNING or Level.SEVERE to.
      aLevel - The log Level as of which to log to the provided streams.
    • toFormatted

      public static String toFormatted(String aMessage, Object... aArguments)
      Wraps MessageFormat.format(String, Object...) to prevent failing when message cannot be formatted.
      Parameters:
      aMessage - the message
      aArguments - the arguments
      Returns:
      the string
    • toBootstrapStandardOut

      public static PrintStream toBootstrapStandardOut()
      Returns the original PrintStream for STDOUT as initialized by the JVM at bootstrapping time (the System.out PrintStream may have got modified by libraries or frameworks). The returned PrintStream's PrintStream.close() method is disabled for preventing the STD OUT stream to be closed upon an auto close try( ... ) { ... } statement!
      Returns:
      The real PrintStream as initially set.
    • toBootstrapStandardError

      public static PrintStream toBootstrapStandardError()
      Returns the original PrintStream for STDERR as initialized by the JVM at bootstrapping time (the System.err PrintStream may have got modified by libraries or frameworks in the meanwhile). The returned PrintStream's PrintStream.close() method is disabled for preventing the STD OUT stream to be closed upon an auto close try( ... ) { ... } statement!
      Returns:
      The real PrintStream as initially set.
    • toBootstrapStandardIn

      public static InputStream toBootstrapStandardIn()
      Returns the original InputStream for STDIN as initialized by the JVM at bootstrapping time (the System.in InputStream may have got modified by libraries or frameworks in the meanwhile). The returned InputStream's InputStream.close() method is disabled for preventing the STD OUT stream to be closed upon an auto close try( ... ) { ... } statement!
      Returns:
      The real InputStream as initially set.
    • toSystemOut

      public static PrintStream toSystemOut()
      Create a PrintStream from System.out being UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such. The returned PrintStream's PrintStream.close() method is disabled for preventing the STD OUT stream to be closed upon an auto close try( ... ) { ... } statement!
      Returns:
      The System.out PrintStream accordingly wrapped.
    • toSystemErr

      public static PrintStream toSystemErr()
      Create a PrintStream from System.err being UTF-8 encoded and filtering out ANSI escape codes in case the underlying terminal does not support the such. The returned PrintStream's PrintStream.close() method is disabled for preventing the STD OUT stream to be closed upon an auto close try( ... ) { ... } statement!
      Returns:
      The System.err PrintStream accordingly wrapped.
    • 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 String toStackTrace()
      Gets the stack trace for the current thread.
      Returns:
      The stack trace from the current thread.
    • getCallerStackTraceElement

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

      public static StackTraceElement getCallerStackTraceElement(Class<?> aCallee)
      Returns the 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!
      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 StackTraceElement getCallerStackTraceElement(StackTraceElement aCallee)
      Returns the 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!
      Parameters:
      aCallee - The callee StackTraceElement 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 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.
      Parameters:
      aCallees - the callees
      Returns:
      the caller StackTraceElement
    • getCallerStackTraceElement

      public 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.
      Parameters:
      aCallees - the callees
      Returns:
      the caller StackTraceElement
    • getCallerStackTraceElement

      public 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.
      Parameters:
      aCalleeClassNames - the callee class names
      Returns:
      the caller StackTraceElement
    • getCallerStackTraceElement

      public static StackTraceElement getCallerStackTraceElement(String aCalleeClassName)
      Returns the 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.
      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.
    • toBestCallerStackTraceElement

      public static StackTraceElement toBestCallerStackTraceElement(String aCalleeClassName)
      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.*". 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.
    • toBestCallerStackTraceElement

      public 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.*".
      Parameters:
      aCallee - The callee StackTraceElement 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.
    • toBestCallerStackTraceElement

      public 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.*".
      Parameters:
      aCallee - The callee StackTraceElement 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.
    • toBestCallerStackTraceElement

      public 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.*". The callees can also be a package namespace where the matchee's must begin with the given package namespace.
      Parameters:
      aCallees - The callees names for which 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.
    • toBestCallerStackTraceElement

      public 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.*".
      Parameters:
      aCallees - The callees StackTraceElement instances for which 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.
    • toBestCallerStackTraceElement

      public 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.*".
      Parameters:
      aCallees - The callees Class instances for which 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.
    • getCallerType

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

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

      public static Class<?> getCallerType(String aCallee)
      Returns the type of the (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.
      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 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.
      Parameters:
      aCallees - the callees
      Returns:
      the caller type
    • toClass

      public static Class<?> toClass(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 String toMethodName(StackTraceElement aStackTraceElement)
      Retrieves the method name from a StackTraceElement.
      Parameters:
      aStackTraceElement - The StackTraceElement from which to retrieve the method name.
      Returns:
      The method name or null in case the StackTraceElement was null.
    • toClassName

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

      public static String toFullyQualifiedClassName(StackTraceElement aStackTraceElement)
      Retrieves the fully qualified class name from a StackTraceElement.
      Parameters:
      aStackTraceElement - The StackTraceElement 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 String toFullyQualifiedMethodName(StackTraceElement aStackTraceElement)
      Retrieves the fully qualified method name from a StackTraceElement. This adds the method name to the fully qualified path name separated by a hash "#".
      Parameters:
      aStackTraceElement - The StackTraceElement 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 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 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 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 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 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 String toFullyQualifiedPackageName(StackTraceElement aStackTraceElement)
      Retrieves the fully qualified package name from a StackTraceElement.
      Parameters:
      aStackTraceElement - The StackTraceElement from which to retrieve the fully qualified package name.
      Returns:
      The fully qualified package name.
    • toClassName

      public 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).
      Parameters:
      aFullyQualifiedClassName - The fully qualified class name.
      Returns:
      The class name without the package declaration.
    • toFullyQualifiedPackageName

      public 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).
      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 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:
      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 String toString(String aToString, 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.
    • annotations

      public static Set<Annotation> annotations(Class<?> aClass)
      Gatherers all annotations annotating a provided Class.
      Parameters:
      aClass - the Class for which to gather all annotations.
      Returns:
      the Set containing all according annotations annotating the given Class
    • annotations

      public static Set<Annotation> annotations(Field aField)
      Gatherers all annotations annotating a provided Field.
      Parameters:
      aField - the Field for which to gather all annotations.
      Returns:
      the Set containing all according annotations annotating the given Field
    • annotations

      public static Set<Annotation> annotations(Method aMethod)
      Gatherers all annotations annotating a provided Method.
      Parameters:
      aMethod - the Method for which to gather all annotations.
      Returns:
      the Set containing all according annotations annotating the given Method
    • annotations

      public static Set<Annotation> annotations(Object aObj)
      Gatherers all annotations annotating a provided Object.
      Parameters:
      aObj - the Object for which to gather all annotations.
      Returns:
      the Set containing all according annotations annotating the given Object
    • findAnnotation

      public static Annotation findAnnotation(Class<? extends Annotation> aAnnotation, Class<?> aClass)
      Seeks for an Annotation of the given type annotating a provided Class, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aClass - the Class which's annotations to crawl.
      Returns:
      the Annotation annotating the given Class or null if none was found
    • findAnnotation

      public static Annotation findAnnotation(Class<? extends Annotation> aAnnotation, Field aField)
      Seeks for an Annotation of the given type annotating a provided Field, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aField - the Field which's annotations to crawl.
      Returns:
      the Annotation annotating the given Field or null if none was found
    • findAnnotation

      public static Annotation findAnnotation(Class<? extends Annotation> aAnnotation, Method aMethod)
      Seeks for an Annotation of the given type annotating a provided Method, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aMethod - the Method which's annotations to crawl.
      Returns:
      the Annotation annotating the given Method or null if none was found
    • findAnnotation

      public static Annotation findAnnotation(Class<? extends Annotation> aAnnotation, Object aObj)
      Seeks for an Annotation of the given type annotating a provided Object, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aObj - the Object which's annotations to crawl.
      Returns:
      the Annotation annotating the given Object or null if none was found
    • hasAnnotation

      public static boolean hasAnnotation(Class<? extends Annotation> aAnnotation, Class<?> aClass)
      Tests if an Annotation of the given type annotates a provided Class, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aClass - the Class which's annotations to crawl.
      Returns:
      True in case the Annotation annotates the given Class.
    • hasAnnotation

      public static boolean hasAnnotation(Class<? extends Annotation> aAnnotation, Field aField)
      Tests if an Annotation of the given type annotates a provided Field, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aField - the Field which's annotations to crawl.
      Returns:
      True in case the Annotation annotates the given Field.
    • hasAnnotation

      public static boolean hasAnnotation(Class<? extends Annotation> aAnnotation, Method aMethod)
      Tests if an Annotation of the given type annotates a provided Method, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aMethod - the Method which's annotations to crawl.
      Returns:
      True in case the Annotation annotates the given Method.
    • hasAnnotation

      public static boolean hasAnnotation(Class<? extends Annotation> aAnnotation, Object aObj)
      Tests if an Annotation of the given type annotates a provided Object, also reckoning annotations nested inside annotations (recursively).
      Parameters:
      aAnnotation - the Annotation type for which to seek.
      aObj - the Object which's annotations to crawl.
      Returns:
      True in case the Annotation annotates the given Object.
    • hasGetterAttribute

      public static boolean hasGetterAttribute(Object anObject, String anAttributeName, Class<?> aReturnType)
      This method tests whether the given java beans getter attribute is found for the given object.
      Parameters:
      anObject - The object which is to be tested.
      anAttributeName - The attribute name.
      aReturnType - The expected (sub-) aClass of the return value
      Returns:
      True if the object has a method with the given java beans name which returns the given (super-) aClass.
    • hasSetterAttribute

      public static boolean hasSetterAttribute(Object anObject, String anAttributeName, Class<?> anAttributeType)
      This method tests whether the given java beans setter attribute is found for the given object.
      Parameters:
      anObject - The object which is to be tested.
      anAttributeName - The attribute name.
      anAttributeType - The expected (sub-) aClass of the attribute
      Returns:
      True if the object has a method with the given java beans name which can be called with an argument of the given aClass.
    • setAttribute

      public static void setAttribute(Object anObject, org.refcodes.struct.Attribute anAttributeValueStruct) throws NoSuchMethodException
      This method sets a java beans attribute for the given object.
      Parameters:
      anObject - The object which's attribute is to be set.
      anAttributeValueStruct - The name-to-value struct for the attrubute to be set.
      Throws:
      NoSuchMethodException - Description of the Exception
    • setAttribute

      public static void setAttribute(Object anObject, String anAttributeName, Object anAttributeValue) throws NoSuchMethodException
      This method sets a java beans attribute for the given object. The corresponding java beans method must begin with a 'set', the following letter must be in upper case and it must only take one argument being of the same (or super-) aClass as the attribute's aClass.
      Parameters:
      anObject - The object which's java beans method is to be called.
      anAttributeName - The attribute name of the java beans method.
      anAttributeValue - The value of the attribute to be set.
      Throws:
      NoSuchMethodException - Description of the Exception
    • containsDir

      @SafeVarargs protected static boolean containsDir(File aDir, List<File>... aDirs)
      Contains dir.
      Parameters:
      aDir - the dir
      aDirs - the dirs
      Returns:
      true, if successful
    • toBeanAttributes

      public static org.refcodes.struct.Attribute[] toBeanAttributes(Object anObject)
      This method uses reflection on order to analyze a given object. The java beans attributes and their values are retrieved and returned in an array of name-to-value pairs.
      Parameters:
      anObject - The object to be analyzed.
      Returns:
      An array of objects containing the java beans name-to-value pairs.