@SuppressWarnings("unchecked")
    public <C> C getContext(String key, Class<C> type) {
        Object value = getContext(key);
        if (value == null) {
            return null;
        } else if (type.isInstance(value)) {
            return (C) value;
        } else {
            throw new ClassCastException(String.format(
                    "Expected context value '%s' to be an instance of %s, but found %s %s",
                    key,
                    type.getName(),
                    value.getClass().getName(),
                    getDebugInfo()));
        }
    }
Gets a named context key-value pair from the current state and
casts the value to the given type.

@param key Key to retrieve.
@param type The type of value expected.
@return Returns the associated value or null if not present.
@throws ClassCastException if the stored value is not null and does not match {@code type}.
smithy-utils/src/main/java/software/amazon/smithy/utils/AbstractCodeWriter.java