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.

Convert a String to any class. Throws an exception if it fails
TT convertedValue = TypeConverter.valueOf(input, cls);
Convert, and check that the result matches the input, without throwing exceptions
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:

Servlet example
// returns "jakarta.servlet.Servlet" in Jakarta artifacts
String jakartaServlet = JakartaTransformerUtils.jakartify("javax.servlet.Servlet");
Jakarta Faces example
// 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.

Check a serialized object
TT output = SerializeTester.serializeAndDeserialize(input);
assertEquals(input, output);

Read a stream into a String

var stream = new ByteArrayInputStream(input.getBytes());
String string = Streams.readString(stream);
Note
readString doesn’t close the input stream.