使用指定ClassLoader加载class

@param className class名字
@param loader    加载class的ClassLoader
@param <T>       class实际类型
@return class
976076
@SuppressWarnings("unchecked")
    public static <T> Class<T> loadClass(String className, ClassLoader loader) {
        Assert.notNull(className, "className不能为null");
        Assert.notNull(loader, "loader不能为null");
        switch (className) {
            case "boolean":
                return (Class<T>) boolean.class;
            case "byte":
                return (Class<T>) byte.class;
            case "char":
                return (Class<T>) char.class;
            case "short":
                return (Class<T>) short.class;
            case "int":
                return (Class<T>) int.class;
            case "long":
                return (Class<T>) long.class;
            case "double":
                return (Class<T>) double.class;
            case "float":
                return (Class<T>) float.class;
            default:
                try {
                    return (Class<T>) loader.loadClass(className);
                } catch (ClassNotFoundException e) {
                    throw new ReflectException("找不到指定class：" + className, e);
                }
        }
    }
405850
@SuppressWarnings("unchecked")
    public static <T> Class<T> loadClass(Class<T> type, ClassLoader classloader) {
        return loadClass(type.getName(), classloader);
    }
1324677
@SuppressWarnings( "unchecked" )
    private <T> Class<T> loadClassIfVisible( String className, ClassLoader classLoader )
    {
        try
        {
            Class<T> klass = (Class<T>) classLoader.loadClass( className );
            return klass;
        }
        catch ( ClassNotFoundException e )
        {
            return null;
        }
    }
1190964
@SuppressWarnings("unchecked")
    public static <T> Class<T> loadClass(String className) {
        return (Class<T>) loadClass(className, getDefaultClassLoader());
    }
263890
public static Object instantiateObject(String className,
                                           ClassLoader classLoader) {
        Object object;
        try {
            object = loadClass(className, classLoader).newInstance();
        } catch ( Throwable e ) {
            throw new RuntimeException( "Unable to instantiate object for class '" + className + "'",
                                        e );
        }
        return object;
    }
1121703
public static <T> T getInstance(String className, ClassLoader loader) {
        return getInstance(loadClass(className, loader));
    }
952226
public static ClassLoader getClassLoader(final Class<?> clazz) {
        // Context class loader can be null
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader != null) {
            return contextClassLoader;
        }
        if (clazz != null) {
            // The class loader for a specific class can also be null
            final ClassLoader clazzClassLoader = clazz.getClassLoader();
            if (clazzClassLoader != null) {
                return clazzClassLoader;
            }
        }
        // The only class loader we can rely on for not being null is the system
        // one
        return ClassLoader.getSystemClassLoader();
    }
991984
private Class loadClass(final String name) {

		ClassLoader loader = NodeExtender.getClassLoader();
		Class loadedClass  = null;

		if (loader == null) {
			loader = getClass().getClassLoader();
		}

		try {

			loadedClass = Class.forName(name, true, loader);

		} catch (Throwable ignore) {}

		if (loadedClass == null) {

			try {

				loadedClass = Class.forName(name);

			} catch (Throwable ignore) {}
		}

		return loadedClass;
	}
1142344
public static Class<? extends WebPage> loadClass(String className) throws ClassNotFoundException {
		return Class.forName(className).asSubclass(WebPage.class);
	}
464425
public Class< ? > loadClass(final String name,
                                final boolean resolve) throws ClassNotFoundException {
        Class cls = loader.get().load( this,
                                       name,
                                       resolve );
        if ( cls == null ) {
            throw new ClassNotFoundException( "Unable to load class: " + name );
        }

        return cls;
    }
649268
private static String which(String classname, ClassLoader loader) {
        String classnameAsResource = classname.replace('.', '/') + ".class";
        if (loader==null) loader = ClassLoader.getSystemClassLoader();

        URL it = loader.getResource(classnameAsResource);
        return it != null ? it.toString() : null;
    }
55279
private void addClassByName(ClassLoader classLoader, String className) {
        try {
            classSet.add(classLoader.loadClass(className));
        } catch (ClassNotFoundException expected) {
            // 由于是扫描package下的class，即时出现异常，也忽略掉.
            log.warn("【警告】未找到class类:'" + className + "'，将忽略不解析此类.");
        }
    }
294089
public Class<?> loadClassFrom(String name, AbstractClassLoader first) throws ClassNotFoundException {
		// try on the system one
		try { return AppClassLoader.class.getClassLoader().loadClass(name); }
		catch (ClassNotFoundException e) { /* not found. */ }
		Class<?> c = null;
		// check it is not loaded on one of the library
		for (int i = 0; i < libs.size(); i++) {
    		AbstractClassLoader cl = libs.get(i);
    		c = cl.isLoaded(name);
    		if (c != null) return c;
    	}
    	// try with the first one
    	if (first != null) {
    		try {
    			c = first.loadClassInLibrary(name);
    			if (c != null) return c;
    		} catch (FileNotFoundException e) {
    			// not found
    		} catch (IOException e) {
    			throw new ClassNotFoundException(e.getMessage());
    		}
    	}
    	// then, try on other libraries
    	for (int i = 0; i < libs.size(); i++) {
    		AbstractClassLoader cl = libs.get(i);
    		if (cl == first) continue;
    		try {
    			c = cl.loadClassInLibrary(name);
    			if (c != null) return c;
    		} catch (FileNotFoundException e) {
    			// not found
    		} catch (IOException e) {
    			throw new ClassNotFoundException(e.getMessage());
    		}
    	}
		StringBuilder msg = new StringBuilder(512);
		msg.append("Class not found: ").append(name);
		msg.append("\r\nFrom class loader: ").append(first);
		msg.append("\r\nlibraries currently registered:");
		for (int i = 0; i < libs.size(); i++)
			msg.append("\r\n - ").append(libs.get(i).toString());
		throw new ClassNotFoundException(msg.toString());
	}

	/** Load a resource. */
	@SuppressWarnings("resource")
	public IO.Readable getResourceIO(String name, byte priority) {
		if (name.length() == 0) return null;
		if (name.charAt(0) == '/') name = name.substring(1);
		IO.Readable io = null;
    	for (int i = 0; i < libs.size(); i++) {
    		AbstractClassLoader cl = libs.get(i);
   			try { io = cl.open(name, priority); }
   			catch (IOException e) { /* ignore */ }
    		if (io != null) return io;
    	}
       	// then try on the core one
        InputStream in = AppClassLoader.class.getClassLoader().getResourceAsStream(name);
        if (in == null) return null;
        return new IOFromInputStream(in, name, Threading.getUnmanagedTaskManager(), priority);
	}
680031
public void setClassLoader (ClassLoader loader)
    {
        _loader = loader;
        PresentsConnection conn = getConnection();
        if (conn != null) {
            conn.setClassLoader(loader);
        }
    }
705494
private Class<?> loadClass(String className) {
		Class<?> clazz = null;
		try {
			clazz = Class.forName(className, this.initialize, ClassUtil.getClassLoader());
		} catch (NoClassDefFoundError e) {
			// 由于依赖库导致的类无法加载，直接跳过此类
		} catch (UnsupportedClassVersionError e) {
			// 版本导致的不兼容的类，跳过
		} catch (Exception e) {
			throw new RuntimeException(e);
			// Console.error(e);
		}
		return clazz;
	}
671192
public static Class<?> getClass(String classname) {
		Class<?> clazz = null;
		try {
			clazz = Class.forName(classname);

		} catch (Exception e) {
			// Try the second approach
		}
		if (null == clazz) {
			Exception classNotFoundEx = null;
			try {
				clazz = Class.forName(classname, true, new ClassLoaderResourceUtils().getClass().getClassLoader());
			} catch (Exception e) {
				// Try the third approach
				classNotFoundEx = e;
			}
			if (null == clazz) {
				ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
				if (null != threadClassLoader) {
					try {
						clazz = Class.forName(classname, true, threadClassLoader);
					} catch (Exception e) {
						throw new BundlingProcessException(e.getMessage() + " [The custom class " + classname
								+ " could not be instantiated, check wether it is available on the classpath and"
								+ " verify that it has a zero-arg constructor].\n" + " The specific error message is: "
								+ e.getClass().getName() + ":" + e.getMessage(), e);
					}
				} else {
					throw new BundlingProcessException(classNotFoundEx.getMessage() + " [The custom class " + classname
							+ " could not be instantiated, check wether it is available on the classpath and"
							+ " verify that it has a zero-arg constructor].\n" + " The specific error message is: "
							+ classNotFoundEx.getClass().getName() + ":" + classNotFoundEx.getMessage(),
							classNotFoundEx);
				}

			}
		}
		return clazz;
	}
343424
public static Class getClassByName(String clsName,ClassLoader loader)
	{
		try
		{
			return Class.forName(clsName,true,loader);
		}

		catch (ClassNotFoundException e)
		{
			throw new RuntimeException(e);
		}
	}