Class JsStringFallbacks

java.lang.Object
com.vaadin.flow.internal.JsStringFallbacks

public final class JsStringFallbacks extends Object
Matches JavaScript that is sent as a string against the JsStringFallback interfaces of the application, so that the browser runs a function from the bundle instead of compiling the string.

The build checks the marked interfaces with collectFallbacks(java.util.Collection<java.lang.Class<?>>) and lists their names in Constants.JS_STRING_FALLBACKS_FILE. The list is read the first time a service matches a string, which only happens in the modes that report string JavaScript.

A string matches what is declared for it when the text is the same, and for a call or a function also the number of parameters: the same identity that JsCall.functionId(String, int, boolean), JsCall.snippetFunctionId(String, List, int, boolean) and BundledEventData.getFunctionId(String) give the function in the bundle.

For internal use only. May be renamed or removed in a future release.

  • Method Details

    • collectFallbacks

      public static JsStringFallbacks.Fallbacks collectFallbacks(Collection<Class<?>> definitionTypes)
      Checks the fallback interfaces and collects what they declare.
      Parameters:
      definitionTypes - the interfaces marked with JsStringFallback, not null
      Returns:
      what the interfaces declare, not null
      Throws:
      IllegalArgumentException - if a type is not a JsDefinition, declares something that no string can match, or two methods declare the same JavaScript with the same parameter count
    • findDeclaredCall

      public static @Nullable JsCall findDeclaredCall(String expression, @Nullable Object[] parameters)
      Finds the declared call that stands in for an executeJs call with a string expression, in the service of the current request.
      Parameters:
      expression - the JavaScript the string call sends, not null
      parameters - the parameters of the string call, not null
      Returns:
      the declared call to send instead, or null if the application declared nothing for the string, or the mode does not report string JavaScript
    • findDeclaredFunction

      public static @Nullable JsCall findDeclaredFunction(String body, @Nullable Object[] captures)
      Finds the declared call of a method returning a JsFunction that stands in for a function created from a string body, in the service of the current request.
      Parameters:
      body - the body of the function, not null
      captures - the values the function captures, not null
      Returns:
      the call to create the declared function with, or null if the application declared nothing for the body, or the mode does not report string JavaScript
    • isDeclaredFilter

      public static boolean isDeclaredFilter(String filter)
      Checks whether the application declared a filter, so that the bundle has a function for it, in the service of the current request.
      Parameters:
      filter - the filter, not null
      Returns:
      true if the filter is declared and the mode reports string JavaScript
    • isDeclaredEventData

      public static boolean isDeclaredEventData(String eventData)
      Checks whether the application declared an event data value, so that the bundle has a function for it, in the service of the current request.
      Parameters:
      eventData - the event data value, possibly mapped to an element, not null
      Returns:
      true if the value is declared and the mode reports string JavaScript
    • reloadFallbacks

      public static void reloadFallbacks(VaadinService service)
      Reads the list again for a service, after a class change updated it while the application runs.
      Parameters:
      service - the service, not null
    • isStringFallbackCall

      public static boolean isStringFallbackCall(Class<?> definitionType, Method method)
      Checks whether the given method of the given definition runs JavaScript in place of an executeJs string, which is the case for a method that does not return a snippet of a JsStringFallback interface, whether the interface declares the method or inherits it.

      Such a method is generated so that its JavaScript sees what the string would see: Element.executeJs(String, Object...) gives the element to the string as one more parameter after the arguments, so a string can use $0 as the element when it has no arguments. Since it is generated differently, its function has an identifier of its own, see functionId(Class, Method).

      Parameters:
      definitionType - the definition the method is called through, not null
      method - a method of the definition that declares JavaScript, not null
      Returns:
      true if the method stands in for a string call
    • functionId

      public static String functionId(Class<?> definitionType, Method method)
      Gets the identifier of the function that the given method of the given definition registers in the generated bundle.

      This is JsCall.functionId(Method), except for a method that stands in for a string call: it is generated differently, so it has an identifier of its own and never replaces the function of another definition that declares the same JavaScript, such as an interface it inherits the method from.

      Parameters:
      definitionType - the definition the method is called through, not null
      method - a method of the definition that declares JavaScript, not null
      Returns:
      the function identifier, not null
    • stringFallbackBody

      public static List<String> stringFallbackBody(String expression, int parameterCount)
      Gets the body of the generated function for a method that stands in for a string call. It runs the declared JavaScript the way Element.executeJs(String, Object...) runs the string: in a function whose this is the element and which sees the element as the parameter after the arguments.
      Parameters:
      expression - the declared JavaScript, not null
      parameterCount - the number of parameters of the method
      Returns:
      the lines of the function body, not null