union: java backend
union is a parcelable which can hold only a single field with a tag.
Example:
union Union {
int n;
String s;
}
In Java, you can instantiate it with
- default constructor: init with first field
- value constructor: Union.n(42) or Union.s("abc")
You can query "tag" before getting the contents from it.
It also supports getter/setter.
Example:
void foo(Union u) {
if (u.getTag() == Union.n) { // query
int n = u.getN(); // getter
...
}
u.setS("abc"); // setter
}
Bug: 150948558
Test: atest aidl_integration_test
Change-Id: I5c2d87e09462c0d3c6617d73fdf0e49c281d551e
diff --git a/aidl_to_java.cpp b/aidl_to_java.cpp
index 658348b..c5d3f97 100644
--- a/aidl_to_java.cpp
+++ b/aidl_to_java.cpp
@@ -762,7 +762,7 @@
} else {
const AidlDefinedType* t = c.typenames.TryGetDefinedType(c.type.GetName());
AIDL_FATAL_IF(t == nullptr, c.type) << "Unknown type: " << c.type.GetName();
- if (t->AsParcelable() != nullptr) {
+ if (t->AsParcelable() != nullptr || t->AsUnionDeclaration() != nullptr) {
if (c.type.IsArray()) {
c.writer << c.parcel << ".readTypedArray(" << c.var << ", " << c.type.GetName()
<< ".CREATOR);\n";