Skip to content

Commit

Permalink
Forward port references to AccessController in VirtualMethod (#12308)
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed May 19, 2023
1 parent 04ef6de commit a8a95e6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lucene/core/src/java/org/apache/lucene/util/VirtualMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,13 +51,20 @@
*
* <pre class="prettyprint">
* final boolean isDeprecatedMethodOverridden =
* oldMethod.getImplementationDistance(this.getClass()) &gt; newMethod.getImplementationDistance(this.getClass());
* AccessController.doPrivileged((PrivilegedAction&lt;Boolean&gt;) () -&gt;
* (oldMethod.getImplementationDistance(this.getClass()) &gt; newMethod.getImplementationDistance(this.getClass())));
*
* <em>// alternatively (more readable):</em>
* final boolean isDeprecatedMethodOverridden =
* VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) &gt; 0
* AccessController.doPrivileged((PrivilegedAction&lt;Boolean&gt;) () -&gt;
* VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) &gt; 0);
* </pre>
*
* <p>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.
*
* <p>{@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}
Expand Down

0 comments on commit a8a95e6

Please sign in to comment.