From 904e6dc6adeb2390c9216d5ed95e2090c4045ce4 Mon Sep 17 00:00:00 2001 From: kmb Date: Thu, 22 Feb 2018 14:32:56 -0800 Subject: Add a check to avoid core library default methods that (accidentally) aren't being desugared. RELNOTES: None. PiperOrigin-RevId: 186675372 GitOrigin-RevId: f13d6f5b153d8713a8af7e2ba0d5dce0e9a577e8 Change-Id: Ie58fefa56a2eabf67ddaef4b0cea565eede64b45 --- .../build/android/desugar/CoreLibrarySupport.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java b/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java index 76eb346..72c7edd 100644 --- a/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java +++ b/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java @@ -20,6 +20,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.lang.reflect.Method; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; @@ -159,7 +160,24 @@ class CoreLibrarySupport { // we can only get here if its a default method, and invokestatic we handled above. Method callee = findInterfaceMethod(clazz, name, desc); if (callee != null && callee.isDefault()) { - return callee.getDeclaringClass(); + Class result = callee.getDeclaringClass(); + if (isRenamedCoreLibrary(result.getName().replace('.', '/')) + || emulatedInterfaces.stream().anyMatch(emulated -> emulated.isAssignableFrom(result))) { + return result; + } + // We get here if the declaring class is a supertype of an emulated interface. In that case + // use the emulated interface instead (since we don't desugar the supertype). Fail in case + // there are multiple possibilities. + Iterator> roots = + emulatedInterfaces + .stream() + .filter( + emulated -> emulated.isAssignableFrom(clazz) && result.isAssignableFrom(emulated)) + .iterator(); + checkState(roots.hasNext()); // must exist + Class substitute = roots.next(); + checkState(!roots.hasNext(), "Ambiguous emulation substitute: %s", callee); + return substitute; } else { checkArgument(opcode != Opcodes.INVOKESPECIAL, "Couldn't resolve interface super call %s.super.%s : %s", owner, name, desc); -- cgit v1.2.3