Patch: FYI: load lib-x-y.so from bootstrap loader
Tom Tromey
tromey@redhat.com
Fri Apr 1 19:18:00 GMT 2005
I'm checking this in on the trunk.
The endorsed dirs patch incorrectly put lib-x-y.so loading into the
extension class loader. However, we actually want to load these
classes from the bootstrap loader. That is the only way that, say,
loading AWT peers will work.
This patch fixes this. This let us get rid of a duplicate copy of
definePackageForNative(). I also renamed a class in here, since
having two classes named VMClassLoader was confusing, especially since
one of them was really just the extension class loader.
This also fixes a small spec divergence where we were calling the
2-argument loadClass from the internals.
Tom
Index: ChangeLog
from Tom Tromey <tromey@redhat.com>
* java/lang/natVMClassLoader.cc (getSystemClassLoaderInternal):
Updated for name change.
(nativeFindClass): New method.
(loadClass): Use nativeFindClass.
* java/lang/natClassLoader.cc (_Jv_FindClass): Use single-argument
form of loadClass.
* java/lang/VMClassLoader.java (tried_libraries, lib_control,
LIB_FULL, LIB_CACHE, LIB_NEVER): New fields from old
VMClassLoader.
(initialize): New method.
(nativeFindClass): Declare.
* gnu/gcj/runtime/natVMClassLoader.cc: Removed.
* gnu/gcj/runtime/VMClassLoader.java: Removed.
* gnu/gcj/runtime/ExtensionClassLoader.java: Renamed from
VMClassLoader.java.
(definePackageForNative): Removed.
(tried_libraries, LIB_CACHE, LIB_FULL, LIB_NEVER, lib_control):
Moved to VMClassLoader.java.
* prims.cc (_Jv_CreateJavaVM): Updated for renaming.
* Makefile.am (gnu/gcj/runtime/ExtensionClassLoader.h): Renamed.
(ordinary_java_source_files): Added ExtensionClassLoader.java,
removed VMClassLoader.java.
(nat_source_files): Removed natVMClassLoader.cc.
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.470
diff -u -r1.470 Makefile.am
--- Makefile.am 1 Apr 2005 17:54:26 -0000 1.470
+++ Makefile.am 1 Apr 2005 19:11:37 -0000
@@ -811,7 +811,7 @@
$(GCJH) -classpath '' -bootclasspath $(top_builddir) \
'java/lang/reflect/Proxy$$ProxyType'
-gnu/gcj/runtime/VMClassLoader.h: gnu/gcj/runtime/VMClassLoader.class
+gnu/gcj/runtime/ExtensionClassLoader.h: gnu/gcj/runtime/ExtensionClassLoader.class
$(GCJH) -classpath '' -bootclasspath $(top_builddir) \
-friend 'class ::java::lang::ClassLoader;' \
$(basename $<)
@@ -2991,6 +2991,7 @@
gnu/gcj/io/MimeTypes.java \
gnu/gcj/io/SimpleSHSStream.java \
gnu/gcj/runtime/BootClassLoader.java \
+gnu/gcj/runtime/ExtensionClassLoader.java \
gnu/gcj/runtime/FileDeleter.java \
gnu/gcj/runtime/FinalizerThread.java \
gnu/gcj/runtime/HelperClassLoader.java \
@@ -3001,7 +3002,6 @@
gnu/gcj/runtime/SharedLibLoader.java \
gnu/gcj/runtime/StringBuffer.java \
gnu/gcj/runtime/SystemClassLoader.java \
-gnu/gcj/runtime/VMClassLoader.java \
gnu/gcj/util/Debug.java \
gnu/java/io/ASN1ParsingException.java \
gnu/java/io/Base64InputStream.java \
@@ -3760,7 +3760,6 @@
gnu/gcj/runtime/natFinalizerThread.cc \
gnu/gcj/runtime/natSharedLibLoader.cc \
gnu/gcj/runtime/natStringBuffer.cc \
-gnu/gcj/runtime/natVMClassLoader.cc \
gnu/gcj/util/natDebug.cc \
gnu/java/lang/natMainThread.cc \
gnu/java/net/natPlainDatagramSocketImpl.cc \
Index: prims.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/prims.cc,v
retrieving revision 1.106
diff -u -r1.106 prims.cc
--- prims.cc 24 Mar 2005 00:04:02 -0000 1.106
+++ prims.cc 1 Apr 2005 19:11:43 -0000
@@ -60,7 +60,7 @@
#include <java/io/PrintStream.h>
#include <java/lang/UnsatisfiedLinkError.h>
#include <java/lang/VirtualMachineError.h>
-#include <gnu/gcj/runtime/VMClassLoader.h>
+#include <gnu/gcj/runtime/ExtensionClassLoader.h>
#include <gnu/gcj/runtime/FinalizerThread.h>
#include <execution.h>
#include <gnu/java/lang/MainThread.h>
@@ -1132,8 +1132,8 @@
_Jv_InitClass (&java::lang::ClassLoader::class$);
// Set up the system class loader and the bootstrap class loader.
- gnu::gcj::runtime::VMClassLoader::initialize();
- java::lang::VMClassLoader::initBootLoader(JvNewStringLatin1(TOOLEXECLIBDIR));
+ gnu::gcj::runtime::ExtensionClassLoader::initialize();
+ java::lang::VMClassLoader::initialize(JvNewStringLatin1(TOOLEXECLIBDIR));
_Jv_RegisterBootstrapPackages();
Index: gnu/gcj/runtime/ExtensionClassLoader.java
===================================================================
RCS file: gnu/gcj/runtime/ExtensionClassLoader.java
diff -N gnu/gcj/runtime/ExtensionClassLoader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gnu/gcj/runtime/ExtensionClassLoader.java 1 Apr 2005 19:11:43 -0000
@@ -0,0 +1,40 @@
+/* Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+/* Author: Kresten Krab Thorup <krab@gnu.org> */
+
+package gnu.gcj.runtime;
+
+import java.net.URL;
+
+// The extension loader for libgcj. Class loader bootstrap is a bit
+// tricky, see prims.cc and SystemClassLoader for some details.
+public final class ExtensionClassLoader extends HelperClassLoader
+{
+ private ExtensionClassLoader ()
+ {
+ }
+
+ private void init()
+ {
+ addDirectoriesFromProperty("java.ext.dirs");
+ }
+
+ // This can be package-private because we only call it from native
+ // code during startup.
+ static void initialize ()
+ {
+ instance.init();
+ system_instance.init();
+ }
+
+ // The only ExtensionClassLoader that can exist.
+ static ExtensionClassLoader instance = new ExtensionClassLoader();
+ // The system class loader.
+ static SystemClassLoader system_instance = new SystemClassLoader(instance);
+}
Index: gnu/gcj/runtime/VMClassLoader.java
===================================================================
RCS file: gnu/gcj/runtime/VMClassLoader.java
diff -N gnu/gcj/runtime/VMClassLoader.java
--- gnu/gcj/runtime/VMClassLoader.java 29 Mar 2005 21:47:03 -0000 1.19
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,94 +0,0 @@
-/* Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-/* Author: Kresten Krab Thorup <krab@gnu.org> */
-
-package gnu.gcj.runtime;
-
-import java.net.URL;
-import java.util.HashSet;
-
-// Despite its name, this class is really the extension loader for
-// libgcj. Class loader bootstrap is a bit tricky, see prims.cc and
-// SystemClassLoader for some details.
-public final class VMClassLoader extends HelperClassLoader
-{
- private VMClassLoader ()
- {
- String p
- = System.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
- "");
- if ("never".equals(p))
- lib_control = LIB_NEVER;
- else if ("cache".equals(p))
- lib_control = LIB_CACHE;
- else if ("full".equals(p))
- lib_control = LIB_FULL;
- else
- lib_control = LIB_CACHE;
- }
-
- private void init()
- {
- addDirectoriesFromProperty("java.ext.dirs");
- }
-
- /** This is overridden to search the internal hash table, which
- * will only search existing linked-in classes. This will make
- * the default implementation of loadClass (in ClassLoader) work right.
- * The implementation of this method is in
- * gnu/gcj/runtime/natVMClassLoader.cc.
- */
- protected native Class findClass(String name)
- throws java.lang.ClassNotFoundException;
-
- // This can be package-private because we only call it from native
- // code during startup.
- static void initialize ()
- {
- instance.init();
- system_instance.init();
- }
-
- // Define a package for something loaded natively.
- void definePackageForNative(String className)
- {
- int lastDot = className.lastIndexOf('.');
- if (lastDot != -1)
- {
- String packageName = className.substring(0, lastDot);
- if (getPackage(packageName) == null)
- {
- // FIXME: this assumes we're defining the core, which
- // isn't necessarily so. We could detect this and set up
- // appropriately. We could also look at a manifest file
- // compiled into the .so.
- definePackage(packageName, "Java Platform API Specification",
- "GNU", "1.4", "gcj", "GNU",
- null, // FIXME: gcj version.
- null);
- }
- }
- }
-
- // This keeps track of shared libraries we've already tried to load.
- private HashSet tried_libraries = new HashSet();
-
- // Holds one of the LIB_* constants; used to determine how shared
- // library loads are done.
- private int lib_control;
-
- // The only VMClassLoader that can exist.
- static VMClassLoader instance = new VMClassLoader();
- // The system class loader.
- static SystemClassLoader system_instance = new SystemClassLoader(instance);
-
- private static final int LIB_FULL = 0;
- private static final int LIB_CACHE = 1;
- private static final int LIB_NEVER = 2;
-}
Index: gnu/gcj/runtime/natVMClassLoader.cc
===================================================================
RCS file: gnu/gcj/runtime/natVMClassLoader.cc
diff -N gnu/gcj/runtime/natVMClassLoader.cc
--- gnu/gcj/runtime/natVMClassLoader.cc 10 Jan 2005 19:39:25 -0000 1.3
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,79 +0,0 @@
-// Native code for VMClassLoader
-
-/* Copyright (C) 2002, 2003, 2005 Free Software Foundation
-
- This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
-details. */
-
-#include <config.h>
-
-#include <gcj/cni.h>
-#include <jvm.h>
-
-#include <gnu/gcj/runtime/VMClassLoader.h>
-#include <java/lang/Class.h>
-#include <java/lang/StringBuffer.h>
-#include <java/net/URLClassLoader.h>
-#include <java/lang/Runtime.h>
-#include <java/util/HashSet.h>
-
-jclass
-gnu::gcj::runtime::VMClassLoader::findClass (jstring name)
-{
- _Jv_Utf8Const *name_u = _Jv_makeUtf8Const (name);
- jclass klass = _Jv_FindClassInCache (name_u);
-
- if (! klass && lib_control != LIB_NEVER)
- {
- // Turn `gnu.pkg.quux' into `lib-gnu-pkg-quux'. Then search for
- // a module named (eg, on Linux) `lib-gnu-pkg-quux.so', followed
- // by `lib-gnu-pkg.so' and `lib-gnu.so'. If loading one of
- // these causes the class to appear in the cache, then use it.
- java::lang::StringBuffer *sb = new java::lang::StringBuffer (JvNewStringLatin1("lib-"));
- // Skip inner classes
- jstring cn;
- jint ci = name->indexOf('$');
- if (ci == -1)
- cn = name;
- else
- cn = name->substring (0, ci);
- jstring so_base_name = (sb->append (cn)->toString ())->replace ('.', '-');
-
- using namespace ::java::lang;
- Runtime *rt = Runtime::getRuntime();
-
- // Compare against `3' because that is the length of "lib".
- while (! klass && so_base_name && so_base_name->length() > 3)
- {
- if (lib_control == LIB_CACHE)
- {
- // If we've already tried this name, we're done.
- if (tried_libraries->contains(so_base_name))
- break;
- tried_libraries->add(so_base_name);
- }
-
- jboolean loaded = rt->loadLibraryInternal (so_base_name);
-
- jint nd = so_base_name->lastIndexOf ('-');
- if (nd == -1)
- so_base_name = NULL;
- else
- so_base_name = so_base_name->substring (0, nd);
-
- if (loaded)
- klass = _Jv_FindClassInCache (name_u);
- }
- }
-
- // Either define the package, or try loading using the interpreter.
- if (klass)
- definePackageForNative(name);
- else
- klass = java::net::URLClassLoader::findClass (name);
-
- return klass;
-}
Index: java/lang/VMClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/VMClassLoader.java,v
retrieving revision 1.15
diff -u -r1.15 VMClassLoader.java
--- java/lang/VMClassLoader.java 24 Mar 2005 00:04:18 -0000 1.15
+++ java/lang/VMClassLoader.java 1 Apr 2005 19:11:44 -0000
@@ -51,6 +51,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.StringTokenizer;
import gnu.gcj.runtime.BootClassLoader;
@@ -87,6 +88,17 @@
// until we've initialized the system, at which point it is created.
static BootClassLoader bootLoader;
+ // This keeps track of shared libraries we've already tried to load.
+ private static HashSet tried_libraries;
+
+ // Holds one of the LIB_* constants; used to determine how shared
+ // library loads are done.
+ private static int lib_control;
+
+ private static final int LIB_FULL = 0;
+ private static final int LIB_CACHE = 1;
+ private static final int LIB_NEVER = 2;
+
/**
* Helper to define a class using a string of bytes. This assumes that
* the security checks have already been performed, if necessary.
@@ -298,6 +310,30 @@
static native void initBootLoader(String libdir);
+ static void initialize(String libdir)
+ {
+ initBootLoader(libdir);
+
+ String p
+ = System.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
+ "");
+ if ("never".equals(p))
+ lib_control = LIB_NEVER;
+ else if ("cache".equals(p))
+ lib_control = LIB_CACHE;
+ else if ("full".equals(p))
+ lib_control = LIB_FULL;
+ else
+ lib_control = LIB_CACHE;
+
+ tried_libraries = new HashSet();
+ }
+
+ /**
+ * Possibly load a .so and search it for classes.
+ */
+ static native Class nativeFindClass(String name);
+
static ClassLoader getSystemClassLoader()
{
// This method is called as the initialization of systemClassLoader,
Index: java/lang/natClassLoader.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClassLoader.cc,v
retrieving revision 1.78
diff -u -r1.78 natClassLoader.cc
--- java/lang/natClassLoader.cc 24 Mar 2005 00:04:19 -0000 1.78
+++ java/lang/natClassLoader.cc 1 Apr 2005 19:11:44 -0000
@@ -26,7 +26,6 @@
#include <java/lang/Character.h>
#include <java/lang/Thread.h>
#include <java/lang/ClassLoader.h>
-#include <gnu/gcj/runtime/VMClassLoader.h>
#include <java/lang/InternalError.h>
#include <java/lang/IllegalAccessError.h>
#include <java/lang/LinkageError.h>
@@ -226,8 +225,9 @@
{
if (loader)
{
- // Load using a user-defined loader, jvmspec 5.3.2
- klass = loader->loadClass(sname, false);
+ // Load using a user-defined loader, jvmspec 5.3.2.
+ // Note that we explicitly must call the single-argument form.
+ klass = loader->loadClass(sname);
// If "loader" delegated the loadClass operation to another
// loader, explicitly register that it is also an initiating
Index: java/lang/natVMClassLoader.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natVMClassLoader.cc,v
retrieving revision 1.8
diff -u -r1.8 natVMClassLoader.cc
--- java/lang/natVMClassLoader.cc 24 Mar 2005 00:04:20 -0000 1.8
+++ java/lang/natVMClassLoader.cc 1 Apr 2005 19:11:44 -0000
@@ -23,7 +23,7 @@
#include <java/lang/VMClassLoader.h>
#include <java/lang/VMCompiler.h>
-#include <gnu/gcj/runtime/VMClassLoader.h>
+#include <gnu/gcj/runtime/ExtensionClassLoader.h>
#include <gnu/gcj/runtime/SystemClassLoader.h>
#include <gnu/gcj/runtime/BootClassLoader.h>
#include <java/lang/ClassLoader.h>
@@ -31,6 +31,9 @@
#include <java/lang/Throwable.h>
#include <java/security/ProtectionDomain.h>
#include <java/lang/ClassFormatError.h>
+#include <java/lang/StringBuffer.h>
+#include <java/lang/Runtime.h>
+#include <java/util/HashSet.h>
void
java::lang::VMClassLoader::resolveClass (jclass klass)
@@ -110,8 +113,8 @@
java::lang::ClassLoader *
java::lang::VMClassLoader::getSystemClassLoaderInternal()
{
- _Jv_InitClass (&gnu::gcj::runtime::VMClassLoader::class$);
- return gnu::gcj::runtime::VMClassLoader::system_instance;
+ _Jv_InitClass (&gnu::gcj::runtime::ExtensionClassLoader::class$);
+ return gnu::gcj::runtime::ExtensionClassLoader::system_instance;
}
jclass
@@ -130,6 +133,68 @@
}
jclass
+java::lang::VMClassLoader::nativeFindClass (jstring name)
+{
+ jclass klass = NULL;
+
+ if (lib_control != LIB_NEVER)
+ {
+ // Turn `gnu.pkg.quux' into `lib-gnu-pkg-quux'. Then search for
+ // a module named (eg, on Linux) `lib-gnu-pkg-quux.so', followed
+ // by `lib-gnu-pkg.so' and `lib-gnu.so'. If loading one of
+ // these causes the class to appear in the cache, then use it.
+ java::lang::StringBuffer *sb
+ = new java::lang::StringBuffer (JvNewStringLatin1("lib-"));
+ // Skip inner classes
+ jstring cn;
+ jint ci = name->indexOf('$');
+ if (ci == -1)
+ cn = name;
+ else
+ cn = name->substring (0, ci);
+ jstring so_base_name
+ = (sb->append (cn)->toString ())->replace ('.', '-');
+
+ using namespace ::java::lang;
+ Runtime *rt = Runtime::getRuntime();
+
+ _Jv_Utf8Const *name_u = NULL;
+
+ // Compare against `3' because that is the length of "lib".
+ while (! klass && so_base_name && so_base_name->length() > 3)
+ {
+ if (lib_control == LIB_CACHE)
+ {
+ // If we've already tried this name, we're done.
+ if (tried_libraries->contains(so_base_name))
+ break;
+ tried_libraries->add(so_base_name);
+ }
+
+ jboolean loaded = rt->loadLibraryInternal (so_base_name);
+
+ jint nd = so_base_name->lastIndexOf ('-');
+ if (nd == -1)
+ so_base_name = NULL;
+ else
+ so_base_name = so_base_name->substring (0, nd);
+
+ if (loaded)
+ {
+ if (name_u == NULL)
+ name_u = _Jv_makeUtf8Const (name);
+ klass = _Jv_FindClassInCache (name_u);
+ }
+ }
+ }
+
+ if (klass)
+ definePackageForNative(name);
+
+ return klass;
+}
+
+jclass
java::lang::VMClassLoader::loadClass(jstring name, jboolean resolve)
{
// We try the boot loader first, so that the endorsed directory
@@ -142,6 +207,8 @@
_Jv_Utf8Const *utf = _Jv_makeUtf8Const (name);
klass = _Jv_FindClassInCache (utf);
}
+ if (! klass)
+ klass = nativeFindClass(name);
if (klass)
{
// We never want to return a class without its supers linked.
More information about the Java-patches
mailing list