Package ortus.boxlang.runtime.interop
Class DynamicObject
java.lang.Object
ortus.boxlang.runtime.interop.DynamicObject
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDynamicObject
(Class<?> targetClass) Create a new class invoker for the given classDynamicObject
(Class<?> targetClass, IBoxContext context) Create a new class invoker for the given classDynamicObject
(Object targetInstance) Create a new class invoker for the given instance -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>[]
argumentsToClasses
(Object... args) Converts the arguments to an array of classesstatic Class
<?> argumentToClass
(Object thisArg) Converts the argument(s) to a class representation according to Java casting rulesassign
(IBoxContext context, Key name, Object value) Assign a value to a fielddereference
(IBoxContext context, Key name, Boolean safe) Dereference this object by a key and return the value, or throw exceptiondereferenceAndInvoke
(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method)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)boolean
Equals override.Find a field by name with no case-sensitivity (upper case) in the classfindMatchingMethod
(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)Get the value of a public or public static field on a class or instanceGet 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.Get a list of field names for the given class with case-sensitivityGet a list of field names for the given class with no case-sensitivity (upper case)Field[]
Get an array of fields of all the public fields for the given classGet a stream of fields of all the public fields for the given classGet a method by name for the given classGet a list of method names for the given classGet a list of method names for the given class with no case-sensitivity (upper case)Get a HashSet of methods of all the unique callable method signatures for the given classGet a stream of methods of all the unique callable method signatures for the given classClass
<?> Verifies if the class has a public or public static field with the given namehasFieldNoCase
(String fieldName) Verifies if the class has a public or public static field with the given name and no case-sensitivity (upper case)int
hashCode()
Hashcode override.Verifies if the class invoker has an instance or notVerifies if the class has a public or public static method with the given namehasMethodNoCase
(String methodName) Verifies if the class has a public or public static method with the given name and no case-sensitivity (upper case)invoke
(IBoxContext context, String methodName, Object... arguments) Invoke can be used to invoke public methods on instances, or static methods on classes/interfaces.invokeConstructor
(IBoxContext context) Invokes the no-arg constructor for the class with the given arguments and stores the instance of the object into thetargetInstance
property for future method calls.invokeConstructor
(IBoxContext context, Object... args) Invokes the constructor for the class with the given arguments and stores the instance of the object into thetargetInstance
property for future method calls.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 thetargetInstance
property for future method calls.invokeStatic
(IBoxContext context, String methodName, Object... arguments) Invokes a static method with the given name and arguments on a class or an interfaceboolean
Verifies if the target calss is an interface or notstatic DynamicObject
Static factory method to create a new class invoker for the given class.static DynamicObject
of
(Class<?> targetClass, IBoxContext context) Static factory method to create a new class invoker for the given class.static DynamicObject
Static factory method to create a new class invoker for the given instance.Set the value of a public or public static field on a class or instancesetTargetClass
(Class<?> targetClass) setTargetInstance
(Object targetInstance) static MethodHandle
toMethodHandle
(Method method) Utility method to convert a method to a method handleunWrap()
Instance method to unwrap itselfstatic Object
Unwrap an object if it's inside a ClassInvoker instanceInstance method to unwrap itself if it's a BoxLang class
-
Field Details
-
CLASS_UTILS
Helper for all class utility methods from apache commons lang 3 -
EMPTY_ARGS
Empty arguments array
-
-
Constructor Details
-
DynamicObject
Create a new class invoker for the given class- Parameters:
targetClass
- The class to create the invoker for
-
DynamicObject
Create a new class invoker for the given class- Parameters:
targetClass
- The class to create the invoker forcontext
- The context to use for the invoker
-
DynamicObject
Create a new class invoker for the given instance- Parameters:
targetInstance
- The instance to create the invoker for
-
-
Method Details
-
of
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 forcontext
- The context to use for the invoker- Returns:
- The class invoker
-
of
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
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
- Returns:
- the targetClass
-
setTargetClass
- Parameters:
targetClass
- the targetClass to set- Returns:
- The Dynamic Object
-
getTargetInstance
- Returns:
- the targetInstance
-
setTargetInstance
- Parameters:
targetInstance
- the targetInstance to set- Returns:
- The Dynamic Object
-
invokeConstructor
Invokes the constructor for the class with the given arguments and stores the instance of the object into thetargetInstance
property for future method calls.- Parameters:
args
- The arguments to pass to the constructor- Returns:
- The instance of the class
-
invokeConstructor
Invokes the constructor for the class with the given arguments and stores the instance of the object into thetargetInstance
property for future method calls.- Parameters:
args
- The arguments to pass to the constructor- Returns:
- The instance of the class
-
invokeConstructor
Invokes the no-arg constructor for the class with the given arguments and stores the instance of the object into thetargetInstance
property for future method calls.- Returns:
- The instance of the class
-
invoke
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 invokermethodName
- The name of the method to invokearguments
- The arguments to pass to the method- Returns:
- The result of the method invocation
-
invokeStatic
Invokes a static method with the given name and arguments on a class or an interface- Parameters:
context
- The context to use for the invokermethodName
- The name of the method to invokearguments
- The arguments to pass to the method- Returns:
- The result of the method invocation
-
getField
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
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 getdefaultValue
- 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
Set the value of a public or public static field on a class or instance- Parameters:
fieldName
- The name of the field to setvalue
- The value to set the field to- Returns:
- The class invoker
-
findField
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
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
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
Get an array of fields of all the public fields for the given class- Returns:
- The fields in the class
-
getFieldsAsStream
Get a stream of fields of all the public fields for the given class- Returns:
- The stream of fields in the class
-
getFieldNames
Get a list of field names for the given class with case-sensitivity- Returns:
- A list of field names
-
getFieldNamesNoCase
Get a list of field names for the given class with no case-sensitivity (upper case)- Returns:
- A list of field names
-
getMethod
Get a method by name for the given class- Returns:
- The method object
-
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
Get a stream of methods of all the unique callable method signatures for the given class- Returns:
- A stream of unique callable methods
-
getMethodNames
Get a list of method names for the given class- Returns:
- A list of method names
-
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
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
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 usemethodName
- The name of the method to checkargumentsAsClasses
- The parameter types of the method to checkarguments
- 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
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
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
Converts the arguments to an array of classes- Parameters:
args
- The arguments to convert- Returns:
- The array of classes
-
unWrap
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
Instance method to unwrap itself- Returns:
- The target instance or class, depending which one is set
-
unWrapBoxLangClass
Instance method to unwrap itself if it's a BoxLang class- Returns:
- The target instance or class, depending which one is set
-
hasInstance
Verifies if the class invoker has an instance or not- Returns:
- True if it has an instance, false otherwise
-
dereference
Dereference this object by a key and return the value, or throw exception- Specified by:
dereference
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the key to dereferencesafe
- 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 interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the key to dereference, which becomes the method namepositionalArguments
- The arguments to pass to the invokablesafe
- 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 interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the key to dereference, which becomes the method namenamedArguments
- The arguments to pass to the invokablesafe
- If true, return null if the method is not found, otherwise throw an exception- Returns:
- The requested return value or null
-
assign
Assign a value to a field- Specified by:
assign
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the field to assignvalue
- The value to assign- Returns:
- The value that was assigned
-
equals
Equals override. Tests if the target class or instance is equal to the other object -
hashCode
public int hashCode()Hashcode override. Returns the hashcode of the target class or instance If both are null, then we return 0
-