|
| 1 | +package org.apache.lucene.util; |
| 2 | + |
| 3 | +/** |
| 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 |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 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. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.IdentityHashMap; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +/** |
| 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> |
| 35 | + * <pre> |
| 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...); |
| 40 | + * </pre> |
| 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> |
| 45 | + * <pre> |
| 46 | + * final boolean isDeprecatedMethodOverridden = |
| 47 | + * oldMethod.getImplementationDistance(this.getClass()) > newMethod.getImplementationDistance(this.getClass()); |
| 48 | + * |
| 49 | + * <em>// alternatively (more readable):</em> |
| 50 | + * final boolean isDeprecatedMethodOverridden = |
| 51 | + * VirtualMethod.compareImplementationDistance(this.getClass(), oldMethod, newMethod) > 0 |
| 52 | + * </pre> |
| 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> |
| 57 | + */ |
| 58 | +public final class VirtualMethod<C> { |
| 59 | + |
| 60 | + private static final Set<Method> singletonSet = Collections.synchronizedSet(new HashSet<Method>()); |
| 61 | + |
| 62 | + private final Class<C> baseClass; |
| 63 | + private final String method; |
| 64 | + private final Class<?>[] parameters; |
| 65 | + private final IdentityHashMap<Class<? extends C>, Integer> cache = |
| 66 | + new IdentityHashMap<Class<? extends C>, Integer>(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new instance for the given {@code baseClass} and method declaration. |
| 70 | + * @throws UnsupportedOperationException if you create a second instance of the same |
| 71 | + * {@code baseClass} and method declaration combination. This enforces the singleton status. |
| 72 | + * @throws IllegalArgumentException if {@code baseClass} does not declare the given method. |
| 73 | + */ |
| 74 | + public VirtualMethod(Class<C> baseClass, String method, Class<?>... parameters) { |
| 75 | + this.baseClass = baseClass; |
| 76 | + this.method = method; |
| 77 | + this.parameters = parameters; |
| 78 | + try { |
| 79 | + if (!singletonSet.add(baseClass.getDeclaredMethod(method, parameters))) |
| 80 | + throw new UnsupportedOperationException( |
| 81 | + "VirtualMethod instances must be singletons and therefore " + |
| 82 | + "assigned to static final members in the same class, they use as baseClass ctor param." |
| 83 | + ); |
| 84 | + } catch (NoSuchMethodException nsme) { |
| 85 | + throw new IllegalArgumentException(baseClass.getName() + " has no such method: "+nsme.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the distance from the {@code baseClass} in which this method is overridden/implemented |
| 91 | + * in the inheritance path between {@code baseClass} and the given subclass {@code subclazz}. |
| 92 | + * @return 0 iff not overridden, else the distance to the base class |
| 93 | + */ |
| 94 | + public synchronized int getImplementationDistance(final Class<? extends C> subclazz) { |
| 95 | + Integer distance = cache.get(subclazz); |
| 96 | + if (distance == null) { |
| 97 | + cache.put(subclazz, distance = Integer.valueOf(reflectImplementationDistance(subclazz))); |
| 98 | + } |
| 99 | + return distance.intValue(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns, if this method is overridden/implemented in the inheritance path between |
| 104 | + * {@code baseClass} and the given subclass {@code subclazz}. |
| 105 | + * <p>You can use this method to detect if a method that should normally be final was overridden |
| 106 | + * by the given instance's class. |
| 107 | + * @return {@code false} iff not overridden |
| 108 | + */ |
| 109 | + public boolean isOverriddenAsOf(final Class<? extends C> subclazz) { |
| 110 | + return getImplementationDistance(subclazz) > 0; |
| 111 | + } |
| 112 | + |
| 113 | + private int reflectImplementationDistance(final Class<? extends C> subclazz) { |
| 114 | + if (!baseClass.isAssignableFrom(subclazz)) |
| 115 | + throw new IllegalArgumentException(subclazz.getName() + " is not a subclass of " + baseClass.getName()); |
| 116 | + boolean overridden = false; |
| 117 | + int distance = 0; |
| 118 | + for (Class<?> clazz = subclazz; clazz != baseClass && clazz != null; clazz = clazz.getSuperclass()) { |
| 119 | + // lookup method, if success mark as overridden |
| 120 | + if (!overridden) { |
| 121 | + try { |
| 122 | + clazz.getDeclaredMethod(method, parameters); |
| 123 | + overridden = true; |
| 124 | + } catch (NoSuchMethodException nsme) { |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // increment distance if overridden |
| 129 | + if (overridden) distance++; |
| 130 | + } |
| 131 | + return distance; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Utility method that compares the implementation/override distance of two methods. |
| 136 | + * @return <ul> |
| 137 | + * <li>> 1, iff {@code m1} is overridden/implemented in a subclass of the class overriding/declaring {@code m2} |
| 138 | + * <li>< 1, iff {@code m2} is overridden in a subclass of the class overriding/declaring {@code m1} |
| 139 | + * <li>0, iff both methods are overridden in the same class (or are not overridden at all) |
| 140 | + * </ul> |
| 141 | + */ |
| 142 | + public static <C> int compareImplementationDistance(final Class<? extends C> clazz, |
| 143 | + final VirtualMethod<C> m1, final VirtualMethod<C> m2) |
| 144 | + { |
| 145 | + return Integer.valueOf(m1.getImplementationDistance(clazz)).compareTo(m2.getImplementationDistance(clazz)); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments