public Class<?> loadClass(String name) throws ClassNotFoundException :加载名称为name的类,返回结果为java.lang.Class类的实例,如果找不到类,则返回ClassNotFoundException异常,该方法中的逻辑就是双亲委派模式的实现
//测试代码ClassLoader.getSystemClassLoader().loadClass("com.chapter11.User")涉及以下方法调用protectedClass<?>loadClass(String name,boolean resolve)throwsClassNotFoundException{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){//当前类加载器的父类加载器未加载此类或当前类加载器未加载此类// 调用当前ClassLoader的findClass()long t1 =System.nanoTime();
c =findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if(resolve){resolveClass(c);//是否允许解析}return c;}}
//在URLClassLoader中重写了findClass方法,代码如下protectedClass<?>findClass(finalString name)throwsClassNotFoundException{finalClass<?> result;try{
result =AccessController.doPrivileged(newPrivilegedExceptionAction<Class<?>>(){publicClass<?>run()throwsClassNotFoundException{String path = name.replace('.','/').concat(".class");Resource res = ucp.getResource(path,false);if(res !=null){try{returndefineClass(name, res);}catch(IOException e){thrownewClassNotFoundException(name, e);}}else{returnnull;}}}, acc);}catch(java.security.PrivilegedActionException pae){throw(ClassNotFoundException) pae.getException();}if(result ==null){thrownewClassNotFoundException(name);}return result;}
protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError:根据给定的字节数组b转换为Class实例,off和len参数表示实际Class信息在byte数组中的位置和长度,其中byte数组b是ClassLoader从外部获取的,这是受保护的方法,只有在自定义ClassLoader子类中可以使用
如题,开了个会没带笔记本电源,点啊弄关机后docker打不开,我以为是docker坏了,结果docker报错: An unexpected error occurred while executing a WSL command. Either shut down WSL down with wsl --shutdown, and/or…