prevent inheritance of writeToParcel, readFromParcel

To prevent overriding writeToParcel and readFromParcel, make them final in cpp and java
because ndk parcelable is not virtual, it doesn't need to be final

Bug: 118246983
Test: ./runtests.sh
Test: m -j
Change-Id: I53fb443fdf3fe3c404d844f8fe6992c0c737c9f1
diff --git a/ast_cpp.cpp b/ast_cpp.cpp
index c90bff4..e5ed6d8 100644
--- a/ast_cpp.cpp
+++ b/ast_cpp.cpp
@@ -189,9 +189,7 @@
                        ArgList&& arg_list)
     : MethodDecl(return_type, name, std::move(arg_list), 0u) {}
 
-MethodDecl::MethodDecl(const std::string& return_type,
-                       const std::string& name,
-                       ArgList&& arg_list,
+MethodDecl::MethodDecl(const std::string& return_type, const std::string& name, ArgList&& arg_list,
                        uint32_t modifiers)
     : return_type_(return_type),
       name_(name),
@@ -200,7 +198,8 @@
       is_virtual_(modifiers & IS_VIRTUAL),
       is_override_(modifiers & IS_OVERRIDE),
       is_pure_virtual_(modifiers & IS_PURE_VIRTUAL),
-      is_static_(modifiers & IS_STATIC) {}
+      is_static_(modifiers & IS_STATIC),
+      is_final_(modifiers & IS_FINAL) {}
 
 void MethodDecl::Write(CodeWriter* to) const {
   if (is_virtual_)
@@ -219,6 +218,8 @@
   if (is_override_)
     to->Write(" override");
 
+  if (is_final_) to->Write(" final");
+
   if (is_pure_virtual_)
     to->Write(" = 0");
 
diff --git a/ast_cpp.h b/ast_cpp.h
index b407ba6..9f9414a 100644
--- a/ast_cpp.h
+++ b/ast_cpp.h
@@ -175,6 +175,7 @@
     IS_OVERRIDE = 1 << 2,
     IS_PURE_VIRTUAL = 1 << 3,
     IS_STATIC = 1 << 4,
+    IS_FINAL = 1 << 5,
   };
 
   MethodDecl(const std::string& return_type,
@@ -197,6 +198,7 @@
   bool is_override_ = false;
   bool is_pure_virtual_ = false;
   bool is_static_ = true;
+  bool is_final_ = false;
 
   DISALLOW_COPY_AND_ASSIGN(MethodDecl);
 };  // class MethodDecl
diff --git a/generate_cpp.cpp b/generate_cpp.cpp
index 0a92ae8..ea66d7d 100644
--- a/generate_cpp.cpp
+++ b/generate_cpp.cpp
@@ -1075,11 +1075,11 @@
 
   unique_ptr<MethodDecl> read(new MethodDecl(kAndroidStatusLiteral, "readFromParcel",
                                              ArgList("const ::android::Parcel* _aidl_parcel"),
-                                             MethodDecl::IS_OVERRIDE));
+                                             MethodDecl::IS_OVERRIDE | MethodDecl::IS_FINAL));
   parcel_class->AddPublic(std::move(read));
-  unique_ptr<MethodDecl> write(new MethodDecl(kAndroidStatusLiteral, "writeToParcel",
-                                              ArgList("::android::Parcel* _aidl_parcel"),
-                                              MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST));
+  unique_ptr<MethodDecl> write(new MethodDecl(
+      kAndroidStatusLiteral, "writeToParcel", ArgList("::android::Parcel* _aidl_parcel"),
+      MethodDecl::IS_OVERRIDE | MethodDecl::IS_CONST | MethodDecl::IS_FINAL));
   parcel_class->AddPublic(std::move(write));
 
   return unique_ptr<Document>{new CppHeader{
diff --git a/generate_java.cpp b/generate_java.cpp
index 1bf26c3..f09ac06 100644
--- a/generate_java.cpp
+++ b/generate_java.cpp
@@ -134,7 +134,7 @@
       new Variable(new Type(types, "android.os.Parcel", 0, false), "_aidl_parcel");
 
   Method* write_method = new Method;
-  write_method->modifiers = PUBLIC | OVERRIDE;
+  write_method->modifiers = PUBLIC | OVERRIDE | FINAL;
   write_method->returnType = new Type(types, "void", 0, false);
   write_method->name = "writeToParcel";
   write_method->parameters.push_back(parcel_variable);
@@ -158,7 +158,7 @@
   parcel_class->elements.push_back(write_method);
 
   Method* read_method = new Method;
-  read_method->modifiers = PUBLIC;
+  read_method->modifiers = PUBLIC | FINAL;
   read_method->returnType = new Type(types, "void", 0, false);
   read_method->name = "readFromParcel";
   read_method->parameters.push_back(parcel_variable);