Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6495917

(reflect) Field.forceGet() to access restricted object

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 6
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      Could you please add a new method forceGet(Object) or extend get with get(Object obj, boolean force). Implementation:

      public Object get(Object obj, boolean force) {
          return get(obj,false);
      }

      public Object get(Object obj, boolean force) {
          if (force) {
              SecurityManager securitymanager = System.getSecurityManager();
              if(securitymanager != null)
                  securitymanager.checkPermission(new ReflectPermission("suppressAccessChecks"));
          }
          return getFieldAccessor(obj,force).get(obj);
      }

      private FieldAccessor getFieldAccessor(Object obj) throws IllegalAccessException {
          return getFieldAccessor(obj, false);
      }

      private FieldAccessor getFieldAccessor(Object obj, boolean force) throws IllegalAccessException {
          if (force == false) force = flag;
          doSecurityCheck(obj);
          boolean flag = override;
          FieldAccessor fieldaccessor = flag ? overrideFieldAccessor : fieldAccessor;
          return fieldaccessor == null ? acquireFieldAccessor(force) : fieldaccessor;
      }

      This streamlines the access process.

      JUSTIFICATION :
      If you wanted to access a restricted object using the field.get method, you would have to
      setAccessible(true); and then reset it again. A simple addition (or an overloaded method), would remove the need to make numerous access checks. This modification still checks revelent permssions and upholds the specification.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Object obj = field.get(var,true);
      ACTUAL -
      You have to use the cumbersome:

      field.setAccessible(true);
      Object obj = field.get(var);
      field.setAccessible(false);

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: