classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: currentClassLoader problem


From: Archie Cobbs
Subject: Re: currentClassLoader problem
Date: Wed, 17 Mar 2004 17:39:28 -0600 (CST)

Tom Tromey wrote:
> On the subject of VMClassLoader, I had to make some changes to
> VMClassLoader.getSystemClassLoader to make it more correct.  At least, 
> according to my reading of the spec.
> 
> In particular, I think a class loader specified by
> java.system.class.loader has to be loaded with the default system
> class loader, but Classpath doesn't do this.
> 
> I've appended my version of VMClassLoader.getSystemClassLoader.
> It has some gcj-specific bits in it.  I'd be curious to hear what
> other people think...

Your analysis seems correct to me.. ie, Classpath is erroneously
using the bootstrap loader instead of the default system class loader
as the loader and parent for the user-specified class loader.

Is is correct that getSystemClassLoaderInternal() is a gcj-specific
method? And why is native code needed, i.e., why can't you just
load an instance of gnu.java.lang.SystemClassLoader (or
gnu.gcj.runtime.VMClassLoader in gcj's case) using the bootstrap
loader, and then use it to load the user-specified class loader?

E.g. something like this (not handling exceptions here):

  static ClassLoader getSystemClassLoader()
  {
    // Get default system class loader.
    // Load class using bootstrap class loader.
    String defaultLoaderClass = "gnu.java.lang.SystemClassLoader");
    ClassLoader systemClassLoader = Class.forName(defaultLoaderClass,
      true, null).getConstructor(new Class[] { ClassLoader.class })
      .newInstance(new Object[1]);

    // Get user-specified system class loader class, if any
    String loaderClass = System.getProperty("java.system.class.loader",
      defaultLoaderClass);

    // Replace system class loader with user-specified loader if different.
    // Load class using default system class loader.
    if (!loaderClass.equals(defaultLoaderClass))
    {
      systemClassLoader = Class.forName(loaderClass, true, systemClassLoader)
        .getConstructor(new Class[] { ClassLoader.class })
        .newInstance(new Object[] { systemClassLoader });
    }

    // Done
    return systemClassLoader;
  }

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]