Class Utils

java.lang.Object
com.vaadin.browserless.internal.Utils

public final class Utils extends Object
General-purpose utilities: serialization, string helpers, class lookup and accessors for the current request, response and UI.

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

  • Method Details

    • serializeToBytes

      public static byte[] serializeToBytes(Serializable obj)
      Serializes given object into a byte array using Java serialization.
      Parameters:
      obj - the object to serialize, not null.
      Returns:
      the serialized form of the object.
    • deserialize

      public static <T extends Serializable> T deserialize(byte[] bytes)
      Deserializes an object from given byte array using Java serialization.
      Type Parameters:
      T - the type of the deserialized object
      Parameters:
      bytes - the serialized form, not null.
      Returns:
      the deserialized object.
    • serializeDeserialize

      public static <T extends Serializable> T serializeDeserialize(T obj)
      Serializes given object and deserializes it back, returning the copy.
      Type Parameters:
      T - the type of the object
      Parameters:
      obj - the object to serialize, not null.
      Returns:
      a deserialized copy of the object.
    • getJvmVersion

      public static int getJvmVersion()
      Returns the major JVM version, e.g. 6 for Java 1.6, 8 for Java 8, 11 for Java 11 etc.
      Returns:
      the major JVM version.
    • parseJvmVersion

      public static int parseJvmVersion(String javaVersion)
      Returns the major JVM version, 1 for 1.1, 2 for 1.2, 3 for 1.3, 4 for 1.4, 5 for 1.5 etc.
      Parameters:
      javaVersion - the value of the java.version system property.
      Returns:
      the major JVM version.
    • splitByWhitespaces

      public static List<String> splitByWhitespaces(String string)
      Splits given string by whitespaces, omitting blank parts.
      Parameters:
      string - the string to split, not null.
      Returns:
      the non-blank parts.
    • containsWhitespace

      public static boolean containsWhitespace(String string)
      Checks whether given string contains any whitespace character.
      Parameters:
      string - the string to check, not null.
      Returns:
      true if the string contains a whitespace character.
    • ellipsize

      public static String ellipsize(String string, int maxLength, String ellipsize)
      Shortens given string to at most maxLength characters, ending it with ellipsize if it had to be shortened.
      Parameters:
      string - the string to shorten, not null.
      maxLength - the maximum length of the result.
      ellipsize - the suffix to append to a shortened string, e.g. "...".
      Returns:
      the possibly shortened string.
    • getErrorParameterType

      public static Class<?> getErrorParameterType(Class<?> clazz)
      For a class implementing the HasErrorParameter interface, determines the type of the exception handled (the type of T). Returns null if the Class doesn't implement the HasErrorParameter interface.
      Parameters:
      clazz - the class to inspect, not null.
      Returns:
      the handled exception type, or null.
    • isRouteNotFound

      public static boolean isRouteNotFound(Class<?> clazz)
      Checks whether given class is an error view handling NotFoundException.
      Parameters:
      clazz - the class to inspect, not null.
      Returns:
      true if the class handles NotFoundException.
    • getCurrentRequest

      public static VaadinRequest getCurrentRequest()
      Returns VaadinService.getCurrentRequest(); fails with an informative error message if there is no current request.
      Returns:
      the current request, not null.
    • getCurrentResponse

      public static VaadinResponse getCurrentResponse()
      Returns VaadinService.getCurrentResponse(); fails with an informative error message if there is no current response.
      Returns:
      the current response, not null.
    • getCurrentUI

      public static UI getCurrentUI()
      Returns the UI.getCurrent(); fails with informative error message if the UI.getCurrent() is null.
      Returns:
      the current UI, not null.
    • getMock

      public static MockRequest getMock(VaadinRequest request)
      Retrieves the mock request which backs up given VaadinRequest.
       Utils.getMock(Utils.getCurrentRequest())
               .addCookie(new Cookie("foo", "bar"));
       
      Parameters:
      request - the Vaadin request, not null.
      Returns:
      the backing mock request.
    • getMock

      public static MockResponse getMock(VaadinResponse response)
      Retrieves the mock response which backs up given VaadinResponse.
       Utils.getMock(Utils.getCurrentResponse()).getCookie("foo").getValue();
       
      Parameters:
      response - the Vaadin response, not null.
      Returns:
      the backing mock response.
    • getMock

      public static MockHttpSession getMock(VaadinSession session)
      Retrieves the mock session which backs up given VaadinSession.
       Utils.getMock(VaadinSession.getCurrent())
       
      Parameters:
      session - the Vaadin session, not null.
      Returns:
      the backing mock HTTP session.
    • getServletContext

      public static jakarta.servlet.ServletContext getServletContext(VaadinContext context)
      Returns the servlet context which backs up given VaadinContext.
      Parameters:
      context - the Vaadin context, not null.
      Returns:
      the backing servlet context.
    • isInitialized

      public static boolean isInitialized(jakarta.servlet.Servlet servlet)
      Checks whether given servlet has been initialized.
      Parameters:
      servlet - the servlet, not null.
      Returns:
      true if the servlet has a servlet config.
    • hasCustomToString

      public static boolean hasCustomToString(Class<?> clazz)
      Checks whether given class overrides Object.toString().
      Parameters:
      clazz - the class to inspect, not null.
      Returns:
      true if toString() is not declared by Object.
    • getPolymerTemplateClass

      public static Class<?> getPolymerTemplateClass()
      Returns the PolymerTemplate class, or null if it's not on the classpath.
      Returns:
      the PolymerTemplate class, or null.
    • hasPolymerTemplates

      public static boolean hasPolymerTemplates()
      Checks whether the PolymerTemplate class is on the classpath.
      Returns:
      true if Polymer templates are available.
    • findClass

      public static Class<?> findClass(String className)
      Looks up a class by name, first via Class.forName(String) and then via the context class loader.
      Parameters:
      className - the fully qualified class name, not null.
      Returns:
      the class, or null if it can't be found.
    • findClassOrThrow

      public static Class<?> findClassOrThrow(String className) throws ClassNotFoundException
      Looks up a class by name like findClass(String), but fails if the class can't be found.
      Parameters:
      className - the fully qualified class name, not null.
      Returns:
      the class, not null.
      Throws:
      ClassNotFoundException - if the class can't be found.
    • sneakyThrow

      public static <E extends Throwable> RuntimeException sneakyThrow(Throwable e) throws E
      Rethrows given checked exception without declaring it, the same way Kotlin code propagates checked exceptions.
      Type Parameters:
      E - the type the compiler is told is thrown
      Parameters:
      e - the exception to rethrow
      Returns:
      never returns; declared so callers can write throw sneakyThrow(e)
      Throws:
      E - always, as e