Refactor the hiddenAPI() method for reusability

A follow up change needs to be able to contribute to the information
the hiddenapi process collates without having a dex file encoded. This
change pushes all the functionality related to information gathering
into the hiddenAPIGenerateCSV() method and then renames it and the
hiddenAPI() method to make it clearer what they do.

Bug: 178361284
Test: m droid
      Verified that hiddenapi files (both aggregated ones and for the
      individual modules) are not affected by this change.
Change-Id: I04417720216a0fbadcd88e6185e7de6570af6216
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index 7e34c83..66d9ca0 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -123,19 +123,21 @@
 	h.annotationsOnly = strings.HasSuffix(name, "-hiddenapi")
 }
 
-// hiddenAPI is called by any module that could contribute to the hiddenapi processing.
+// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi
+// processing.
 //
 // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
 // jar list.
 //
 // Otherwise, it generates ninja rules to do the following:
-// 1. Generates CSV files needed for hiddenapi processing.
+// 1. Extract information needed for hiddenapi processing from the module and output it into CSV
+//    files.
 // 2. Conditionally adds the supplied dex file to the list of files used to generate the
 //    hiddenAPISingletonPathsStruct.stubsFlag file.
 // 3. Conditionally creates a copy of the supplied dex file into which it has encoded the hiddenapi
 //    flags and returns this instead of the supplied dex jar, otherwise simply returns the supplied
 //    dex jar.
-func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
+func (h *hiddenAPI) hiddenAPIExtractAndEncode(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
 	implementationJar android.Path, uncompressDex bool) android.OutputPath {
 
 	if !h.active {
@@ -146,13 +148,7 @@
 	// used as a source of information for hidden API processing otherwise it will result in
 	// duplicate entries in the files.
 	if primary {
-		// Create ninja rules to generate various CSV files needed by hiddenapi and store the paths
-		// in the hiddenAPI structure.
-		h.hiddenAPIGenerateCSV(ctx, implementationJar)
-
-		// Save the unencoded dex jar so it can be used when generating the
-		// hiddenAPISingletonPathsStruct.stubFlags file.
-		h.bootDexJarPath = dexJar
+		h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar)
 	}
 
 	if !h.annotationsOnly {
@@ -168,7 +164,12 @@
 	return dexJar
 }
 
-func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) {
+// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
+// jar, and outputs it to the appropriate module specific CSV file.
+//
+// It also makes the dex jar available for use when generating the
+// hiddenAPISingletonPathsStruct.stubFlags.
+func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path) {
 	stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
 
 	flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
@@ -207,6 +208,10 @@
 		FlagWithOutput("--output=", indexCSV)
 	rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
 	h.indexCSVPath = indexCSV
+
+	// Save the unencoded dex jar so it can be used when generating the
+	// hiddenAPISingletonPathsStruct.stubFlags file.
+	h.bootDexJarPath = dexJar
 }
 
 var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
diff --git a/java/hiddenapi_singleton_test.go b/java/hiddenapi_singleton_test.go
index fcaa94a..77cfff4 100644
--- a/java/hiddenapi_singleton_test.go
+++ b/java/hiddenapi_singleton_test.go
@@ -76,14 +76,6 @@
 	}
 }
 
-func checkRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
-	actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
-	expected = strings.TrimSpace(expected)
-	if actual != expected {
-		t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
-	}
-}
-
 func TestHiddenAPIIndexSingleton(t *testing.T) {
 	ctx, _ := testHiddenAPIBootJars(t, `
 		java_library {
@@ -108,7 +100,7 @@
 
 	hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
 	indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
-	checkRuleInputs(t, `
+	CheckHiddenAPIRuleInputs(t, `
 .intermediates/bar/android_common/hiddenapi/index.csv
 .intermediates/foo/android_common/hiddenapi/index.csv
 `,
diff --git a/java/java.go b/java/java.go
index cee14cc..dfbcd6f 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1805,7 +1805,7 @@
 		primary = primary && !j.IsReplacedByPrebuilt()
 
 		// Hidden API CSV generation and dex encoding
-		dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile,
+		dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile,
 			proptools.Bool(j.dexProperties.Uncompress_dex))
 
 		// merge dex jar with resources if necessary
@@ -2965,7 +2965,7 @@
 			primary := j.Prebuilt().UsePrebuilt()
 
 			// Hidden API CSV generation and dex encoding
-			dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, outputFile,
+			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, outputFile,
 				proptools.Bool(j.dexProperties.Uncompress_dex))
 
 			j.dexJarFile = dexOutputFile
diff --git a/java/testing.go b/java/testing.go
index 5fcf84c..781106f 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -18,6 +18,7 @@
 	"fmt"
 	"reflect"
 	"sort"
+	"strings"
 	"testing"
 
 	"android/soong/android"
@@ -237,3 +238,11 @@
 		t.Errorf("expected %#q, found %#q", expected, actual)
 	}
 }
+
+func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) {
+	actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n"))
+	expected = strings.TrimSpace(expected)
+	if actual != expected {
+		t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual)
+	}
+}