Class DynamicObject

java.lang.Object
ortus.boxlang.runtime.interop.DynamicObject
All Implemented Interfaces:
IReferenceable

public class DynamicObject extends Object implements IReferenceable
This class is used to represent a BX/Java Class and invoke methods on classes using invoke dynamic. This class is not in charge of casting the results. That is up to the caller to determine. We basically just invoke and return the results! To create a new class invoker you can use the following:


 ClassInvoker target = new ClassInvoker( String.class );
 ClassInvoker target = ClassInvoker.of( String.class );
 ClassInvoker target = new ClassInvoker( new String() );
 ClassInvoker target = ClassInvoker.of( new String() );
 
 
You can then use the following methods to invoke methods on the class: - invokeConstructor( Object... args ) - Invoke a constructor on the class, and store the instance for future method calls - invokeStaticMethod( String methodName, Object... args ) - Invoke a static method on the class - invoke( String methodName, Object... args ) - Invoke a method on the instance of the class
  • Field Details

    • CLASS_UTILS

      public static final Class<org.apache.commons.lang3.ClassUtils> CLASS_UTILS
      Helper for all class utility methods from apache commons lang 3
    • EMPTY_ARGS

      public static final Object[] EMPTY_ARGS
      Empty arguments array
  • Constructor Details

    • DynamicObject

      public DynamicObject(Class<?> targetClass)
      Create a new class invoker for the given class
      Parameters:
      targetClass - The class to create the invoker for
    • DynamicObject

      public DynamicObject(Class<?> targetClass, IBoxContext context)
      Create a new class invoker for the given class
      Parameters:
      targetClass - The class to create the invoker for
      context - The context to use for the invoker
    • DynamicObject

      public DynamicObject(Object targetInstance)
      Create a new class invoker for the given instance
      Parameters:
      targetInstance - The instance to create the invoker for
  • Method Details

    • of

      public static DynamicObject of(Class<?> targetClass, IBoxContext context)
      Static factory method to create a new class invoker for the given class. Mostly used for nice fluent chaining
      Parameters:
      targetClass - The class to create the invoker for
      context - The context to use for the invoker
      Returns:
      The class invoker
    • of

      public static DynamicObject of(Class<?> targetClass)
      Static factory method to create a new class invoker for the given class. Mostly used for nice fluent chaining
      Parameters:
      targetClass - The class to create the invoker for
      Returns:
      The class invoker
    • of

      public static DynamicObject of(Object targetInstance)
      Static factory method to create a new class invoker for the given instance. Mostly used for nice fluent chaining
      Parameters:
      targetInstance - The instance to create the invoker for
      Returns:
      The class invoker
    • getTargetClass

      public Class<?> getTargetClass()
      Returns:
      the targetClass
    • setTargetClass

      public DynamicObject setTargetClass(Class<?> targetClass)
      Parameters:
      targetClass - the targetClass to set
      Returns:
      The Dynamic Object
    • getTargetInstance

      public Object getTargetInstance()
      Returns:
      the targetInstance
    • setTargetInstance

      public DynamicObject setTargetInstance(Object targetInstance)
      Parameters:
      targetInstance - the targetInstance to set
      Returns:
      The Dynamic Object
    • invokeConstructor

      public DynamicObject invokeConstructor(IBoxContext context, Object... args)
      Invokes the constructor for the class with the given arguments and stores the instance of the object into the targetInstance property for future method calls.
      Parameters:
      args - The arguments to pass to the constructor
      Returns:
      The instance of the class
    • invokeConstructor

      public DynamicObject invokeConstructor(IBoxContext context, Map<Key,Object> args)
      Invokes the constructor for the class with the given arguments and stores the instance of the object into the targetInstance property for future method calls.
      Parameters:
      args - The arguments to pass to the constructor
      Returns:
      The instance of the class
    • invokeConstructor

      public DynamicObject invokeConstructor(IBoxContext context)
      Invokes the no-arg constructor for the class with the given arguments and stores the instance of the object into the targetInstance property for future method calls.
      Returns:
      The instance of the class
    • invoke

      public Object invoke(IBoxContext context, String methodName, Object... arguments)
      Invoke can be used to invoke public methods on instances, or static methods on classes/interfaces. If it's determined that the method handle is static, then the target instance is ignored. If it's determined that the method handle is not static, then the target instance is used.
      Parameters:
      context - The context to use for the invoker
      methodName - The name of the method to invoke
      arguments - The arguments to pass to the method
      Returns:
      The result of the method invocation
    • invokeStatic

      public Object invokeStatic(IBoxContext context, String methodName, Object... arguments)
      Invokes a static method with the given name and arguments on a class or an interface
      Parameters:
      context - The context to use for the invoker
      methodName - The name of the method to invoke
      arguments - The arguments to pass to the method
      Returns:
      The result of the method invocation
    • getField

      public Optional<Object> getField(String fieldName)
      Get the value of a public or public static field on a class or instance
      Parameters:
      fieldName - The name of the field to get
      Returns:
      The value of the field wrapped in an Optional
    • getField

      public Optional<Object> getField(String fieldName, Object defaultValue)
      Get the value of a public or public static field on a class or instance but if it doesn't exist return the default value passed in.
      Parameters:
      fieldName - The name of the field to get
      defaultValue - The default value to return if the field doesn't exist
      Returns:
      The value of the field or the default value wrapped in an Optional
    • setField

      public DynamicObject setField(String fieldName, Object value)
      Set the value of a public or public static field on a class or instance
      Parameters:
      fieldName - The name of the field to set
      value - The value to set the field to
      Returns:
      The class invoker
    • findField

      public Field findField(String fieldName)
      Find a field by name with no case-sensitivity (upper case) in the class
      Parameters:
      fieldName - The name of the field to find
      Returns:
      The field if discovered
    • hasField

      public Boolean hasField(String fieldName)
      Verifies if the class has a public or public static field with the given name
      Parameters:
      fieldName - The name of the field to check
      Returns:
      True if the field exists, false otherwise
    • hasFieldNoCase

      public Boolean hasFieldNoCase(String fieldName)
      Verifies if the class has a public or public static field with the given name and no case-sensitivity (upper case)
      Parameters:
      fieldName - The name of the field to check
      Returns:
      True if the field exists, false otherwise
    • getFields

      public Field[] getFields()
      Get an array of fields of all the public fields for the given class
      Returns:
      The fields in the class
    • getFieldsAsStream

      public Stream<Field> getFieldsAsStream()
      Get a stream of fields of all the public fields for the given class
      Returns:
      The stream of fields in the class
    • getFieldNames

      public List<String> getFieldNames()
      Get a list of field names for the given class with case-sensitivity
      Returns:
      A list of field names
    • getFieldNamesNoCase

      public List<String> getFieldNamesNoCase()
      Get a list of field names for the given class with no case-sensitivity (upper case)
      Returns:
      A list of field names
    • getMethod

      public Method getMethod(String name)
      Get a method by name for the given class
      Returns:
      The method object
    • getMethods

      public Set<Method> getMethods()
      Get a HashSet of methods of all the unique callable method signatures for the given class
      Returns:
      A unique set of callable methods
    • getMethodsAsStream

      public Stream<Method> getMethodsAsStream()
      Get a stream of methods of all the unique callable method signatures for the given class
      Returns:
      A stream of unique callable methods
    • getMethodNames

      public List<String> getMethodNames()
      Get a list of method names for the given class
      Returns:
      A list of method names
    • getMethodNamesNoCase

      public List<String> getMethodNamesNoCase()
      Get a list of method names for the given class with no case-sensitivity (upper case)
      Returns:
      A list of method names with no case
    • hasMethod

      public Boolean hasMethod(String methodName)
      Verifies if the class has a public or public static method with the given name
      Parameters:
      methodName - The name of the method to check
      Returns:
      True if the method exists, false otherwise
    • hasMethodNoCase

      public Boolean hasMethodNoCase(String methodName)
      Verifies if the class has a public or public static method with the given name and no case-sensitivity (upper case)
      Parameters:
      methodName - The name of the method to check
      Returns:
      True if the method exists, false otherwise
    • findMatchingMethod

      public Method findMatchingMethod(IBoxContext context, String methodName, Class<?>[] argumentsAsClasses, Object... arguments)
      This method is used to verify if the class has the same method signature as the incoming one with no case-sensitivity (upper case)
      Parameters:
      context - The context to use
      methodName - The name of the method to check
      argumentsAsClasses - The parameter types of the method to check
      arguments - The arguments to pass to the method
      Returns:
      The matched method signature. If not found and safe is true, it returns null, otherwise it throws an exception
      Throws:
      NoMethodException - If the method is not found and safe is false
    • toMethodHandle

      public static MethodHandle toMethodHandle(Method method)
      Utility method to convert a method to a method handle
      Parameters:
      method - The method to convert
      Returns:
      The method handle representing the method or an exception if it fails
    • isInterface

      public boolean isInterface()
      Verifies if the target calss is an interface or not
      Returns:
    • argumentToClass

      public static Class<?> argumentToClass(Object thisArg)
      Converts the argument(s) to a class representation according to Java casting rules
      Parameters:
      thisArg - The argument to convert
      Returns:
      The class representation of the argument
    • argumentsToClasses

      public static Class<?>[] argumentsToClasses(Object... args)
      Converts the arguments to an array of classes
      Parameters:
      args - The arguments to convert
      Returns:
      The array of classes
    • unWrap

      public static Object unWrap(Object param)
      Unwrap an object if it's inside a ClassInvoker instance
      Parameters:
      param - The object to unwrap
      Returns:
      The target instance or class, depending which one is set
    • unWrap

      public Object unWrap()
      Instance method to unwrap itself
      Returns:
      The target instance or class, depending which one is set
    • unWrapBoxLangClass

      public Object unWrapBoxLangClass()
      Instance method to unwrap itself if it's a BoxLang class
      Returns:
      The target instance or class, depending which one is set
    • hasInstance

      public Boolean hasInstance()
      Verifies if the class invoker has an instance or not
      Returns:
      True if it has an instance, false otherwise
    • dereference

      public Object dereference(IBoxContext context, Key name, Boolean safe)
      Dereference this object by a key and return the value, or throw exception
      Specified by:
      dereference in interface IReferenceable
      Parameters:
      context - The context we're executing inside of
      name - The name of the key to dereference
      safe - If true, return null if the method is not found, otherwise throw an exception
      Returns:
      The requested object
    • dereferenceAndInvoke

      public Object dereferenceAndInvoke(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe)
      Dereference this object by a key and invoke the result as an invokable (UDF, java method)
      Specified by:
      dereferenceAndInvoke in interface IReferenceable
      Parameters:
      context - The context we're executing inside of
      name - The name of the key to dereference, which becomes the method name
      positionalArguments - The arguments to pass to the invokable
      safe - If true, return null if the method is not found, otherwise throw an exception
      Returns:
      The requested return value or null
    • dereferenceAndInvoke

      public Object dereferenceAndInvoke(IBoxContext context, Key name, Map<Key,Object> namedArguments, Boolean safe)
      Dereference this object by a key and invoke the result as an invokable (UDF, java method)
      Specified by:
      dereferenceAndInvoke in interface IReferenceable
      Parameters:
      context - The context we're executing inside of
      name - The name of the key to dereference, which becomes the method name
      namedArguments - The arguments to pass to the invokable
      safe - If true, return null if the method is not found, otherwise throw an exception
      Returns:
      The requested return value or null
    • assign

      public Object assign(IBoxContext context, Key name, Object value)
      Assign a value to a field
      Specified by:
      assign in interface IReferenceable
      Parameters:
      context - The context we're executing inside of
      name - The name of the field to assign
      value - The value to assign
      Returns:
      The value that was assigned
    • equals

      public boolean equals(Object obj)
      Equals override. Tests if the target class or instance is equal to the other object
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Hashcode override. Returns the hashcode of the target class or instance If both are null, then we return 0
      Overrides:
      hashCode in class Object