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