Skip to content

Commit

Permalink
Reconvert the lambdas in AndroidDataConverter to anonymous inner clas…
Browse files Browse the repository at this point in the history
…ses.

Tag some static members with @AutoCodec.
Replace some lambdas with explicit functions or classes.

PiperOrigin-RevId: 200467500
  • Loading branch information
mjhalupka authored and Copybara-Service committed Jun 13, 2018
1 parent fc83d75 commit ed1e759
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@ public class AndroidDataConverter<T> extends ParametrizedMapFn<T> {
* Converts Android data to the "SerializedAndroidData" format used by the Android data processing
* actions.
*/
@AutoCodec
static final AndroidDataConverter<MergableAndroidData> MERGABLE_DATA_CONVERTER =
AndroidDataConverter.<MergableAndroidData>builder(JoinerType.SEMICOLON_AMPERSAND)
.withRoots(MergableAndroidData::getResourceRoots)
Expand Down Expand Up @@ -137,24 +139,54 @@ private Builder(JoinerType joinerType) {
}

Builder<T> withRoots(Function<T, ImmutableList<PathFragment>> rootsFunction) {
return with(t -> rootsToString(rootsFunction.apply(t)));
// Anonymous inner class for serialization.
return with(
new Function<T, String>() {
@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<T> withArtifact(Function<T, Artifact> artifactFunction) {
return with(t -> artifactFunction.apply(t).getExecPathString());
// Anonymous inner class for serialization.
return with(
new Function<T, String>() {
@Override
public String apply(T t) {
return artifactFunction.apply(t).getExecPathString();
}
});
}

Builder<T> maybeWithArtifact(Function<T, Artifact> nullableArtifactFunction) {
// Anonymous inner class for serialization.
return with(
t -> {
@Nullable Artifact artifact = nullableArtifactFunction.apply(t);
return artifact == null ? "" : artifact.getExecPathString();
new Function<T, String>() {
@Override
public String apply(T t) {
@Nullable Artifact artifact = nullableArtifactFunction.apply(t);
return artifact == null ? "" : artifact.getExecPathString();
}
});
}

Builder<T> withLabel(Function<T, Label> 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<T, String>() {
@Override
public String apply(T t) {
return joinerType.escape(labelFunction.apply(t).toString());
}
});
}

Builder<T> with(Function<T, String> stringFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,34 @@
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<ParsedAndroidAssets> AAPT2_ASSET_DEP_TO_ARG =
@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ParsedAndroidAssets> AAPT2_ASSET_DEP_TO_ARG =
AndroidDataConverter.<ParsedAndroidAssets>builder(JoinerType.SEMICOLON_AMPERSAND)
.withRoots(ParsedAndroidAssets::getResourceRoots)
.withRoots(ParsedAndroidAssets::getAssetRoots)
.withLabel(ParsedAndroidAssets::getLabel)
.withArtifact(ParsedAndroidAssets::getSymbols)
.build();

private static final AndroidDataConverter<ParsedAndroidAssets> AAPT2_ASSET_DEP_TO_ARG_NO_PARSE =
@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ParsedAndroidAssets> AAPT2_ASSET_DEP_TO_ARG_NO_PARSE =
AndroidDataConverter.<ParsedAndroidAssets>builder(JoinerType.SEMICOLON_AMPERSAND)
.withRoots(ParsedAndroidAssets::getResourceRoots)
.withRoots(ParsedAndroidAssets::getAssetRoots)
.withLabel(ParsedAndroidAssets::getLabel)
.withArtifact(ParsedAndroidAssets::getCompiledSymbols)
.build();

private static final AndroidDataConverter<ValidatedAndroidData> AAPT2_RESOURCE_DEP_TO_ARG =
@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ValidatedAndroidData> AAPT2_RESOURCE_DEP_TO_ARG =
AndroidDataConverter.<ValidatedAndroidData>builder(JoinerType.COLON_COMMA)
.withRoots(ValidatedAndroidData::getResourceRoots)
.withRoots(ValidatedAndroidData::getAssetRoots)
Expand All @@ -50,17 +55,18 @@ public class AndroidResourcesProcessorBuilder {
.maybeWithArtifact(ValidatedAndroidData::getSymbols)
.build();

private static final AndroidDataConverter<ValidatedAndroidData>
AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE =
AndroidDataConverter.<ValidatedAndroidData>builder(JoinerType.COLON_COMMA)
.withRoots(ValidatedAndroidData::getResourceRoots)
.withRoots(ValidatedAndroidData::getAssetRoots)
.withArtifact(ValidatedAndroidData::getManifest)
.maybeWithArtifact(ValidatedAndroidData::getAapt2RTxt)
.maybeWithArtifact(ValidatedAndroidData::getCompiledSymbols)
.build();

private static final AndroidDataConverter<ValidatedAndroidData> RESOURCE_DEP_TO_ARG =
@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ValidatedAndroidData> AAPT2_RESOURCE_DEP_TO_ARG_NO_PARSE =
AndroidDataConverter.<ValidatedAndroidData>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<ValidatedAndroidData> RESOURCE_DEP_TO_ARG =
AndroidDataConverter.<ValidatedAndroidData>builder(JoinerType.COLON_COMMA)
.withRoots(ValidatedAndroidData::getResourceRoots)
.withRoots(ValidatedAndroidData::getAssetRoots)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidatedAndroidData> AAPT_CONVERTER =
@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ValidatedAndroidData> AAPT_CONVERTER =
AndroidDataConverter.<ValidatedAndroidData>builder(JoinerType.COLON_COMMA)
.with(chooseDepsToArg(AndroidAaptVersion.AAPT))
.build();
private static final AndroidDataConverter<ValidatedAndroidData> AAPT2_CONVERTER =

@AutoCodec @VisibleForSerialization
static final AndroidDataConverter<ValidatedAndroidData> AAPT2_CONVERTER =
AndroidDataConverter.<ValidatedAndroidData>builder(JoinerType.COLON_COMMA)
.with(chooseDepsToArg(AndroidAaptVersion.AAPT2))
.build();
Expand Down Expand Up @@ -92,12 +97,16 @@ private void build(

private static Function<ValidatedAndroidData, String> 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<ValidatedAndroidData, String>() {
@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() : "");
}
};
}
}

0 comments on commit ed1e759

Please sign in to comment.