Java Object Type Case cast(Class type, Object key, Object value)

Here you can find the source of cast(Class type, Object key, Object value)

Description

cast

License

Apache License

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T cast(Class<T> type, Object key, Object value) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w w  . j a  va  2  s  .c o  m*/
     * @since 1.0
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T> T cast(Class<T> type, Object key, Object value) throws Exception {
        if (value == null) {
            return null;
        }
        if (!type.isAssignableFrom(value.getClass())) {
            throw new Exception("Value has invalid type" + (key == null ? "" : " for key: " + key)
                    + " -- expected '" + type.getName() + "', got: " + value.getClass().getName());
        }
        return (T) value;
    }
}

Related

  1. cast(Class clazz, Object o, T def)
  2. cast(Class clazz, Object obj)
  3. cast(Class clazz, Object object)
  4. cast(Class targetClass, Object obj)
  5. cast(Class toType, Object value)
  6. cast(Class type, Object obj)
  7. cast(double[] arr)
  8. cast(final Class clazz)
  9. cast(final Number number, final Class returnType)