1 | // Native code for VMClassLoader
|
---|
2 |
|
---|
3 | /* Copyright (C) 2002 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | #include <config.h>
|
---|
12 |
|
---|
13 | #include <gcj/cni.h>
|
---|
14 | #include <jvm.h>
|
---|
15 |
|
---|
16 | #include <gnu/gcj/runtime/VMClassLoader.h>
|
---|
17 | #include <java/lang/Class.h>
|
---|
18 | #include <java/lang/StringBuffer.h>
|
---|
19 | #include <java/net/URLClassLoader.h>
|
---|
20 | #include <java/lang/Runtime.h>
|
---|
21 |
|
---|
22 | jclass
|
---|
23 | gnu::gcj::runtime::VMClassLoader::findClass (jstring name)
|
---|
24 | {
|
---|
25 | _Jv_Utf8Const *name_u = _Jv_makeUtf8Const (name);
|
---|
26 | jclass klass = _Jv_FindClassInCache (name_u, 0);
|
---|
27 |
|
---|
28 | if (! klass)
|
---|
29 | {
|
---|
30 | // Turn `gnu.pkg.quux' into `lib-gnu-pkg-quux'. Then search for
|
---|
31 | // a module named (eg, on Linux) `lib-gnu-pkg-quux.so', followed
|
---|
32 | // by `lib-gnu-pkg.so' and `lib-gnu.so'. If loading one of
|
---|
33 | // these causes the class to appear in the cache, then use it.
|
---|
34 | java::lang::StringBuffer *sb = new java::lang::StringBuffer (JvNewStringLatin1("lib-"));
|
---|
35 | // Skip inner classes
|
---|
36 | jstring cn;
|
---|
37 | jint ci = name->indexOf('$');
|
---|
38 | if (ci == -1)
|
---|
39 | cn = name;
|
---|
40 | else
|
---|
41 | cn = name->substring (0, ci);
|
---|
42 | jstring so_base_name = (sb->append (cn)->toString ())->replace ('.', '-');
|
---|
43 |
|
---|
44 | // Compare against `3' because that is the length of "lib".
|
---|
45 | while (! klass && so_base_name && so_base_name->length() > 3)
|
---|
46 | {
|
---|
47 | using namespace ::java::lang;
|
---|
48 | Runtime *rt = Runtime::getRuntime();
|
---|
49 | jboolean loaded = rt->loadLibraryInternal (so_base_name);
|
---|
50 |
|
---|
51 | jint nd = so_base_name->lastIndexOf ('-');
|
---|
52 | if (nd == -1)
|
---|
53 | so_base_name = NULL;
|
---|
54 | else
|
---|
55 | so_base_name = so_base_name->substring (0, nd);
|
---|
56 |
|
---|
57 | if (loaded)
|
---|
58 | klass = _Jv_FindClassInCache (name_u, 0);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Now try loading using the interpreter.
|
---|
63 | if (! klass)
|
---|
64 | klass = java::net::URLClassLoader::findClass (name);
|
---|
65 |
|
---|
66 | return klass;
|
---|
67 | }
|
---|