Use dependency tags for java modules

Test: java_test.go
Change-Id: Id265a2acd6e6c4ce7764f77c888e22b1fddc02c4
diff --git a/java/java.go b/java/java.go
index 195a078..7349065 100644
--- a/java/java.go
+++ b/java/java.go
@@ -147,45 +147,46 @@
 	AidlIncludeDirs() android.Paths
 }
 
-func (j *Module) BootClasspath(ctx android.BaseContext) string {
-	if ctx.Device() {
-		switch j.deviceProperties.Sdk_version {
-		case "":
-			return "core-libart"
-		case "current":
-			// TODO: !TARGET_BUILD_APPS
-			// TODO: export preprocessed framework.aidl from android_stubs_current
-			return "android_stubs_current"
-		case "system_current":
-			return "android_system_stubs_current"
-		default:
-			return "sdk_v" + j.deviceProperties.Sdk_version
-		}
-	} else {
-		if j.deviceProperties.Dex {
-			return "core-libart"
-		} else {
-			return ""
-		}
-	}
+type dependencyTag struct {
+	blueprint.BaseDependencyTag
+	name string
 }
 
-func (j *Module) deps(ctx android.BottomUpMutatorContext) {
-	var deps []string
+var (
+	javaStaticLibTag = dependencyTag{name: "staticlib"}
+	javaLibTag       = dependencyTag{name: "javalib"}
+	bootClasspathTag = dependencyTag{name: "bootclasspath"}
+	frameworkResTag  = dependencyTag{name: "framework-res"}
+	sdkDependencyTag = dependencyTag{name: "sdk"}
+)
 
+func (j *Module) deps(ctx android.BottomUpMutatorContext) {
 	if !j.properties.No_standard_libraries {
-		bootClasspath := j.BootClasspath(ctx)
-		if bootClasspath != "" {
-			deps = append(deps, bootClasspath)
+		if ctx.Device() {
+			switch j.deviceProperties.Sdk_version {
+			case "":
+				ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
+			case "current":
+				// TODO: !TARGET_BUILD_APPS
+				// TODO: export preprocessed framework.aidl from android_stubs_current
+				ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_stubs_current")
+			case "system_current":
+				ctx.AddDependency(ctx.Module(), bootClasspathTag, "android_system_stubs_current")
+			default:
+				ctx.AddDependency(ctx.Module(), sdkDependencyTag, "sdk_v"+j.deviceProperties.Sdk_version)
+			}
+		} else {
+			if j.deviceProperties.Dex {
+				ctx.AddDependency(ctx.Module(), bootClasspathTag, "core-libart")
+			}
 		}
+
 		if ctx.Device() && j.deviceProperties.Sdk_version == "" {
-			deps = append(deps, config.DefaultLibraries...)
+			ctx.AddDependency(ctx.Module(), javaLibTag, config.DefaultLibraries...)
 		}
 	}
-	deps = append(deps, j.properties.Java_libs...)
-	deps = append(deps, j.properties.Java_static_libs...)
-
-	ctx.AddDependency(ctx.Module(), nil, deps...)
+	ctx.AddDependency(ctx.Module(), javaLibTag, j.properties.Java_libs...)
+	ctx.AddDependency(ctx.Module(), javaStaticLibTag, j.properties.Java_static_libs...)
 }
 
 func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
@@ -228,27 +229,23 @@
 			return
 		}
 
-		if otherName == j.BootClasspath(ctx) {
+		switch tag {
+		case bootClasspathTag:
 			bootClasspath = android.OptionalPathForPath(javaDep.ClasspathFile())
-		} else if inList(otherName, config.DefaultLibraries) {
+		case javaLibTag:
 			classpath = append(classpath, javaDep.ClasspathFile())
-		} else if inList(otherName, j.properties.Java_libs) {
-			classpath = append(classpath, javaDep.ClasspathFile())
-		} else if inList(otherName, j.properties.Java_static_libs) {
+		case javaStaticLibTag:
 			classpath = append(classpath, javaDep.ClasspathFile())
 			classJarSpecs = append(classJarSpecs, javaDep.ClassJarSpecs()...)
 			resourceJarSpecs = append(resourceJarSpecs, javaDep.ResourceJarSpecs()...)
-		} else if otherName == "framework-res" {
+		case frameworkResTag:
 			if ctx.ModuleName() == "framework" {
 				// framework.jar has a one-off dependency on the R.java and Manifest.java files
 				// generated by framework-res.apk
 				srcFileLists = append(srcFileLists, module.(*AndroidApp).aaptJavaFileList)
 			}
-		} else {
-			panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
-		}
-		aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...)
-		if sdkDep, ok := module.(sdkDependency); ok {
+		case sdkDependencyTag:
+			sdkDep := module.(sdkDependency)
 			if sdkDep.AidlPreprocessed().Valid() {
 				if aidlPreprocess.Valid() {
 					ctx.ModuleErrorf("multiple dependencies with preprocessed aidls:\n %q\n %q",
@@ -257,7 +254,11 @@
 					aidlPreprocess = sdkDep.AidlPreprocessed()
 				}
 			}
+		default:
+			panic(fmt.Errorf("unknown dependency %q for %q", otherName, ctx.ModuleName()))
 		}
+
+		aidlIncludeDirs = append(aidlIncludeDirs, javaDep.AidlIncludeDirs()...)
 	})
 
 	return classpath, bootClasspath, classJarSpecs, resourceJarSpecs, aidlPreprocess,
@@ -510,16 +511,10 @@
 // Java prebuilts
 //
 
-type JavaPrebuiltProperties struct {
-	Srcs []string
-}
-
 type JavaPrebuilt struct {
 	android.ModuleBase
 	prebuilt android.Prebuilt
 
-	properties JavaPrebuiltProperties
-
 	classpathFile                   android.Path
 	classJarSpecs, resourceJarSpecs []jarSpec
 }
@@ -532,11 +527,7 @@
 }
 
 func (j *JavaPrebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	if len(j.properties.Srcs) != 1 {
-		ctx.ModuleErrorf("expected exactly one jar in srcs")
-		return
-	}
-	prebuilt := android.PathForModuleSrc(ctx, j.properties.Srcs[0])
+	prebuilt := j.prebuilt.Path(ctx)
 
 	classJarSpec, resourceJarSpec := TransformPrebuiltJarToClasses(ctx, prebuilt)
 
@@ -567,9 +558,7 @@
 func JavaPrebuiltFactory() android.Module {
 	module := &JavaPrebuilt{}
 
-	module.AddProperties(
-		&module.properties,
-		&module.prebuilt.Properties)
+	module.AddProperties(&module.prebuilt.Properties)
 
 	android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
 	return module
@@ -612,7 +601,7 @@
 	module := &sdkPrebuilt{}
 
 	module.AddProperties(
-		&module.properties,
+		&module.prebuilt.Properties,
 		&module.sdkProperties)
 
 	android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)