On Oct 14, 2015, at 3:38 AM, Remi Forax <[email protected]> wrote:
>
> Given that j.l.r.Method is mutable, the best way to have performance is too
> encapsulate it in a non mutable class, if possible.
OK, I'll bite. Here's a way to make Method its own non-mutable encapsulation,
a la List::set or (future feature) frozen arrays.
diff --git
a/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
b/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
--- a/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
+++ b/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
@@ -120,15 +120,36 @@
*
* @param flag the new value for the {@code accessible} flag
* @throws SecurityException if the request is denied.
+ * @throws UnsupportedOperationException if the accessible object is frozen
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*/
- public void setAccessible(boolean flag) throws SecurityException {
+ public void setAccessible(boolean flag) throws SecurityException,
UnsupportedOperationException {
+ checkFrozen();
SecurityManager sm = System.getSecurityManager();
if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
setAccessible0(this, flag);
}
+ protected void checkFrozen() {
+ if (frozen) throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Make a frozen copy of this accessible object, with the accessible
+ * bit set to false. If this object is already frozen, return the
+ * same object. If this object is not already frozen, return a frozen
+ * copy, with the accessibility flag forced to {@code false}.
+ */
+ public abstract AccessibleObject asNonAccessible();
+
+ /**
+ * Return {@code true} if this object was produced by {@link
#isNonAccessible}.
+ */
+ public boolean isNonAccessible() {
+ return frozen;
+ }
+
/* Check that you aren't exposing java.lang.Class.<init> or sensitive
fields in java.lang.Class. */
private static void setAccessible0(AccessibleObject obj, boolean flag)
@@ -166,6 +187,9 @@
// outside this package.
boolean override;
+ // Indicates whether this object is frozen in the non-accessible state.
+ boolean frozen;
+
// Reflection factory used by subclasses for creating field,
// method, and constructor accessors. Note that this is called
// very early in the bootstrapping process.