Java Class InstanceOf instanceOf(Object object, Class clazz)

Here you can find the source of instanceOf(Object object, Class clazz)

Description

A runtime version of the instanceof keyword.

License

Apache License

Parameter

Parameter Description
object the left hand object (as in the instanceof keyword)
clazz the right hand class (as in the instanceof keyword)

Return

true if object is an instance of the clazz

Declaration

public static boolean instanceOf(Object object, Class<?> clazz) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 DANS - Data Archiving and Networked Services
 *
 * 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
 *  /*from   w  w  w.  j  av a2s . c om*/
 * 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.
 *******************************************************************************/

public class Main {
    /**
     * A runtime version of the instanceof keyword. Instanceof cannot check the right hand 
     * for run time classes. This function can. 
     * @param object the left hand object (as in the instanceof keyword)
     * @param clazz the right hand class (as in the instanceof keyword) 
     * @return true if object is an instance of the clazz 
     */
    public static boolean instanceOf(Object object, Class<?> clazz) {
        return instanceOf(object.getClass(), clazz);
    }

    public static boolean instanceOf(Class<?> lclass, Class<?> clazz) {
        if (lclass.equals(clazz))
            return true;
        if (clazz.isInterface())
            return classImplements(lclass, clazz);
        else
            return classExtends(lclass, clazz);
    }

    /**
     * Checks if a class or one of its super classes implements a certain interface. 
     * @param clazz the class to check 
     * @param interfaces the interfaces to search for
     * @return true if clazz implements one of the interfaces
     */
    public static boolean classImplements(Class<?> clazz, Class<?>... interfaces) {
        if (clazz.isInterface()) {
            for (Class<?> checkImpl : interfaces) {
                if (checkImpl.equals(clazz))
                    return true;
            }
        }

        Class<?>[] implementations = clazz.getInterfaces();
        for (Class<?> intf : implementations) {
            for (Class<?> checkImpl : interfaces) {
                if (intf.equals(checkImpl))
                    return true;
            }
        }

        return clazz.getSuperclass() == null ? false : classImplements(clazz.getSuperclass(), interfaces);
    }

    /**
     * Checks if a class extends another class
     * 
     * @param clazz
     *        the class to check
     * @param superClasses
     *        the superClasses to check for
     * @return true if clazz extends one of the superClasses
     */
    public static boolean classExtends(Class<?> clazz, Class<?>... superClasses) {
        Class<?> superClazz = clazz.getSuperclass();
        if (superClazz == null)
            return false;
        for (Class<?> superClass : superClasses) {
            if (superClass.equals(superClazz))
                return true;
        }
        return classExtends(superClazz, superClasses);
    }
}

Related

  1. instanceOf(Object o, Class c)
  2. instanceOf(Object o, Class... classes)
  3. instanceOf(Object obj, String className)
  4. instanceOf(Object obj, String className)
  5. instanceOf(Object obj, String name)
  6. instanceOfAll(Object o, Class... clazzes)
  7. instanceofByteArray(Object o)
  8. instanceOfs(Class[] classes, Object[] objs)