classpath-patches
[Top][All Lists]
Advanced

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

[cp-patches] RFC: implementation of getPackage for the vm reference


From: Nicolas Geoffray
Subject: [cp-patches] RFC: implementation of getPackage for the vm reference
Date: Fri, 21 Oct 2005 23:28:16 +0200
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051019)

Hi all,

This patch was written when we wanted to test our vm and gnu classpath with jonas. Jonas complained (crashed) because it got a null package for a system class. This
patch corrects the issue, and should be adequate for all vms.

A new hashmap is added to store the bootstrap classloader's packages.
A new method is defined inside VMClassLoader, called getVMPackage. This method shall only be called by Class.getPackage when the class' classloader is null. It looks for the
package in definedPackages, and adds if it's not found. Therefore, only
a system class can add a package to definedPackages. The VMClassLoader.getPackage
method only looks for the package, it doesn't add one.


2005-10-21  Nicolas Geoffray  <address@hidden>

        Reported by: Gael Thomas <address@hidden>
       * vm/reference/java/lang/VMClassLoader.java:
        Added new definedPackages field to store packages
        loaded by the bootstrap classloader
        (getVMPackage): new method. Finds or adds a package.
        Shall only be called by Class.getPackage
        (getPackage): uses the new definedPackages field
        (getPackages): uses the new definedPackages field
        * java/lang/Class.java:
        (getPackage): if the classloader of the class is null
        then call VMClassLoader.getVMPackage


Nicolas
Index: vm/reference/java/lang/VMClassLoader.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/vm/reference/java/lang/VMClassLoader.java,v
retrieving revision 1.32
diff -u -r1.32 VMClassLoader.java
--- vm/reference/java/lang/VMClassLoader.java   21 Oct 2005 10:47:17 -0000      
1.32
+++ vm/reference/java/lang/VMClassLoader.java   21 Oct 2005 21:03:37 -0000
@@ -40,6 +40,7 @@
 package java.lang;
 
 import gnu.classpath.SystemProperties;
+import gnu.classpath.Configuration;
 
 import java.io.File;
 import java.io.IOException;
@@ -190,29 +191,75 @@
     return v.elements();
   }
 
+
+  /** packages loaded by the bootstrap class loader */
+  static final HashMap definedPackages = new HashMap();
+
+
   /**
-   * Helper to get a package from the bootstrap class loader.  The default
-   * implementation of returning null may be adequate, or you may decide
-   * that this needs some native help.
+   * Helper to get a package from the bootstrap class loader.
    *
    * @param name the name to find
    * @return the named package, if it exists
    */
   static Package getPackage(String name)
   {
-    return null;
+    synchronized(definedPackages)
+      {
+        return (Package)definedPackages.get(name);
+      }
+  }
+
+
+  /**
+   * Helper to find/add a package to the bootstrap class loader.
+   * This method shall only be called by Class.getPackage()
+   *
+   * @param name the name to find/add
+   * @return the named package, if it exists or can be created
+   */
+  static final Package getVMPackage(String name)
+  {
+
+    // name is empty for primitive types
+    if (name == "") return null;
+
+    Package p = getPackage(name);
+ 
+    if (p == null)
+      {
+        p = new Package(name,
+              System.getProperty("java.specification.name"),
+              System.getProperty("java.specification.vendor"),
+              System.getProperty("java.specification.version"),
+              "GNU Classpath",
+              "GNU",
+              Configuration.CLASSPATH_VERSION,
+              null);
+
+        synchronized (definedPackages)
+          {
+            definedPackages.put(name, p);
+          }
+      }
+    return p;
   }
 
+  
   /**
-   * Helper to get all packages from the bootstrap class loader.  The default
-   * implementation of returning an empty array may be adequate, or you may
-   * decide that this needs some native help.
+   * Helper to get all packages from the bootstrap class loader.  
    *
    * @return all named packages, if any exist
    */
   static Package[] getPackages()
   {
-    return new Package[0];
+    Package[] packages;
+    synchronized(definedPackages)
+      {
+        packages = new Package[definedPackages.size()];
+        definedPackages.values().toArray(packages);
+      }
+    return packages;
   }
 
   /**
Index: java/lang/Class.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/Class.java,v
retrieving revision 1.40
diff -u -r1.40 Class.java
--- java/lang/Class.java        15 Sep 2005 14:42:36 -0000      1.40
+++ java/lang/Class.java        21 Oct 2005 21:06:12 -0000
@@ -594,7 +594,8 @@
     ClassLoader cl = getClassLoader();
     if (cl != null)
       return cl.getPackage(getPackagePortion(getName()));
-    return null;
+    else
+      return VMClassLoader.getVMPackage(getPackagePortion(getName()));
   }
 
   /**

reply via email to

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