home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / reflect / AccessibleObject.java next >
Encoding:
Java Source  |  1998-03-20  |  3.3 KB  |  99 lines

  1. /*
  2.  * @(#)AccessibleObject.java    1.8 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang.reflect;
  16.  
  17. /**
  18.  * The AccessibleObject class is the base class for Field, Method and
  19.  * Constructor objects.  It provides the ability to flag a reflected
  20.  * object as suppressing default Java language access control checks
  21.  * when it is used.  The access checks--for public, default (package)
  22.  * access, protected, and private members--are performed when Fields,
  23.  * Methods or Constructors are used to set or get fields, to invoke
  24.  * methods, or to create and initialize new instances of classes,
  25.  * respectively.
  26.  *
  27.  * <p>Setting the <tt>accessible</tt> flag in a reflected object
  28.  * permits sophisticated applications with sufficient privilege, such
  29.  * as Java Object Serialization or other persistence mechanisms, to
  30.  * manipulate objects in a manner that would normally be prohibited.
  31.  *
  32.  * @see Field
  33.  * @see Method
  34.  * @see Constructor
  35.  * @see ReflectPermission
  36.  *
  37.  * @since JDK1.2
  38.  */
  39. public
  40. class AccessibleObject {
  41.  
  42.     /**
  43.      * The Permission object that is used to check whether a client
  44.      * has sufficient privilege to defeat Java language access
  45.      * control checks.
  46.      */
  47.     static final private java.security.Permission ACCESS_PERMISSION =
  48.     new ReflectPermission("access");
  49.  
  50.     /**
  51.      * Convenience method to set the <tt>accessible</tt> flag for an
  52.      * array of objects with a single security check (for efficiency).
  53.      *
  54.      * @param array the array of AccessibleObjects
  55.      * @param flag the new value for the <tt>accessible</tt> flag in each object
  56.      * @throws SecurityException if the request is denied
  57.      */
  58.     public static void setAccessible(AccessibleObject[] array, boolean flag)
  59.     throws SecurityException {
  60.     SecurityManager sm = System.getSecurityManager();
  61.     if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
  62.     for (int i = 0; i < array.length; i++) {
  63.         array[i].override = flag;
  64.     }
  65.     }
  66.  
  67.     /**
  68.      * Set the <tt>accessible</tt> flag for this object to
  69.      * the indicated boolean value.  A value of <tt>true</tt> indicates that
  70.      * the reflected object should suppress Java language access
  71.      * checking when it is used.  A value of <tt>false</tt> indicates 
  72.      * that the reflected object should enforce Java language access checks.
  73.      *
  74.      * @param flag the new value for the <tt>accessible</tt> flag
  75.      * @throws SecurityException if the request is denied
  76.      */
  77.     public void setAccessible(boolean flag) throws SecurityException {
  78.     SecurityManager sm = System.getSecurityManager();
  79.     if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
  80.     this.override = flag;
  81.     }
  82.  
  83.     /**
  84.      * Get the value of the <tt>accessible</tt> flag for this object.
  85.      */
  86.     public boolean isAccessible() {
  87.     return override;
  88.     }
  89.  
  90.     /**
  91.      * Constructor: only used by the Java Virtual Machine.
  92.      */
  93.     protected AccessibleObject() {}
  94.  
  95.     // N.B. jvm depends on this field name, and initializes to <tt>false</tt>.
  96.     private boolean override;
  97.  
  98. }
  99.