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_language.cpp b/aidl_language.cpp
index b9985e5..04a4493 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -1110,6 +1110,32 @@
writer->Write("}\n");
}
+AidlUnionDecl::AidlUnionDecl(const AidlLocation& location, const std::string& name,
+ const std::string& package, const std::string& comments,
+ std::vector<std::unique_ptr<AidlVariableDeclaration>>* variables,
+ std::vector<std::string>* type_params)
+ : AidlParcelable(location, name, package, comments, "" /*cpp_header*/, type_params),
+ variables_(std::move(*variables)) {}
+
+std::set<AidlAnnotation::Type> AidlUnionDecl::GetSupportedAnnotations() const {
+ return {AidlAnnotation::Type::VINTF_STABILITY, AidlAnnotation::Type::HIDE,
+ AidlAnnotation::Type::JAVA_PASSTHROUGH};
+}
+
+void AidlUnionDecl::Dump(CodeWriter* writer) const {
+ DumpHeader(writer);
+ writer->Write("union %s {\n", GetName().c_str());
+ writer->Indent();
+ for (const auto& field : GetFields()) {
+ if (field->GetType().IsHidden()) {
+ AddHideComment(writer);
+ }
+ writer->Write("%s;\n", field->ToString().c_str());
+ }
+ writer->Dedent();
+ writer->Write("}\n");
+}
+
// TODO: we should treat every backend all the same in future.
bool AidlInterface::LanguageSpecificCheckValid(const AidlTypenames& typenames,
Options::Language lang) const {