Java Class InstanceOf instanceOf(Class objectClass, Class typeClass)

Here you can find the source of instanceOf(Class objectClass, Class typeClass)

Description

Tests if a class is the same class as, or sub-class of, or implements typeClass.

License

Apache License

Parameter

Parameter Description
objectClass Class to test
typeClass Class to test against

Return

true if objectClass is the same class as, or sub-class of, or implements typeClass

Declaration

public static boolean instanceOf(Class<?> objectClass, Class<?> typeClass) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.//from   w  ww .  j  a va2s  .  co  m
 *******************************************************************************/

public class Main {
    /**
     * Tests if a class is the same class as, or sub-class of, or implements <code>typeClass</code>.
     * @param objectClass Class to test
     * @param typeClass Class to test against
     * @return <code>true</code> if <code>objectClass</code> is the same class as, or sub-class of, or implements <code>typeClass</code>
     */
    public static boolean instanceOf(Class<?> objectClass, Class<?> typeClass) {
        if (objectClass == typeClass) {
            return true;
        }
        if (objectClass.isInterface()) {
            if (typeClass.isInterface()) {
                // objectClass == interface, typeClass == interface
                Class<?>[] ifaces = objectClass.getInterfaces();
                for (Class<?> iface : ifaces) {
                    if (iface == typeClass) {
                        return true;
                    }
                }
            } else {
                // objectClass == interface, typeClass != interface
                Class<?>[] ifaces = typeClass.getInterfaces();
                for (Class<?> iface : ifaces) {
                    if (iface == objectClass) {
                        return true;
                    }
                }
            }
        } else {
            if (typeClass.isInterface()) {
                // objectClass != interface, typeClass == interface
                while (objectClass != null) {
                    Class<?>[] ifaces = objectClass.getInterfaces();
                    for (Class<?> iface : ifaces) {
                        if (iface == typeClass) {
                            return true;
                        }
                    }
                    objectClass = objectClass.getSuperclass();
                }
            } else {
                // objectClass != interface, typeClass != interface
                while (objectClass != null) {
                    if (objectClass == typeClass) {
                        return true;
                    }
                    objectClass = objectClass.getSuperclass();
                }
            }
        }
        return false;
    }
}

Related

  1. instanceOf(Class clazz, Class superClass)
  2. instanceOf(Class clazz, Class target)
  3. instanceOf(Class left, Class right)
  4. instanceOf(Class tester, Class instance)
  5. instanceOf(Class beanClass, Object element)
  6. instanceOf(Class parant, Class chield)
  7. instanceOf(Class test, Class clazz)
  8. instanceOf(final Class classVerifiable, final Class classAssignable)
  9. instanceOf(final Object a, final Class b)