Java Class Load loadClass(String className, Class targetType, ClassLoader cl)

Here you can find the source of loadClass(String className, Class targetType, ClassLoader cl)

Description

load Class

License

Open Source License

Declaration

public static <T> Class<T> loadClass(String className, Class<T> targetType, ClassLoader cl) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *     Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
 *     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 
 *    //from ww  w . ja v a  2 s.com
 *        http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/

public class Main {
    public static <T> Class<T> loadClass(String className, Class<T> targetType, ClassLoader cl) throws Exception {
        return loadClass(className, targetType, false, cl);
    }

    public static <T> Class<T> loadClass(String className, Class<T> targetType, boolean ignoreMissingClass,
            ClassLoader cl) throws Exception {
        String targetTypeName;
        if (targetType != null) {
            targetTypeName = targetType.getName();
        } else {
            targetTypeName = "(unspecified assignment type)";
        }

        Class clazz;
        try {
            clazz = Class.forName(className, true, cl);
        } catch (NoClassDefFoundError err) {
            throw new Exception(err);
            //throw new Exception(SystemErrorTypes.SVC_FACTORY_INST_NOT_FOUND,
            //      new Object[] {targetTypeName, className}, err);
        } catch (ClassNotFoundException e) {
            if (ignoreMissingClass) {
                return null;
            }
            throw new Exception(e);
            //         throw new ServiceException(SystemErrorTypes.SVC_FACTORY_INST_NOT_FOUND,
            //            new Object[] {targetTypeName, className}, e);
        }

        if (targetType != null && !targetType.isAssignableFrom(clazz)) {
            throw new Exception(
                    "instance cannot cast - targetTypeName: " + targetTypeName + ", className: " + className);
            //         throw new ServiceException(SystemErrorTypes.SVC_FACTORY_INST_CANNOT_CAST,
            //            new Object[] {targetTypeName, className});
        }

        @SuppressWarnings("unchecked")
        Class<T> result = clazz;

        return result;
    }
}

Related

  1. loadClass(String classname, Class clazz)
  2. loadClass(String classname, Class clazz)
  3. loadClass(String classname, Class clazz)
  4. loadClass(String className, Class context, boolean checkParent)
  5. loadClass(String className, Class ofType)
  6. loadClass(String className, Class type)
  7. loadClass(String className, ClassLoader cl)
  8. loadClass(String className, ClassLoader classLoader)
  9. loadClass(String className, ClassLoader classLoader)