Record Class JsCall

java.lang.Object
java.lang.Record
com.vaadin.flow.js.JsCall
Record Components:
definitionType - the JavaScript definition the call was made on
methodName - the name of the called method
arguments - the arguments of the call, in declaration order, any of which may be null
All Implemented Interfaces:
Serializable

public record JsCall(Class<?> definitionType, String methodName, List<Object> arguments) extends Record implements Serializable
A call made through Element.executeJs(Class): which definition interface, which method of it, and the arguments that were passed.

The call is what is scheduled, and what a driver of the client side that can not run JavaScript sees in the pending invocation queue. A browser is sent less than this: the identifier of the function to run and the arguments, never the JavaScript itself, which it looks up in the bundle, and never the interface or the method, which stay on the server. Such a driver can dispatch on the interface and the method, or hand the call to an implementation of the same interface with invokeOn(Object) and let Java dispatch it:

 if (call.definitionType() == FocusJs.class) {
     call.invokeOn(new FocusSimulation(Element.get(pending.getOwner())));
 }
 

A call of a method that declares a JsSnippet, made through the implementation that JsSnippets.of(Class) hands out, is a call too, but it is not scheduled and runs nothing: it is what the snippet it answers with carries. Its function is the one of the snippet registry that the client calls with the arguments to get the value of the snippet, and isSnippet() tells the two apart.

Since:
25.4
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsCall(Class<?> definitionType, String methodName, List<Object> arguments)
    Creates a call of the given method of the given JavaScript definition.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value of the arguments record component.
    Creates a short key that identifies a snippet created by this call: its function and its captures.
    Returns the value of the definitionType record component.
    tools.jackson.databind.node.ArrayNode
    Encodes the arguments of this call as the captures of a snippet.
    final boolean
    Indicates whether some other object is "equal to" this one.
    Gets the arguments of this call as the client receives them, which spreads the array that a variadic call collects its trailing arguments into.
    static String
    Gets the identifier of the function that the given method of a JavaScript definition declares.
    static String
    functionId(String expression, int parameterCount, boolean variadic)
    Gets the identifier of the function that runs the given JavaScript with the given parameters, which is the key the generated bundle registers that function under and the only thing the client is told about a call.
    Gets the JavaScript that this call runs in a browser, as declared by JsExpression on the called method.
    Gets the identifier of the function of this call, which is what the client looks the JavaScript up in the bundle by: the function that runs the call, or for a snippet the function that produces the value of the snippet from the arguments.
    Gets the names of the parameters that the function a snippet call declares is called with.
    static List<String>
    Gets the names of the parameters that the function a snippet evaluates to is called with.
    final int
    Returns a hash code value for this object.
    boolean
    Checks whether all captures of a snippet created by this call are plain values: strings, numbers of the boxed primitive types, BigInteger or BigDecimal, booleans, characters, enums or null.
    invokeOn(Object implementation)
    Runs this call on an implementation of the JavaScript definition, which is how a driver of the client side reproduces it without running the JavaScript.
    boolean
    Whether the called method declares a JsSnippet, which a call of it answers with rather than running JavaScript.
    boolean
    Whether the called method collects a variable number of arguments into its last parameter.
    Returns the value of the methodName record component.
    parametersFor(@Nullable Element runOn)
    Gets what this call is sent with: its arguments as the client receives them, and then the element to run the function on, which the client applies the function to.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • JsCall

      public JsCall(Class<?> definitionType, String methodName, List<Object> arguments)
      Creates a call of the given method of the given JavaScript definition.
      Parameters:
      definitionType - the JavaScript definition, not null
      methodName - the name of the called method, not null
      arguments - the arguments of the call, not null
  • Method Details

    • functionId

      public static String functionId(String expression, int parameterCount, boolean variadic)
      Gets the identifier of the function that runs the given JavaScript with the given parameters, which is the key the generated bundle registers that function under and the only thing the client is told about a call.

      A hash of the JavaScript, so that the name of the Java that declared it stays on the server. What the parameters of the generated function are made of is hashed with it, so that two methods declaring the same JavaScript are the same function only when it runs the same way: for the same number of parameters, and with the last one collecting a variable number of arguments in both or in neither.

      Parameters:
      expression - the declared JavaScript, not null
      parameterCount - the number of parameters of the declaring method
      variadic - whether its last parameter collects a variable number of arguments
      Returns:
      the function identifier, not null
    • functionId

      public static String functionId(Method method)
      Gets the identifier of the function that the given method of a JavaScript definition declares.

      For a method that runs its JavaScript when it is called, this is functionId(String, int, boolean). For a method that returns a JsSnippet, the generated function is not run by the call but evaluated later by what the snippet is passed to, and it is generated differently: it is not async, and it wraps the expression in a function taking the JsFunctionParameterNames parameters. What sets it apart is hashed with the JavaScript, so a snippet and a call that declare the same JavaScript are different functions, and so are two snippets called with different parameters. A method annotated with JsWithExecutionContext is generated with the execution context as a leading parameter, and is told apart the same way.

      Parameters:
      method - a method annotated with JsExpression, not null
      Returns:
      the function identifier, not null
      Throws:
      IllegalArgumentException - if the method is not annotated with JsExpression, or declares parameter names that can not be generated
    • getFunctionParameterNames

      public static List<String> getFunctionParameterNames(Method method)
      Gets the names of the parameters that the function a snippet evaluates to is called with. They come from JsFunctionParameterNames on the given method. If the method has no such annotation, they come from the annotation on the snippet type that the method returns.

      The names become parameters of the generated function, so each of them is checked to be one that a function in a JavaScript module can declare and that does not hide one of the captured arguments.

      Parameters:
      method - a method of a JavaScript definition, not null
      Returns:
      the parameter names in the order they are passed, empty when neither the method nor the snippet type it returns is annotated with JsFunctionParameterNames
      Throws:
      IllegalArgumentException - if a name is not a JavaScript identifier, is a reserved word, names a captured argument or is declared twice
    • getFunctionId

      public String getFunctionId()
      Gets the identifier of the function of this call, which is what the client looks the JavaScript up in the bundle by: the function that runs the call, or for a snippet the function that produces the value of the snippet from the arguments.
      Returns:
      the function identifier, not null
    • getFunctionParameterNames

      public List<String> getFunctionParameterNames()
      Gets the names of the parameters that the function a snippet call declares is called with.
      Returns:
      the parameter names, empty when the called method declares none
      See Also:
    • isSnippet

      public boolean isSnippet()
      Whether the called method declares a JsSnippet, which a call of it answers with rather than running JavaScript.
      Returns:
      true when the called method returns a snippet type
    • isVariadic

      public boolean isVariadic()
      Whether the called method collects a variable number of arguments into its last parameter.

      The generated function collects them into a rest parameter, which does not count towards the length of the function, so the client is told how many arguments a call of such a function carries rather than reading it off the function.

      Returns:
      true when the called method is variadic
    • encodeSnippetCaptures

      public tools.jackson.databind.node.ArrayNode encodeSnippetCaptures()
      Encodes the arguments of this call as the captures of a snippet. The captures are sent to the client together with the snippet.

      A snippet calls this when it is created. So an error is thrown right away, not later when the snippet is used, in these cases:

      • the method does not declare a snippet, or
      • an argument cannot be sent to the client.
      A snippet that is sent more than once, such as one that an event listener uses, calls this again each time it is sent. An element is then sent with the id that it has at that time.
      Returns:
      the encoded captures, not null
      Throws:
      IllegalArgumentException - if the called method does not declare a JsSnippet
      RuntimeException - if any argument cannot be encoded to be sent to the client
    • createSnippetKey

      public String createSnippetKey()
      Creates a short key that identifies a snippet created by this call: its function and its captures. Two snippets with the same key are treated as the same.

      If all captures are plain values (see hasOnlyPlainSnippetCaptures()), the key is made from the function and the values. Two calls of the same method with equal plain values then have the same key.

      Any other capture, for example an element, may be encoded differently each time the snippet is sent: an element gets a new id when it moves to another UI. So for such captures, the key is made from a random value, and it is different for each call of this method.

      Returns:
      the key of the snippet, not null
      Throws:
      IllegalArgumentException - if the called method does not declare a JsSnippet
      RuntimeException - if any argument cannot be encoded to be sent to the client
    • hasOnlyPlainSnippetCaptures

      public boolean hasOnlyPlainSnippetCaptures()
      Checks whether all captures of a snippet created by this call are plain values: strings, numbers of the boxed primitive types, BigInteger or BigDecimal, booleans, characters, enums or null. Plain values are encoded in the same way each time the snippet is sent. It is also true when there are no captures.
      Returns:
      true if all captures are plain values
    • flattenArguments

      public List<Object> flattenArguments()
      Gets the arguments of this call as the client receives them, which spreads the array that a variadic call collects its trailing arguments into.

      arguments() holds what Java passed, where a variadic tail is a single array argument. The browser gets each of its values on its own, so that an element, a return channel or a function among them is sent as the reference it is rather than as part of a nested array.

      Returns:
      the arguments as the client receives them, not null
    • parametersFor

      public Object[] parametersFor(@Nullable Element runOn)
      Gets what this call is sent with: its arguments as the client receives them, and then the element to run the function on, which the client applies the function to.

      The element a call was made on goes into that last place, and a call made on nothing in particular - page JavaScript, which works on globals - puts null there, so a client reads the two the same way.

      Parameters:
      runOn - the element to run the function on, or null for page JavaScript, which runs on nothing in particular
      Returns:
      the parameters of the call, not null
    • getExpression

      public String getExpression()
      Gets the JavaScript that this call runs in a browser, as declared by JsExpression on the called method.

      The expression is not sent to the client, which runs the function that the build generated from the same declaration. It is available here for the server side, for instance for a test that asserts what a browser would run.

      Returns:
      the JavaScript expression, not null
    • invokeOn

      public Object invokeOn(Object implementation)
      Runs this call on an implementation of the JavaScript definition, which is how a driver of the client side reproduces it without running the JavaScript.
      Parameters:
      implementation - an implementation of definitionType(), not null
      Returns:
      the value returned by the implementation, or null for a void method
      Throws:
      IllegalArgumentException - if the implementation does not implement definitionType()
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • definitionType

      public Class<?> definitionType()
      Returns the value of the definitionType record component.
      Returns:
      the value of the definitionType record component
    • methodName

      public String methodName()
      Returns the value of the methodName record component.
      Returns:
      the value of the methodName record component
    • arguments

      public List<Object> arguments()
      Returns the value of the arguments record component.
      Returns:
      the value of the arguments record component