Translate java libraries to java_library

In androidmk, translate BUILD_JAVA_LIBRARY to java_library plus
installable: true, and BUILD_STATIC_JAVA_LIBRARY to java_library.
In bpfix, rewrite java_library_static to java_library.

Bug: 110885583
Test: androidmk_test.go, bpfix_test.go
Change-Id: I63c2f759ae9c62a43f3439526552d2cd8e8cedc3
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index e3eb82a..29c7365 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -747,8 +747,8 @@
 	"BUILD_NATIVE_BENCHMARK":      "cc_benchmark",
 	"BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",
 
-	"BUILD_JAVA_LIBRARY":             "java_library",
-	"BUILD_STATIC_JAVA_LIBRARY":      "java_library_static",
+	"BUILD_JAVA_LIBRARY":             "java_library_installable", // will be rewritten to java_library by bpfix
+	"BUILD_STATIC_JAVA_LIBRARY":      "java_library",
 	"BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
 	"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
 	"BUILD_PACKAGE":                  "android_app",
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index ed5ae02..80e7a75 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -512,7 +512,7 @@
 			LOCAL_PROGUARD_ENABLED := obfuscation optimization
 			# Custom
 			LOCAL_PROGUARD_ENABLED := custom
-			include $(BUILD_JAVA_LIBRARY)
+			include $(BUILD_STATIC_JAVA_LIBRARY)
 		`,
 		expected: `
 			java_library {
@@ -535,11 +535,53 @@
 		`,
 	},
 	{
+		desc: "java library",
+		in: `
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := a.java
+			include $(BUILD_STATIC_JAVA_LIBRARY)
+
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := b.java
+			include $(BUILD_JAVA_LIBRARY)
+
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := c.java
+			LOCAL_UNINSTALLABLE_MODULE := true
+			include $(BUILD_JAVA_LIBRARY)
+
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := d.java
+			LOCAL_UNINSTALLABLE_MODULE := false
+			include $(BUILD_JAVA_LIBRARY)
+		`,
+		expected: `
+			java_library {
+				srcs: ["a.java"],
+			}
+
+			java_library {
+				installable: true,
+				srcs: ["b.java"],
+			}
+
+			java_library {
+				installable: false,
+				srcs: ["c.java"],
+			}
+
+			java_library {
+				installable: true,
+				srcs: ["d.java"],
+			}
+		`,
+	},
+	{
 		desc: "errorprone options for java library",
 		in: `
 			include $(CLEAR_VARS)
 			LOCAL_ERROR_PRONE_FLAGS := -Xep:AsyncCallableReturnsNull:ERROR -Xep:AsyncFunctionReturnsNull:ERROR
-			include $(BUILD_JAVA_LIBRARY)
+			include $(BUILD_STATIC_JAVA_LIBRARY)
 		`,
 		expected: `
 			java_library {
@@ -631,7 +673,7 @@
 				],
 			}
 
-			java_library_static {
+			java_library {
 				srcs: ["test.java"],
 				static_libs: [],
 			}
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index 05be3bd..056a01b 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -71,6 +71,14 @@
 		fix:  rewriteTestModuleTypes,
 	},
 	{
+		name: "rewriteAndroidmkJavaLibs",
+		fix:  rewriteAndroidmkJavaLibs,
+	},
+	{
+		name: "rewriteJavaStaticLibs",
+		fix:  rewriteJavaStaticLibs,
+	},
+	{
 		name: "mergeMatchingModuleProperties",
 		fix:  runPatchListMod(mergeMatchingModuleProperties),
 	},
@@ -241,7 +249,7 @@
 		hasResourceDirs := hasNonEmptyLiteralListProperty(mod, "resource_dirs")
 
 		if hasAndroidLibraries || hasStaticAndroidLibraries || hasResourceDirs {
-			if mod.Type == "java_library_static" {
+			if mod.Type == "java_library_static" || mod.Type == "java_library" {
 				mod.Type = "android_library"
 			}
 		}
@@ -289,7 +297,7 @@
 			switch mod.Type {
 			case "android_app":
 				mod.Type = "android_test"
-			case "java_library":
+			case "java_library", "java_library_installable":
 				mod.Type = "java_test"
 			case "java_library_host":
 				mod.Type = "java_test_host"
@@ -300,6 +308,51 @@
 	return nil
 }
 
+// rewriteJavaStaticLibs rewrites java_library_static into java_library
+func rewriteJavaStaticLibs(f *Fixer) error {
+	for _, def := range f.tree.Defs {
+		mod, ok := def.(*parser.Module)
+		if !ok {
+			continue
+		}
+
+		if mod.Type == "java_library_static" {
+			mod.Type = "java_library"
+		}
+	}
+
+	return nil
+}
+
+// rewriteAndroidmkJavaLibs rewrites java_library_installable into java_library plus installable: true
+func rewriteAndroidmkJavaLibs(f *Fixer) error {
+	for _, def := range f.tree.Defs {
+		mod, ok := def.(*parser.Module)
+		if !ok {
+			continue
+		}
+
+		if mod.Type != "java_library_installable" {
+			continue
+		}
+
+		mod.Type = "java_library"
+
+		_, hasInstallable := mod.GetProperty("installable")
+		if !hasInstallable {
+			prop := &parser.Property{
+				Name: "installable",
+				Value: &parser.Bool{
+					Value: true,
+				},
+			}
+			mod.Properties = append(mod.Properties, prop)
+		}
+	}
+
+	return nil
+}
+
 func runPatchListMod(modFunc func(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error) func(*Fixer) error {
 	return func(f *Fixer) error {
 		// Make sure all the offsets are accurate
@@ -346,6 +399,7 @@
 	"defaults",
 	"device_supported",
 	"host_supported",
+	"installable",
 }
 
 func reorderCommonProperties(mod *parser.Module, buf []byte, patchlist *parser.PatchList) error {
diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go
index 6ba93f6..16dfce0 100644
--- a/bpfix/bpfix/bpfix_test.go
+++ b/bpfix/bpfix/bpfix_test.go
@@ -497,3 +497,61 @@
 		})
 	}
 }
+
+func TestReplaceJavaStaticLibs(t *testing.T) {
+	tests := []struct {
+		name string
+		in   string
+		out  string
+	}{
+		{
+			name: "static lib",
+			in: `
+				java_library_static {
+					name: "foo",
+				}
+			`,
+			out: `
+				java_library {
+					name: "foo",
+				}
+			`,
+		},
+		{
+			name: "java lib",
+			in: `
+				java_library {
+					name: "foo",
+				}
+			`,
+			out: `
+				java_library {
+					name: "foo",
+				}
+			`,
+		},
+		{
+			name: "java installable lib",
+			in: `
+				java_library {
+					name: "foo",
+					installable: true,
+				}
+			`,
+			out: `
+				java_library {
+					name: "foo",
+					installable: true,
+				}
+			`,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			runPass(t, test.in, test.out, func(fixer *Fixer) error {
+				return rewriteJavaStaticLibs(fixer)
+			})
+		})
+	}
+}