1. /*
  2. * @(#)AccessibleObject.java 1.15 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang.reflect;
  11. /**
  12. * The AccessibleObject class is the base class for Field, Method and
  13. * Constructor objects. It provides the ability to flag a reflected
  14. * object as suppressing default Java language access control checks
  15. * when it is used. The access checks--for public, default (package)
  16. * access, protected, and private members--are performed when Fields,
  17. * Methods or Constructors are used to set or get fields, to invoke
  18. * methods, or to create and initialize new instances of classes,
  19. * respectively.
  20. *
  21. * <p>Setting the <tt>accessible</tt> flag in a reflected object
  22. * permits sophisticated applications with sufficient privilege, such
  23. * as Java Object Serialization or other persistence mechanisms, to
  24. * manipulate objects in a manner that would normally be prohibited.
  25. *
  26. * @see Field
  27. * @see Method
  28. * @see Constructor
  29. * @see ReflectPermission
  30. *
  31. * @since 1.2
  32. */
  33. public
  34. class AccessibleObject {
  35. /**
  36. * The Permission object that is used to check whether a client
  37. * has sufficient privilege to defeat Java language access
  38. * control checks.
  39. */
  40. static final private java.security.Permission ACCESS_PERMISSION =
  41. new ReflectPermission("suppressAccessChecks");
  42. /**
  43. * Convenience method to set the <tt>accessible</tt> flag for an
  44. * array of objects with a single security check (for efficiency).
  45. *
  46. * <p>First, if there is a security manager, its
  47. * <code>checkPermission</code> method is called with a
  48. * <code>ReflectPermission("suppressAccessChecks")</code> permission.
  49. *
  50. * <p>A <code>SecurityException</code> is raised if <code>flag</code> is
  51. * <code>true</code> but accessibility of any of the elements of the input
  52. * <code>array</code> may not be changed (for example, if the element
  53. * object is a {@link Constructor} object for the class {@link
  54. * java.lang.Class}). In the event of such a SecurityException, the
  55. * accessiblity of objects is set to <code>flag</code> for array elements
  56. * upto (and excluding) the element for which the exception occurred; the
  57. * accessiblity of elements beyond (and including) the element for which
  58. * the exception occurred is unchanged.
  59. *
  60. * @param array the array of AccessibleObjects
  61. * @param flag the new value for the <tt>accessible</tt> flag
  62. * in each object
  63. * @throws SecurityException if the request is denied.
  64. * @see SecurityManager#checkPermission
  65. * @see java.lang.RuntimePermission
  66. */
  67. public static void setAccessible(AccessibleObject[] array, boolean flag)
  68. throws SecurityException {
  69. SecurityManager sm = System.getSecurityManager();
  70. if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
  71. for (int i = 0; i < array.length; i++) {
  72. setAccessible0(array[i], flag);
  73. }
  74. }
  75. /**
  76. * Set the <tt>accessible</tt> flag for this object to
  77. * the indicated boolean value. A value of <tt>true</tt> indicates that
  78. * the reflected object should suppress Java language access
  79. * checking when it is used. A value of <tt>false</tt> indicates
  80. * that the reflected object should enforce Java language access checks.
  81. *
  82. * <p>First, if there is a security manager, its
  83. * <code>checkPermission</code> method is called with a
  84. * <code>ReflectPermission("suppressAccessChecks")</code> permission.
  85. *
  86. * <p>A <code>SecurityException</code> is raised if <code>flag</code> is
  87. * <code>true</code> but accessibility of this object may not be changed
  88. * (for example, if this element object is a {@link Constructor} object for
  89. * the class {@link java.lang.Class}).
  90. *
  91. * <p>A <code>SecurityException</code> is raised if this object is a {@link
  92. * java.lang.reflect.Constructor} object for the class
  93. * <code>java.lang.Class</code>, and <code>flag</code> is true.
  94. *
  95. * @param flag the new value for the <tt>accessible</tt> flag
  96. * @throws SecurityException if the request is denied.
  97. * @see SecurityManager#checkPermission
  98. * @see java.lang.RuntimePermission
  99. */
  100. public void setAccessible(boolean flag) throws SecurityException {
  101. SecurityManager sm = System.getSecurityManager();
  102. if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
  103. setAccessible0(this, flag);
  104. }
  105. /* Check that you aren't exposing java.lang.Class.<init>. */
  106. private static void setAccessible0(AccessibleObject obj, boolean flag)
  107. throws SecurityException
  108. {
  109. if (obj instanceof Constructor && flag == true) {
  110. Constructor c = (Constructor)obj;
  111. if (c.getDeclaringClass() == Class.class) {
  112. throw new SecurityException("Can not make a java.lang.Class" +
  113. " constructor accessible");
  114. }
  115. }
  116. obj.override = flag;
  117. }
  118. /**
  119. * Get the value of the <tt>accessible</tt> flag for this object.
  120. */
  121. public boolean isAccessible() {
  122. return override;
  123. }
  124. /**
  125. * Constructor: only used by the Java Virtual Machine.
  126. */
  127. protected AccessibleObject() {}
  128. // N.B. jvm depends on this field name, and initializes to <tt>false</tt>.
  129. private boolean override;
  130. }