feat(java_indexer): add option to put tapps in the CU corpus (#4960)

this is intended to match the behavior of the --use_compilation_corpus_as_default option in the c++ and go indexers

followup changes will apply this same logic to other types of corpus-less nodes
diff --git a/kythe/java/com/google/devtools/kythe/analyzers/base/KytheEntrySets.java b/kythe/java/com/google/devtools/kythe/analyzers/base/KytheEntrySets.java
index 0ac320c..0361f7a 100644
--- a/kythe/java/com/google/devtools/kythe/analyzers/base/KytheEntrySets.java
+++ b/kythe/java/com/google/devtools/kythe/analyzers/base/KytheEntrySets.java
@@ -53,6 +53,7 @@
 
   private final VName compilationVName;
   private final Map<String, VName> inputVNames;
+  protected boolean useCompilationCorpusAsDefault;
 
   public KytheEntrySets(
       StatisticsCollector statistics,
@@ -78,6 +79,10 @@
     }
   }
 
+  public final void setUseCompilationCorpusAsDefault(boolean d) {
+    this.useCompilationCorpusAsDefault = d;
+  }
+
   /** Returns the {@link FactEmitter} used to emit generated {@link EntrySet}s. */
   public final FactEmitter getEmitter() {
     return emitter;
@@ -338,6 +343,7 @@
     if (ms != null) {
       builder.setProperty("code", ms);
     }
+    builder.setCorpusPath(defaultCorpusPath());
     EntrySet node = emitAndReturn(builder);
     emitEdge(node.getVName(), EdgeKind.PARAM, head, 0);
     emitOrdinalEdges(node.getVName(), EdgeKind.PARAM, arguments, 1);
@@ -361,6 +367,15 @@
     return inputVName == null ? null : EntrySet.extendVName(compilationVName, inputVName);
   }
 
+  /**
+   * Returns a CorpusPath containing the default corpus. This will either be empty string or the
+   * compilation unit's corpus depending on the --use_compilation_corpus_as_default option.
+   */
+  public CorpusPath defaultCorpusPath() {
+    return new CorpusPath(
+        useCompilationCorpusAsDefault ? compilationVName.getCorpus() : "", "", "");
+  }
+
   protected EntrySet emitAndReturn(EntrySet.Builder b) {
     return emitAndReturn(b.build());
   }
diff --git a/kythe/java/com/google/devtools/kythe/analyzers/java/JavaEntrySets.java b/kythe/java/com/google/devtools/kythe/analyzers/java/JavaEntrySets.java
index 498f604..6523209 100644
--- a/kythe/java/com/google/devtools/kythe/analyzers/java/JavaEntrySets.java
+++ b/kythe/java/com/google/devtools/kythe/analyzers/java/JavaEntrySets.java
@@ -73,6 +73,7 @@
       JavaIndexerConfig config) {
     super(statistics, emitter, compilationVName, requiredInputs);
     this.config = config;
+    setUseCompilationCorpusAsDefault(config.getUseCompilationCorpusAsDefault());
   }
 
   Map<Symbol, VName> getSymbolNodes() {
diff --git a/kythe/java/com/google/devtools/kythe/analyzers/java/JavaIndexerConfig.java b/kythe/java/com/google/devtools/kythe/analyzers/java/JavaIndexerConfig.java
index 6ae2a3f..f6f34ff 100644
--- a/kythe/java/com/google/devtools/kythe/analyzers/java/JavaIndexerConfig.java
+++ b/kythe/java/com/google/devtools/kythe/analyzers/java/JavaIndexerConfig.java
@@ -20,7 +20,6 @@
 import com.google.devtools.kythe.analyzers.base.IndexerConfig;
 
 public class JavaIndexerConfig extends IndexerConfig {
-
   @Parameter(names = "--emit_jvm_signatures", description = "Generate vnames with jvm signatures.")
   private boolean emitJvmSignatures;
 
@@ -69,6 +68,11 @@
               + " compiler insists on being read from the local file system.")
   private String temporaryDirectory;
 
+  @Parameter(
+      names = "--use_compilation_corpus_as_default",
+      description = "Use the CompilationUnit VName corpus as the default.")
+  private boolean useCompilationCorpusAsDefault;
+
   public JavaIndexerConfig(String programName) {
     super(programName);
   }
@@ -101,6 +105,10 @@
     return temporaryDirectory;
   }
 
+  public boolean getUseCompilationCorpusAsDefault() {
+    return useCompilationCorpusAsDefault;
+  }
+
   public JavaIndexerConfig setIgnoreVNamePaths(boolean ignoreVNamePaths) {
     this.ignoreVNamePaths = ignoreVNamePaths;
     return this;
@@ -136,4 +144,9 @@
     this.temporaryDirectory = temporaryDirectory;
     return this;
   }
+
+  public JavaIndexerConfig setUseCompilationCorpusAsDefault(boolean useCompilationCorpusAsDefault) {
+    this.useCompilationCorpusAsDefault = useCompilationCorpusAsDefault;
+    return this;
+  }
 }
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
index 1d3f13a..d331aa0 100644
--- a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/BUILD
@@ -511,3 +511,17 @@
     name = "abstract_class_test",
     compilation = ":abstract_class",
 )
+
+java_library(
+    name = "default_corpus",
+    srcs = ["DefaultCorpus.java"],
+)
+
+java_verifier_test(
+    name = "default_corpus_test",
+    compilation = ":default_corpus",
+    indexer_opts = [
+        "--verbose",
+        "--use_compilation_corpus_as_default",
+    ],
+)
diff --git a/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/DefaultCorpus.java b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/DefaultCorpus.java
new file mode 100644
index 0000000..97140fd
--- /dev/null
+++ b/kythe/javatests/com/google/devtools/kythe/analyzers/java/testdata/pkg/DefaultCorpus.java
@@ -0,0 +1,14 @@
+package pkg;
+
+import java.util.List;
+
+// Test that when the --use_compilation_corpus_as_default option is enabled,
+// tapps are placed in the CU's corpus. In this example, the "List<String>" tapp
+// should be given the "kythe" corpus.
+
+class DefaultCorpus {
+  //- @myvar defines/binding MyVar?
+  //- MyVar typed ListStringType=vname(_,"kythe",_,_,"java")
+  //- ListStringType.node/kind "tapp"
+  private List<String> myvar;
+}