Java Object Type Case cast(B b0, Class cls)

Here you can find the source of cast(B b0, Class cls)

Description

Casts an object to the class or interface represented by the specified Class object.

License

Open Source License

Parameter

Parameter Description
B the type parameter
D the type parameter
b0 An object instance to be casted to the specified Java type.
cls Target Java type.

Exception

Parameter Description
ClassCastException In case which is the given object is not instance of thespecified Java type.

Return

Object instance safely casted to the requested Java type.

Declaration

@SuppressWarnings("unchecked")
public static <B, D> D cast(B b0, Class<D> cls) 

Method Source Code

//package com.java2s;
/*-/*w  w w  . j  ava  2s  .  c om*/
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

public class Main {
    /**
     * Casts an object to the class or interface represented by the specified
     * <tt>Class</tt> object. The method logic is similar to Java method
     * <tt>Class.cast(Object)</tt> with the only difference that unlike Java's
     * version the type name of the current object instance is specified in the
     * error message if casting fails to simplify error tracking.
     *
     * @param <B> the type parameter
     * @param <D> the type parameter
     * @param b0  An object instance to be casted to the specified Java type.
     * @param cls Target Java type.
     * @return Object instance safely casted to the requested Java type.
     * @throws ClassCastException In case which is the given object is not instance of the
     *                            specified Java type.
     */
    @SuppressWarnings("unchecked")
    public static <B, D> D cast(B b0, Class<D> cls) {
        D d0 = null;
        if (b0 != null) {
            if (!cls.isInstance(b0)) {
                throw new ClassCastException(
                        String.format("Failed to cast from '%s' to '%s'", b0.getClass().getName(), cls.getName()));
            } else {
                d0 = (D) b0;
            }
        }

        return d0;
    }
}

Related

  1. cast(byte b)
  2. cast(byte[] bytes)
  3. cast(Class c)
  4. cast(Class c, Object o)