Add a convenience method to InstrumentedFilesCollector.

This simplifies the users a bit, and makes it easier to refactor the code.

--
MOS_MIGRATED_REVID=101802767
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
index 0f644f3..26340fb 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
@@ -490,8 +490,8 @@
   InstrumentedFilesProvider getInstrumentedFilesProvider(Iterable<Artifact> files) {
     return cppConfiguration.isLipoContextCollector()
         ? InstrumentedFilesProviderImpl.EMPTY
-        : new InstrumentedFilesProviderImpl(new InstrumentedFilesCollector(
-            ruleContext, CppRuleClasses.INSTRUMENTATION_SPEC, CC_METADATA_COLLECTOR, files));
+        : InstrumentedFilesCollector.collect(
+            ruleContext, CppRuleClasses.INSTRUMENTATION_SPEC, CC_METADATA_COLLECTOR, files);
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java b/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java
index 83d3d7a..a95ff65 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java
@@ -32,7 +32,6 @@
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider;
-import com.google.devtools.build.lib.rules.test.InstrumentedFilesProviderImpl;
 import com.google.devtools.build.lib.util.FileTypeSet;
 import com.google.devtools.build.lib.vfs.PathFragment;
 
@@ -50,8 +49,8 @@
     NestedSet<Artifact> middleman = CompilationHelper.getAggregatingMiddleman(
         ruleContext, Actions.escapeLabel(ruleContext.getLabel()), filesToBuild);
 
-    InstrumentedFilesCollector instrumentedFilesCollector =
-        new InstrumentedFilesCollector(ruleContext,
+    InstrumentedFilesProvider instrumentedFilesProvider =
+        InstrumentedFilesCollector.collect(ruleContext,
             // what do *we* know about whether this is a source file or not
             new InstrumentationSpec(FileTypeSet.ANY_FILE, "srcs", "deps", "data"),
             InstrumentedFilesCollector.NO_METADATA_COLLECTOR, filesToBuild);
@@ -68,8 +67,7 @@
         .add(RunfilesProvider.class, runfilesProvider)
         .setFilesToBuild(filesToBuild)
         .setRunfilesSupport(null, getExecutable(filesToBuild))
-        .add(InstrumentedFilesProvider.class, new InstrumentedFilesProviderImpl(
-            instrumentedFilesCollector))
+        .add(InstrumentedFilesProvider.class, instrumentedFilesProvider)
         .add(MiddlemanProvider.class, new MiddlemanProvider(middleman))
         .add(FilegroupPathProvider.class,
             new FilegroupPathProvider(getFilegroupPath(ruleContext)))
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
index b713af8..d786c37 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCommon.java
@@ -47,7 +47,6 @@
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.LocalMetadataCollector;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider;
-import com.google.devtools.build.lib.rules.test.InstrumentedFilesProviderImpl;
 import com.google.devtools.build.lib.syntax.Label;
 import com.google.devtools.build.lib.util.FileType;
 import com.google.devtools.build.lib.vfs.FileSystemUtils;
@@ -520,20 +519,19 @@
 
   public void addTransitiveInfoProviders(RuleConfiguredTargetBuilder builder,
       NestedSet<Artifact> filesToBuild, @Nullable Artifact classJar) {
-    InstrumentedFilesCollector instrumentedFilesCollector =
-        new InstrumentedFilesCollector(ruleContext, semantics.getCoverageInstrumentationSpec(),
+    InstrumentedFilesProvider instrumentedFilesProvider =
+        InstrumentedFilesCollector.collect(ruleContext, semantics.getCoverageInstrumentationSpec(),
             JAVA_METADATA_COLLECTOR, filesToBuild);
 
     builder
-        .add(InstrumentedFilesProvider.class, new InstrumentedFilesProviderImpl(
-            instrumentedFilesCollector))
+        .add(InstrumentedFilesProvider.class, instrumentedFilesProvider)
         .add(JavaExportsProvider.class, new JavaExportsProvider(collectTransitiveExports()))
         .addOutputGroup(OutputGroupProvider.FILES_TO_COMPILE, getFilesToCompile(classJar));
 
     if (!TargetUtils.isTestRule(ruleContext.getTarget())) {
       builder.addOutputGroup(OutputGroupProvider.BASELINE_COVERAGE,
           BaselineCoverageAction.getBaselineCoverageArtifacts(ruleContext,
-              instrumentedFilesCollector.getInstrumentedFiles()));
+              instrumentedFilesProvider.getInstrumentedFiles()));
     }
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java
index a537eb9..fe9c2b6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java
@@ -42,7 +42,6 @@
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.LocalMetadataCollector;
 import com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider;
-import com.google.devtools.build.lib.rules.test.InstrumentedFilesProviderImpl;
 import com.google.devtools.build.lib.syntax.Label;
 import com.google.devtools.build.lib.util.FileType;
 import com.google.devtools.build.lib.vfs.PathFragment;
@@ -126,10 +125,8 @@
     PythonSourcesProvider sourcesProvider =
         new PythonSourcesProvider(transitivePythonSources, usesSharedLibraries());
     builder
-        .add(InstrumentedFilesProvider.class, new InstrumentedFilesProviderImpl(
-            new InstrumentedFilesCollector(ruleContext,
-                semantics.getCoverageInstrumentationSpec(), METADATA_COLLECTOR,
-                filesToBuild)))
+        .add(InstrumentedFilesProvider.class, InstrumentedFilesCollector.collect(ruleContext,
+            semantics.getCoverageInstrumentationSpec(), METADATA_COLLECTOR, filesToBuild))
         .add(PythonSourcesProvider.class, sourcesProvider)
         .addSkylarkTransitiveInfo(PythonSourcesProvider.SKYLARK_NAME, sourcesProvider)
         // Python targets are not really compilable. The best we can do is make sure that all
diff --git a/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesCollector.java b/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesCollector.java
index 2860751..73f4cc1 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesCollector.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesCollector.java
@@ -15,6 +15,7 @@
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.actions.Action;
 import com.google.devtools.build.lib.actions.Artifact;
 import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
@@ -38,6 +39,13 @@
  * A helper class for collecting instrumented files and metadata for a target.
  */
 public final class InstrumentedFilesCollector {
+  public static InstrumentedFilesProvider collect(RuleContext ruleContext, InstrumentationSpec spec,
+      LocalMetadataCollector localMetadataCollector, Iterable<Artifact> rootFiles) {
+    InstrumentedFilesCollector collector = new InstrumentedFilesCollector(ruleContext, spec,
+        localMetadataCollector, rootFiles);
+    return new InstrumentedFilesProviderImpl(collector.instrumentedFiles,
+        collector.instrumentationMetadataFiles, ImmutableMap.<String, String>of());
+  }
 
   /**
    * The set of file types and attributes to visit to collect instrumented files for a certain rule
@@ -121,7 +129,7 @@
   private final NestedSet<Artifact> instrumentationMetadataFiles;
   private final NestedSet<Artifact> instrumentedFiles;
 
-  public InstrumentedFilesCollector(RuleContext ruleContext, InstrumentationSpec spec,
+  private InstrumentedFilesCollector(RuleContext ruleContext, InstrumentationSpec spec,
       LocalMetadataCollector localMetadataCollector, Iterable<Artifact> rootFiles) {
     this.ruleContext = ruleContext;
     this.spec = spec;
diff --git a/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesProviderImpl.java b/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesProviderImpl.java
index 5bf2155..90605e6 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesProviderImpl.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/test/InstrumentedFilesProviderImpl.java
@@ -25,20 +25,10 @@
  * An implementation class for the InstrumentedFilesProvider interface.
  */
 public final class InstrumentedFilesProviderImpl implements InstrumentedFilesProvider {
-  public static final InstrumentedFilesProvider EMPTY = new InstrumentedFilesProvider() {
-    @Override
-    public NestedSet<Artifact> getInstrumentedFiles() {
-      return NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER);
-    }
-    @Override
-    public NestedSet<Artifact> getInstrumentationMetadataFiles() {
-      return NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER);
-    }
-    @Override
-    public Map<String, String> getExtraEnv() {
-      return ImmutableMap.of();
-    }
-  };
+  public static final InstrumentedFilesProvider EMPTY = new InstrumentedFilesProviderImpl(
+      NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER),
+      NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER),
+      ImmutableMap.<String, String>of());
 
   private final NestedSet<Artifact> instrumentedFiles;
   private final NestedSet<Artifact> instrumentationMetadataFiles;
@@ -51,12 +41,6 @@
     this.extraEnv = ImmutableMap.copyOf(extraEnv);
   }
 
-  public InstrumentedFilesProviderImpl(InstrumentedFilesCollector collector) {
-    this.instrumentedFiles = collector.getInstrumentedFiles();
-    this.instrumentationMetadataFiles = collector.getInstrumentationMetadataFiles();
-    this.extraEnv = ImmutableMap.of();
-  }
-
   @Override
   public NestedSet<Artifact> getInstrumentedFiles() {
     return instrumentedFiles;