Dedup code for generating prebuilt_stubs_sources in snapshot

Removes duplicated code for generating a versioned and non-versioned
form of the prebuilt_stubs_sources module in the snapshot.

Bug: 143678475
Test: m nothing

Change-Id: Ic431b6fcb6f3c7c85f1a04cb6f087424aaf0cffa
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 83a1ad5..96c8416 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -1979,25 +1979,27 @@
 	snapshotRelativeDir := filepath.Join("java", d.Name()+"_stubs_sources")
 	builder.UnzipToSnapshot(stubsSrcJar, snapshotRelativeDir)
 
-	name := d.Name()
-	bp := builder.AndroidBpFile()
-	bp.Printfln("prebuilt_stubs_sources {")
-	bp.Indent()
-	bp.Printfln("name: %q,", builder.VersionedSdkMemberName(name))
-	bp.Printfln("sdk_member_name: %q,", name)
-	bp.Printfln("srcs: [%q],", snapshotRelativeDir)
-	bp.Dedent()
-	bp.Printfln("}")
-	bp.Printfln("")
+	d.generatePrebuiltStubsSources(builder, snapshotRelativeDir, true)
 
 	// This module is for the case when the source tree for the unversioned module
 	// doesn't exist (i.e. building in an unbundled tree). "prefer:" is set to false
 	// so that this module does not eclipse the unversioned module if it exists.
+	d.generatePrebuiltStubsSources(builder, snapshotRelativeDir, false)
+}
+
+func (d *Droidstubs) generatePrebuiltStubsSources(builder android.SnapshotBuilder, snapshotRelativeDir string, versioned bool) {
+	bp := builder.AndroidBpFile()
+	name := d.Name()
 	bp.Printfln("prebuilt_stubs_sources {")
 	bp.Indent()
-	bp.Printfln("name: %q,", name)
+	if versioned {
+		bp.Printfln("name: %q,", builder.VersionedSdkMemberName(name))
+		bp.Printfln("sdk_member_name: %q,", name)
+	} else {
+		bp.Printfln("name: %q,", name)
+		bp.Printfln("prefer: false,")
+	}
 	bp.Printfln("srcs: [%q],", snapshotRelativeDir)
-	bp.Printfln("prefer: false,")
 	bp.Dedent()
 	bp.Printfln("}")
 	bp.Printfln("")
diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go
index a4f54ef..e2e40bb 100644
--- a/sdk/sdk_test.go
+++ b/sdk/sdk_test.go
@@ -515,8 +515,8 @@
 
 prebuilt_stubs_sources {
     name: "myjavaapistubs",
-    srcs: ["java/myjavaapistubs_stubs_sources"],
     prefer: false,
+    srcs: ["java/myjavaapistubs_stubs_sources"],
 }
 
 cc_prebuilt_library_shared {