Update package path for Java.

We used to put IFoo.java with package android.hardware.foo.V1_0
under android/hardware/foo/1.0; the last element is fixed to
V1_0.

Test: compiles
Test: hidl_test_java

Bug: 33554192
Change-Id: I6a6a7d6c0a4c95e7c0797643bc98bec5c12889ca
diff --git a/Coordinator.cpp b/Coordinator.cpp
index 284bbce..11e24f8 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -194,7 +194,7 @@
 }
 
 std::string Coordinator::getPackagePath(
-        const FQName &fqName, bool relative) const {
+        const FQName &fqName, bool relative, bool sanitized) const {
 
     auto it = findPackageRoot(fqName);
     auto prefix = *it;
@@ -230,7 +230,7 @@
     packagePath.append(packageSuffix.substr(startPos));
     packagePath.append("/");
 
-    packagePath.append(fqName.version());
+    packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
     packagePath.append("/");
 
     return packagePath;
diff --git a/Coordinator.h b/Coordinator.h
index a87ad38..34298a4 100644
--- a/Coordinator.h
+++ b/Coordinator.h
@@ -50,10 +50,12 @@
     // "vendor/<something>/interfaces"], package roots of
     // ["android.hardware", "vendor.<something>.hardware"], and a
     // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
-    // will return "hardware/interfaces/nfc/V1_0".
+    // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
+    // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
 
     std::string getPackagePath(
-            const FQName &fqName, bool relative = false) const;
+            const FQName &fqName, bool relative = false,
+            bool sanitized = false) const;
 
     // Given package roots of ["android.hardware",
     // "vendor.<something>.hardware"] and a FQName of
diff --git a/FQName.cpp b/FQName.cpp
index 02e31e2..700af9f 100644
--- a/FQName.cpp
+++ b/FQName.cpp
@@ -175,6 +175,14 @@
     return mMajor + "." + mMinor;
 }
 
+std::string FQName::sanitizedVersion() const {
+    CHECK(mMajor.empty() == mMinor.empty());
+    if (mMajor.empty() && mMinor.empty()) {
+        return "";
+    }
+    return "V" + mMajor + "_" + mMinor;
+}
+
 std::string FQName::atVersion() const {
     std::string v = version();
     return v.empty() ? "" : ("@" + v);
@@ -368,13 +376,7 @@
         return;
     }
 
-    // Form "Vmajor_minor".
-    std::string versionString = "V";
-    versionString.append(getPackageMajorVersion());
-    versionString.append("_");
-    versionString.append(getPackageMinorVersion());
-
-    components->push_back(versionString);
+    components->push_back(sanitizedVersion());
 }
 
 std::string FQName::getPackageMajorVersion() const {
diff --git a/FQName.h b/FQName.h
index 5e5ef08..d4601ab 100644
--- a/FQName.h
+++ b/FQName.h
@@ -51,6 +51,8 @@
     std::string atVersion() const;
     // Return version in the form "1.0" if it is present, otherwise empty string.
     std::string version() const;
+    // Return version in the form "V1_0" if it is present, otherwise empty string.
+    std::string sanitizedVersion() const;
 
     // The next two methods return the name part of the FQName, that is, the
     // part after the version field.  For example:
diff --git a/generateJava.cpp b/generateJava.cpp
index 9f3c0db..97c836e 100644
--- a/generateJava.cpp
+++ b/generateJava.cpp
@@ -62,7 +62,8 @@
 
         std::string path = outputPath;
         path.append(mCoordinator->convertPackageRootToPath(mPackage));
-        path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
+        path.append(mCoordinator->getPackagePath(mPackage, true /* relative */,
+                true /* sanitized */));
         path.append(typeName);
         path.append(".java");
 
@@ -118,7 +119,8 @@
 
     std::string path = outputPath;
     path.append(mCoordinator->convertPackageRootToPath(mPackage));
-    path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
+    path.append(mCoordinator->getPackagePath(mPackage, true /* relative */,
+            true /* sanitized */));
     path.append(ifaceName);
     path.append(".java");
 
diff --git a/main.cpp b/main.cpp
index f365778..e28d187 100644
--- a/main.cpp
+++ b/main.cpp
@@ -148,7 +148,8 @@
     out << "\n#"
         << "\nGEN := $(intermediates)/"
         << coordinator->convertPackageRootToPath(packageFQName)
-        << coordinator->getPackagePath(packageFQName, true /* relative */);
+        << coordinator->getPackagePath(packageFQName, true /* relative */,
+                true /* sanitized */);
     if (typeName == nullptr) {
         out << fqName.name() << ".java";
     } else {
@@ -353,7 +354,7 @@
     out << "\n#"
         << "\nGEN := $(intermediates)/"
         << coordinator->convertPackageRootToPath(packageFQName)
-        << coordinator->getPackagePath(packageFQName, true /* relative */)
+        << coordinator->getPackagePath(packageFQName, true /* relative */, true /* sanitized */)
         << "Constants.java";
 
     out << "\n$(GEN): $(HIDL)\n";
@@ -940,7 +941,7 @@
         path.append(coordinator->convertPackageRootToPath(packageFQName));
 
         path.append(coordinator->getPackagePath(
-                    packageFQName, true /* relative */));
+                    packageFQName, true /* relative */, true /* sanitized */));
 
         path.append("Constants.java");
     }