一、前言
关于datanucleus-enhancer源码包中org.datanucleus.enhancer.EnhancerClassLoader增强型类加载器,实现指定自定义代理指定加载器类功能,主要包括defineClass、根据名称实例化loadClass、根据名称加载资源findResource功能方法,具体看源码说明。
二、源码说明
package org.datanucleus.enhancer;@b@@b@import java.net.URL;@b@import org.datanucleus.ClassLoaderResolver;@b@import org.datanucleus.exceptions.ClassNotResolvedException;@b@@b@public final class EnhancerClassLoader extends ClassLoader@b@{@b@ ClassLoaderResolver delegate;@b@ boolean loadingClass = false;@b@ boolean loadingResource = false;@b@@b@ public EnhancerClassLoader()@b@ {@b@ super(Thread.currentThread().getContextClassLoader());@b@ }@b@@b@ public EnhancerClassLoader(ClassLoaderResolver iDelegate)@b@ {@b@ this.delegate = iDelegate;@b@ }@b@@b@ public synchronized void defineClass(String fullClassName, byte[] bytes, ClassLoaderResolver clr)@b@ {@b@ ClassLoaderResolver oldDelegate = this.delegate;@b@ this.delegate = clr;@b@ try@b@ {@b@ defineClass(fullClassName, bytes, 0, bytes.length);@b@ }@b@ finally@b@ {@b@ this.delegate = oldDelegate;@b@ }@b@ }@b@@b@ public synchronized Class loadClass(String name)@b@ throws ClassNotFoundException@b@ {@b@ if (this.loadingClass)@b@ {@b@ throw new ClassNotFoundException("Class " + name + " not found");@b@ }@b@ this.loadingClass = true;@b@ try@b@ {@b@ if (this.delegate != null)@b@ {@b@ try@b@ {@b@ Class localClass1 = this.delegate.classForName(name);@b@@b@ this.loadingClass = false; return localClass1;@b@ }@b@ catch (ClassNotResolvedException cnrex)@b@ {@b@ throw new ClassNotFoundException(((ClassNotResolvedException)cnrex).toString(), (Throwable)cnrex);@b@ }@b@ }@b@ cnrex = super.loadClass(name);@b@@b@ return cnrex;@b@ }@b@ catch (ClassNotFoundException ex)@b@ {@b@ if (this.delegate != null);@b@ throw ex;@b@ }@b@ finally@b@ {@b@ this.loadingClass = false;@b@ }@b@ }@b@@b@ protected synchronized URL findResource(String name)@b@ {@b@ if (this.loadingResource)@b@ {@b@ return null;@b@ }@b@ this.loadingResource = true;@b@ try@b@ {@b@ URL url = super.findResource(name);@b@ if ((url == null) && @b@ (this.delegate != null))@b@ {@b@ url = this.delegate.getResource(name, null);@b@ }@b@@b@ URL localURL1 = url;@b@@b@ return localURL1; } finally { this.loadingResource = false;@b@ }@b@ }@b@}