Fully separate AIDL and Java type namespaces

For android::aidl::ValidatableType, rename:
  - QualifiedName() -> CanonicalName()
  - Name() -> ShortName()

For android::aidl::java::Type, add a new JavaType() method.
Use that method exclusively when generating Java.

Bug: 26875320
Test: unit and integration tests pass

Change-Id: Ib1d09f4f84d3270634e611f0911f60a2e685cefc
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index bf205f7..94aa39a 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -189,7 +189,7 @@
   AidlType ambiguous_type("IBar", 0, "", false /* not an array */);
   const java::Type* type = java_types_.Find(ambiguous_type);
   ASSERT_TRUE(type);
-  EXPECT_EQ("one.IBar", type->QualifiedName());
+  EXPECT_EQ("one.IBar", type->CanonicalName());
 }
 
 TEST_F(AidlTest, WritePreprocessedFile) {
diff --git a/ast_java.cpp b/ast_java.cpp
index fae38ca..44e298d 100644
--- a/ast_java.cpp
+++ b/ast_java.cpp
@@ -71,7 +71,7 @@
     to->Write("%s\n", this->comment.c_str());
   }
   WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
-  to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
+  to->Write("%s %s", this->variable->type->JavaType().c_str(),
             this->variable->name.c_str());
   if (this->value.length() != 0) {
     to->Write(" = %s", this->value.c_str());
@@ -102,7 +102,7 @@
   for (int i = 0; i < this->dimension; i++) {
     dim += "[]";
   }
-  to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
+  to->Write("%s%s %s", this->type->JavaType().c_str(), dim.c_str(),
             this->name.c_str());
 }
 
@@ -118,7 +118,7 @@
   if (this->object != NULL) {
     this->object->Write(to);
   } else if (this->clazz != NULL) {
-    to->Write("%s", this->clazz->QualifiedName().c_str());
+    to->Write("%s", this->clazz->JavaType().c_str());
   }
   to->Write(".%s", name.c_str());
 }
@@ -157,7 +157,7 @@
   this->lvalue->Write(to);
   to->Write(" = ");
   if (this->cast != NULL) {
-    to->Write("(%s)", this->cast->QualifiedName().c_str());
+    to->Write("(%s)", this->cast->JavaType().c_str());
   }
   this->rvalue->Write(to);
 }
@@ -203,7 +203,7 @@
     this->obj->Write(to);
     to->Write(".");
   } else if (this->clazz != NULL) {
-    to->Write("%s.", this->clazz->QualifiedName().c_str());
+    to->Write("%s.", this->clazz->JavaType().c_str());
   }
   to->Write("%s(", this->name.c_str());
   WriteArgumentList(to, this->arguments);
@@ -247,7 +247,7 @@
     : type(t), size(s) {}
 
 void NewArrayExpression::Write(CodeWriter* to) const {
-  to->Write("new %s[", this->type->QualifiedName().c_str());
+  to->Write("new %s[", this->type->JavaType().c_str());
   size->Write(to);
   to->Write("]");
 }
@@ -268,7 +268,7 @@
 Cast::Cast(const Type* t, Expression* e) : type(t), expression(e) {}
 
 void Cast::Write(CodeWriter* to) const {
-  to->Write("((%s)", this->type->QualifiedName().c_str());
+  to->Write("((%s)", this->type->JavaType().c_str());
   expression->Write(to);
   to->Write(")");
 }
@@ -284,7 +284,7 @@
   if (this->rvalue != NULL) {
     to->Write(" = ");
     if (this->cast != NULL) {
-      to->Write("(%s)", this->cast->QualifiedName().c_str());
+      to->Write("(%s)", this->cast->JavaType().c_str());
     }
     this->rvalue->Write(to);
   }
@@ -384,7 +384,7 @@
     for (i = 0; i < this->returnTypeDimension; i++) {
       dim += "[]";
     }
-    to->Write("%s%s ", this->returnType->QualifiedName().c_str(), dim.c_str());
+    to->Write("%s%s ", this->returnType->JavaType().c_str(), dim.c_str());
   }
 
   to->Write("%s(", this->name.c_str());
@@ -406,7 +406,7 @@
     } else {
       to->Write(", ");
     }
-    to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
+    to->Write("%s", this->exceptions[i]->JavaType().c_str());
   }
 
   if (this->statements == NULL) {
@@ -437,7 +437,7 @@
     to->Write("interface ");
   }
 
-  string name = this->type->Name();
+  string name = this->type->JavaType();
   size_t pos = name.rfind('.');
   if (pos != string::npos) {
     name = name.c_str() + pos + 1;
@@ -446,7 +446,7 @@
   to->Write("%s", name.c_str());
 
   if (this->extends != NULL) {
-    to->Write(" extends %s", this->extends->QualifiedName().c_str());
+    to->Write(" extends %s", this->extends->JavaType().c_str());
   }
 
   N = this->interfaces.size();
@@ -457,7 +457,7 @@
       to->Write(" extends");
     }
     for (i = 0; i < N; i++) {
-      to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
+      to->Write(" %s", this->interfaces[i]->JavaType().c_str());
     }
   }
 
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index 237b993..b692637 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -65,7 +65,7 @@
   Field* descriptor =
       new Field(STATIC | FINAL | PRIVATE,
                 new Variable(types->StringType(), "DESCRIPTOR"));
-  descriptor->value = "\"" + interfaceType->QualifiedName() + "\"";
+  descriptor->value = "\"" + interfaceType->JavaType() + "\"";
   this->elements.push_back(descriptor);
 
   // ctor
@@ -125,7 +125,7 @@
 
   Method* m = new Method;
   m->comment = "/**\n * Cast an IBinder object into an ";
-  m->comment += interfaceType->QualifiedName();
+  m->comment += interfaceType->JavaType();
   m->comment += " interface,\n";
   m->comment += " * generating a proxy if needed.\n */";
   m->modifiers = PUBLIC | STATIC;
@@ -158,7 +158,7 @@
   Comparison* iinNotNull = new Comparison(iin, "!=", NULL_VALUE);
   Comparison* instOfCheck =
       new Comparison(iin, " instanceof ",
-                     new LiteralExpression(interfaceType->QualifiedName()));
+                     new LiteralExpression(interfaceType->JavaType()));
   IfStatement* instOfStatement = new IfStatement();
   instOfStatement->expression = new Comparison(iinNotNull, "&&", instOfCheck);
   instOfStatement->statements = new StatementBlock;
diff --git a/type_java.cpp b/type_java.cpp
index b792ba7..8698666 100644
--- a/type_java.cpp
+++ b/type_java.cpp
@@ -49,38 +49,37 @@
            bool canBeOut, const string& declFile, int declLine)
     : ValidatableType(kind, package, name, declFile, declLine),
       m_types(types),
-      m_package(package),
-      m_name(name),
+      m_javaType((package.empty()) ? name : package + "." + name),
       m_canWriteToParcel(canWriteToParcel),
       m_canBeOut(canBeOut) {
 }
 
 string Type::CreatorName() const { return ""; }
 
-string Type::InstantiableName() const { return QualifiedName(); }
+string Type::InstantiableName() const { return JavaType(); }
 
 void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
                          int flags) const {
   fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
-          __LINE__, m_qualifiedName.c_str());
-  addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_qualifiedName +
+          __LINE__, m_javaType.c_str());
+  addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_javaType +
                                    " */"));
 }
 
 void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
                             Variable* parcel, Variable**) const {
   fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
-          __LINE__, m_qualifiedName.c_str());
+          __LINE__, m_javaType.c_str());
   addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
-                                   m_qualifiedName + " */"));
+                                   m_javaType + " */"));
 }
 
 void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
                           Variable**) const {
   fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
-          __LINE__, m_qualifiedName.c_str());
+          __LINE__, m_javaType.c_str());
   addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
-                                   m_qualifiedName + " */"));
+                                   m_javaType + " */"));
 }
 
 Expression* Type::BuildWriteToParcelFlags(int flags) const {
@@ -584,7 +583,7 @@
 }
 
 string UserDataType::CreatorName() const {
-  return QualifiedName() + ".CREATOR";
+  return JavaType() + ".CREATOR";
 }
 
 void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
@@ -653,7 +652,7 @@
            canWriteToParcel, true, declFile, declLine) {}
 
 string UserDataArrayType::CreatorName() const {
-  return QualifiedName() + ".CREATOR";
+  return JavaType() + ".CREATOR";
 }
 
 void UserDataArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
@@ -664,14 +663,14 @@
 
 void UserDataArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
                                          Variable* parcel, Variable**) const {
-  string creator = v->type->QualifiedName() + ".CREATOR";
+  string creator = v->type->JavaType() + ".CREATOR";
   addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
                                               new LiteralExpression(creator))));
 }
 
 void UserDataArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
                                        Variable* parcel, Variable**) const {
-  string creator = v->type->QualifiedName() + ".CREATOR";
+  string creator = v->type->JavaType() + ".CREATOR";
   addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
                             new LiteralExpression(creator)));
 }
@@ -712,7 +711,7 @@
 
 GenericListType::GenericListType(const JavaTypeNamespace* types,
                                  const Type* contained_type)
-    : Type(types, "java.util", "List<" + contained_type->QualifiedName() + ">",
+    : Type(types, "java.util", "List<" + contained_type->JavaType() + ">",
            ValidatableType::KIND_BUILT_IN, true, true),
       m_contained_type(contained_type),
       m_creator(contained_type->CreatorName()) {}
@@ -722,7 +721,7 @@
 }
 
 string GenericListType::InstantiableName() const {
-  return "java.util.ArrayList<" + m_contained_type->QualifiedName() + ">";
+  return "java.util.ArrayList<" + m_contained_type->JavaType() + ">";
 }
 
 void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
diff --git a/type_java.h b/type_java.h
index 3a63e8d..e77f076 100644
--- a/type_java.h
+++ b/type_java.h
@@ -47,7 +47,7 @@
   const ValidatableType* ArrayType() const override { return m_array_type.get(); }
   const ValidatableType* NullableType() const override { return nullptr; }
 
-  inline std::string Package() const { return m_package; }
+  virtual std::string JavaType() const { return m_javaType; }
   virtual std::string CreatorName() const;
   virtual std::string InstantiableName() const;
 
@@ -69,9 +69,7 @@
   Type();
   Type(const Type&);
 
-  std::string m_package;
-  std::string m_name;
-  std::string m_qualifiedName;
+  std::string m_javaType;
   std::string m_declFile;
   bool m_canWriteToParcel;
   bool m_canBeOut;
@@ -205,6 +203,7 @@
  public:
   StringType(const JavaTypeNamespace* types);
 
+  std::string JavaType() const override { return "java.lang.String"; }
   std::string CreatorName() const override;
 
   void WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
diff --git a/type_namespace.h b/type_namespace.h
index ae6501a..81f801a 100644
--- a/type_namespace.h
+++ b/type_namespace.h
@@ -51,10 +51,11 @@
   virtual const ValidatableType* ArrayType() const = 0;
   virtual const ValidatableType* NullableType() const = 0;
 
-  // Name() returns the short name of an object (without package qualifiers).
-  virtual std::string Name() const { return type_name_; }
-  // QualifiedName() returns the canonical AIDL type, with packages.
-  virtual std::string QualifiedName() const { return canonical_name_; }
+  // ShortName() is the class name without a package.
+  std::string ShortName() const { return type_name_; }
+  // CanonicalName() returns the canonical AIDL type, with packages.
+  std::string CanonicalName() const { return canonical_name_; }
+
   int Kind() const { return kind_; }
   std::string HumanReadableKind() const;
   std::string DeclFile() const { return origin_file_; }
@@ -173,7 +174,7 @@
 
 template<typename T>
 bool LanguageTypeNamespace<T>::Add(const T* type) {
-  const T* existing = FindTypeByCanonicalName(type->QualifiedName());
+  const T* existing = FindTypeByCanonicalName(type->CanonicalName());
   if (!existing) {
     types_.emplace_back(type);
     return true;
@@ -182,13 +183,13 @@
   if (existing->Kind() == ValidatableType::KIND_BUILT_IN) {
     LOG(ERROR) << type->DeclFile() << ":" << type->DeclLine()
                << " attempt to redefine built in class "
-               << type->QualifiedName();
+               << type->CanonicalName();
     return false;
   }
 
   if (type->Kind() != existing->Kind()) {
     LOG(ERROR) << type->DeclFile() << ":" << type->DeclLine()
-               << " attempt to redefine " << type->QualifiedName()
+               << " attempt to redefine " << type->CanonicalName()
                << " as " << type->HumanReadableKind();
     LOG(ERROR) << existing->DeclFile() << ":" << existing->DeclLine()
                << " previously defined here as "
@@ -231,12 +232,12 @@
   for (const auto& type : types_) {
     // Always prefer a exact match if possible.
     // This works for primitives and class names qualified with a package.
-    if (type->QualifiedName() == name) {
+    if (type->CanonicalName() == name) {
       ret = type.get();
       break;
     }
     // We allow authors to drop packages when refering to a class name.
-    if (type->Name() == name) {
+    if (type->ShortName() == name) {
       ret = type.get();
     }
   }
@@ -329,7 +330,7 @@
     if (!arg_type) {
       return false;
     }
-    type_name = arg_type->QualifiedName();
+    type_name = arg_type->CanonicalName();
   }
 
   // Map the container name to its canonical form for supported containers.