From ed1e7594b23100f89755491f36e46886b4a51c8d Mon Sep 17 00:00:00 2001 From: mjhalupka Date: Wed, 13 Jun 2018 15:48:48 -0700 Subject: [PATCH] Reconvert the lambdas in AndroidDataConverter to anonymous inner classes. Tag some static members with @AutoCodec. Replace some lambdas with explicit functions or classes. PiperOrigin-RevId: 200467500 --- .../lib/rules/android/AndroidBinary.java | 11 ++++- .../rules/android/AndroidDataConverter.java | 44 ++++++++++++++++--- .../AndroidResourcesProcessorBuilder.java | 34 ++++++++------ .../android/RClassGeneratorActionBuilder.java | 25 +++++++---- 4 files changed, 85 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java index e6a1261d92c17e..d78ba32791d750 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java @@ -29,6 +29,7 @@ import com.google.common.collect.Iterables; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; +import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; import com.google.devtools.build.lib.actions.CommandLine; import com.google.devtools.build.lib.actions.FailAction; import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; @@ -50,6 +51,7 @@ import com.google.devtools.build.lib.analysis.actions.ParameterFileWriteAction; import com.google.devtools.build.lib.analysis.actions.SpawnAction; import com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate; +import com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate.OutputPathMapper; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.whitelisting.Whitelist; import com.google.devtools.build.lib.cmdline.Label; @@ -1370,7 +1372,14 @@ private static Artifact createTemplatedMergerActions( new SpawnActionTemplate.Builder(inputTree, outputTree) .setExecutable(ruleContext.getExecutablePrerequisite("$dexmerger", Mode.HOST)) .setMnemonics("DexShardsToMerge", "DexMerger") - .setOutputPathMapper(input -> input.getParentRelativePath()); + .setOutputPathMapper( + // Use an anonymous inner class for serialization. + new OutputPathMapper() { + @Override + public PathFragment parentRelativeOutputPath(TreeFileArtifact input) { + return input.getParentRelativePath(); + } + }); CustomCommandLine.Builder commandLine = CustomCommandLine.builder() .addPlaceholderTreeArtifactExecPath("--input", inputTree) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDataConverter.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDataConverter.java index 28d242c033ce3f..0db9971af09a22 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDataConverter.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDataConverter.java @@ -19,6 +19,7 @@ import com.google.devtools.build.lib.analysis.actions.CustomCommandLine.VectorArg; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.errorprone.annotations.CompileTimeConstant; import java.util.Objects; @@ -37,6 +38,7 @@ public class AndroidDataConverter extends ParametrizedMapFn { * Converts Android data to the "SerializedAndroidData" format used by the Android data processing * actions. */ + @AutoCodec static final AndroidDataConverter MERGABLE_DATA_CONVERTER = AndroidDataConverter.builder(JoinerType.SEMICOLON_AMPERSAND) .withRoots(MergableAndroidData::getResourceRoots) @@ -137,24 +139,54 @@ private Builder(JoinerType joinerType) { } Builder withRoots(Function> rootsFunction) { - return with(t -> rootsToString(rootsFunction.apply(t))); + // Anonymous inner class for serialization. + return with( + new Function() { + @Override + public String apply(T t) { + // Copied from rootsToString to get rid of internal Lambda. + return rootsFunction + .apply(t) + .stream() + .map(PathFragment::toString) + .collect(Collectors.joining("#")); + } + }); } Builder withArtifact(Function artifactFunction) { - return with(t -> artifactFunction.apply(t).getExecPathString()); + // Anonymous inner class for serialization. + return with( + new Function() { + @Override + public String apply(T t) { + return artifactFunction.apply(t).getExecPathString(); + } + }); } Builder maybeWithArtifact(Function nullableArtifactFunction) { + // Anonymous inner class for serialization. return with( - t -> { - @Nullable Artifact artifact = nullableArtifactFunction.apply(t); - return artifact == null ? "" : artifact.getExecPathString(); + new Function() { + @Override + public String apply(T t) { + @Nullable Artifact artifact = nullableArtifactFunction.apply(t); + return artifact == null ? "" : artifact.getExecPathString(); + } }); } Builder withLabel(Function labelFunction) { // Escape labels, since they are known to contain separating characters (specifically, ':'). - return with(t -> joinerType.escape(labelFunction.apply(t).toString())); + // Anonymous inner class for serialization. + return with( + new Function() { + @Override + public String apply(T t) { + return joinerType.escape(labelFunction.apply(t).toString()); + } + }); } Builder with(Function stringFunction) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java index af7189353d7283..e289c7e3196959 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourcesProcessorBuilder.java @@ -18,13 +18,16 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion; import com.google.devtools.build.lib.rules.android.AndroidDataConverter.JoinerType; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; import java.util.Collections; import java.util.List; /** Builder for creating resource processing action. */ public class AndroidResourcesProcessorBuilder { - private static final AndroidDataConverter AAPT2_ASSET_DEP_TO_ARG = + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT2_ASSET_DEP_TO_ARG = AndroidDataConverter.builder(JoinerType.SEMICOLON_AMPERSAND) .withRoots(ParsedAndroidAssets::getResourceRoots) .withRoots(ParsedAndroidAssets::getAssetRoots) @@ -32,7 +35,8 @@ public class AndroidResourcesProcessorBuilder { .withArtifact(ParsedAndroidAssets::getSymbols) .build(); - private static final AndroidDataConverter AAPT2_ASSET_DEP_TO_ARG_NO_PARSE = + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT2_ASSET_DEP_TO_ARG_NO_PARSE = AndroidDataConverter.builder(JoinerType.SEMICOLON_AMPERSAND) .withRoots(ParsedAndroidAssets::getResourceRoots) .withRoots(ParsedAndroidAssets::getAssetRoots) @@ -40,7 +44,8 @@ public class AndroidResourcesProcessorBuilder { .withArtifact(ParsedAndroidAssets::getCompiledSymbols) .build(); - private static final AndroidDataConverter AAPT2_RESOURCE_DEP_TO_ARG = + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT2_RESOURCE_DEP_TO_ARG = AndroidDataConverter.builder(JoinerType.COLON_COMMA) .withRoots(ValidatedAndroidData::getResourceRoots) .withRoots(ValidatedAndroidData::getAssetRoots) @@ -50,17 +55,18 @@ public class AndroidResourcesProcessorBuilder { .maybeWithArtifact(ValidatedAndroidData::getSymbols) .build(); - private static final AndroidDataConverter - AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE = - AndroidDataConverter.builder(JoinerType.COLON_COMMA) - .withRoots(ValidatedAndroidData::getResourceRoots) - .withRoots(ValidatedAndroidData::getAssetRoots) - .withArtifact(ValidatedAndroidData::getManifest) - .maybeWithArtifact(ValidatedAndroidData::getAapt2RTxt) - .maybeWithArtifact(ValidatedAndroidData::getCompiledSymbols) - .build(); - - private static final AndroidDataConverter RESOURCE_DEP_TO_ARG = + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE = + AndroidDataConverter.builder(JoinerType.COLON_COMMA) + .withRoots(ValidatedAndroidData::getResourceRoots) + .withRoots(ValidatedAndroidData::getAssetRoots) + .withArtifact(ValidatedAndroidData::getManifest) + .maybeWithArtifact(ValidatedAndroidData::getAapt2RTxt) + .maybeWithArtifact(ValidatedAndroidData::getCompiledSymbols) + .build(); + + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter RESOURCE_DEP_TO_ARG = AndroidDataConverter.builder(JoinerType.COLON_COMMA) .withRoots(ValidatedAndroidData::getResourceRoots) .withRoots(ValidatedAndroidData::getAssetRoots) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java index c4e33f1eee668f..7798f019b2b534 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java @@ -16,16 +16,21 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion; import com.google.devtools.build.lib.rules.android.AndroidDataConverter.JoinerType; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; import java.util.function.Function; /** Builds up the spawn action for $android_rclass_generator. */ public class RClassGeneratorActionBuilder { - private static final AndroidDataConverter AAPT_CONVERTER = + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT_CONVERTER = AndroidDataConverter.builder(JoinerType.COLON_COMMA) .with(chooseDepsToArg(AndroidAaptVersion.AAPT)) .build(); - private static final AndroidDataConverter AAPT2_CONVERTER = + + @AutoCodec @VisibleForSerialization + static final AndroidDataConverter AAPT2_CONVERTER = AndroidDataConverter.builder(JoinerType.COLON_COMMA) .with(chooseDepsToArg(AndroidAaptVersion.AAPT2)) .build(); @@ -92,12 +97,16 @@ private void build( private static Function chooseDepsToArg( final AndroidAaptVersion version) { - return container -> { - Artifact rTxt = - version == AndroidAaptVersion.AAPT2 ? container.getAapt2RTxt() : container.getRTxt(); - return (rTxt != null ? rTxt.getExecPath() : "") - + "," - + (container.getManifest() != null ? container.getManifest().getExecPath() : ""); + // Use an anonymous inner class for serialization. + return new Function() { + @Override + public String apply(ValidatedAndroidData container) { + Artifact rTxt = + version == AndroidAaptVersion.AAPT2 ? container.getAapt2RTxt() : container.getRTxt(); + return (rTxt != null ? rTxt.getExecPath() : "") + + "," + + (container.getManifest() != null ? container.getManifest().getExecPath() : ""); + } }; } }