protected Class<?>loadClass(String name,boolean resolve)throws ClassNotFoundException
{// 獲取加載鎖synchronized(getClassLoadingLock(name)){// 首先,檢查該類是否已被加載
Class<?> c =findLoadedClass(name);if(c == null){long t0 = System.nanoTime();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.long t1 = 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){resolveClass(c);}return c;}}
newInstance
public T newInstance()throws InstantiationException, IllegalAccessException
{if(System.getSecurityManager()!= null){checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(),false);}// NOTE: the following code may not be strictly correct under// the current Java memory model.// Constructor lookupif(cachedConstructor == null){if(this== Class.class){thrownewIllegalAccessException("Can not call newInstance() on the Class for java.lang.Class");}try{
Class<?>[] empty ={};// 獲取空構造函數(如果沒有空構造函數會報錯)final Constructor<T> c =getConstructor0(empty, Member.DECLARED);// Disable accessibility checks on the constructor// since we have to do the security check here anyway// (the stack depth is wrong for the Constructor's// security check to work)
java.security.AccessController.doPrivileged(newjava.security.PrivilegedAction<Void>(){public Void run(){
c.setAccessible(true);return null;}});
cachedConstructor = c;}catch(NoSuchMethodException e){throw(InstantiationException)newInstantiationException(getName()).initCause(e);}}
Constructor<T> tmpConstructor = cachedConstructor;// Security check (same as in java.lang.reflect.Constructor)int modifiers = tmpConstructor.getModifiers();if(!Reflection.quickCheckMemberAccess(this, modifiers)){
Class<?> caller = Reflection.getCallerClass();if(newInstanceCallerCache != caller){
Reflection.ensureMemberAccess(caller,this, null, modifiers);
newInstanceCallerCache = caller;}}// Run constructortry{// 調用無參構造函數,返回實例return tmpConstructor.newInstance((Object[])null);}catch(InvocationTargetException e){
Unsafe.getUnsafe().throwException(e.getTargetException());// Not reachedreturn null;}}