clean up ast_java: FieldVariable, Assignment, MethodCall

ast_java doesn't need to know about Type in JavaTypeNamespace.

This commit removes Type in FieldVariable, Assignment, MethodCall

Bug: 110967839
Test: ./runtests.sh
Test: m -j
Change-Id: I496c9785745c415426bd1df0be755e07b7039dd1
diff --git a/ast_java.cpp b/ast_java.cpp
index 0be172c..1dd3818 100644
--- a/ast_java.cpp
+++ b/ast_java.cpp
@@ -22,6 +22,13 @@
 using std::vector;
 using std::string;
 
+template <class... Ts>
+struct overloaded : Ts... {
+  using Ts::operator()...;
+};
+template <class... Ts>
+overloaded(Ts...)->overloaded<Ts...>;
+
 namespace android {
 namespace aidl {
 namespace java {
@@ -114,18 +121,15 @@
 
 void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
 
-FieldVariable::FieldVariable(Expression* o, const string& n)
-    : object(o), clazz(nullptr), name(n) {}
+FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {}
 
-FieldVariable::FieldVariable(const Type* c, const string& n)
-    : object(nullptr), clazz(c), name(n) {}
+FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
 
 void FieldVariable::Write(CodeWriter* to) const {
-  if (this->object != nullptr) {
-    this->object->Write(to);
-  } else if (this->clazz != nullptr) {
-    to->Write("%s", this->clazz->JavaType().c_str());
-  }
+  visit(
+      overloaded{[&](Expression* e) { e->Write(to); },
+                 [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
+      this->receiver);
   to->Write(".%s", name.c_str());
 }
 
@@ -161,17 +165,15 @@
   to->Write(";\n");
 }
 
-Assignment::Assignment(Variable* l, Expression* r)
-    : lvalue(l), rvalue(r), cast(nullptr) {}
+Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
 
-Assignment::Assignment(Variable* l, Expression* r, const Type* c)
-    : lvalue(l), rvalue(r), cast(c) {}
+Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {}
 
 void Assignment::Write(CodeWriter* to) const {
   this->lvalue->Write(to);
   to->Write(" = ");
-  if (this->cast != nullptr) {
-    to->Write("(%s)", this->cast->JavaType().c_str());
+  if (this->cast) {
+    to->Write("(%s)", this->cast->c_str());
   }
   this->rvalue->Write(to);
 }
@@ -185,20 +187,19 @@
   va_end(args);
 }
 
-MethodCall::MethodCall(Expression* o, const string& n) : obj(o), name(n) {}
+MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {}
 
-MethodCall::MethodCall(const Type* t, const string& n) : clazz(t), name(n) {}
+MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
 
-MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
-    : obj(o), name(n) {
+MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) {
   va_list args;
   va_start(args, argc);
   init(argc, args);
   va_end(args);
 }
 
-MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
-    : clazz(t), name(n) {
+MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...)
+    : receiver(t), name(n) {
   va_list args;
   va_start(args, argc);
   init(argc, args);
@@ -213,12 +214,13 @@
 }
 
 void MethodCall::Write(CodeWriter* to) const {
-  if (this->obj != nullptr) {
-    this->obj->Write(to);
-    to->Write(".");
-  } else if (this->clazz != nullptr) {
-    to->Write("%s.", this->clazz->JavaType().c_str());
-  }
+  visit(
+      overloaded{[&](Expression* e) {
+                   e->Write(to);
+                   to->Write(".");
+                 },
+                 [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
+      this->receiver);
   to->Write("%s(", this->name.c_str());
   WriteArgumentList(to, this->arguments);
   to->Write(")");