使用双亲委派机制获取Class对象

@param name Class类路径
@return
@throws ClassNotFoundException
507373
@Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        Class clazz = byteCodesClass.get(name);
        if (clazz == null) {
            return super.loadClass(name, false);
        }
        return clazz;
    }
325515
protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    c = findClass(name);
                }
            }
            return c;
    }
1169876
protected Class findClass( String name )
        throws ClassNotFoundException
    {
        // Deal with this cast somehow... maybe have this class use
        // ClassManagerImpl type directly.
        // Don't add the method to BshClassManager... it's really an impl thing
        ClassManagerImpl bcm = (ClassManagerImpl)getClassManager();

        // Should we try to load the class ourselves or delegate?
        // look for overlay loader

        ClassLoader cl = bcm.getLoaderForClass( name );

        // If there is a designated loader and it's not us delegate to it
        if ( cl != null && cl != this ) try {
            return cl.loadClass( name );
        } catch ( ClassNotFoundException e ) {
            throw new ClassNotFoundException(
                "Designated loader could not find class: "+e );
        }

        // Let URLClassLoader try any paths it may have
        if ( getURLs().length > 0 ) try {
            return super.findClass(name);
        } catch ( ClassNotFoundException e ) {
            //System.out.println(
            //  "base loader here caught class not found: "+name );
        }


        // If there is a baseLoader and it's not us delegate to it
        cl = bcm.getBaseLoader();

        if ( cl != null && cl != this ) try {
            return cl.loadClass( name );
        } catch ( ClassNotFoundException e ) { }

        // Try system loader
        return bcm.plainClassForName( name );
    }
914667
public Class<?> getClassByNameOrNull(String name) {
		Map<String, WeakReference<Class<?>>> map;

		synchronized (CACHE_CLASSES) {
			map = CACHE_CLASSES.get(classLoader);
			if (map == null) {
				map = Collections.synchronizedMap(
						new WeakHashMap<String, WeakReference<Class<?>>>());
				CACHE_CLASSES.put(classLoader, map);
			}
		}

		Class<?> clazz = null;
		WeakReference<Class<?>> ref = map.get(name);
		if (ref != null) {
			clazz = ref.get();
		}

		if (clazz == null) {
			try {
				clazz = Class.forName(name, true, classLoader);
			} catch (ClassNotFoundException e) {
				// Leave a marker that the class isn't found
				map.put(name, new WeakReference<Class<?>>(NEGATIVE_CACHE_SENTINEL));
				return null;
			}
			// two putters can race here, but they'll put the same class
			map.put(name, new WeakReference<Class<?>>(clazz));
			return clazz;
		} else if (clazz == NEGATIVE_CACHE_SENTINEL) {
			return null; // not found
		} else {
			// cache hit
			return clazz;
		}
	}
900900
public Class loadClass(final String name, boolean lookupScriptFiles, boolean preferClassOverScript)
            throws ClassNotFoundException, CompilationFailedException {
        return loadClass(name, lookupScriptFiles, preferClassOverScript, false);
    }
340433
protected JClass loadClass(String name)
  {
    // JDK classes are not loaded, but javax are (e.g. for JSF)
    if (name.startsWith("java.") || name.startsWith("java/")) {
      return getStaticClassLoader().forName(name);
    }

    String classPath = name.replace('.', '/') + ".class";
    URL url = _loader.getResource(classPath);

    if (url != null) {
      JavaClass jClass = new JavaClass(this);
      jClass.setURL(url);

      jClass.setThisClass(name.replace('.', '/'));

      return jClass;
    }

    return null;
  }
29976
public static Class<Object> getInnerClassType(Class<?> declaringClass, String name) throws ClassNotFoundException {
		return WhiteboxImpl.getInnerClassType(declaringClass, name);
	}
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);
	}
260657
@Override
  protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (!loadedClassFiles) {
      classFileMap = extractClassFileMap();
      loadedClassFiles = true;
    }

    if (classFileMap == null) {
      throw new ClassNotFoundException(name);
    }

    String internalName = name.replace('.', '/');
    CompiledClass compiledClass = classFileMap.get(internalName);
    if (compiledClass == null) {
      throw new ClassNotFoundException(name);
    }

    // Make sure the class's package is present.
    String pkg = compiledClass.getPackageName();
    if (getPackage(pkg) == null) {
      definePackage(pkg, null, null, null, null, null, null, null);
    }

    byte[] bytes = compiledClass.getBytes();
    return defineClass(name, bytes, 0, bytes.length);
  }
655457
protected Class<?> loadSourceClass( final String name ) {
        final String fileName = '/' + name.replace('.', '/') + ".java";
        final URL url = getResource( fileName );
        if ( url == null )
            return null;
        try (FileReader reader
                = new FileReader((InputStream) url.getContent())) {
            Interpreter.debug("Loading class from source file: " + fileName);
            declaringInterpreter.eval( reader );
        } catch ( IOException | EvalError e ) {
            if (Interpreter.DEBUG.get())
                e.printStackTrace();
        }
        try {
            return plainClassForName( name );
        } catch ( final ClassNotFoundException e ) {
            Interpreter.debug("Class not found in source file: " + name);
            return null;
        }
    }
1174805
public static Class loadClass(Class loadClass,String name)
        throws ClassNotFoundException
    {
        ClassNotFoundException cnfe=null;
        ClassLoader loader=Thread.currentThread().getContextClassLoader();
        if (loader!=null)
        {
            try
            {
               return loader.loadClass(name);
            }
            catch (ClassNotFoundException e)
            {
               cnfe=e;
            }
        }

        loader=loadClass.getClassLoader();
        if (loader!=null)
        {
            try
            {
               return loader.loadClass(name);
            }
            catch (ClassNotFoundException e)
            {
               if(cnfe==null)cnfe=e;
            }
        }


       try
       {
            return Class.forName(name);
       }
       catch (ClassNotFoundException e)
       {
            if(cnfe==null)cnfe=e;
	    throw cnfe;
       }

    }
1107980
public static Class<?> classForName(final String name, final ClassLoader loader)
      throws ClassNotFoundException {
    if (name.startsWith("[")) {
      throw new UnsupportedOperationException("No support for arrays, etc.  Name was: " + name);
    } else if (name.equals("boolean")) {
      return boolean.class;
    } else if (name.equals("byte")) {
      return byte.class;
    } else if (name.equals("char")) {
      return char.class;
    } else if (name.equals("short")) {
      return short.class;
    } else if (name.equals("int")) {
      return int.class;
    } else if (name.equals("long")) {
      return long.class;
    } else if (name.equals("float")) {
      return float.class;
    } else if (name.equals("double")) {
      return double.class;
    } else if (name.equals("void")) {
      return void.class;
    } else {
      return loader.loadClass(name);
    }
  }
76861
@Nonnull
  public Class <?> loadClass (final String sClassName) throws ClassNotFoundException
  {
    if (m_aBestCandidate != null)
    {
      try
      {
        return m_aBestCandidate.loadClass (sClassName);
      }
      catch (final Throwable t)
      {
        m_aBestCandidate = null;
      }
    }

    Throwable aThrowable = null;
    Class <?> ret = null;
    IClassLoadHelper aLoadHelper = null;

    final Iterator <IClassLoadHelper> iter = m_aLoadHelpers.iterator ();
    while (iter.hasNext ())
    {
      aLoadHelper = iter.next ();
      try
      {
        ret = aLoadHelper.loadClass (sClassName);
        break;
      }
      catch (final Throwable t)
      {
        aThrowable = t;
      }
    }

    if (ret == null)
    {
      if (aThrowable instanceof ClassNotFoundException)
        throw (ClassNotFoundException) aThrowable;
      throw new ClassNotFoundException ("Unable to load class " + sClassName + " by any known loaders.", aThrowable);
    }

    // Remember
    m_aBestCandidate = aLoadHelper;
    return ret;
  }
57915
protected Type<?> loadType( String name )
    {
        Type<?> type = typeLoader.loadType( name );
        if (type == null) throw new TypeNotFoundException( name , this.getClass());
        return type;
    }
842234
public static Class<?> toClass(final String name) {
		try {
			return Class.forName(name);
		} catch (final ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
1344743
public ClassDocImpl lookupClass(String name) {
        ClassSymbol c = getClassSymbol(name);
        if (c != null) {
            return getClassDoc(c);
        } else {
            return null;
        }
    }
409980
public synchronized Class<?> loadClass(String className) throws ClassNotFoundException {
	// if class is already loaded flush the classloader and do new classloader
	PCLBlock cl = index.get(className);
	if (cl != null) {
	    return cl.loadClass(className);
	}
	throw new ClassNotFoundException("class " + className + " not found");
    }