Fix java_import and android_library_import conversions

java_import and android_library_import modules can't be handled
directly in androidmk because the results may depend on properties
that haven't been parsed yet.  Add a bpfix pass (which is
automatically included at the end of androidmk) to select
android_library_import vs. java_import based on the extension
of the prebuilt file, and convert the srcs property to jars or aars
as appropriate.

Bug: 73724997
Test: androidmk_test.go
Change-Id: I1024742e9e96d5e1e88c3cc139eeb0d5a2f6849b
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index 3252791..45df1a5 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -492,6 +492,36 @@
 			}
 		`,
 	},
+	{
+		desc: "java prebuilt",
+		in: `
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := test.jar
+			LOCAL_MODULE_CLASS := JAVA_LIBRARIES
+			include $(BUILD_PREBUILT)
+		`,
+		expected: `
+			java_import {
+				jars: ["test.jar"],
+
+			}
+		`,
+	},
+	{
+		desc: "aar prebuilt",
+		in: `
+			include $(CLEAR_VARS)
+			LOCAL_SRC_FILES := test.aar
+			LOCAL_MODULE_CLASS := JAVA_LIBRARIES
+			include $(BUILD_PREBUILT)
+		`,
+		expected: `
+			android_library_import {
+				aars: ["test.aar"],
+
+			}
+		`,
+	},
 }
 
 func reformatBlueprint(input string) string {
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index 67a5909..2358f0c 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -19,6 +19,7 @@
 import (
 	"bytes"
 	"fmt"
+	"path/filepath"
 
 	"github.com/google/blueprint/parser"
 )
@@ -26,7 +27,8 @@
 // A FixRequest specifies the details of which fixes to apply to an individual file
 // A FixRequest doesn't specify whether to do a dry run or where to write the results; that's in cmd/bpfix.go
 type FixRequest struct {
-	simplifyKnownRedundantVariables bool
+	simplifyKnownRedundantVariables    bool
+	rewriteIncorrectAndroidmkPrebuilts bool
 }
 
 func NewFixRequest() FixRequest {
@@ -36,6 +38,7 @@
 func (r FixRequest) AddAll() (result FixRequest) {
 	result = r
 	result.simplifyKnownRedundantVariables = true
+	result.rewriteIncorrectAndroidmkPrebuilts = true
 	return result
 }
 
@@ -87,6 +90,12 @@
 			return err
 		}
 	}
+	if config.rewriteIncorrectAndroidmkPrebuilts {
+		err := rewriteIncorrectAndroidmkPrebuilts(tree)
+		if err != nil {
+			return err
+		}
+	}
 	return nil
 }
 
@@ -95,6 +104,38 @@
 	return removeMatchingModuleListProperties(tree, "export_include_dirs", "local_include_dirs")
 }
 
+func rewriteIncorrectAndroidmkPrebuilts(tree *parser.File) error {
+	for _, def := range tree.Defs {
+		mod, ok := def.(*parser.Module)
+		if !ok {
+			continue
+		}
+		if mod.Type != "java_import" {
+			continue
+		}
+		srcs, ok := getLiteralListProperty(mod, "srcs")
+		if !ok {
+			continue
+		}
+		if len(srcs.Values) == 0 {
+			continue
+		}
+		src, ok := srcs.Values[0].(*parser.String)
+		if !ok {
+			continue
+		}
+		switch filepath.Ext(src.Value) {
+		case ".jar":
+			renameProperty(mod, "srcs", "jars")
+		case ".aar":
+			renameProperty(mod, "srcs", "aars")
+			mod.Type = "android_library_import"
+		}
+	}
+
+	return nil
+}
+
 // removes from <items> every item present in <removals>
 func filterExpressionList(items *parser.List, removals *parser.List) {
 	writeIndex := 0
@@ -146,3 +187,11 @@
 	list, ok = prop.Value.(*parser.List)
 	return list, ok
 }
+
+func renameProperty(mod *parser.Module, from, to string) {
+	for _, prop := range mod.Properties {
+		if prop.Name == from {
+			prop.Name = to
+		}
+	}
+}