if o is of the given type, returns o cast to it; otherwise returns null. - Java Reflection

Java examples for Reflection:Object

Description

if o is of the given type, returns o cast to it; otherwise returns null.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w ww .  ja  v a  2  s  . c o m*/
     * if o is of the given type, returns o cast to it; otherwise returns null.
     */
    public static <T extends U, U> T as(Class<T> type, U o) {
        if (type.isInstance(o)) {
            return (T) o;
        }
        return null;
    }
}

Related Tutorials