diff options
author | 2018-07-10 05:59:01 -0700 | |
---|---|---|
committer | 2018-07-10 06:00:38 -0700 | |
commit | 8c11fe9415e6d30069bdba559f6943747dd56e9f (patch) | |
tree | bd47cfafc521b2ec821adc422ada68690f85cd28 /src/main/java/com/google | |
parent | c45606ebc029303f3ef533aeaf8ff9b0406f27d9 (diff) |
Use generic classes instead of cquery specific class.
Also introduce NamedThreadSafeOutputFormatterCallback for common code.
RELNOTES: None
PiperOrigin-RevId: 203932877
Diffstat (limited to 'src/main/java/com/google')
5 files changed, 77 insertions, 52 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java index 31681b1af0..b2018a23fa 100644 --- a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java +++ b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java @@ -18,8 +18,8 @@ import com.google.devtools.build.lib.analysis.TargetAndConfiguration; import com.google.devtools.build.lib.analysis.ViewCreationFailedException; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.events.Event; -import com.google.devtools.build.lib.query2.ConfiguredTargetQueryEnvironment; -import com.google.devtools.build.lib.query2.CqueryThreadsafeCallback; +import com.google.devtools.build.lib.query2.NamedThreadSafeOutputFormatterCallback; +import com.google.devtools.build.lib.query2.PostAnalysisQueryEnvironment; import com.google.devtools.build.lib.query2.engine.QueryEvalResult; import com.google.devtools.build.lib.query2.engine.QueryException; import com.google.devtools.build.lib.query2.engine.QueryExpression; @@ -35,7 +35,7 @@ import java.util.stream.Collectors; * Version of {@link BuildTool} that handles all work for queries based on results from the analysis * phase. */ -public abstract class PostAnalysisQueryBuildTool extends BuildTool { +public abstract class PostAnalysisQueryBuildTool<T> extends BuildTool { private final QueryExpression queryExpression; @@ -76,8 +76,7 @@ public abstract class PostAnalysisQueryBuildTool extends BuildTool { } } - // TODO(twerth): Make this more generic when introducting a PostAnalysisQueryEnvironment. - protected abstract ConfiguredTargetQueryEnvironment getQueryEnvironment( + protected abstract PostAnalysisQueryEnvironment<T> getQueryEnvironment( BuildRequest request, BuildConfiguration hostConfiguration, BuildConfiguration targetConfig, @@ -125,30 +124,30 @@ public abstract class PostAnalysisQueryBuildTool extends BuildTool { WalkableGraph walkableGraph = SkyframeExecutorWrappingWalkableGraph.of(env.getSkyframeExecutor()); - ConfiguredTargetQueryEnvironment configuredTargetQueryEnvironment = + PostAnalysisQueryEnvironment<T> postAnalysisQueryEnvironment = getQueryEnvironment(request, hostConfiguration, targetConfig, walkableGraph); - Iterable<CqueryThreadsafeCallback> callbacks = - configuredTargetQueryEnvironment.getDefaultOutputFormatters( - configuredTargetQueryEnvironment.getAccessor(), + Iterable<NamedThreadSafeOutputFormatterCallback<T>> callbacks = + postAnalysisQueryEnvironment.getDefaultOutputFormatters( + postAnalysisQueryEnvironment.getAccessor(), env.getReporter(), env.getSkyframeExecutor(), hostConfiguration, runtime.getRuleClassProvider().getTrimmingTransitionFactory(), env.getPackageManager()); - String outputFormat = configuredTargetQueryEnvironment.getOutputFormat(); - CqueryThreadsafeCallback callback = - CqueryThreadsafeCallback.getCallback(outputFormat, callbacks); + String outputFormat = postAnalysisQueryEnvironment.getOutputFormat(); + NamedThreadSafeOutputFormatterCallback<T> callback = + NamedThreadSafeOutputFormatterCallback.selectCallback(outputFormat, callbacks); if (callback == null) { env.getReporter() .handle( Event.error( String.format( "Invalid output format '%s'. Valid values are: %s", - outputFormat, CqueryThreadsafeCallback.callbackNames(callbacks)))); + outputFormat, + NamedThreadSafeOutputFormatterCallback.callbackNames(callbacks)))); return; } - QueryEvalResult result = - configuredTargetQueryEnvironment.evaluateQuery(queryExpression, callback); + QueryEvalResult result = postAnalysisQueryEnvironment.evaluateQuery(queryExpression, callback); if (result.isEmpty()) { env.getReporter().handle(Event.info("Empty query results")); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/ConfiguredTargetQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/ConfiguredTargetQueryEnvironment.java index 242edb534a..f48c384d9c 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/ConfiguredTargetQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/ConfiguredTargetQueryEnvironment.java @@ -141,17 +141,18 @@ public class ConfiguredTargetQueryEnvironment } @Override - public ImmutableList<CqueryThreadsafeCallback> getDefaultOutputFormatters( - TargetAccessor<ConfiguredTarget> accessor, - Reporter reporter, - SkyframeExecutor skyframeExecutor, - BuildConfiguration hostConfiguration, - @Nullable RuleTransitionFactory trimmingTransitionFactory, - PackageManager packageManager) { + public ImmutableList<NamedThreadSafeOutputFormatterCallback<ConfiguredTarget>> + getDefaultOutputFormatters( + TargetAccessor<ConfiguredTarget> accessor, + Reporter reporter, + SkyframeExecutor skyframeExecutor, + BuildConfiguration hostConfiguration, + @Nullable RuleTransitionFactory trimmingTransitionFactory, + PackageManager packageManager) { AspectResolver aspectResolver = cqueryOptions.aspectDeps.createResolver(packageManager, reporter); OutputStream out = reporter.getOutErr().getOutputStream(); - return new ImmutableList.Builder<CqueryThreadsafeCallback>() + return new ImmutableList.Builder<NamedThreadSafeOutputFormatterCallback<ConfiguredTarget>>() .add( new LabelAndConfigurationOutputFormatterCallback( reporter, cqueryOptions, out, skyframeExecutor, accessor)) diff --git a/src/main/java/com/google/devtools/build/lib/query2/CqueryThreadsafeCallback.java b/src/main/java/com/google/devtools/build/lib/query2/CqueryThreadsafeCallback.java index 1466433c9a..032d6344fd 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/CqueryThreadsafeCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/CqueryThreadsafeCallback.java @@ -13,14 +13,11 @@ // limitations under the License. package com.google.devtools.build.lib.query2; -import static java.util.stream.Collectors.joining; import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Streams; import com.google.devtools.build.lib.analysis.ConfiguredTarget; import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor; -import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback; import com.google.devtools.build.lib.query2.output.CqueryOptions; import com.google.devtools.build.lib.skyframe.SkyframeExecutor; import java.io.IOException; @@ -34,7 +31,7 @@ import java.util.List; * that is populated by child classes. */ public abstract class CqueryThreadsafeCallback - extends ThreadSafeOutputFormatterCallback<ConfiguredTarget> { + extends NamedThreadSafeOutputFormatterCallback<ConfiguredTarget> { protected final Reporter reporter; protected final CqueryOptions options; @@ -59,22 +56,6 @@ public abstract class CqueryThreadsafeCallback this.accessor = (ConfiguredTargetAccessor) accessor; } - public abstract String getName(); - - public static String callbackNames(Iterable<CqueryThreadsafeCallback> callbacks) { - return Streams.stream(callbacks).map(CqueryThreadsafeCallback::getName).collect(joining(", ")); - } - - public static CqueryThreadsafeCallback getCallback( - String type, Iterable<CqueryThreadsafeCallback> callbacks) { - for (CqueryThreadsafeCallback callback : callbacks) { - if (callback.getName().equals(type)) { - return callback; - } - } - return null; - } - public void addResult(String string) { result.add(string); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/NamedThreadSafeOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/NamedThreadSafeOutputFormatterCallback.java new file mode 100644 index 0000000000..a768f22fa8 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/query2/NamedThreadSafeOutputFormatterCallback.java @@ -0,0 +1,42 @@ +// Copyright 2018 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.query2; + +import static java.util.stream.Collectors.joining; + +import com.google.common.collect.Streams; +import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback; + +/** A {@link ThreadSafeOutputFormatterCallback} that has a name to select on. */ +public abstract class NamedThreadSafeOutputFormatterCallback<T> + extends ThreadSafeOutputFormatterCallback<T> { + public abstract String getName(); + + public static <T> String callbackNames( + Iterable<NamedThreadSafeOutputFormatterCallback<T>> callbacks) { + return Streams.stream(callbacks) + .map(NamedThreadSafeOutputFormatterCallback::getName) + .collect(joining(", ")); + } + + public static <T> NamedThreadSafeOutputFormatterCallback<T> selectCallback( + String type, Iterable<NamedThreadSafeOutputFormatterCallback<T>> callbacks) { + for (NamedThreadSafeOutputFormatterCallback<T> callback : callbacks) { + if (callback.getName().equals(type)) { + return callback; + } + } + return null; + } +} diff --git a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java index 89aae592bc..df61da858a 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java @@ -18,7 +18,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import com.google.devtools.build.lib.analysis.ConfiguredTarget; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget; import com.google.devtools.build.lib.cmdline.Label; @@ -85,7 +84,7 @@ import javax.annotation.Nullable; * * <p>Aspects are also not supported, but probably should be in some fashion. */ -abstract class PostAnalysisQueryEnvironment<T> extends AbstractBlazeQueryEnvironment<T> { +public abstract class PostAnalysisQueryEnvironment<T> extends AbstractBlazeQueryEnvironment<T> { protected final BuildConfiguration defaultTargetConfiguration; protected final BuildConfiguration hostConfiguration; private final String parserPrefix; @@ -133,13 +132,16 @@ abstract class PostAnalysisQueryEnvironment<T> extends AbstractBlazeQueryEnviron this.accessor = targetAccessor; } - public abstract ImmutableList<CqueryThreadsafeCallback> getDefaultOutputFormatters( - TargetAccessor<ConfiguredTarget> accessor, - Reporter reporter, - SkyframeExecutor skyframeExecutor, - BuildConfiguration hostConfiguration, - @Nullable RuleTransitionFactory trimmingTransitionFactory, - PackageManager packageManager); + public abstract ImmutableList<NamedThreadSafeOutputFormatterCallback<T>> + getDefaultOutputFormatters( + TargetAccessor<T> accessor, + Reporter reporter, + SkyframeExecutor skyframeExecutor, + BuildConfiguration hostConfiguration, + @Nullable RuleTransitionFactory trimmingTransitionFactory, + PackageManager packageManager); + + public abstract String getOutputFormat(); protected abstract KeyExtractor<T, ConfiguredTargetKey> getConfiguredTargetKeyExtractor(); |