summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkmb <kmb@google.com>2018-02-22 14:32:56 -0800
committerIvan Gavrilovic <gavra@google.com>2018-05-04 10:38:43 +0100
commit904e6dc6adeb2390c9216d5ed95e2090c4045ce4 (patch)
tree9e9e95e36712e0efe8597d153d535729283a0e80
parent4f68e2ecebee00ab4fe882c691bb750ce6dab64b (diff)
downloaddesugar-904e6dc6adeb2390c9216d5ed95e2090c4045ce4.tar.gz
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
-rw-r--r--java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java20
1 files changed, 19 insertions, 1 deletions
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<Class<?>> 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);