X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/VirtualMethod.java diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/VirtualMethod.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/VirtualMethod.java new file mode 100644 index 0000000..11937db --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/VirtualMethod.java @@ -0,0 +1,150 @@ +package org.apache.lucene.util; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashSet; +import java.util.WeakHashMap; +import java.util.Set; + +/** + * A utility for keeping backwards compatibility on previously abstract methods + * (or similar replacements). + *
Before the replacement method can be made abstract, the old method must kept deprecated. + * If somebody still overrides the deprecated method in a non-final class, + * you must keep track, of this and maybe delegate to the old method in the subclass. + * The cost of reflection is minimized by the following usage of this class:
+ *Define static final fields in the base class ({@code BaseClass}), + * where the old and new method are declared:
+ *+ * static final VirtualMethod<BaseClass> newMethod = + * new VirtualMethod<BaseClass>(BaseClass.class, "newName", parameters...); + * static final VirtualMethod<BaseClass> oldMethod = + * new VirtualMethod<BaseClass>(BaseClass.class, "oldName", parameters...); + *+ *
This enforces the singleton status of these objects, as the maintenance of the cache would be too costly else. + * If you try to create a second instance of for the same method/{@code baseClass} combination, an exception is thrown. + *
To detect if e.g. the old method was overridden by a more far subclass on the inheritance path to the current + * instance's class, use a non-static field:
+ *+ * final boolean isDeprecatedMethodOverridden = + * oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass()); + * + * // alternatively (more readable): + * final boolean isDeprecatedMethodOverridden = + * VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0 + *+ *
{@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} deprecations).
+ * + * @lucene.internal + */ +public final class VirtualMethodYou can use this method to detect if a method that should normally be final was overridden + * by the given instance's class. + * @return {@code false} iff not overridden + */ + public boolean isOverriddenAsOf(final Class extends C> subclazz) { + return getImplementationDistance(subclazz) > 0; + } + + private int reflectImplementationDistance(final Class extends C> subclazz) { + if (!baseClass.isAssignableFrom(subclazz)) + throw new IllegalArgumentException(subclazz.getName() + " is not a subclass of " + baseClass.getName()); + boolean overridden = false; + int distance = 0; + for (Class> clazz = subclazz; clazz != baseClass && clazz != null; clazz = clazz.getSuperclass()) { + // lookup method, if success mark as overridden + if (!overridden) { + try { + clazz.getDeclaredMethod(method, parameters); + overridden = true; + } catch (NoSuchMethodException nsme) { + } + } + + // increment distance if overridden + if (overridden) distance++; + } + return distance; + } + + /** + * Utility method that compares the implementation/override distance of two methods. + * @return