Skip to content

Commit

Permalink
Do not enable backwards layer for Lucene's own subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed May 18, 2023
1 parent 4dc2cee commit bbc3f39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lucene/core/src/java/org/apache/lucene/search/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,30 @@ public abstract class Query {
new VirtualMethod<>(Query.class, "rewrite", IndexReader.class);
private static final VirtualMethod<Query> newMethod =
new VirtualMethod<>(Query.class, "rewrite", IndexSearcher.class);
private final boolean isDeprecatedRewriteMethodOverridden =
AccessController.doPrivileged(
(PrivilegedAction<Boolean>)
() ->
VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod)
> 0);
private final boolean isDeprecatedRewriteMethodOverridden;

/** Constructor for query classes. */
public Query() {
var clazz = this.getClass();
if (clazz.getName().startsWith("org.apache.lucene.")) {
// we know that all Lucene's own classes have been updated to use new API, so reflection is
// not needed
isDeprecatedRewriteMethodOverridden = false;
} else {
isDeprecatedRewriteMethodOverridden = detectDeprectatedRewriteMethodOverridden(clazz);
}
}

/** Test only method (used to test backwards layer) */
Query(Void forTestOnly) {
isDeprecatedRewriteMethodOverridden = detectDeprectatedRewriteMethodOverridden(this.getClass());
}

private static boolean detectDeprectatedRewriteMethodOverridden(Class<? extends Query> clazz) {
final PrivilegedAction<Boolean> action =
() -> VirtualMethod.compareImplementationDistance(clazz, oldMethod, newMethod) > 0;
return AccessController.doPrivileged(action);
}

/**
* Prints a query to a string, with <code>field</code> assumed to be the default field and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public Query rewrite(IndexReader reader) throws IOException {
private abstract static class RewriteCountingQuery extends Query {
int rewriteCount = 0;

RewriteCountingQuery() {
super(null); // enforce usage of backwards compatibility mode
}

abstract RewriteCountingQuery getInnerQuery();
}

Expand Down

0 comments on commit bbc3f39

Please sign in to comment.