Java Assert Not Null assertInstanceOfNotNull(Object obj, Class expClass)

Here you can find the source of assertInstanceOfNotNull(Object obj, Class expClass)

Description

Checks if the passed object is of the class specified, null values throw an exception

License

Open Source License

Declaration

public static void assertInstanceOfNotNull(Object obj, Class<?> expClass) 

Method Source Code

//package com.java2s;
/**/*  ww  w .j a  v  a 2s. c  o  m*/
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) 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:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: AssertUtil.java,v 1.7 2009/03/30 07:53:04 mtaal Exp $
 */

public class Main {
    /**
     * Checks if the passed object is of the class specified, null values throw an exception
     */
    public static void assertInstanceOfNotNull(Object obj, Class<?> expClass) {
        if (obj == null) {
            throw new AssertionError(
                    "Checking instanceof but object is null, expecting class: "
                            + expClass.getName());
        }
        if (!(expClass.isAssignableFrom(obj.getClass()))) {
            throw new AssertionError("Expected class: "
                    + expClass.getName() + " but object has class: "
                    + obj.getClass().getName());
        }
    }
}

Related

  1. assertArgNotNull(T obj, String name)
  2. assertArgumentNotNull(T argument, String argumentName)
  3. assertElementNotNull(Object element, Class elementClass)
  4. assertFieldNotNull(Object fieldValue, String fieldName)
  5. assertFormatResultNotNull(Object r)
  6. assertNotNull(final Object o, final String message)
  7. assertNotNull(final Object obj, final String msg)
  8. assertNotNull(final Object obj, final String reference)
  9. assertNotNull(final Object object, final String message)