@SuppressWarnings({"unchecked"})
    protected <E> E getTypedValue(String key, Class<E> type) {
        E found = null;
        Object o = backingMap.get(key);
        if (o != null) {
            if (!type.isAssignableFrom(o.getClass())) {
                String msg = "Invalid object found in SubjectContext Map under key [" + key + "].  Expected type "
                        + "was [" + type.getName() + "], but the object under that key is of type "
                        + "[" + o.getClass().getName() + "].";
                throw new IllegalArgumentException(msg);
            }
            found = (E) o;
        }
        return found;
    }
Performs a {@link #get get} operation but additionally ensures that the value returned is of the specified
{@code type}.  If there is no value, {@code null} is returned.

@param key  the attribute key to look up a value
@param type the expected type of the value
@param <E>  the expected type of the value
@return the typed value or {@code null} if the attribute does not exist.
core/src/main/java/org/apache/shiro/util/MapContext.java