emul/src/main/java/java/lang/reflect/AccessibleObject.java
branchemul
changeset 553 388e48c0a37a
parent 258 21b390daf444
     1.1 --- a/emul/src/main/java/java/lang/reflect/AccessibleObject.java	Tue Dec 04 13:29:17 2012 +0100
     1.2 +++ b/emul/src/main/java/java/lang/reflect/AccessibleObject.java	Wed Jan 23 20:16:48 2013 +0100
     1.3 @@ -25,9 +25,6 @@
     1.4  
     1.5  package java.lang.reflect;
     1.6  
     1.7 -import java.security.AccessController;
     1.8 -import sun.reflect.Reflection;
     1.9 -import sun.reflect.ReflectionFactory;
    1.10  import java.lang.annotation.Annotation;
    1.11  
    1.12  /**
    1.13 @@ -57,14 +54,6 @@
    1.14  public class AccessibleObject implements AnnotatedElement {
    1.15  
    1.16      /**
    1.17 -     * The Permission object that is used to check whether a client
    1.18 -     * has sufficient privilege to defeat Java language access
    1.19 -     * control checks.
    1.20 -     */
    1.21 -    static final private java.security.Permission ACCESS_PERMISSION =
    1.22 -        new ReflectPermission("suppressAccessChecks");
    1.23 -
    1.24 -    /**
    1.25       * Convenience method to set the {@code accessible} flag for an
    1.26       * array of objects with a single security check (for efficiency).
    1.27       *
    1.28 @@ -91,11 +80,7 @@
    1.29       */
    1.30      public static void setAccessible(AccessibleObject[] array, boolean flag)
    1.31          throws SecurityException {
    1.32 -        SecurityManager sm = System.getSecurityManager();
    1.33 -        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
    1.34 -        for (int i = 0; i < array.length; i++) {
    1.35 -            setAccessible0(array[i], flag);
    1.36 -        }
    1.37 +        throw new SecurityException();
    1.38      }
    1.39  
    1.40      /**
    1.41 @@ -124,23 +109,7 @@
    1.42       * @see java.lang.RuntimePermission
    1.43       */
    1.44      public void setAccessible(boolean flag) throws SecurityException {
    1.45 -        SecurityManager sm = System.getSecurityManager();
    1.46 -        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
    1.47 -        setAccessible0(this, flag);
    1.48 -    }
    1.49 -
    1.50 -    /* Check that you aren't exposing java.lang.Class.<init>. */
    1.51 -    private static void setAccessible0(AccessibleObject obj, boolean flag)
    1.52 -        throws SecurityException
    1.53 -    {
    1.54 -        if (obj instanceof Constructor && flag == true) {
    1.55 -            Constructor<?> c = (Constructor<?>)obj;
    1.56 -            if (c.getDeclaringClass() == Class.class) {
    1.57 -                throw new SecurityException("Can not make a java.lang.Class" +
    1.58 -                                            " constructor accessible");
    1.59 -            }
    1.60 -        }
    1.61 -        obj.override = flag;
    1.62 +        throw new SecurityException();
    1.63      }
    1.64  
    1.65      /**
    1.66 @@ -165,13 +134,6 @@
    1.67      // outside this package.
    1.68      boolean override;
    1.69  
    1.70 -    // Reflection factory used by subclasses for creating field,
    1.71 -    // method, and constructor accessors. Note that this is called
    1.72 -    // very early in the bootstrapping process.
    1.73 -    static final ReflectionFactory reflectionFactory =
    1.74 -        AccessController.doPrivileged(
    1.75 -            new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
    1.76 -
    1.77      /**
    1.78       * @throws NullPointerException {@inheritDoc}
    1.79       * @since 1.5
    1.80 @@ -202,73 +164,4 @@
    1.81      public Annotation[] getDeclaredAnnotations()  {
    1.82          throw new AssertionError("All subclasses should override this method");
    1.83      }
    1.84 -
    1.85 -
    1.86 -    // Shared access checking logic.
    1.87 -
    1.88 -    // For non-public members or members in package-private classes,
    1.89 -    // it is necessary to perform somewhat expensive security checks.
    1.90 -    // If the security check succeeds for a given class, it will
    1.91 -    // always succeed (it is not affected by the granting or revoking
    1.92 -    // of permissions); we speed up the check in the common case by
    1.93 -    // remembering the last Class for which the check succeeded.
    1.94 -    //
    1.95 -    // The simple security check for Constructor is to see if
    1.96 -    // the caller has already been seen, verified, and cached.
    1.97 -    // (See also Class.newInstance(), which uses a similar method.)
    1.98 -    //
    1.99 -    // A more complicated security check cache is needed for Method and Field
   1.100 -    // The cache can be either null (empty cache), a 2-array of {caller,target},
   1.101 -    // or a caller (with target implicitly equal to this.clazz).
   1.102 -    // In the 2-array case, the target is always different from the clazz.
   1.103 -    volatile Object securityCheckCache;
   1.104 -
   1.105 -    void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers)
   1.106 -        throws IllegalAccessException
   1.107 -    {
   1.108 -        if (caller == clazz) {  // quick check
   1.109 -            return;             // ACCESS IS OK
   1.110 -        }
   1.111 -        Object cache = securityCheckCache;  // read volatile
   1.112 -        Class<?> targetClass = clazz;
   1.113 -        if (obj != null
   1.114 -            && Modifier.isProtected(modifiers)
   1.115 -            && ((targetClass = obj.getClass()) != clazz)) {
   1.116 -            // Must match a 2-list of { caller, targetClass }.
   1.117 -            if (cache instanceof Class[]) {
   1.118 -                Class<?>[] cache2 = (Class<?>[]) cache;
   1.119 -                if (cache2[1] == targetClass &&
   1.120 -                    cache2[0] == caller) {
   1.121 -                    return;     // ACCESS IS OK
   1.122 -                }
   1.123 -                // (Test cache[1] first since range check for [1]
   1.124 -                // subsumes range check for [0].)
   1.125 -            }
   1.126 -        } else if (cache == caller) {
   1.127 -            // Non-protected case (or obj.class == this.clazz).
   1.128 -            return;             // ACCESS IS OK
   1.129 -        }
   1.130 -
   1.131 -        // If no return, fall through to the slow path.
   1.132 -        slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
   1.133 -    }
   1.134 -
   1.135 -    // Keep all this slow stuff out of line:
   1.136 -    void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers,
   1.137 -                               Class<?> targetClass)
   1.138 -        throws IllegalAccessException
   1.139 -    {
   1.140 -        Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
   1.141 -
   1.142 -        // Success: Update the cache.
   1.143 -        Object cache = ((targetClass == clazz)
   1.144 -                        ? caller
   1.145 -                        : new Class<?>[] { caller, targetClass });
   1.146 -
   1.147 -        // Note:  The two cache elements are not volatile,
   1.148 -        // but they are effectively final.  The Java memory model
   1.149 -        // guarantees that the initializing stores for the cache
   1.150 -        // elements will occur before the volatile write.
   1.151 -        securityCheckCache = cache;         // write volatile
   1.152 -    }
   1.153  }