add error message when CanBeJavaOnlyImmutable fails

Bug: n/a
Test: aidl_unittests
Change-Id: Ifb950c28f52fcf3379ede83157c6b939fd6929d7
diff --git a/aidl_language.cpp b/aidl_language.cpp
index 92f4961..82adaf8 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -897,33 +897,49 @@
   if (!AidlParcelable::CheckValid(typenames)) {
     return false;
   }
-  std::set<std::string> fieldnames;
+
   for (const auto& v : GetFields()) {
     const bool field_valid = v->CheckValid(typenames);
     success = success && field_valid;
-    bool duplicated;
-    if (IsJavaOnlyImmutable()) {
-      success = success && typenames.CanBeJavaOnlyImmutable(v->GetType());
-      duplicated = !fieldnames.emplace(CapitalizeFirstLetter(*v, v->GetName())).second;
-    } else {
-      if (IsFixedSize()) {
-        if (field_valid && !typenames.CanBeFixedSize(v->GetType())) {
-          AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
-                        << "non-fixed size field named " << v->GetName() << ".";
-          success = false;
-        }
-      }
-      duplicated = !fieldnames.emplace(v->GetName()).second;
-    }
+  }
 
+  std::set<std::string> fieldnames;
+  for (const auto& v : GetFields()) {
+    bool duplicated = !fieldnames.emplace(v->GetName()).second;
     if (duplicated) {
       AIDL_ERROR(this) << "The parcelable '" << this->GetName() << "' has duplicate field name '"
-                       << v->GetName() << "'"
-                       << (IsJavaOnlyImmutable() ? " after capitalizing the first letter" : "");
+                       << v->GetName() << "'";
       return false;
     }
   }
 
+  if (IsFixedSize()) {
+    for (const auto& v : GetFields()) {
+      if (!typenames.CanBeFixedSize(v->GetType())) {
+        AIDL_ERROR(v) << "The @FixedSize parcelable '" << this->GetName() << "' has a "
+                      << "non-fixed size field named " << v->GetName() << ".";
+        success = false;
+      }
+    }
+  }
+
+  if (IsJavaOnlyImmutable()) {
+    std::set<std::string> getters;
+    for (const auto& v : GetFields()) {
+      if (!typenames.CanBeJavaOnlyImmutable(v->GetType())) {
+        AIDL_ERROR(v) << "The @JavaOnlyImmutable parcelable '" << this->GetName() << "' has a "
+                      << "non-immutable field named '" << v->GetName() << "'.";
+        success = false;
+      }
+      bool duplicated = !getters.emplace(CapitalizeFirstLetter(*v, v->GetName())).second;
+      if (duplicated) {
+        AIDL_ERROR(this) << "The parcelable '" << this->GetName() << "' has duplicate field name '"
+                         << v->GetName() << "' after capitalizing the first letter";
+        return false;
+      }
+    }
+  }
+
   return success;
 }
 
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index be01fa4..325ea7c 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -2805,8 +2805,13 @@
 TEST_F(AidlTest, RejectMutableParcelableFromJavaOnlyImmutableParcelable) {
   io_delegate_.SetFileContents("Foo.aidl", "@JavaOnlyImmutable parcelable Foo { Bar bar; }");
   io_delegate_.SetFileContents("Bar.aidl", "parcelable Bar { String a; }");
+  string expected_error =
+      "ERROR: Foo.aidl:1.40-44: The @JavaOnlyImmutable parcelable 'Foo' has a non-immutable field "
+      "named 'bar'.\n";
+  CaptureStderr();
   Options options = Options::From("aidl --lang=java Foo.aidl -I .");
   EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
+  EXPECT_EQ(expected_error, GetCapturedStderr());
 }
 
 TEST_F(AidlTest, ImmutableParcelableCannotBeInOut) {