Java Object Type Case castTo(Object o, Class cls)

Here you can find the source of castTo(Object o, Class cls)

Description

cast To

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T castTo(Object o, Class<T> cls) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2012 Google, Inc./*from ww  w  . java 2s. co m*/
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *  Google, Inc. - initial API and implementation
 *******************************************************************************/

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T castTo(Object o, Class<T> cls) {
        Class<? extends Object> oc = o.getClass();
        if (cls.isAssignableFrom(oc))
            return (T) o;
        System.out.println("ReflectionUtils.castTo() -- returning null");
        return null;
    }
}

Related

  1. castOrNull(Object o, Class clazz)
  2. castOrNull(Object o, Class cls)
  3. castOrThrowError(Class clazz, T1 object, String messageTemplate)
  4. castSafe(Class inClass, Object inValue, T inDefault)
  5. caststring1(String tmpq)
  6. castToArray(Object object)
  7. castToClass_strict(Number valueIn, Class toClassIn)
  8. castToObject(Object value)
  9. castToPrimitive(Object value, String targetType)