1 package org.apache.lucene.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.lang.reflect.Method;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.WeakHashMap;
27 * A utility for keeping backwards compatibility on previously abstract methods
28 * (or similar replacements).
29 * <p>Before the replacement method can be made abstract, the old method must kept deprecated.
30 * If somebody still overrides the deprecated method in a non-final class,
31 * you must keep track, of this and maybe delegate to the old method in the subclass.
32 * The cost of reflection is minimized by the following usage of this class:</p>
33 * <p>Define <strong>static final</strong> fields in the base class ({@code BaseClass}),
34 * where the old and new method are declared:</p>
36 * static final VirtualMethod<BaseClass> newMethod =
37 * new VirtualMethod<BaseClass>(BaseClass.class, "newName", parameters...);
38 * static final VirtualMethod<BaseClass> oldMethod =
39 * new VirtualMethod<BaseClass>(BaseClass.class, "oldName", parameters...);
41 * <p>This enforces the singleton status of these objects, as the maintenance of the cache would be too costly else.
42 * If you try to create a second instance of for the same method/{@code baseClass} combination, an exception is thrown.
43 * <p>To detect if e.g. the old method was overridden by a more far subclass on the inheritance path to the current
44 * instance's class, use a <strong>non-static</strong> field:</p>
46 * final boolean isDeprecatedMethodOverridden =
47 * oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass());
49 * <em>// alternatively (more readable):</em>
50 * final boolean isDeprecatedMethodOverridden =
51 * VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0
53 * <p>{@link #getImplementationDistance} returns the distance of the subclass that overrides this method.
54 * The one with the larger distance should be used preferable.
55 * This way also more complicated method rename scenarios can be handled
56 * (think of 2.9 {@code TokenStream} deprecations).</p>
60 public final class VirtualMethod<C> {
62 private static final Set<Method> singletonSet = Collections.synchronizedSet(new HashSet<Method>());
64 private final Class<C> baseClass;
65 private final String method;
66 private final Class<?>[] parameters;
67 private final WeakHashMap<Class<? extends C>, Integer> cache =
68 new WeakHashMap<Class<? extends C>, Integer>();
71 * Creates a new instance for the given {@code baseClass} and method declaration.
72 * @throws UnsupportedOperationException if you create a second instance of the same
73 * {@code baseClass} and method declaration combination. This enforces the singleton status.
74 * @throws IllegalArgumentException if {@code baseClass} does not declare the given method.
76 public VirtualMethod(Class<C> baseClass, String method, Class<?>... parameters) {
77 this.baseClass = baseClass;
79 this.parameters = parameters;
81 if (!singletonSet.add(baseClass.getDeclaredMethod(method, parameters)))
82 throw new UnsupportedOperationException(
83 "VirtualMethod instances must be singletons and therefore " +
84 "assigned to static final members in the same class, they use as baseClass ctor param."
86 } catch (NoSuchMethodException nsme) {
87 throw new IllegalArgumentException(baseClass.getName() + " has no such method: "+nsme.getMessage());
92 * Returns the distance from the {@code baseClass} in which this method is overridden/implemented
93 * in the inheritance path between {@code baseClass} and the given subclass {@code subclazz}.
94 * @return 0 iff not overridden, else the distance to the base class
96 public synchronized int getImplementationDistance(final Class<? extends C> subclazz) {
97 Integer distance = cache.get(subclazz);
98 if (distance == null) {
99 cache.put(subclazz, distance = Integer.valueOf(reflectImplementationDistance(subclazz)));
101 return distance.intValue();
105 * Returns, if this method is overridden/implemented in the inheritance path between
106 * {@code baseClass} and the given subclass {@code subclazz}.
107 * <p>You can use this method to detect if a method that should normally be final was overridden
108 * by the given instance's class.
109 * @return {@code false} iff not overridden
111 public boolean isOverriddenAsOf(final Class<? extends C> subclazz) {
112 return getImplementationDistance(subclazz) > 0;
115 private int reflectImplementationDistance(final Class<? extends C> subclazz) {
116 if (!baseClass.isAssignableFrom(subclazz))
117 throw new IllegalArgumentException(subclazz.getName() + " is not a subclass of " + baseClass.getName());
118 boolean overridden = false;
120 for (Class<?> clazz = subclazz; clazz != baseClass && clazz != null; clazz = clazz.getSuperclass()) {
121 // lookup method, if success mark as overridden
124 clazz.getDeclaredMethod(method, parameters);
126 } catch (NoSuchMethodException nsme) {
130 // increment distance if overridden
131 if (overridden) distance++;
137 * Utility method that compares the implementation/override distance of two methods.
139 * <li>> 1, iff {@code m1} is overridden/implemented in a subclass of the class overriding/declaring {@code m2}
140 * <li>< 1, iff {@code m2} is overridden in a subclass of the class overriding/declaring {@code m1}
141 * <li>0, iff both methods are overridden in the same class (or are not overridden at all)
144 public static <C> int compareImplementationDistance(final Class<? extends C> clazz,
145 final VirtualMethod<C> m1, final VirtualMethod<C> m2)
147 return Integer.valueOf(m1.getImplementationDistance(clazz)).compareTo(m2.getImplementationDistance(clazz));