diff options
author | Éamonn McManus <emcmanus@google.com> | 2021-05-13 08:35:43 -0700 |
---|---|---|
committer | Google Java Core Libraries <java-libraries-firehose+copybara@google.com> | 2021-05-13 08:36:14 -0700 |
commit | c9d881c6fcf052f291caf8db118435b87335cf42 (patch) | |
tree | 2822d284d974a4e31b44769bb6332d1f2599a941 /common | |
parent | 4fa79ccb5f9ac6dc79c3c6830b03a49a7f1f0fa2 (diff) | |
download | auto-c9d881c6fcf052f291caf8db118435b87335cf42.tar.gz |
Fix a crash when `getLocalAndInheritedMethods` is called with a raw-type ancestor.
RELNOTES=Fixed a crash when `getLocalAndInheritedMethods` is called with a raw-type ancestor.
PiperOrigin-RevId: 373583104
Diffstat (limited to 'common')
-rw-r--r-- | common/src/main/java/com/google/auto/common/Overrides.java | 9 | ||||
-rw-r--r-- | common/src/test/java/com/google/auto/common/OverridesTest.java | 15 |
2 files changed, 18 insertions, 6 deletions
diff --git a/common/src/main/java/com/google/auto/common/Overrides.java b/common/src/main/java/com/google/auto/common/Overrides.java index 19a45862..ccedc1d2 100644 --- a/common/src/main/java/com/google/auto/common/Overrides.java +++ b/common/src/main/java/com/google/auto/common/Overrides.java @@ -15,6 +15,8 @@ */ package com.google.auto.common; +import static java.util.stream.Collectors.toList; + import com.google.common.base.Preconditions; import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; @@ -114,12 +116,11 @@ abstract class Overrides { // can't be overridden. return false; } - TypeElement overriddenType; if (!(overridden.getEnclosingElement() instanceof TypeElement)) { return false; // We don't know how this could happen but we avoid blowing up if it does. } - overriddenType = MoreElements.asType(overridden.getEnclosingElement()); + TypeElement overriddenType = MoreElements.asType(overridden.getEnclosingElement()); // We erase the types before checking subtypes, because the TypeMirror we get for List<E> is // not a subtype of the one we get for Collection<E> since the two E instances are not the // same. For the purposes of overriding, type parameters in the containing type should not @@ -261,6 +262,10 @@ abstract class Overrides { TypeElement element = MoreElements.asType(declared.asElement()); List<? extends TypeMirror> actuals = declared.getTypeArguments(); List<? extends TypeParameterElement> formals = element.getTypeParameters(); + if (actuals.isEmpty()) { + // Either the formal type arguments are also empty or `declared` is raw. + actuals = formals.stream().map(t -> t.getBounds().get(0)).collect(toList()); + } Verify.verify(actuals.size() == formals.size()); for (int i = 0; i < actuals.size(); i++) { typeBindings.put(formals.get(i), actuals.get(i)); diff --git a/common/src/test/java/com/google/auto/common/OverridesTest.java b/common/src/test/java/com/google/auto/common/OverridesTest.java index afb79760..f630382d 100644 --- a/common/src/test/java/com/google/auto/common/OverridesTest.java +++ b/common/src/test/java/com/google/auto/common/OverridesTest.java @@ -221,19 +221,26 @@ public class OverridesTest { } static class TypesForGenerics { - interface XCollection<E> { + interface GCollection<E> { boolean add(E x); } - interface XList<E> extends XCollection<E> { - @Override public boolean add(E x); + interface GList<E> extends GCollection<E> { + @Override boolean add(E x); } - static class StringList implements XList<String> { + static class StringList implements GList<String> { @Override public boolean add(String x) { return false; } } + + @SuppressWarnings("rawtypes") + static class RawList implements GList { + @Override public boolean add(Object x) { + return false; + } + } } @SuppressWarnings("rawtypes") |