Fix: not buildable with pre-23 SDK when with ParcelFileDescriptor
This is fixing a regression that when the input AIDL is using
ParcelFileDescriptor, the generated code is not buildable with SDKs
older than 23. While promoting ParcelFileDescriptor to one of the
built-in types, [write|read]TypedObject APIs were used, but they are
introduced with 23 and thus not avaialble in older SDKs.
Fixing the issue by reading/writing ParcelFileDescriptor without using
the APIs but doing the same as the APIs in the generated code.
Bug: 119523810
Test: aidl_unittests
Change-Id: Ie46e262b3f2746cb75e810603cc83b364634d9f0
diff --git a/aidl_to_java.cpp b/aidl_to_java.cpp
index 6e8a0b5..f476a09 100644
--- a/aidl_to_java.cpp
+++ b/aidl_to_java.cpp
@@ -274,7 +274,19 @@
}},
{"ParcelFileDescriptor",
[](const CodeGeneratorContext& c) {
- c.writer << c.parcel << ".writeTypedObject(" << c.var << ", " << GetFlagFor(c) << ");\n";
+ // This is same as writeTypedObject which was introduced with SDK 23.
+ // Keeping below code so that the generated code is buildable with older SDK.
+ c.writer << "if ((" << c.var << "!=null)) {\n";
+ c.writer.Indent();
+ c.writer << c.parcel << ".writeInt(1);\n";
+ c.writer << c.var << ".writeToParcel(" << c.parcel << ", " << GetFlagFor(c) << ");\n";
+ c.writer.Dedent();
+ c.writer << "}\n";
+ c.writer << "else {\n";
+ c.writer.Indent();
+ c.writer << c.parcel << ".writeInt(0);\n";
+ c.writer.Dedent();
+ c.writer << "}\n";
}},
{"ParcelFileDescriptor[]",
[](const CodeGeneratorContext& c) {
@@ -462,8 +474,19 @@
}},
{"ParcelFileDescriptor",
[](const CodeGeneratorContext& c) {
- c.writer << c.var << " = " << c.parcel
- << ".readTypedObject(android.os.ParcelFileDescriptor.CREATOR);\n";
+ // This is same as readTypedObject which was introduced with SDK 23.
+ // Keeping below code so that the generated code is buildable with older SDK.
+ c.writer << "if ((0!=" << c.parcel << ".readInt())) {\n";
+ c.writer.Indent();
+ c.writer << c.var << " = " << "android.os.ParcelFileDescriptor.CREATOR.createFromParcel(" << c.parcel
+ << ");\n";
+ c.writer.Dedent();
+ c.writer << "}\n";
+ c.writer << "else {\n";
+ c.writer.Indent();
+ c.writer << c.var << " = null;\n";
+ c.writer.Dedent();
+ c.writer << "}\n";
}},
{"ParcelFileDescriptor[]",
[](const CodeGeneratorContext& c) {
@@ -597,8 +620,11 @@
}},
{"ParcelFileDescriptor",
[](const CodeGeneratorContext& c) {
- c.writer << c.var << " = " << c.parcel
- << ".readTypedObject(android.os.ParcelFileDescriptor.CREATOR);\n";
+ c.writer << "if ((0!=" << c.parcel << ".readInt())) {\n";
+ c.writer.Indent();
+ c.writer << c.var << " = " << "android.os.ParcelFileDescriptor.CREATOR.createFromParcel(" << c.parcel << ");\n";
+ c.writer.Dedent();
+ c.writer << "}\n";
}},
{"ParcelFileDescriptor[]",
[](const CodeGeneratorContext& c) {