Jakarta EE Components
Utilities
Small helpers for converting strings to types, moving between javax and jakarta names, testing serialization and reading streams.
Convert strings to any type
By convention, most classes that can be constructed from a String have a valueOf(String) method.
TypeConverter uses it to convert a String to any type you specify, which greatly reduces the code you need.
String to any class. Throws an exception if it failsTT convertedValue = TypeConverter.valueOf(input, cls);
CheckedValue<TT> checkedValue = TypeConverter.checkAndConvert(input, cls);
if (checkedValue.isValid()) {
// get and operate on a value in a type-safe way
TT value = checkedValue.getValue();
}
To keep it simple, TypeConverter does not support custom converters.
If you need them, other libraries handle custom converters well, including Jakarta Faces converters.
Translate javax names to jakarta
jakartify() converts strings from the javax namespace to jakarta, depending on the environment it runs in:
// returns "jakarta.servlet.Servlet" in Jakarta artifacts
String jakartaServlet = JakartaTransformerUtils.jakartify("javax.servlet.Servlet");
// returns "jakarta.faces.FacesException: message X" in Jakarta artifacts
String jakartaError = JakartaTransformerUtils.jakartify("javax.faces.FacesException: message X");
The environment is detected at run time, not at compile time. It works for class names, and also for error messages and any other strings.
This is especially useful when the Maven Shade plugin produces both javax and jakarta JARs from the same source code:
the correct strings are produced without extra Shade configuration or complex regular expressions.
Test serialization
serializeAndDeserialize() checks whether an object can really be serialized.
It returns the result of serializing and deserializing the input, so you can check the new object’s state.
TT output = SerializeTester.serializeAndDeserialize(input);
assertEquals(input, output);