Package com.vaadin.browserless.internal
Class Utils
java.lang.Object
com.vaadin.browserless.internal.Utils
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 Summary
Modifier and TypeMethodDescriptionstatic booleancontainsWhitespace(String string) Checks whether given string contains any whitespace character.static <T extends Serializable>
Tdeserialize(byte[] bytes) Deserializes an object from given byte array using Java serialization.static StringShortens given string to at mostmaxLengthcharacters, ending it withellipsizeif it had to be shortened.static Class<?> Looks up a class by name, first viaClass.forName(String)and then via the context class loader.static Class<?> findClassOrThrow(String className) Looks up a class by name likefindClass(String), but fails if the class can't be found.static VaadinRequestReturnsVaadinService.getCurrentRequest(); fails with an informative error message if there is no current request.static VaadinResponseReturnsVaadinService.getCurrentResponse(); fails with an informative error message if there is no current response.static UIReturns theUI.getCurrent(); fails with informative error message if the UI.getCurrent() is null.static Class<?> getErrorParameterType(Class<?> clazz) For a class implementing theHasErrorParameterinterface, determines the type of the exception handled (the type ofT).static intReturns the major JVM version, e.g. 6 for Java 1.6, 8 for Java 8, 11 for Java 11 etc.static MockRequestgetMock(VaadinRequest request) Retrieves the mock request which backs up givenVaadinRequest.static MockResponsegetMock(VaadinResponse response) Retrieves the mock response which backs up givenVaadinResponse.static MockHttpSessiongetMock(VaadinSession session) Retrieves the mock session which backs up givenVaadinSession.static Class<?> Returns thePolymerTemplateclass, or null if it's not on the classpath.static jakarta.servlet.ServletContextgetServletContext(VaadinContext context) Returns the servlet context which backs up givenVaadinContext.static booleanhasCustomToString(Class<?> clazz) Checks whether given class overridesObject.toString().static booleanChecks whether thePolymerTemplateclass is on the classpath.static booleanisInitialized(jakarta.servlet.Servlet servlet) Checks whether given servlet has been initialized.static booleanisRouteNotFound(Class<?> clazz) Checks whether given class is an error view handlingNotFoundException.static intparseJvmVersion(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.static <T extends Serializable>
TserializeDeserialize(T obj) Serializes given object and deserializes it back, returning the copy.static byte[]Serializes given object into a byte array using Java serialization.static <E extends Throwable>
RuntimeExceptionRethrows given checked exception without declaring it, the same way Kotlin code propagates checked exceptions.splitByWhitespaces(String string) Splits given string by whitespaces, omitting blank parts.
-
Method Details
-
serializeToBytes
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
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
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
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 thejava.versionsystem property.- Returns:
- the major JVM version.
-
splitByWhitespaces
Splits given string by whitespaces, omitting blank parts.- Parameters:
string- the string to split, not null.- Returns:
- the non-blank parts.
-
containsWhitespace
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
Shortens given string to at mostmaxLengthcharacters, ending it withellipsizeif 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
For a class implementing theHasErrorParameterinterface, determines the type of the exception handled (the type ofT). Returns null if the Class doesn't implement theHasErrorParameterinterface.- Parameters:
clazz- the class to inspect, not null.- Returns:
- the handled exception type, or null.
-
isRouteNotFound
Checks whether given class is an error view handlingNotFoundException.- Parameters:
clazz- the class to inspect, not null.- Returns:
- true if the class handles
NotFoundException.
-
getCurrentRequest
ReturnsVaadinService.getCurrentRequest(); fails with an informative error message if there is no current request.- Returns:
- the current request, not null.
-
getCurrentResponse
ReturnsVaadinService.getCurrentResponse(); fails with an informative error message if there is no current response.- Returns:
- the current response, not null.
-
getCurrentUI
Returns theUI.getCurrent(); fails with informative error message if the UI.getCurrent() is null.- Returns:
- the current UI, not null.
-
getMock
Retrieves the mock request which backs up givenVaadinRequest.Utils.getMock(Utils.getCurrentRequest()) .addCookie(new Cookie("foo", "bar"));- Parameters:
request- the Vaadin request, not null.- Returns:
- the backing mock request.
-
getMock
Retrieves the mock response which backs up givenVaadinResponse.Utils.getMock(Utils.getCurrentResponse()).getCookie("foo").getValue();- Parameters:
response- the Vaadin response, not null.- Returns:
- the backing mock response.
-
getMock
Retrieves the mock session which backs up givenVaadinSession.Utils.getMock(VaadinSession.getCurrent())
- Parameters:
session- the Vaadin session, not null.- Returns:
- the backing mock HTTP session.
-
getServletContext
Returns the servlet context which backs up givenVaadinContext.- 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
Checks whether given class overridesObject.toString().- Parameters:
clazz- the class to inspect, not null.- Returns:
- true if
toString()is not declared byObject.
-
getPolymerTemplateClass
Returns thePolymerTemplateclass, or null if it's not on the classpath.- Returns:
- the
PolymerTemplateclass, or null.
-
hasPolymerTemplates
public static boolean hasPolymerTemplates()Checks whether thePolymerTemplateclass is on the classpath.- Returns:
- true if Polymer templates are available.
-
findClass
Looks up a class by name, first viaClass.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
Looks up a class by name likefindClass(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
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, ase
-