aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/android/java/com/google/devtools/build/android/desugar
diff options
context:
space:
mode:
authorGravatar kmb <kmb@google.com>2018-03-01 16:26:21 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-01 16:27:53 -0800
commitf090082d62c3ea779d2dd33eb0fd7355b0ee9456 (patch)
treed4c2fd6f2b3ac8708e5a8a66105cdd64e9f3f603 /src/tools/android/java/com/google/devtools/build/android/desugar
parent99be8b417a95224f66b3e00c9103cbde460d3b07 (diff)
Android desugar config options to exclude methods from interface emulation
RELNOTES: None. PiperOrigin-RevId: 187551970
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/desugar')
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java17
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java16
2 files changed, 30 insertions, 3 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java b/src/tools/android/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java
index c73874e977..e22c596c8e 100644
--- a/src/tools/android/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java
+++ b/src/tools/android/java/com/google/devtools/build/android/desugar/CoreLibrarySupport.java
@@ -44,6 +44,7 @@ class CoreLibrarySupport {
private final ClassLoader targetLoader;
/** Internal name prefixes that we want to move to a custom package. */
private final ImmutableSet<String> renamedPrefixes;
+ private final ImmutableSet<String> excludeFromEmulation;
/** Internal names of interfaces whose default and static interface methods we'll emulate. */
private final ImmutableSet<Class<?>> emulatedInterfaces;
/** Map from {@code owner#name} core library members to their new owners. */
@@ -58,13 +59,16 @@ class CoreLibrarySupport {
GeneratedClassStore store,
List<String> renamedPrefixes,
List<String> emulatedInterfaces,
- List<String> memberMoves) {
+ List<String> memberMoves,
+ List<String> excludeFromEmulation) {
this.rewriter = rewriter;
this.targetLoader = targetLoader;
this.store = store;
checkArgument(
renamedPrefixes.stream().allMatch(prefix -> prefix.startsWith("java/")), renamedPrefixes);
this.renamedPrefixes = ImmutableSet.copyOf(renamedPrefixes);
+ this.excludeFromEmulation = ImmutableSet.copyOf(excludeFromEmulation);
+
ImmutableSet.Builder<Class<?>> classBuilder = ImmutableSet.builder();
for (String itf : emulatedInterfaces) {
checkArgument(itf.startsWith("java/util/"), itf);
@@ -86,6 +90,8 @@ class CoreLibrarySupport {
checkArgument(!isRenamedCoreLibrary(pair.get(0).substring(0, sep)),
"Original renamed, no need to move it: %s", move);
checkArgument(isRenamedCoreLibrary(pair.get(1)), "Target not renamed: %s", move);
+ checkArgument(!this.excludeFromEmulation.contains(pair.get(0)),
+ "Retargeted invocation %s shouldn't overlap with excluded", move);
movesBuilder.put(pair.get(0), renameCoreLibrary(pair.get(1)));
}
@@ -245,6 +251,9 @@ 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()) {
+ if (isExcluded(callee)) {
+ return null;
+ }
Class<?> result = callee.getDeclaringClass();
if (isRenamedCoreLibrary(result.getName().replace('.', '/'))
|| emulatedInterfaces.stream().anyMatch(emulated -> emulated.isAssignableFrom(result))) {
@@ -294,6 +303,12 @@ class CoreLibrarySupport {
return null;
}
+ private boolean isExcluded(Method method) {
+ String unprefixedOwner =
+ rewriter.unprefix(method.getDeclaringClass().getName().replace('.', '/'));
+ return excludeFromEmulation.contains(unprefixedOwner + "#" + method.getName());
+ }
+
private Class<?> loadFromInternal(String internalName) {
try {
return targetLoader.loadClass(internalName.replace('/', '.'));
diff --git a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
index dd1992a6f8..8b8635bc2a 100644
--- a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
+++ b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
@@ -292,6 +292,17 @@ class Desugar {
)
public List<String> retargetCoreLibraryMembers;
+ /** Members not to rewrite. */
+ @Option(
+ name = "dont_rewrite_core_library_invocation",
+ defaultValue = "", // ignored
+ allowMultiple = true,
+ documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
+ effectTags = {OptionEffectTag.UNKNOWN},
+ help = "Method invocations not to rewrite, given as \"class/Name#method\"."
+ )
+ public List<String> dontTouchCoreLibraryMembers;
+
/** Set to work around b/62623509 with JaCoCo versions prior to 0.7.9. */
// TODO(kmb): Remove when Android Studio doesn't need it anymore (see b/37116789)
@Option(
@@ -409,8 +420,9 @@ class Desugar {
store,
options.rewriteCoreLibraryPrefixes,
options.emulateCoreLibraryInterfaces,
- options.retargetCoreLibraryMembers)
- : null;
+ options.retargetCoreLibraryMembers,
+ options.dontTouchCoreLibraryMembers)
+ : null;
desugarClassesInInput(
inputFiles,