How to deal with Type Safety problem

The following code calls variable() method to set up the variable in the process instance, and the data type is “Object”.

import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderProcessService implements OrderProcessInterface
{
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public void triggerOrderProcess(final OrderDetails body)
    {
        final OrderProcessData orderProcessData = new OrderProcessData();
        orderProcessData.setHasPostpaidEntries(body.isHasPostpaidEntries());

        runtimeService.createProcessInstanceBuilder()
            .processDefinitionKey("simpleProcess")
            .variable("orderId", body.getOrderId())
            .variable("orderProcessData", orderProcessData)
            .start();
    }

}

The problem is, when I try to retrieve the value using the following code snippet of the task (Delegate class), I have to cast the Object back to String. Is there an elegant way, such as generic type, to deal with type safety problem? (viz. to avoid ClassCastException)

    @Override
    public void execute(final DelegateExecution execution)
    {
        logger.info("Started {}", LocalDateTime.now());

        final String orderId = (String) execution.getVariable("orderId");

The VariableScope interface, which DelegateExecution extends, has a generic method
<T> T getVariable(String variableName, Class<T> variableClass);
to retrieve a workflow variable in a more strongly typed way. Of course this doesn’t alleviate the potential runtime errors if the type of variable stored in the workflow doesn’t match what the code is expecting.

Indeed… it’s better than nothing though. Thanks for the answer.