Java Assert assertInstance(final Object object, final Class c)

Here you can find the source of assertInstance(final Object object, final Class c)

Description

Verifies that the given object is an instance of the given class.

License

Open Source License

Parameter

Parameter Description
object The object to check; may be <code>null</code>.
c The class which the object should be; must not be <code>null</code>.

Declaration

public static final void assertInstance(final Object object, final Class<?> c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013, 2015 IBM Corporation and others.
 * 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:/*from  w  ww.j  a v  a  2  s . co  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Verifies that the given object is an instance of the given class.
     *
     * @param object
     *            The object to check; may be <code>null</code>.
     * @param c
     *            The class which the object should be; must not be <code>null</code>.
     */
    public static final void assertInstance(final Object object, final Class<?> c) {
        assertInstance(object, c, false);
    }

    /**
     * Verifies the given object is an instance of the given class. It is possible to specify
     * whether the object is permitted to be <code>null</code>.
     *
     * @param object
     *            The object to check; may be <code>null</code>.
     * @param c
     *            The class which the object should be; must not be <code>null</code>.
     * @param allowNull
     *            Whether the object is allowed to be <code>null</code>.
     */
    private static final void assertInstance(final Object object, final Class<?> c, final boolean allowNull) {
        if (object == null && allowNull) {
            return;
        }

        if (object == null || c == null) {
            throw new NullPointerException();
        } else if (!c.isInstance(object)) {
            throw new IllegalArgumentException();
        }
    }
}

Related

  1. assertInBounds(int[] coordinates)
  2. assertIndex(final int size, final int index)
  3. assertIndex(final String s, final int index)
  4. assertIndex(int index, String msg)
  5. assertInputNotEmpty(final String input, final String message)
  6. assertInstance(Object object, Class c)
  7. assertInstanceOf(Object obj, Class classType)
  8. assertInstanceOf(Object obj, Class expClass)
  9. assertInstanceOf(Object object, Class expectClass)