sun.reflect.misc.ReflectUtil.checkPackageAccess(declaringClass)
contains the comment:

// FIXME: not sure what to check here.

As its name suggests, I think it's intended to call
SecurityManager.checkPackageAccess() to make sure the current
thread is allowed to access the package of the declaringClass.

So, all we need to do is find the package names and pass it to the
current security mamager.

Here's a simple test case that you can try with your favourite class
library:
------------------------------------------------------------------------
package xxx;

import java.net.*;
import java.lang.reflect.*;

class MySecurityManager extends SecurityManager
{
  public void checkPackageAccess(String s)
  {
    System.out.println("checkPackageAccess " + s);
  }

}

public class ttt
{
  public static void main(String[] argv) throws Exception
  {
    System.setSecurityManager(new MySecurityManager());
    ttt[] a = new ttt[4];
    System.out.println("Checking " + a.getClass());
    sun.reflect.misc.ReflectUtil.checkPackageAccess(a.getClass());
  }
}
------------------------------------------------------------------------

Andrew.



2006-11-27  Andrew Haley  <[EMAIL PROTECTED]>

        * sun/reflect/misc/ReflectUtil.java (checkPackageAccess):
        Implement.

Index: libjava/sun/reflect/misc/ReflectUtil.java
===================================================================
--- libjava/sun/reflect/misc/ReflectUtil.java   (revision 119120)
+++ libjava/sun/reflect/misc/ReflectUtil.java   (working copy)
@@ -51,9 +51,29 @@
   {
   }

+  /**
+   * Check if the current thread is allowed to access the package of
+   * the declaringClass.
+   *
+   * @param declaringClass class name to check access to
+   * @throws SecurityException if permission is denied
+   * @throws NullPointerException if declaringClass is null
+   */
   public static void checkPackageAccess(Class declaringClass)
   {
-    // FIXME: not sure what to check here.
+    SecurityManager sm;
+    if ((sm = System.getSecurityManager()) != null)
+      {
+       while (declaringClass.isArray())
+         declaringClass = declaringClass.getComponentType();
+       String name = declaringClass.getName();
+       int i = name.lastIndexOf('.');
+       if (i != -1) // if declaringClass is a member of a package
+         {
+           name = name.substring(0, i);
+           sm.checkPackageAccess(name);
+         }
+      }
   }

   /**
I

Reply via email to