Add toString to Java code

* toString is mainly for debugging purposes.
* For structs and interfaces, add Object.toString.
* For enums, add MyEnum.toString(int) and MyEnum.dumpBitfield(int).

Use them as follows:
* For enums, use the static method E.toString(int).
* For bitfields, use the static method E.dumpBitfield(int).
* For all arrays, use java.utils.Arrays.deepToString(o)
* For everything else, use one of the following:
    * o.toString(), if o is not null
    * Object.toString(o)
    * String.valueOf(o)
* Note that for array / vec of enums / bitfields, the raw integer
  value is dumped.

Bug: 33459772
Test: hidl_test_java

Change-Id: Ifb1ed519770b907e0a4e345b2c3109dc322a23b2
diff --git a/ScalarType.cpp b/ScalarType.cpp
index 9b6c0c3..b8bd427 100644
--- a/ScalarType.cpp
+++ b/ScalarType.cpp
@@ -218,6 +218,40 @@
     out << streamName << " += toHexString(" << name << ");\n";
 }
 
+void ScalarType::emitConvertToJavaHexString(
+        Formatter &out,
+        const std::string &name) const {
+    switch(mKind) {
+        case KIND_BOOL: {
+            out << "((" << name << ") ? \"0x1\" : \"0x0\")";
+            break;
+        }
+        case KIND_INT8:     // fallthrough
+        case KIND_UINT8:    // fallthrough
+        case KIND_INT16:    // fallthrough
+        case KIND_UINT16: {
+            // Because Byte and Short doesn't have toHexString, we have to use Integer.toHexString.
+            out << "Integer.toHexString(" << getJavaWrapperType() << ".toUnsignedInt(("
+                << getJavaType(false /* forInitializer */) << ")(" << name << ")))";
+            break;
+        }
+        case KIND_INT32:    // fallthrough
+        case KIND_UINT32:   // fallthrough
+        case KIND_INT64:    // fallthrough
+        case KIND_UINT64: {
+            out << getJavaWrapperType() << ".toHexString(" << name << ")";
+            break;
+        }
+        case KIND_FLOAT:    // fallthrough
+        case KIND_DOUBLE:   // fallthrough
+        default: {
+            // no hex for floating point numbers.
+            out << name;
+            break;
+        }
+    }
+}
+
 void ScalarType::emitJavaFieldReaderWriter(
         Formatter &out,
         size_t /* depth */,