Support Java Lang level 9 for droiddoc in Soong.

Test: m -j docs / m -j metalava-api-stubs-docs
Bug: b/70351683
Change-Id: I7220d04f508f6bb5ffd281cf31bc8c0069c219e5
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 308c141..13a8e78 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -30,7 +30,7 @@
 		blueprint.RuleParams{
 			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
-				`${config.JavadocCmd} -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
+				`${config.JavadocCmd} -source $javaVersion -encoding UTF-8 @$out.rsp @$srcJarDir/list ` +
 				`$opts $bootclasspathArgs $classpathArgs -sourcepath $sourcepath ` +
 				`-d $outDir -quiet  && ` +
 				`${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
@@ -44,7 +44,7 @@
 			RspfileContent: "$in",
 			Restat:         true,
 		},
-		"outDir", "srcJarDir", "stubsDir", "srcJars", "opts",
+		"outDir", "srcJarDir", "stubsDir", "javaVersion", "srcJars", "opts",
 		"bootclasspathArgs", "classpathArgs", "sourcepath", "docZip")
 
 	apiCheck = pctx.AndroidStaticRule("apiCheck",
@@ -69,7 +69,7 @@
 		blueprint.RuleParams{
 			Command: `rm -rf "$outDir" "$srcJarDir" "$stubsDir" && mkdir -p "$outDir" "$srcJarDir" "$stubsDir" && ` +
 				`${config.ZipSyncCmd} -d $srcJarDir -l $srcJarDir/list -f "*.java" $srcJars && ` +
-				`${config.JavaCmd} -jar ${config.MetalavaJar} -encoding UTF-8 -source 1.8 @$out.rsp @$srcJarDir/list ` +
+				`${config.JavaCmd} -jar ${config.MetalavaJar} -encoding UTF-8 -source $javaVersion @$out.rsp @$srcJarDir/list ` +
 				`$bootclasspathArgs $classpathArgs -sourcepath $sourcepath --no-banner --color --quiet ` +
 				`--stubs $stubsDir $opts && ` +
 				`${config.SoongZipCmd} -write_if_changed -d -o $docZip -C $outDir -D $outDir && ` +
@@ -85,7 +85,8 @@
 			RspfileContent: "$in",
 			Restat:         true,
 		},
-		"outDir", "srcJarDir", "stubsDir", "srcJars", "bootclasspathArgs", "classpathArgs", "sourcepath", "opts", "docZip")
+		"outDir", "srcJarDir", "stubsDir", "srcJars", "javaVersion", "bootclasspathArgs",
+		"classpathArgs", "sourcepath", "opts", "docZip")
 )
 
 func init() {
@@ -141,6 +142,9 @@
 		// Directories rooted at the Android.bp file to pass to aidl tool
 		Local_include_dirs []string
 	}
+
+	// If not blank, set the java version passed to javadoc as -source
+	Java_version *string
 }
 
 type ApiToCheck struct {
@@ -329,10 +333,16 @@
 		sdkDep := decodeSdkDep(ctx, String(j.properties.Sdk_version))
 		if sdkDep.useDefaultLibs {
 			ctx.AddDependency(ctx.Module(), bootClasspathTag, config.DefaultBootclasspathLibraries...)
+			if ctx.Config().TargetOpenJDK9() {
+				ctx.AddDependency(ctx.Module(), systemModulesTag, config.DefaultSystemModules)
+			}
 			if !Bool(j.properties.No_framework_libs) {
 				ctx.AddDependency(ctx.Module(), libTag, []string{"ext", "framework"}...)
 			}
 		} else if sdkDep.useModule {
+			if ctx.Config().TargetOpenJDK9() {
+				ctx.AddDependency(ctx.Module(), systemModulesTag, sdkDep.systemModules)
+			}
 			ctx.AddDependency(ctx.Module(), bootClasspathTag, sdkDep.modules...)
 		}
 	}
@@ -470,6 +480,15 @@
 			default:
 				ctx.ModuleErrorf("depends on non-java module %q", otherName)
 			}
+		case systemModulesTag:
+			if deps.systemModules != nil {
+				panic("Found two system module dependencies")
+			}
+			sm := module.(*SystemModules)
+			if sm.outputFile == nil {
+				panic("Missing directory for system module dependency")
+			}
+			deps.systemModules = sm.outputFile
 		}
 	})
 	// do not pass exclude_srcs directly when expanding srcFiles since exclude_srcs
@@ -508,13 +527,16 @@
 	implicits = append(implicits, deps.classpath...)
 
 	var bootClasspathArgs, classpathArgs string
-	if ctx.Config().UseOpenJDK9() {
+
+	javaVersion := getJavaVersion(ctx, String(j.properties.Java_version), String(j.properties.Sdk_version))
+	if javaVersion == "1.9" || ctx.Config().UseOpenJDK9() {
 		if len(deps.bootClasspath) > 0 {
-			// For OpenJDK 9 we use --patch-module to define the core libraries code.
-			// TODO(tobiast): Reorganize this when adding proper support for OpenJDK 9
-			// modules. Here we treat all code in core libraries as being in java.base
-			// to work around the OpenJDK 9 module system. http://b/62049770
-			bootClasspathArgs = "--patch-module=java.base=" + strings.Join(deps.bootClasspath.Strings(), ":")
+			var systemModules classpath
+			if deps.systemModules != nil {
+				systemModules = append(systemModules, deps.systemModules)
+			}
+			bootClasspathArgs = systemModules.FormJavaSystemModulesPath("--system ", ctx.Device())
+			bootClasspathArgs = bootClasspathArgs + " --patch-module java.base=."
 		}
 	} else {
 		if len(deps.bootClasspath.Strings()) > 0 {
@@ -541,6 +563,7 @@
 			"outDir":            android.PathForModuleOut(ctx, "docs", "out").String(),
 			"srcJarDir":         android.PathForModuleOut(ctx, "docs", "srcjars").String(),
 			"stubsDir":          android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
+			"javaVersion":       javaVersion,
 			"srcJars":           strings.Join(j.srcJars.Strings(), " "),
 			"opts":              opts,
 			"bootclasspathArgs": bootClasspathArgs,
@@ -612,7 +635,23 @@
 	implicits = append(implicits, deps.bootClasspath...)
 	implicits = append(implicits, deps.classpath...)
 
-	bootClasspathArgs := deps.bootClasspath.FormJavaClassPath("-bootclasspath")
+	var bootClasspathArgs string
+	javaVersion := getJavaVersion(ctx, String(d.Javadoc.properties.Java_version), String(d.Javadoc.properties.Sdk_version))
+	if javaVersion == "1.9" {
+		if len(deps.bootClasspath) > 0 {
+			var systemModules classpath
+			if deps.systemModules != nil {
+				systemModules = append(systemModules, deps.systemModules)
+			}
+			bootClasspathArgs = systemModules.FormJavaSystemModulesPath("--system ", ctx.Device())
+			bootClasspathArgs = bootClasspathArgs + " --patch-module java.base=."
+		}
+	} else {
+		if len(deps.bootClasspath.Strings()) > 0 {
+			// For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
+			bootClasspathArgs = deps.bootClasspath.FormJavaClassPath("-bootclasspath")
+		}
+	}
 	classpathArgs := deps.classpath.FormJavaClassPath("-classpath")
 
 	argFiles := ctx.ExpandSources(d.properties.Arg_files, nil)
@@ -801,7 +840,7 @@
 		date = `date -d`
 	}
 
-	doclavaOpts := "-source 1.8 -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
+	doclavaOpts := "-source " + javaVersion + " -J-Xmx1600m -J-XX:-OmitStackTraceInFastThrow -XDignore.symbol.file " +
 		"-doclet com.google.doclava.Doclava -docletpath " + jsilver.String() + ":" + doclava.String() + " " +
 		"-templatedir " + templateDir + " " + htmlDirArgs + " " + htmlDir2Args + " " +
 		"-hdf page.build " + ctx.Config().BuildId() + "-" + ctx.Config().BuildNumberFromFile() + " " +
@@ -829,6 +868,7 @@
 				"srcJarDir":         android.PathForModuleOut(ctx, "docs", "srcjars").String(),
 				"stubsDir":          android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
 				"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
+				"javaVersion":       javaVersion,
 				"opts":              opts,
 				"bootclasspathArgs": bootClasspathArgs,
 				"classpathArgs":     classpathArgs,
@@ -844,6 +884,7 @@
 			"srcJarDir":         android.PathForModuleOut(ctx, "docs", "srcjars").String(),
 			"stubsDir":          android.PathForModuleOut(ctx, "docs", "stubsDir").String(),
 			"srcJars":           strings.Join(d.Javadoc.srcJars.Strings(), " "),
+			"javaVersion":       javaVersion,
 			"bootclasspathArgs": bootClasspathArgs,
 			"classpathArgs":     classpathArgs,
 			"sourcepath":        strings.Join(d.Javadoc.sourcepaths.Strings(), ":"),