Class JSUtils


  • public class JSUtils
    extends java.lang.Object
    A collection of utilities for common procedures in the interaction between Java and JavaScript code through Nashorn.
    Since:
    Ptolemy II 11.0
    Version:
    $Id$
    Author:
    Marten Lohstroh
    See Also:
    ScriptObjectMirror
    • Constructor Summary

      Constructors 
      Constructor Description
      JSUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T castSafely​(java.lang.Object obj, java.lang.Class<T> objClass, boolean allowEmpty)
      Attempt to safely cast the given object into a given class.
      static <T> boolean checkSafety​(java.lang.Object obj, java.lang.Class<T> objClass, boolean allowEmpty)
      Test whether a given object can be safely cast into a given class.
      static <T> boolean isSafe​(java.lang.Object obj, java.lang.Class<T> objClass)
      Test whether a given object can be safely cast into a given class.
      static <T> boolean isSafeNotEmpty​(java.lang.Object obj, java.lang.Class<T> objClass)
      Test whether a given object can be safely cast into a given class.
      • Methods inherited from class java.lang.Object

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

      • JSUtils

        public JSUtils()
    • Method Detail

      • castSafely

        public static <T> T castSafely​(java.lang.Object obj,
                                       java.lang.Class<T> objClass,
                                       boolean allowEmpty)
        Attempt to safely cast the given object into a given class. If the object is null, then the cast is considered unsafe. Additionally, if the argument allowEmpty is set to false, then only consider the cast safe if the object is not empty. If the cast is safe, return the object, return null otherwise. Note that no ClassCastException is thrown by this method. If the cast fails, it returns null.
        Parameters:
        obj - The object to be cast.
        objClass - The class to cast the object to.
        allowEmpty - Whether empty objects are desired.
        Returns:
        The object if the cast is successful, null otherwise.
      • isSafe

        public static <T> boolean isSafe​(java.lang.Object obj,
                                         java.lang.Class<T> objClass)
        Test whether a given object can be safely cast into a given class. If the object is null, then the cast is considered unsafe. If the cast is safe, return true, return false otherwise. Note that no ClassCastException is thrown by this method. If the cast fails, it returns false.
        Parameters:
        obj - The object to be cast.
        objClass - The class to cast the object to.
        Returns:
        True if the cast is successful, false otherwise.
      • isSafeNotEmpty

        public static <T> boolean isSafeNotEmpty​(java.lang.Object obj,
                                                 java.lang.Class<T> objClass)
        Test whether a given object can be safely cast into a given class. If the object is null, then the cast is considered unsafe. Moreover, only if the object is not empty the cast is considered safe. If the cast is safe, return true, return false otherwise. Note that no ClassCastException is thrown by this method. If the cast fails, it returns false.
        Parameters:
        obj - The object to be cast.
        objClass - The class to cast the object to.
        Returns:
        True if the cast is successful, false otherwise.
      • checkSafety

        public static <T> boolean checkSafety​(java.lang.Object obj,
                                              java.lang.Class<T> objClass,
                                              boolean allowEmpty)
        Test whether a given object can be safely cast into a given class. If the object is null, then the cast is considered unsafe. Additionally, if the argument allowEmpty is set to false, then only consider the cast safe if the object is not empty. If the cast is safe, return true, return false otherwise. Note that no ClassCastException is thrown by this method. If the cast fails, it returns false.
        Parameters:
        obj - The object to be cast.
        objClass - The class to cast the object to.
        allowEmpty - Whether empty objects are desired.
        Returns:
        True if the cast is successful, false otherwise.