<tt>Class</tt> objects for array classes are not created by classloaders, but are created automatically as required by the Java runtime. The classloaderfor an array class, as returned by {@link Class#getClassLoader()} is the same as the classloaderfor its element type; if the element type is a primitive type, then the array classhas no classloader.
<p> The <tt>ClassLoader</tt> classuses a delegation model to search for classes and resources. Each instance of <tt>ClassLoader</tt> has an associated parent classloader. When requested to find a classor resource, a <tt>ClassLoader</tt> instance will delegate the search for the classor resource to its parent classloader before attempting to find the classor resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a <tt>ClassLoader</tt> instance.
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException{ synchronized (getClassLoadingLock(name)) {//lock ..... // First, check if the class has already been loaded // 是否已经加载 Class<?> c = findLoadedClass(name); if (c == null) { longt0= System.nanoTime(); try { if (parent != null) { //父加载器加载类 c = parent.loadClass(name, false); } else { //BootstrapClass loader 加载过(BootstrapClassLoader JVM一部分,C/C++代码实现) 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. longt1= System.nanoTime(); c = findClass(name);//父类无法加载,自行查找 // this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { //links the specified class resolveClass(c); } return c; } }
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class<?> c = findLoadedClass(name); if (c == null) { // If still not found, then invoke findClass in order // to find the class. c = findClass(name); } if (resolve) { resolveClass(c); } return c; } } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { try { byte[] b = loadClassData(name); if (b != null) { return defineClass(name, b, 0, b.length);//..... } } catch (IOException e) { e.printStackTrace(); } returnsuper.findClass(name); }
在native method:defineClass时会查找父类是否加载,如果没有,则进行加载。 when defineClass –> 自定义ClassLoader下–> 在制定目录下查找父类 如Object.class时,由于制定目录下无此class 因此 Exception