dynamicclassloader.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,097 行 · 第 1/4 页

JAVA
2,097
字号
    }    if (parent instanceof DynamicClassLoader)      ((DynamicClassLoader) parent).buildResourcePathSpecificFirst(pathList);    else {      String tail = CauchoSystem.getClassPath();      if (tail != null) {	char sep = CauchoSystem.getPathSeparatorChar();		String []values = tail.split("[" + sep + "]");	for (int i = 0; i < values.length; i++) {	  pathList.add(values[i]);	}      }    }  }  /**   * Returns true if any of the classes have been modified.   */  public final boolean isModified()  {    return isModified(_isEnableDependencyCheck);  }  /**   * Returns true if any of the classes have been modified.   */  public final boolean isModified(boolean enable)  {    if (_lifecycle.isDestroyed()) {      return true;    }    DependencyContainer dependencies = _dependencies;    if (dependencies == null) {      return true;    }    if (enable) {      boolean isModified = dependencies.isModified();      return isModified;    }    else {      boolean isModified = isModified(getParent());      return isModified;    }  }  /**   * Returns true if any of the classes have been modified, forcing a check.   */  public final boolean isModifiedNow()  {    if (_lifecycle.isDestroyed())      return true;    DependencyContainer dependencies = _dependencies;    if (dependencies == null)      return true;    return dependencies.isModifiedNow();  }  /**   * Logs the reason for modification.   */  public final boolean logModified(Logger log)  {    if (_lifecycle.isDestroyed())      return true;    DependencyContainer dependencies = _dependencies;    if (dependencies != null)      return dependencies.logModified(log);    else      return false;  }  /**   * Returns true if any of the classes have been modified.   */  public final void resetDependencyCheckInterval()  {    if (_lifecycle.isDestroyed())      return;    DependencyContainer dependencies = _dependencies;    if (dependencies == null)      return;    dependencies.resetDependencyCheckInterval();  }  /**   * Returns true if any of the classes have been modified.   */  public final void clearModified()  {    if (_lifecycle.isDestroyed())      return;    DependencyContainer dependencies = _dependencies;    if (dependencies == null)      return;    dependencies.clearModified();  }  /**   * Returns true if any of the classes have been modified.   */  public static boolean isModified(ClassLoader loader)  {    for (; loader != null; loader = loader.getParent()) {      if (loader instanceof DynamicClassLoader)        return ((DynamicClassLoader) loader).isModified();    }    return false;  }  /**   * Makes any changed classes for the virtual class loader.   */  public final void make()    throws Exception  {    for (ClassLoader loader = getParent();         loader != null;         loader = loader.getParent()) {      if (loader instanceof Make) {        ((Make) loader).make();        break;      }    }    if (_makeList != null)      _makeList.make();  }  /**   * Defines a new package.   */  @Override  protected Package definePackage(String name,                                  String a1, String a2, String a3,                                  String b1, String b2, String b3,                                  URL url)  {    name = name.replace('/', '.');    name = name.replace('\\', '.');    if (name.endsWith("."))      name = name.substring(0, name.length() - 1);    return super.definePackage(name, a1, a2, a3, b1, b2, b3, url);  }  /**   * Initialize the class loader.   */  public void init()  {    if (! _lifecycle.toActive())      return;    try {      sendAddLoaderEvent();            ArrayList<ClassLoaderListener> listeners = getListeners();      if (listeners != null) {        for (int i = 0; i < listeners.size(); i++) {          ClassLoaderListener listener = listeners.get(i);          listener.classLoaderInit(this);        }      }    } catch (Exception e) {      log().log(Level.WARNING, e.toString(), e);    }  }  /**   * Validates the class loader.   */  public void validate()    throws ConfigException  {    ArrayList<Loader> loaders = getLoaders();    if (loaders == null)      throw new IllegalStateException(_L.l("Class loader {0} is closed during initialization.", this));    for (int i = 0; i < loaders.size(); i++)      loaders.get(i).validate();  }    public void scan()  {  }  @Override  public Class<?> loadClass(String name) throws ClassNotFoundException  {    // the Sun JDK implementation of ClassLoader delegates this call    // to loadClass(name, false), but there is no guarantee that other    // implementations do.    return loadClass(name, false);  }  /**   * Load a class using this class loader   *   * @param name the classname to load   * @param resolve if true, resolve the class   *   * @return the loaded classes   */  @Override  protected Class loadClass(String name, boolean resolve)    throws ClassNotFoundException  {    // XXX: removed sync block, since handled below    Class cl = loadClassImpl(name, resolve);    if (cl != null)      return cl;    else      throw new ClassNotFoundException(name + " in " + this);  }  /**   * Load a class using this class loader   *   * @param name the classname to load   * @param resolve if true, resolve the class   *   * @return the loaded classes   */  public Class loadClassImpl(String name, boolean resolve)    throws ClassNotFoundException  {    if (_entryCache != null) {      ClassEntry entry = _entryCache.get(name);      if (entry != null) {	Class cl = entry.getEntryClass();	if (cl != null)	  return cl;      }    }        // The JVM has already cached the classes, so we don't need to    Class cl = findLoadedClass(name);    if (cl != null) {      if (resolve)        resolveClass(cl);      return cl;    }    if (_lifecycle.isDestroyed()) {      log().fine(L().l("Loading class {0} when class loader {1} has been closed.",                       name, this));      return super.loadClass(name, resolve);    }    boolean normalJdkOrder = isNormalJdkOrder(name);    if (_lifecycle.isBeforeInit())      init();    // Force scanning if any loaders have been added    sendAddLoaderEvent();    if (normalJdkOrder) {      try {        ClassLoader parent = getParent();	if (parent instanceof DynamicClassLoader)	  cl = ((DynamicClassLoader) parent).loadClassImpl(name, resolve);        else if (parent != null) {          cl = Class.forName(name, false, parent);	}        else          cl = findSystemClass(name);      } catch (ClassNotFoundException e) {      }      if (cl == null) {        cl = findClassImpl(name);      }    }    else {      try {        cl = findClass(name);      } catch (ClassNotFoundException e) {        ClassLoader parent = getParent();        if (parent != null)          cl = Class.forName(name, false, parent);        else          cl = findSystemClass(name);      }    }    if (resolve && cl != null)      resolveClass(cl);    return cl;  }  /**   * Load a class using this class loader   *   * @param name the classname using either '/' or '.'   *   * @return the loaded class   */  @Override  protected Class findClass(String name)    throws ClassNotFoundException  {    Class cl = findClassImpl(name);    if (cl != null)      return cl;    else      throw new ClassNotFoundException(name);  }  /**   * Load a class using this class loader   *   * @param name the classname using either '/' or '.'   *   * @return the loaded class   */  protected Class findClassImpl(String name)    throws ClassNotFoundException  {    if (_isVerbose)      verbose(name, "findClass");    if (_lifecycle.isDestroyed()) {      log().fine("Class loader has been closed.");      return super.findClass(name);    }    if (_lifecycle.isBeforeInit())      init();    /*    if (! _lifecycle.isActive())      return super.findClass(name);    */    // server/2439    name = name.replace('/', '.');    name = name.replace('\\', '.');    ClassEntry entry = null;    entry = _entryCache == null ? null : _entryCache.get(name);    if (entry == null) {      int len = _loaders.size();      // special case for osgi      for (int i = 0; i < len; i++) {	Class cl = _loaders.get(i).loadClass(name);	if (cl != null)	  return cl;      }            entry = getClassEntry(name);    }    if (entry == null)      return null;    if (entry != null && _isVerbose)      verbose(name, (isNormalJdkOrder(name) ? "found" : "found (took priority from parent)"));    if (_isEnableDependencyCheck)      entry.addDependencies(_dependencies);    // Currently, the entry must be in the entry cache for synchronization    // to work.  The same entry must be returned for two separate threads    // trying to load the class at the same time.    synchronized (this) {      if (_entryCache == null)        _entryCache = new Hashtable<String,ClassEntry>(8);      ClassEntry oldEntry = _entryCache.get(name);      if (oldEntry != null)	entry = oldEntry;      else	_entryCache.put(name, entry);    }    try {      return loadClass(entry);    } catch (RuntimeException e) {      throw e;    } catch (ClassNotFoundException e) {      throw e;    } catch (Exception e) {      log().log(Level.FINE, e.toString(), e);      throw new ClassNotFoundException(name + " [" + e + "]", e);    }  }  /**   * Returns the matching class entry.   */  protected ClassEntry getClassEntry(String name)    throws ClassNotFoundException  {    String pathName = name.replace('.', '/') + ".class";        ArrayList<Loader> loaders = getLoaders();    for (int i = 0; i < loaders.size(); i++) {      Loader loader = loaders.get(i);      ClassEntry entry = loader.getClassEntry(name, pathName);      if (entry != null)        return entry;    }    return null;  }  /**   * Loads the class from the loader.  The loadClass must be in the   * top classLoader because the defineClass must be owned by the top.   */  protected Class loadClass(ClassEntry entry)      throws IOException, ClassNotFoundException  {    synchronized (entry) {      Class cl = entry.getEntryClass();      if (cl != null)        return cl;      entry.preLoad();      String name = entry.getName();      int p = name.lastIndexOf('.');      if (p > 0) {        String packageName = name.substring(0, p);        Package pkg = getPackage(packageName);        ClassPackage classPackage = entry.getClassPackage();        if (pkg != null) {        }        else if (classPackage != null) {          definePackage(packageName,                        classPackage.getSpecificationTitle(),                        classPackage.getSpecificationVersion(),                        classPackage.getSpecificationVendor(),                        classPackage.getImplementationTitle(),                        classPackage.getImplementationVersion(),                        classPackage.getImplementationVendor(),                        null);        }        else {          definePackage(packageName,                        null,                        null,                        null,                        null,                        null,                        null,                        null);        }      }      ByteBuffer buffer = new ByteBuffer();      entry.load(buffer);      byte []bBuf = buffer.getBuffer();      int bLen = buffer.length();      if (_classFileTransformerList != null) {	Class redefineClass = null;	String className = name.replace('.', '/');	if (bBuf.length != bLen) {	  byte []tempBuf = new byte[bLen];	  System.arraycopy(bBuf, 0, tempBuf, 0, bLen);	  bBuf = tempBuf;	}	ProtectionDomain protectionDomain = null;		for (int i = 0; i < _classFileTransformerList.size(); i++) {	  ClassFileTransformer transformer = _classFileTransformerList.get(i);	  	  try {	    byte []enhancedBuffer = transformer.transform(this,							  className,							  redefineClass,							  protectionDomain,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?