invokeTypeMethod

invokeTypeMethod(targetContext:    ApplicationContext;
                 targetTypeMethod: Method;
                 paramList:        ParamListType): Any;

The invokeTypeMethod method of the Type class sends the type method specified in the targetTypeMethod parameter containing a variable list of parameters specified in the paramList parameter to the receiver type instance, after switching to the execution context of the specified targetContext parameter value.

The return type of the invokeTypeMethod method allows for an optional return value from the method being called. If the called method returns a value, the Any result must be cast to the appropriate type, to access that result.

After the method has finished, the execution context switches back to the current context. For details about using this method to call user methods from packages, see "Calling User Methods from Packages", in Chapter 8 of the JADE Developer’s Reference.

The targetTypeMethod parameter must be a valid type method, which is executed when the invokeTypeMethod method is called. Use the paramList parameter to specify a variable list of parameters of any type that are passed to the type method specified in the targetTypeMethod parameter when it is executed.

If the number or type of the actual parameters passed to a method by a parameter list does not correspond exactly to the formal parameter list declaration, an exception or an unpredictable result can occur, as the compiler is unable to perform any type‑checking on the values that are passed to a parameter list. However, the Method class isCallCompatibleWith method enables you to validate the number and type of parameters.

For details about the ParamListType pseudo type, see "ParamListType" under "Pseudo Types", in Chapter 1 of the JADE Developer’s Reference. See also "Passing Variable Parameters to Methods" under "JADE Language Syntax", in Chapter 1 of the JADE Developer’s Reference.

The invokeIOTypeMethod method can be used if the method represented by the targetTypeMethod parameter takes io or output parameters.

As the application context used by invokeTypeMethod is transient, it can switch to a context only within the same process. The mechanism is not designed to call a method running in another process in the node or in another node. In addition, as the context is transient, any connection between a context and a method to be invoked must be set up again if an application is stopped and then restarted.

If you want to save events to be called persistently so that methods would still be called if the application stops and restarts (for example, in a scheduler application), you would have to re‑supply a context when the application restarts and events are loaded. The target method and object could be persistent, but the context is not.

Although the callback mechanism is designed with packages in mind, you can also use it to allow a method to be invoked from within the same context. If the context in the invokeTypeMethod call is null, the current context (that is, appContext) is used. This therefore enables you to invoke a specific saved type method (for example, MyClass::myTypeMethod) rather than calling the Type class sendTypeMsg method, which allows you to provide only the name of the type method to which the message is sent.

Within a package, the package writer may want to check that the method supplied by the user of the package is appropriate.

The Method class isCallCompatibleWith method checks that the target method supplied by the package user cannot be invoked only on the specified target object but that it has a signature that is compatible with that expected by the package. The Method class isCallCompatibleWith method has the following signature.

isCallCompatibleWith(targetObject:  Object;
                     exampleMethod: Method): Boolean;

The following is an example of the invokeTypeMethod method being called from a method in a package exporting schema, invoking a type method using the provided context, type, method, and parameters provided from the importing schema.

callUserTypeMethod(userEvent: ScheduledEvent);
begin
    if userEvent.targetContext <> null and userEvent.targetType <> null and 
                userEvent.targetMethod <> null then
        userEvent.invokeTypeMethod(userEvent.targetContext, userEvent.targetType, 
                userEvent.targetTypeMethod, userEvent.methodParams);
        userEvent.myScheduler := null;
        delete userEvent;
    endif;
end;

2020.0.01 and higher