summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorArchie Cobbs <archie@dellroad.org>2005-01-07 15:08:21 +0000
committerArchie Cobbs <archie@dellroad.org>2005-01-07 15:08:21 +0000
commitec858596f78dd4ddc318edd3d3f4ec537780c854 (patch)
treeea7ebb6d209ae7bbc3a1c34e3bbbea043cfbc3ec /java
parent53a43c88b4600340bd75f20120d26ed6afdc2065 (diff)
downloadclasspath-ec858596f78dd4ddc318edd3d3f4ec537780c854.tar.gz
Re-committing now that the 0.13 release has been tagged:
* NEWS: Document changes. * java/lang/Class.java (newInstance(), getClassLoader(), forName(String), forName(String, boolean, ClassLoader)): Use new VMStackWalker methods. * java/lang/ClassLoader.java (getParent(), getSystemClassLoader()): Likewise. * java/lang/Package.java (getPackages()): Likewise. * java/lang/SecurityManager.java (getClassContext()): Likewise. * java/util/ResourceBundle.java (getBundle()): Likewise. * java/lang/Runtime.java (load(), loadLibrary()): Load the native library using the calling class' class loader. * java/lang/System.java (load(), loadLibrary()): Likewise. (currentClassLoader()): implement via currentLoadedClass(). * vm/reference/gnu/classpath/VMStackWalker.java: New class. * vm/reference/java/lang/VMRuntime.java (nativeLoad()): Add a ClassLoader parameter. * vm/reference/java/lang/VMSecurityManager.java: Removed.
Diffstat (limited to 'java')
-rw-r--r--java/lang/Class.java19
-rw-r--r--java/lang/ClassLoader.java7
-rw-r--r--java/lang/Package.java7
-rw-r--r--java/lang/Runtime.java72
-rw-r--r--java/lang/SecurityManager.java10
-rw-r--r--java/lang/System.java14
-rw-r--r--java/util/ResourceBundle.java47
7 files changed, 91 insertions, 85 deletions
diff --git a/java/lang/Class.java b/java/lang/Class.java
index 7bd3a3b09..d3bc1dea4 100644
--- a/java/lang/Class.java
+++ b/java/lang/Class.java
@@ -38,6 +38,8 @@ exception statement from your version. */
package java.lang;
+import gnu.classpath.VMStackWalker;
+
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
@@ -157,7 +159,7 @@ public final class Class implements Serializable
Class result = VMClass.forName (name);
if (result == null)
result = Class.forName(name, true,
- VMSecurityManager.getClassContext()[1].getClassLoader());
+ VMStackWalker.getCallingClassLoader());
return result;
}
@@ -198,9 +200,8 @@ public final class Class implements Serializable
SecurityManager sm = SecurityManager.current;
if (sm != null)
{
- // Get the calling class and classloader
- Class c = VMSecurityManager.getClassContext()[1];
- ClassLoader cl = c.getClassLoader();
+ // Get the calling classloader
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl != null)
sm.checkPermission(new RuntimePermission("getClassLoader"));
}
@@ -278,9 +279,8 @@ public final class Class implements Serializable
SecurityManager sm = SecurityManager.current;
if (sm != null)
{
- // Get the calling class and classloader
- Class c = VMSecurityManager.getClassContext()[1];
- ClassLoader cl = VMClass.getClassLoader(c);
+ // Get the calling classloader
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl != null && !cl.isAncestorOf(loader))
sm.checkPermission(new RuntimePermission("getClassLoader"));
}
@@ -1132,8 +1132,9 @@ public final class Class implements Serializable
int modifiers = constructor.getModifiers();
if (!Modifier.isPublic(modifiers))
{
- Class caller = VMSecurityManager.getClassContext()[1];
- if (caller != this &&
+ Class caller = VMStackWalker.getCallingClass();
+ if (caller != null &&
+ caller != this &&
(Modifier.isPrivate(modifiers)
|| getClassLoader() != caller.getClassLoader()
|| !getPackagePortion(getName())
diff --git a/java/lang/ClassLoader.java b/java/lang/ClassLoader.java
index 943bab5ad..a5093136d 100644
--- a/java/lang/ClassLoader.java
+++ b/java/lang/ClassLoader.java
@@ -39,6 +39,7 @@ exception statement from your version. */
package java.lang;
import gnu.classpath.SystemProperties;
+import gnu.classpath.VMStackWalker;
import gnu.java.util.DoubleEnumeration;
import gnu.java.util.EmptyEnumeration;
@@ -520,8 +521,7 @@ public abstract class ClassLoader
SecurityManager sm = SecurityManager.current;
if (sm != null)
{
- Class c = VMSecurityManager.getClassContext()[1];
- ClassLoader cl = c.getClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl != null && ! cl.isAncestorOf(this))
sm.checkPermission(new RuntimePermission("getClassLoader"));
}
@@ -763,8 +763,7 @@ public abstract class ClassLoader
SecurityManager sm = SecurityManager.current;
if (sm != null)
{
- Class c = VMSecurityManager.getClassContext()[1];
- ClassLoader cl = c.getClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl != null && cl != StaticData.systemClassLoader)
sm.checkPermission(new RuntimePermission("getClassLoader"));
}
diff --git a/java/lang/Package.java b/java/lang/Package.java
index 89945cadd..23838d681 100644
--- a/java/lang/Package.java
+++ b/java/lang/Package.java
@@ -37,6 +37,8 @@ exception statement from your version. */
package java.lang;
+import gnu.classpath.VMStackWalker;
+
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
@@ -273,7 +275,7 @@ public class Package
public static Package getPackage(String name)
{
// Get the caller's classloader
- ClassLoader cl = VMSecurityManager.currentClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
return cl != null ? cl.getPackage(name) : null;
}
@@ -286,8 +288,7 @@ public class Package
public static Package[] getPackages()
{
// Get the caller's classloader
- Class c = VMSecurityManager.getClassContext()[1];
- ClassLoader cl = c.getClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
// Sun's implementation returns the packages loaded by the bootstrap
// classloader if cl is null, but right now our bootstrap classloader
// does not create any Packages.
diff --git a/java/lang/Runtime.java b/java/lang/Runtime.java
index 58d1f5c0d..d0e7ac593 100644
--- a/java/lang/Runtime.java
+++ b/java/lang/Runtime.java
@@ -39,6 +39,7 @@ exception statement from your version. */
package java.lang;
import gnu.classpath.SystemProperties;
+import gnu.classpath.VMStackWalker;
import java.io.File;
import java.io.IOException;
@@ -628,16 +629,33 @@ public class Runtime
* before the final ".so" if the VM was invoked by the name "java_g". There
* may be a security check, of <code>checkLink</code>.
*
+ * <p>
+ * The library is loaded using the class loader associated with the
+ * class associated with the invoking method.
+ *
* @param filename the file to load
* @throws SecurityException if permission is denied
* @throws UnsatisfiedLinkError if the library is not found
*/
public void load(String filename)
{
+ load(filename, VMStackWalker.getCallingClassLoader());
+ }
+
+ /**
+ * Same as <code>load(String)</code> but using the given loader.
+ *
+ * @param filename the file to load
+ * @param loader class loader, or <code>null</code> for the boot loader
+ * @throws SecurityException if permission is denied
+ * @throws UnsatisfiedLinkError if the library is not found
+ */
+ void load(String filename, ClassLoader loader)
+ {
SecurityManager sm = SecurityManager.current; // Be thread-safe!
if (sm != null)
sm.checkLink(filename);
- if (loadLib(filename) == 0)
+ if (loadLib(filename, loader) == 0)
throw new UnsatisfiedLinkError("Could not load library " + filename);
}
@@ -645,15 +663,16 @@ public class Runtime
* Do a security check on the filename and then load the native library.
*
* @param filename the file to load
+ * @param loader class loader, or <code>null</code> for the boot loader
* @return 0 on failure, nonzero on success
* @throws SecurityException if file read permission is denied
*/
- private static int loadLib(String filename)
+ private static int loadLib(String filename, ClassLoader loader)
{
SecurityManager sm = SecurityManager.current; // Be thread-safe!
if (sm != null)
sm.checkRead(filename);
- return VMRuntime.nativeLoad(filename);
+ return VMRuntime.nativeLoad(filename, loader);
}
/**
@@ -668,6 +687,10 @@ public class Runtime
* <code>System.mapLibraryName(libname)</code>. There may be a security
* check, of <code>checkLink</code>.
*
+ * <p>
+ * The library is loaded using the class loader associated with the
+ * class associated with the invoking method.
+ *
* @param libname the library to load
*
* @throws SecurityException if permission is denied
@@ -678,30 +701,37 @@ public class Runtime
*/
public void loadLibrary(String libname)
{
+ loadLibrary(libname, VMStackWalker.getCallingClassLoader());
+ }
+
+ /**
+ * Same as <code>loadLibrary(String)</code> but using the given loader.
+ *
+ * @param libname the library to load
+ * @param loader class loader, or <code>null</code> for the boot loader
+ * @throws SecurityException if permission is denied
+ * @throws UnsatisfiedLinkError if the library is not found
+ */
+ void loadLibrary(String libname, ClassLoader loader)
+ {
SecurityManager sm = SecurityManager.current; // Be thread-safe!
if (sm != null)
sm.checkLink(libname);
-
String filename;
- ClassLoader cl = VMSecurityManager.currentClassLoader();
- if (cl != null)
+ if (loader != null && (filename = loader.findLibrary(libname)) != null)
{
- filename = cl.findLibrary(libname);
- if (filename != null)
- {
- if (loadLib(filename) != 0)
- return;
- else
- throw new UnsatisfiedLinkError("Could not load library " + filename);
- }
+ if (loadLib(filename, loader) != 0)
+ return;
}
-
- filename = VMRuntime.mapLibraryName(libname);
- for (int i = 0; i < libpath.length; i++)
- if (loadLib(libpath[i] + filename) != 0)
- return;
-
- throw new UnsatisfiedLinkError("Could not find library " + libname + ".");
+ else
+ {
+ filename = VMRuntime.mapLibraryName(libname);
+ for (int i = 0; i < libpath.length; i++)
+ if (loadLib(libpath[i] + filename, loader) != 0)
+ return;
+ }
+ throw new UnsatisfiedLinkError("Native library `" + libname
+ + "' not found (as file `" + filename + "')");
}
/**
diff --git a/java/lang/SecurityManager.java b/java/lang/SecurityManager.java
index d5c47a8d8..f47b2ff83 100644
--- a/java/lang/SecurityManager.java
+++ b/java/lang/SecurityManager.java
@@ -38,6 +38,8 @@ exception statement from your version. */
package java.lang;
+import gnu.classpath.VMStackWalker;
+
import java.awt.AWTPermission;
import java.io.File;
import java.io.FileDescriptor;
@@ -179,7 +181,10 @@ public class SecurityManager
*/
protected Class[] getClassContext()
{
- return VMSecurityManager.getClassContext();
+ Class[] stack1 = VMStackWalker.getClassContext();
+ Class[] stack2 = new Class[stack1.length - 1];
+ System.arraycopy(stack1, 1, stack2, 0, stack1.length - 1);
+ return stack2;
}
/**
@@ -201,7 +206,8 @@ public class SecurityManager
*/
protected ClassLoader currentClassLoader()
{
- return VMSecurityManager.currentClassLoader();
+ Class cl = currentLoadedClass();
+ return cl != null ? cl.getClassLoader() : null;
}
/**
diff --git a/java/lang/System.java b/java/lang/System.java
index a639cac40..d0e32a3e5 100644
--- a/java/lang/System.java
+++ b/java/lang/System.java
@@ -39,6 +39,7 @@ exception statement from your version. */
package java.lang;
+import gnu.classpath.VMStackWalker;
import gnu.classpath.SystemProperties;
import java.io.InputStream;
@@ -480,6 +481,10 @@ public final class System
* check may be performed, <code>checkLink</code>. This just calls
* <code>Runtime.getRuntime().load(filename)</code>.
*
+ * <p>
+ * The library is loaded using the class loader associated with the
+ * class associated with the invoking method.
+ *
* @param filename the code file to load
* @throws SecurityException if permission is denied
* @throws UnsatisfiedLinkError if the file cannot be loaded
@@ -487,7 +492,7 @@ public final class System
*/
public static void load(String filename)
{
- Runtime.getRuntime().load(filename);
+ Runtime.getRuntime().load(filename, VMStackWalker.getCallingClassLoader());
}
/**
@@ -495,6 +500,10 @@ public final class System
* check may be performed, <code>checkLink</code>. This just calls
* <code>Runtime.getRuntime().load(filename)</code>.
*
+ * <p>
+ * The library is loaded using the class loader associated with the
+ * class associated with the invoking method.
+ *
* @param libname the library file to load
* @throws SecurityException if permission is denied
* @throws UnsatisfiedLinkError if the file cannot be loaded
@@ -502,7 +511,8 @@ public final class System
*/
public static void loadLibrary(String libname)
{
- Runtime.getRuntime().loadLibrary(libname);
+ Runtime.getRuntime().loadLibrary(libname,
+ VMStackWalker.getCallingClassLoader());
}
/**
diff --git a/java/util/ResourceBundle.java b/java/util/ResourceBundle.java
index 8432b9a46..16dfed9e8 100644
--- a/java/util/ResourceBundle.java
+++ b/java/util/ResourceBundle.java
@@ -39,6 +39,7 @@ exception statement from your version. */
package java.util;
+import gnu.classpath.VMStackWalker;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessController;
@@ -104,48 +105,6 @@ public abstract class ResourceBundle
private Locale locale;
/**
- * We override SecurityManager in order to access getClassContext().
- */
- private static final class Security extends SecurityManager
- {
- /**
- * Avoid accessor method of private constructor.
- */
- Security()
- {
- }
-
- /**
- * Return the ClassLoader of the class which called into this
- * ResourceBundle, or null if it cannot be determined.
- */
- ClassLoader getCallingClassLoader()
- {
- Class[] stack = getClassContext();
- for (int i = 0; i < stack.length; i++)
- {
- if (stack[i] != Security.class && stack[i] != ResourceBundle.class)
- return stack[i].getClassLoader();
- }
-
- return null;
- }
- }
-
- /** A security context for grabbing the correct class loader. */
- private static final Security security
- = (Security) AccessController.doPrivileged(new PrivilegedAction()
- {
- // This will always work since java.util classes have (all) system
- // permissions.
- public Object run()
- {
- return new Security();
- }
- }
- );
-
- /**
* The resource bundle cache.
*/
private static Map bundleCache;
@@ -256,7 +215,7 @@ public abstract class ResourceBundle
*/
public static ResourceBundle getBundle(String baseName)
{
- ClassLoader cl = security.getCallingClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return getBundle(baseName, Locale.getDefault(), cl);
@@ -276,7 +235,7 @@ public abstract class ResourceBundle
*/
public static ResourceBundle getBundle(String baseName, Locale locale)
{
- ClassLoader cl = security.getCallingClassLoader();
+ ClassLoader cl = VMStackWalker.getCallingClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return getBundle(baseName, locale, cl);