void t_java_generator::generate_typedef(t_typedef* ttypedef) {}
/**
- * Enums are a class with a set of static constants.
+ * Enums are a class with a set of static constants.
*
* @param tenum The enumeration
*/
// Print header
f_consts <<
autogen_comment() <<
- java_package() <<
+ java_package() <<
java_type_imports();
f_consts <<
indent(out) <<
"public " << (in_class ? "static " : "") << "class " << tstruct->get_name() << " ";
-
+
if (is_exception) {
out << "extends Exception ";
}
out << "implements TBase, java.io.Serializable ";
-
+
scope_up(out);
// Members are public for -java, private for -javabean
const vector<t_field*>& members = tstruct->get_members();
- vector<t_field*>::const_iterator m_iter;
+ vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (bean_style_) {
indent(out) << "private ";
indent() << "}" << endl <<
endl;
}
-
+
// Default constructor
indent(out) <<
"public " << tstruct->get_name() << "() {" << endl;
if (!members.empty()) {
indent(out) <<
"public " << tstruct->get_name() << "(" << endl;
- indent_up();
+ indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ) {
indent(out) << type_name((*m_iter)->get_type()) << " " <<
(*m_iter)->get_name();
out <<
indent() << "TField field;" << endl <<
indent() << "iprot.readStructBegin();" << endl;
-
+
// Loop over reading in fields
indent(out) <<
"while (true)" << endl;
scope_up(out);
-
+
// Read beginning field marker
indent(out) <<
"field = iprot.readFieldBegin();" << endl;
-
+
// Check for field STOP marker and break
indent(out) <<
"if (field.type == TType.STOP) { " << endl;
indent_down();
indent(out) <<
"}" << endl;
-
+
// Switch statement on the field we are reading
indent(out) <<
"switch (field.id)" << endl;
scope_up(out);
-
+
// Generate deserialization code for known cases
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
indent(out) <<
indent() << "break;" << endl;
indent_down();
}
-
+
// In the default case we skip the field
out <<
indent() << "default:" << endl <<
indent() << " TProtocolUtil.skip(iprot, field.type);" << endl <<
indent() << " break;" << endl;
-
+
scope_down(out);
// Read field end marker
indent(out) <<
"iprot.readFieldEnd();" << endl;
-
+
scope_down(out);
-
+
out <<
indent() << "iprot.readStructEnd();" << endl;
// Write field closer
indent(out) <<
"oprot.writeFieldEnd();" << endl;
-
+
if (optional) {
indent_down();
indent(out) << "}" << endl;
// Method to return the size of the collection
indent(out) << "public int get" << cap_name << "Size() {" << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? 0 : " <<
+ indent(out) << "return (this." << field_name << " == null) ? 0 : " <<
"this." << field_name << ".size();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
indent(out) << "public java.util.Iterator<" <<
type_name(element_type) << "> get" << cap_name << "Iterator() {" << endl;
indent_up();
- indent(out) << "return (this." << field_name << " == null) ? null : " <<
+ indent(out) << "return (this." << field_name << " == null) ? null : " <<
"this." << field_name << ".iterator();" << endl;
indent_down();
indent(out) << "}" << endl << endl;
" {" << endl << endl;
indent_up();
vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
+ vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
generate_java_doc(f_service_, *f_iter);
indent(f_service_) << "public " << function_signature(*f_iter) << ";" <<
*/
void t_java_generator::generate_service_helpers(t_service* tservice) {
vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
+ vector<t_function*>::iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
t_struct* ts = (*f_iter)->get_arglist();
generate_java_struct_definition(f_service_, ts, false, true);
indent(f_service_) <<
"public Client(TProtocol prot)" << endl;
scope_up(f_service_);
- indent(f_service_) <<
+ indent(f_service_) <<
"this(prot, prot);" << endl;
scope_down(f_service_);
f_service_ << endl;
}
scope_down(f_service_);
f_service_ << endl;
-
+
if (extends.empty()) {
f_service_ <<
indent() << "protected TProtocol iprot_;" << endl <<
// Generate client method implementations
vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::const_iterator f_iter;
+ vector<t_function*>::const_iterator f_iter;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
string funname = (*f_iter)->get_name();
// Get the struct of function call params
t_struct* arg_struct = (*f_iter)->get_arglist();
-
+
// Declare the function arguments
const vector<t_field*>& fields = arg_struct->get_members();
vector<t_field*>::const_iterator fld_iter;
}
scope_down(f_service_);
f_service_ << endl;
-
+
t_function send_function(g_type_void,
string("send_") + (*f_iter)->get_name(),
(*f_iter)->get_arglist());
indent(f_service_) <<
"public " << function_signature(&recv_function) << endl;
scope_up(f_service_);
-
+
// TODO(mcslee): Message validation here, was the seqid etc ok?
f_service_ <<
f_service_ <<
indent() << "throw new TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
}
-
+
// Close function
scope_down(f_service_);
f_service_ << endl;
void t_java_generator::generate_service_server(t_service* tservice) {
// Generate the dispatch methods
vector<t_function*> functions = tservice->get_functions();
- vector<t_function*>::iterator f_iter;
+ vector<t_function*>::iterator f_iter;
// Extends stuff
string extends = "";
f_service_ <<
indent() << "processMap_.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl;
}
-
+
scope_down(f_service_);
f_service_ << endl;
-
+
if (extends.empty()) {
f_service_ <<
indent() << "protected static interface ProcessFunction {" << endl <<
f_service_ <<
indent() << "protected final HashMap<String,ProcessFunction> processMap_ = new HashMap<String,ProcessFunction>();" << endl;
}
-
+
f_service_ << endl;
-
+
// Generate the server implementation
indent(f_service_) <<
"public boolean process(TProtocol iprot, TProtocol oprot) throws TException" << endl;
indent() << " return true;" << endl <<
indent() << "}" << endl <<
indent() << "fn.process(msg.seqid, iprot, oprot);" << endl;
-
+
f_service_ <<
indent() << "return true;" << endl;
indent() << "try {" << endl;
indent_up();
}
-
+
// Generate the function call
t_struct* arg_struct = tfunction->get_arglist();
const std::vector<t_field*>& fields = arg_struct->get_members();
indent(out) <<
name << " = iprot.";
-
+
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
throw "compiler error: cannot serialize void field in a struct: " +
name;
break;
- case t_base_type::TYPE_STRING:
+ case t_base_type::TYPE_STRING:
if (((t_base_type*)type)->is_binary()) {
out << "readBinary();";
} else {
t_type* ttype,
string prefix) {
scope_up(out);
-
+
string obj;
if (ttype->is_map()) {
}
indent(out)
- << prefix << " = new " << type_name(ttype, false, true)
+ << prefix << " = new " << type_name(ttype, false, true)
// size the collection correctly
- << "("
+ << "("
<< (ttype->is_list() ? "" : "2*" )
<< obj << ".size"
<< ");" << endl;
"for (int " << i << " = 0; " <<
i << " < " << obj << ".size" << "; " <<
"++" << i << ")" << endl;
-
+
scope_up(out);
-
+
if (ttype->is_map()) {
generate_deserialize_map_element(out, (t_map*)ttype, prefix);
} else if (ttype->is_set()) {
} else if (ttype->is_list()) {
generate_deserialize_list_element(out, (t_list*)ttype, prefix);
}
-
+
scope_down(out);
// Read container end
throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
prefix + tfield->get_name();
}
-
+
if (type->is_struct() || type->is_xception()) {
generate_serialize_struct(out,
(t_struct*)type,
string name = prefix + tfield->get_name();
indent(out) <<
"oprot.";
-
+
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
t_type* ttype,
string prefix) {
scope_up(out);
-
+
if (ttype->is_map()) {
indent(out) <<
"oprot.writeMapBegin(new TMap(" <<
} else if (ttype->is_list()) {
generate_serialize_list_element(out, (t_list*)ttype, iter);
}
-
- if (ttype->is_map()) {
- indent(out) <<
- "oprot.writeMapEnd();" << endl;
- } else if (ttype->is_set()) {
- indent(out) <<
- "oprot.writeSetEnd();" << endl;
- } else if (ttype->is_list()) {
- indent(out) <<
- "oprot.writeListEnd();" << endl;
- }
-
+
scope_down(out);
-
- scope_down(out);
+
+ if (ttype->is_map()) {
+ indent(out) <<
+ "oprot.writeMapEnd();" << endl;
+ } else if (ttype->is_set()) {
+ indent(out) <<
+ "oprot.writeSetEnd();" << endl;
+ } else if (ttype->is_list()) {
+ indent(out) <<
+ "oprot.writeListEnd();" << endl;
+ }
+
+ scope_down(out);
}
/**
* Serializes the members of a map.
- */
+ */
void t_java_generator::generate_serialize_map_element(ofstream& out,
t_map* tmap,
string iter,
*/
string t_java_generator::type_to_enum(t_type* type) {
type = get_true_type(type);
-
+
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {