|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.auto.common; |
| 17 | + |
| 18 | +import javax.lang.model.element.ExecutableElement; |
| 19 | +import javax.lang.model.element.Modifier; |
| 20 | +import javax.lang.model.element.TypeElement; |
| 21 | +import javax.lang.model.type.DeclaredType; |
| 22 | +import javax.lang.model.type.ExecutableType; |
| 23 | +import javax.lang.model.util.Elements; |
| 24 | +import javax.lang.model.util.Types; |
| 25 | + |
| 26 | +/** |
| 27 | + * Determines if one method overrides another. This class defines two ways of doing that: |
| 28 | + * {@code NativeOverrides} uses the method |
| 29 | + * {@link Elements#overrides(ExecutableElement, ExecutableElement, TypeElement)} while |
| 30 | + * {@code ExplicitOverrides} reimplements that method in a way that is more consistent between |
| 31 | + * compilers, in particular between javac and ecj (the Eclipse compiler). |
| 32 | + * |
| 33 | + * @author [email protected] (Éamonn McManus) |
| 34 | + */ |
| 35 | +abstract class Overrides { |
| 36 | + abstract boolean overrides( |
| 37 | + ExecutableElement overrider, ExecutableElement overridden, TypeElement in); |
| 38 | + |
| 39 | + static class NativeOverrides extends Overrides { |
| 40 | + private final Elements elementUtils; |
| 41 | + |
| 42 | + NativeOverrides(Elements elementUtils) { |
| 43 | + this.elementUtils = elementUtils; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + boolean overrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement in) { |
| 48 | + return elementUtils.overrides(overrider, overridden, in); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + static class ExplicitOverrides extends Overrides { |
| 53 | + private final Types typeUtils; |
| 54 | + private final Elements elementUtils; |
| 55 | + |
| 56 | + ExplicitOverrides(Types typeUtils, Elements elementUtils) { |
| 57 | + this.typeUtils = typeUtils; |
| 58 | + this.elementUtils = elementUtils; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean overrides(ExecutableElement overrider, ExecutableElement overridden, |
| 63 | + TypeElement in) { |
| 64 | + if (overrider.equals(overridden)) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + if (!overrider.getSimpleName().equals(overridden.getSimpleName())) { |
| 68 | + // They must have the same name. |
| 69 | + return false; |
| 70 | + } |
| 71 | + if (overridden.getModifiers().contains(Modifier.STATIC)) { |
| 72 | + // Static methods can't be overridden (though they can be hidden by other static methods). |
| 73 | + return false; |
| 74 | + } |
| 75 | + Visibility overriddenVisibility = Visibility.ofElement(overridden); |
| 76 | + Visibility overriderVisibility = Visibility.ofElement(overrider); |
| 77 | + if (overriddenVisibility.equals(Visibility.PRIVATE) |
| 78 | + || overriderVisibility.compareTo(overriddenVisibility) < 0) { |
| 79 | + // Private methods can't be overridden, and methods can't be overridden by less-visible |
| 80 | + // methods. The latter condition is enforced by the compiler so in theory we might report |
| 81 | + // an "incorrect" result here for code that javac would not have allowed. |
| 82 | + return false; |
| 83 | + } |
| 84 | + DeclaredType inType = MoreTypes.asDeclared(in.asType()); |
| 85 | + ExecutableType overriderExecutable; |
| 86 | + ExecutableType overriddenExecutable; |
| 87 | + try { |
| 88 | + overriderExecutable = MoreTypes.asExecutable(typeUtils.asMemberOf(inType, overrider)); |
| 89 | + overriddenExecutable = MoreTypes.asExecutable(typeUtils.asMemberOf(inType, overridden)); |
| 90 | + } catch (IllegalArgumentException e) { |
| 91 | + // This might mean that at least one of the methods is not in fact declared in or inherited |
| 92 | + // by `in` (in which case we should indeed return false); or it might mean that we are |
| 93 | + // tickling an Eclipse bug such as https://bugs.eclipse.org/bugs/show_bug.cgi?id=499026 |
| 94 | + // (in which case we can't do any better than returning false). |
| 95 | + return false; |
| 96 | + } |
| 97 | + if (!typeUtils.isSubsignature(overriderExecutable, overriddenExecutable)) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (!MoreElements.methodVisibleFromPackage(overridden, MoreElements.getPackage(overrider))) { |
| 101 | + // If the overridden method is a package-private method in a different package then it |
| 102 | + // can't be overridden. |
| 103 | + return false; |
| 104 | + } |
| 105 | + TypeElement overriddenType; |
| 106 | + if (!(overridden.getEnclosingElement() instanceof TypeElement)) { |
| 107 | + return false; |
| 108 | + // We don't know how this could happen but we avoid blowing up if it does. |
| 109 | + } |
| 110 | + overriddenType = MoreElements.asType(overridden.getEnclosingElement()); |
| 111 | + // We erase the types before checking subtypes, because the TypeMirror we get for List<E> is |
| 112 | + // not a subtype of the one we get for Collection<E> since the two E instances are not the |
| 113 | + // same. For the purposes of overriding, type parameters in the containing type should not |
| 114 | + // matter because if the code compiles at all then they must be consistent. |
| 115 | + if (!typeUtils.isSubtype( |
| 116 | + typeUtils.erasure(in.asType()), typeUtils.erasure(overriddenType.asType()))) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + if (in.getKind().isClass()) { |
| 120 | + // Method mC in or inherited by class C (JLS 8.4.8.1)... |
| 121 | + if (overriddenType.getKind().isClass()) { |
| 122 | + // ...overrides from C another method mA declared in class A. The only condition we |
| 123 | + // haven't checked is that C does not inherit mA. |
| 124 | + return !elementUtils.getAllMembers(in).contains(overridden); |
| 125 | + } else if (overriddenType.getKind().isInterface()) { |
| 126 | + // ...overrides from C another method mI declared in interface I. We've already checked |
| 127 | + // the conditions (assuming that the only alternative to mI being abstract or default is |
| 128 | + // mI being static, which we eliminated above). However, it appears that the logic here |
| 129 | + // is necessary in order to be compatible with javac's `overrides` method. An inherited |
| 130 | + // abstract method does not override another method. (But, if it is not inherited, |
| 131 | + // it does, including if `in` inherits a concrete method of the same name from its |
| 132 | + // superclass.) |
| 133 | + if (overrider.getModifiers().contains(Modifier.ABSTRACT)) { |
| 134 | + return !elementUtils.getAllMembers(in).contains(overridden); |
| 135 | + } else { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } else { |
| 139 | + // We don't know what this is so say no. |
| 140 | + return false; |
| 141 | + } |
| 142 | + } else { |
| 143 | + return in.getKind().isInterface(); |
| 144 | + // Method mI in or inherited by interface I (JLS 9.4.1.1). We've already checked everything. |
| 145 | + // If this is not an interface then we don't know what it is so we say no. |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments