From a8a95e64ce8586db07ea22569e291568cbe1fe4f Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 19 May 2023 16:34:54 +0200 Subject: [PATCH] Forward port references to AccessController in VirtualMethod (#12308) --- .../java/org/apache/lucene/util/VirtualMethod.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/VirtualMethod.java b/lucene/core/src/java/org/apache/lucene/util/VirtualMethod.java index 05eef2a8661..a7c2a71cc1c 100644 --- a/lucene/core/src/java/org/apache/lucene/util/VirtualMethod.java +++ b/lucene/core/src/java/org/apache/lucene/util/VirtualMethod.java @@ -17,6 +17,8 @@ package org.apache.lucene.util; import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -49,13 +51,20 @@ import java.util.Set; * *
  *  final boolean isDeprecatedMethodOverridden =
- *   oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass());
+ *   AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
+ *    (oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass())));
  *
  *  // alternatively (more readable):
  *  final boolean isDeprecatedMethodOverridden =
- *   VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0
+ *   AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
+ *    VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0);
  * 
* + *

It is important to use {@link AccessController#doPrivileged(PrivilegedAction)} for the actual + * call to get the implementation distance because the subclass may be in a different package. The + * static constructors do not need to use {@code AccessController} because it just initializes our + * own method reference. The caller should have access to all declared members in its own class. + * *

{@link #getImplementationDistance} returns the distance of the subclass that overrides this * method. The one with the larger distance should be used preferable. This way also more * complicated method rename scenarios can be handled (think of 2.9 {@code TokenStream}