Here you can find the source of classForName(String name)
Perform resolution of a class name.
Parameter | Description |
---|---|
name | The class name |
Parameter | Description |
---|---|
ClassNotFoundException | From Class#forName(String). |
@SuppressWarnings("rawtypes") public static Class classForName(String name) throws ClassNotFoundException
//package com.java2s; public class Main { /**/*from w w w.j a v a 2 s . c o m*/ * <p>Perform resolution of a class name.<p/> * * @param name The class name * @return The class reference. * @throws ClassNotFoundException From {@link Class#forName(String)}. */ @SuppressWarnings("rawtypes") public static Class classForName(String name) throws ClassNotFoundException { try { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); if (contextClassLoader != null) { return contextClassLoader.loadClass(name); } } catch (Throwable ignore) { } return Class.forName(name); } /** * Perform resolution of a class name. * <p/> * Here we first check the context classloader, if one, before delegating to * {@link Class#forName(String, boolean, ClassLoader)} using the caller's classloader * * @param name The class name * @param caller The class from which this call originated (in order to access that class's loader). * @return The class reference. * @throws ClassNotFoundException From {@link Class#forName(String, boolean, ClassLoader)}. */ @SuppressWarnings("rawtypes") public static Class classForName(String name, Class caller) throws ClassNotFoundException { try { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); if (contextClassLoader != null) { return contextClassLoader.loadClass(name); } } catch (Throwable ignore) { } return Class.forName(name, true, caller.getClassLoader()); } }