]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/compiler/cpp/src/thrift/generate/t_javame_generator.cc
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / compiler / cpp / src / thrift / generate / t_javame_generator.cc
CommitLineData
f67539c2
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <sstream>
21#include <string>
22#include <fstream>
23#include <iostream>
24#include <vector>
25#include <cctype>
26
27#include <sys/stat.h>
28#include <stdexcept>
29
30#include "thrift/platform.h"
31#include "thrift/generate/t_oop_generator.h"
32
33using std::map;
34using std::ostream;
35using std::ostringstream;
36using std::string;
37using std::stringstream;
38using std::vector;
39
40static const string endl = "\n"; // avoid ostream << std::endl flushes
41
42/**
43 * Java code generator.
44 *
45 */
46class t_javame_generator : public t_oop_generator {
47public:
48 t_javame_generator(t_program* program,
49 const std::map<std::string, std::string>& parsed_options,
50 const std::string& option_string)
51 : t_oop_generator(program) {
52 (void)parsed_options;
53 (void)option_string;
54 std::map<std::string, std::string>::const_iterator iter;
55
56 /* no options yet */
57 for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
58 throw "unknown option javame:" + iter->first;
59 }
60
61 out_dir_base_ = "gen-javame";
62 }
63
64 /**
65 * Init and close methods
66 */
67
68 void init_generator() override;
69 void close_generator() override;
70
71 void generate_consts(std::vector<t_const*> consts) override;
72
73 /**
74 * Program-level generation functions
75 */
76
77 void generate_typedef(t_typedef* ttypedef) override;
78 void generate_enum(t_enum* tenum) override;
79 void generate_struct(t_struct* tstruct) override;
80 void generate_union(t_struct* tunion);
81 void generate_xception(t_struct* txception) override;
82 void generate_service(t_service* tservice) override;
83
84 void print_const_value(std::ostream& out,
85 std::string name,
86 t_type* type,
87 t_const_value* value,
88 bool in_static,
89 bool defval = false);
90 std::string render_const_value(std::ostream& out,
91 std::string name,
92 t_type* type,
93 t_const_value* value);
94
95 /**
96 * Service-level generation functions
97 */
98
99 void generate_java_struct(t_struct* tstruct, bool is_exception);
100
101 void generate_java_struct_definition(std::ostream& out,
102 t_struct* tstruct,
103 bool is_xception = false,
104 bool in_class = false,
105 bool is_result = false);
106 void generate_java_struct_equality(std::ostream& out, t_struct* tstruct);
107 void generate_java_struct_compare_to(std::ostream& out, t_struct* tstruct);
108 void generate_java_struct_reader(std::ostream& out, t_struct* tstruct);
109 void generate_java_validator(std::ostream& out, t_struct* tstruct);
110 void generate_java_struct_result_writer(std::ostream& out, t_struct* tstruct);
111 void generate_java_struct_writer(std::ostream& out, t_struct* tstruct);
112 void generate_java_struct_tostring(std::ostream& out, t_struct* tstruct);
113 void generate_java_struct_clear(std::ostream& out, t_struct* tstruct);
114 void generate_field_value_meta_data(std::ostream& out, t_type* type);
115 std::string get_java_type_string(t_type* type);
116 void generate_reflection_setters(std::ostringstream& out,
117 t_type* type,
118 std::string field_name,
119 std::string cap_name);
120 void generate_reflection_getters(std::ostringstream& out,
121 t_type* type,
122 std::string field_name,
123 std::string cap_name);
124 void generate_generic_field_getters_setters(std::ostream& out, t_struct* tstruct);
125 void generate_java_bean_boilerplate(std::ostream& out, t_struct* tstruct);
126
127 void generate_function_helpers(t_function* tfunction);
128 std::string get_cap_name(std::string name);
129 std::string generate_isset_check(t_field* field);
130 std::string generate_isset_check(std::string field);
131 void generate_isset_set(ostream& out, t_field* field);
132 std::string isset_field_id(t_field* field);
133
134 void generate_primitive_service_interface(t_service* tservice);
135 void generate_service_interface(t_service* tservice);
136 void generate_service_helpers(t_service* tservice);
137 void generate_service_client(t_service* tservice);
138 void generate_service_server(t_service* tservice);
139 void generate_process_function(t_service* tservice, t_function* tfunction);
140
141 void generate_java_union(t_struct* tstruct);
142 void generate_union_constructor(ostream& out, t_struct* tstruct);
143 void generate_union_getters_and_setters(ostream& out, t_struct* tstruct);
144 void generate_union_abstract_methods(ostream& out, t_struct* tstruct);
145 void generate_check_type(ostream& out, t_struct* tstruct);
146 void generate_read_value(ostream& out, t_struct* tstruct);
147 void generate_write_value(ostream& out, t_struct* tstruct);
148 void generate_get_field_desc(ostream& out, t_struct* tstruct);
149 void generate_get_struct_desc(ostream& out, t_struct* tstruct);
150 void generate_get_field_name(ostream& out, t_struct* tstruct);
151
152 void generate_union_comparisons(ostream& out, t_struct* tstruct);
153 void generate_union_hashcode(ostream& out, t_struct* tstruct);
154
155 /**
156 * Serialization constructs
157 */
158
159 void generate_deserialize_field(std::ostream& out, t_field* tfield, std::string prefix = "");
160
161 void generate_deserialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = "");
162
163 void generate_deserialize_container(std::ostream& out, t_type* ttype, std::string prefix = "");
164
165 void generate_deserialize_set_element(std::ostream& out, t_set* tset, std::string prefix = "");
166
167 void generate_deserialize_map_element(std::ostream& out, t_map* tmap, std::string prefix = "");
168
169 void generate_deserialize_list_element(std::ostream& out,
170 t_list* tlist,
171 std::string prefix = "");
172
173 void generate_serialize_field(std::ostream& out, t_field* tfield, std::string prefix = "");
174
175 void generate_serialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = "");
176
177 void generate_serialize_container(std::ostream& out, t_type* ttype, std::string prefix = "");
178
179 void generate_serialize_map_element(std::ostream& out,
180 t_map* tmap,
181 std::string iter,
182 std::string map);
183
184 void generate_serialize_set_element(std::ostream& out, t_set* tmap, std::string iter);
185
186 void generate_serialize_list_element(std::ostream& out, t_list* tlist, std::string iter);
187
188 void generate_java_doc(std::ostream& out, t_field* field) override;
189
190 void generate_java_doc(std::ostream& out, t_doc* tdoc) override;
191
192 void generate_java_doc(std::ostream& out, t_function* tdoc) override;
193
194 void generate_java_docstring_comment(std::ostream& out, string contents) override;
195
196 void generate_deep_copy_container(std::ostream& out,
197 std::string source_name_p1,
198 std::string source_name_p2,
199 std::string result_name,
200 t_type* type);
201 void generate_deep_copy_non_container(std::ostream& out,
202 std::string source_name,
203 std::string dest_name,
204 t_type* type);
205
206 bool has_bit_vector(t_struct* tstruct);
207
208 /**
209 * Helper rendering functions
210 */
211
212 std::string java_package();
213 std::string java_type_imports();
214 std::string java_thrift_imports();
215 std::string type_name(t_type* ttype,
216 bool in_container = false,
217 bool in_init = false,
218 bool skip_generic = false);
219 std::string base_type_name(t_base_type* tbase, bool in_container = false);
220 std::string declare_field(t_field* tfield, bool init = false);
221 std::string function_signature(t_function* tfunction, std::string prefix = "");
222 std::string argument_list(t_struct* tstruct, bool include_types = true);
223 std::string type_to_enum(t_type* ttype);
224 std::string get_enum_class_name(t_type* type) override;
225 void generate_struct_desc(ostream& out, t_struct* tstruct);
226 void generate_field_descs(ostream& out, t_struct* tstruct);
227 std::string box_type(t_type* type, string value);
228
229 bool type_can_be_null(t_type* ttype) {
230 ttype = get_true_type(ttype);
231
232 return ttype->is_container() || ttype->is_struct() || ttype->is_xception() || ttype->is_string()
233 || ttype->is_enum();
234 }
235
236 std::string constant_name(std::string name);
237
238private:
239 /**
240 * File streams
241 */
242
243 std::string package_name_;
244 ofstream_with_content_based_conditional_update f_service_;
245 std::string package_dir_;
246};
247
248/**
249 * Prepares for file generation by opening up the necessary file output
250 * streams.
251 *
252 * @param tprogram The program to generate
253 */
254void t_javame_generator::init_generator() {
255 // Make output directory
256 MKDIR(get_out_dir().c_str());
257 package_name_ = program_->get_namespace("java");
258
259 string dir = package_name_;
260 string subdir = get_out_dir();
261 string::size_type loc;
262 while ((loc = dir.find(".")) != string::npos) {
263 subdir = subdir + "/" + dir.substr(0, loc);
264 MKDIR(subdir.c_str());
265 dir = dir.substr(loc + 1);
266 }
267 if (dir.size() > 0) {
268 subdir = subdir + "/" + dir;
269 MKDIR(subdir.c_str());
270 }
271
272 package_dir_ = subdir;
273}
274
275/**
276 * Packages the generated file
277 *
278 * @return String of the package, i.e. "package org.apache.thriftdemo;"
279 */
280string t_javame_generator::java_package() {
281 if (!package_name_.empty()) {
282 return string("package ") + package_name_ + ";\n\n";
283 }
284 return "";
285}
286
287/**
288 * Prints standard java imports
289 *
290 * @return List of imports for Java types that are used in here
291 */
292string t_javame_generator::java_type_imports() {
293 return string() + "import java.util.Hashtable;\n" + "import java.util.Vector;\n"
294 + "import java.util.Enumeration;\n\n";
295}
296
297/**
298 * Prints standard java imports
299 *
300 * @return List of imports necessary for thrift
301 */
302string t_javame_generator::java_thrift_imports() {
303 return string() + "import org.apache.thrift.*;\n" + "import org.apache.thrift.meta_data.*;\n"
304 + "import org.apache.thrift.transport.*;\n" + "import org.apache.thrift.protocol.*;\n\n";
305}
306
307/**
308 * Nothing in Java
309 */
310void t_javame_generator::close_generator() {
311}
312
313/**
314 * Generates a typedef. This is not done in Java, since it does
315 * not support arbitrary name replacements, and it'd be a wacky waste
316 * of overhead to make wrapper classes.
317 *
318 * @param ttypedef The type definition
319 */
320void t_javame_generator::generate_typedef(t_typedef* ttypedef) {
321 (void)ttypedef;
322}
323
324/**
325 * Enums are a class with a set of static constants.
326 *
327 * @param tenum The enumeration
328 */
329void t_javame_generator::generate_enum(t_enum* tenum) {
330 // Make output file
331 string f_enum_name = package_dir_ + "/" + (tenum->get_name()) + ".java";
332 ofstream_with_content_based_conditional_update f_enum;
333 f_enum.open(f_enum_name.c_str());
334
335 // Comment and package it
336 f_enum << autogen_comment() << java_package();
337
338 generate_java_doc(f_enum, tenum);
339 indent(f_enum) << "public class " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
340 scope_up(f_enum);
341 f_enum << endl;
342
343 vector<t_enum_value*> constants = tenum->get_constants();
344 vector<t_enum_value*>::iterator c_iter;
345 for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
346 int value = (*c_iter)->get_value();
347 generate_java_doc(f_enum, *c_iter);
348 indent(f_enum) << "public static final " << tenum->get_name() << " " << (*c_iter)->get_name()
349 << " = new " << tenum->get_name() << "(" << value << ");" << endl;
350 }
351 f_enum << endl;
352
353 // Field for thriftCode
354 indent(f_enum) << "private final int value;" << endl << endl;
355
356 indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl;
357 indent(f_enum) << " this.value = value;" << endl;
358 indent(f_enum) << "}" << endl << endl;
359
360 indent(f_enum) << "/**" << endl;
361 indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL."
362 << endl;
363 indent(f_enum) << " */" << endl;
364 indent(f_enum) << "public int getValue() {" << endl;
365 indent(f_enum) << " return value;" << endl;
366 indent(f_enum) << "}" << endl << endl;
367
368 indent(f_enum) << "/**" << endl;
369 indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL."
370 << endl;
371 indent(f_enum) << " * @return null if the value is not found." << endl;
372 indent(f_enum) << " */" << endl;
373 indent(f_enum) << "public static " + tenum->get_name() + " findByValue(int value) { " << endl;
374
375 indent_up();
376
377 indent(f_enum) << "switch (value) {" << endl;
378 indent_up();
379
380 for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
381 int value = (*c_iter)->get_value();
382 indent(f_enum) << "case " << value << ":" << endl;
383 indent(f_enum) << " return " << (*c_iter)->get_name() << ";" << endl;
384 }
385
386 indent(f_enum) << "default:" << endl;
387 indent(f_enum) << " return null;" << endl;
388
389 indent_down();
390
391 indent(f_enum) << "}" << endl;
392
393 indent_down();
394
395 indent(f_enum) << "}" << endl;
396
397 scope_down(f_enum);
398
399 f_enum.close();
400}
401
402/**
403 * Generates a class that holds all the constants.
404 */
405void t_javame_generator::generate_consts(std::vector<t_const*> consts) {
406 if (consts.empty()) {
407 return;
408 }
409
410 string f_consts_name = package_dir_ + "/" + program_name_ + "Constants.java";
411 ofstream_with_content_based_conditional_update f_consts;
412 f_consts.open(f_consts_name.c_str());
413
414 // Print header
415 f_consts << autogen_comment() << java_package() << java_type_imports();
416
417 f_consts << "public class " << program_name_ << "Constants {" << endl << endl;
418 indent_up();
419 vector<t_const*>::iterator c_iter;
420 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
421 print_const_value(f_consts,
422 (*c_iter)->get_name(),
423 (*c_iter)->get_type(),
424 (*c_iter)->get_value(),
425 false);
426 }
427 indent_down();
428 indent(f_consts) << "}" << endl;
429 f_consts.close();
430}
431
432/**
433 * Prints the value of a constant with the given type. Note that type checking
434 * is NOT performed in this function as it is always run beforehand using the
435 * validate_types method in main.cc
436 */
437void t_javame_generator::print_const_value(std::ostream& out,
438 string name,
439 t_type* type,
440 t_const_value* value,
441 bool in_static,
442 bool defval) {
443 type = get_true_type(type);
444
445 indent(out);
446 if (!defval) {
447 out << (in_static ? "" : "public static final ") << type_name(type) << " ";
448 }
449 if (type->is_base_type()) {
450 string v2 = render_const_value(out, name, type, value);
451 out << name << " = " << v2 << ";" << endl << endl;
452 } else if (type->is_enum()) {
453 out << name << " = " << render_const_value(out, name, type, value) << ";" << endl << endl;
454 } else if (type->is_struct() || type->is_xception()) {
455 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
456 vector<t_field*>::const_iterator f_iter;
457 const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
458 map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
459 out << name << " = new " << type_name(type, false, true) << "();" << endl;
460 if (!in_static) {
461 indent(out) << "static {" << endl;
462 indent_up();
463 }
464 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
465 t_type* field_type = NULL;
466 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
467 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
468 field_type = (*f_iter)->get_type();
469 }
470 }
471 if (field_type == NULL) {
472 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
473 }
474 string val = render_const_value(out, name, field_type, v_iter->second);
475 indent(out) << name << ".";
476 std::string cap_name = get_cap_name(v_iter->first->get_string());
477 out << "set" << cap_name << "(" << val << ");" << endl;
478 }
479 if (!in_static) {
480 indent_down();
481 indent(out) << "}" << endl;
482 }
483 out << endl;
484 } else if (type->is_map()) {
485 out << name << " = new " << type_name(type, false, true) << "();" << endl;
486 if (!in_static) {
487 indent(out) << "static {" << endl;
488 indent_up();
489 }
490 t_type* ktype = ((t_map*)type)->get_key_type();
491 t_type* vtype = ((t_map*)type)->get_val_type();
492 const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map();
493 map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter;
494 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
495 string key = render_const_value(out, name, ktype, v_iter->first);
496 string val = render_const_value(out, name, vtype, v_iter->second);
497 indent(out) << name << ".put(" << box_type(ktype, key) << ", " << box_type(vtype, val) << ");"
498 << endl;
499 }
500 if (!in_static) {
501 indent_down();
502 indent(out) << "}" << endl;
503 }
504 out << endl;
505 } else if (type->is_list() || type->is_set()) {
506 out << name << " = new " << type_name(type, false, true) << "();" << endl;
507 if (!in_static) {
508 indent(out) << "static {" << endl;
509 indent_up();
510 }
511 t_type* etype;
512 if (type->is_list()) {
513 etype = ((t_list*)type)->get_elem_type();
514 } else {
515 etype = ((t_set*)type)->get_elem_type();
516 }
517 const vector<t_const_value*>& val = value->get_list();
518 vector<t_const_value*>::const_iterator v_iter;
519 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
520 string val = render_const_value(out, name, etype, *v_iter);
521 if (type->is_list()) {
522 indent(out) << name << ".addElement(" << box_type(etype, val) << ");" << endl;
523 } else {
524 indent(out) << name << ".put(" << box_type(etype, val) << ", " << box_type(etype, val)
525 << ");" << endl;
526 }
527 }
528 if (!in_static) {
529 indent_down();
530 indent(out) << "}" << endl;
531 }
532 out << endl;
533 } else {
534 throw "compiler error: no const of type " + type->get_name();
535 }
536}
537
538string t_javame_generator::render_const_value(ostream& out,
539 string name,
540 t_type* type,
541 t_const_value* value) {
542 (void)name;
543 type = get_true_type(type);
544 std::ostringstream render;
545
546 if (type->is_base_type()) {
547 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
548 switch (tbase) {
549 case t_base_type::TYPE_STRING:
550 render << '"' << get_escaped_string(value) << '"';
551 break;
552 case t_base_type::TYPE_BOOL:
553 render << ((value->get_integer() > 0) ? "true" : "false");
554 break;
555 case t_base_type::TYPE_I8:
556 render << "(byte)" << value->get_integer();
557 break;
558 case t_base_type::TYPE_I16:
559 render << "(short)" << value->get_integer();
560 break;
561 case t_base_type::TYPE_I32:
562 render << value->get_integer();
563 break;
564 case t_base_type::TYPE_I64:
565 render << value->get_integer() << "L";
566 break;
567 case t_base_type::TYPE_DOUBLE:
568 if (value->get_type() == t_const_value::CV_INTEGER) {
569 render << "(double)" << value->get_integer();
570 } else {
571 render << value->get_double();
572 }
573 break;
574 default:
575 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
576 }
577 } else if (type->is_enum()) {
578 render << type_name(type, false, false) << "." << value->get_identifier();
579 } else {
580 string t = tmp("tmp");
581 print_const_value(out, t, type, value, true);
582 render << t;
583 }
584
585 return render.str();
586}
587
588string t_javame_generator::box_type(t_type* type, string value) {
589 if (type->is_base_type()) {
590 switch (((t_base_type*)type)->get_base()) {
591 case t_base_type::TYPE_BOOL:
592 return "new Boolean(" + value + ")";
593 case t_base_type::TYPE_I8:
594 return "new Byte(" + value + ")";
595 case t_base_type::TYPE_I16:
596 return "new Short(" + value + ")";
597 case t_base_type::TYPE_I32:
598 return "new Integer(" + value + ")";
599 case t_base_type::TYPE_I64:
600 return "new Long(" + value + ")";
601 case t_base_type::TYPE_DOUBLE:
602 return "new Double(" + value + ")";
603 default:
604 break;
605 }
606 }
607 return value;
608}
609
610/**
611 * Generates a struct definition for a thrift data type. This will be a TBase
612 * implementor.
613 *
614 * @param tstruct The struct definition
615 */
616void t_javame_generator::generate_struct(t_struct* tstruct) {
617 if (tstruct->is_union()) {
618 generate_java_union(tstruct);
619 } else {
620 generate_java_struct(tstruct, false);
621 }
622}
623
624/**
625 * Exceptions are structs, but they inherit from Exception
626 *
627 * @param tstruct The struct definition
628 */
629void t_javame_generator::generate_xception(t_struct* txception) {
630 generate_java_struct(txception, true);
631}
632
633/**
634 * Java struct definition.
635 *
636 * @param tstruct The struct definition
637 */
638void t_javame_generator::generate_java_struct(t_struct* tstruct, bool is_exception) {
639 // Make output file
640 string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".java";
641 ofstream_with_content_based_conditional_update f_struct;
642 f_struct.open(f_struct_name.c_str());
643
644 f_struct << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
645
646 generate_java_struct_definition(f_struct, tstruct, is_exception);
647 f_struct.close();
648}
649
650/**
651 * Java union definition.
652 *
653 * @param tstruct The struct definition
654 */
655void t_javame_generator::generate_java_union(t_struct* tstruct) {
656 // Make output file
657 string f_struct_name = package_dir_ + "/" + (tstruct->get_name()) + ".java";
658 ofstream_with_content_based_conditional_update f_struct;
659 f_struct.open(f_struct_name.c_str());
660
661 f_struct << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
662
663 generate_java_doc(f_struct, tstruct);
664
665 bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
666
667 indent(f_struct) << "public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
668 << " extends TUnion ";
669
670 scope_up(f_struct);
671
672 generate_struct_desc(f_struct, tstruct);
673 generate_field_descs(f_struct, tstruct);
674
675 f_struct << endl;
676
677 generate_union_constructor(f_struct, tstruct);
678
679 f_struct << endl;
680
681 generate_union_abstract_methods(f_struct, tstruct);
682
683 f_struct << endl;
684
685 generate_union_getters_and_setters(f_struct, tstruct);
686
687 f_struct << endl;
688
689 generate_union_comparisons(f_struct, tstruct);
690
691 f_struct << endl;
692
693 generate_union_hashcode(f_struct, tstruct);
694
695 f_struct << endl;
696
697 scope_down(f_struct);
698
699 f_struct.close();
700}
701
702void t_javame_generator::generate_union_constructor(ostream& out, t_struct* tstruct) {
703 indent(out) << "public " << type_name(tstruct) << "() {" << endl;
704 indent(out) << " super();" << endl;
705 indent(out) << "}" << endl << endl;
706
707 indent(out) << "public " << type_name(tstruct) << "(_Fields setField, Object value) {" << endl;
708 indent(out) << " super(setField, value);" << endl;
709 indent(out) << "}" << endl << endl;
710
711 indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {"
712 << endl;
713 indent(out) << " super(other);" << endl;
714 indent(out) << "}" << endl;
715
716 indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl;
717 indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
718 indent(out) << "}" << endl << endl;
719
720 // generate "constructors" for each field
721 const vector<t_field*>& members = tstruct->get_members();
722 vector<t_field*>::const_iterator m_iter;
723 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
724 indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "("
725 << type_name((*m_iter)->get_type()) << " value) {" << endl;
726 indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
727 indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl;
728 indent(out) << " return x;" << endl;
729 indent(out) << "}" << endl << endl;
730 }
731}
732
733void t_javame_generator::generate_union_getters_and_setters(ostream& out, t_struct* tstruct) {
734 const vector<t_field*>& members = tstruct->get_members();
735 vector<t_field*>::const_iterator m_iter;
736
737 bool first = true;
738 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
739 if (first) {
740 first = false;
741 } else {
742 out << endl;
743 }
744
745 t_field* field = (*m_iter);
746
747 generate_java_doc(out, field);
748 indent(out) << "public " << type_name(field->get_type()) << " get"
749 << get_cap_name(field->get_name()) << "() {" << endl;
750 indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {"
751 << endl;
752 indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();"
753 << endl;
754 indent(out) << " } else {" << endl;
755 indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
756 << "' because union is currently set to \" + getFieldDesc(getSetField()).name);"
757 << endl;
758 indent(out) << " }" << endl;
759 indent(out) << "}" << endl;
760
761 out << endl;
762
763 generate_java_doc(out, field);
764 indent(out) << "public void set" << get_cap_name(field->get_name()) << "("
765 << type_name(field->get_type()) << " value) {" << endl;
766 if (type_can_be_null(field->get_type())) {
767 indent(out) << " if (value == null) throw new NullPointerException();" << endl;
768 }
769 indent(out) << " setField_ = _Fields." << constant_name(field->get_name()) << ";" << endl;
770 indent(out) << " value_ = value;" << endl;
771 indent(out) << "}" << endl;
772 }
773}
774
775void t_javame_generator::generate_union_abstract_methods(ostream& out, t_struct* tstruct) {
776 generate_check_type(out, tstruct);
777 out << endl;
778 generate_read_value(out, tstruct);
779 out << endl;
780 generate_write_value(out, tstruct);
781 out << endl;
782 generate_get_field_desc(out, tstruct);
783 out << endl;
784 generate_get_struct_desc(out, tstruct);
785 out << endl;
786}
787
788void t_javame_generator::generate_check_type(ostream& out, t_struct* tstruct) {
789 indent(out)
790 << "protected void checkType(_Fields setField, Object value) throws ClassCastException {"
791 << endl;
792 indent_up();
793
794 indent(out) << "switch (setField) {" << endl;
795 indent_up();
796
797 const vector<t_field*>& members = tstruct->get_members();
798 vector<t_field*>::const_iterator m_iter;
799
800 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
801 t_field* field = (*m_iter);
802
803 indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
804 indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true)
805 << ") {" << endl;
806 indent(out) << " break;" << endl;
807 indent(out) << " }" << endl;
808 indent(out) << " throw new ClassCastException(\"Was expecting value of type "
809 << type_name(field->get_type(), true, false) << " for field '" << field->get_name()
810 << "', but got \" + value.getClass().getSimpleName());" << endl;
811 // do the real check here
812 }
813
814 indent(out) << "default:" << endl;
815 indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl;
816
817 indent_down();
818 indent(out) << "}" << endl;
819
820 indent_down();
821 indent(out) << "}" << endl;
822}
823
824void t_javame_generator::generate_read_value(ostream& out, t_struct* tstruct) {
825 indent(out) << "protected Object readValue(TProtocol iprot, TField field) throws TException {"
826 << endl;
827
828 indent_up();
829
830 indent(out) << "_Fields setField = _Fields.findByThriftId(field.id);" << endl;
831 indent(out) << "if (setField != null) {" << endl;
832 indent_up();
833 indent(out) << "switch (setField) {" << endl;
834 indent_up();
835
836 const vector<t_field*>& members = tstruct->get_members();
837 vector<t_field*>::const_iterator m_iter;
838
839 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
840 t_field* field = (*m_iter);
841
842 indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
843 indent_up();
844 indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {"
845 << endl;
846 indent_up();
847 indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";"
848 << endl;
849 generate_deserialize_field(out, field, "");
850 indent(out) << "return " << field->get_name() << ";" << endl;
851 indent_down();
852 indent(out) << "} else {" << endl;
853 indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
854 indent(out) << " return null;" << endl;
855 indent(out) << "}" << endl;
856 indent_down();
857 }
858
859 indent(out) << "default:" << endl;
860 indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any "
861 "of the case statements!\");" << endl;
862
863 indent_down();
864 indent(out) << "}" << endl;
865
866 indent_down();
867 indent(out) << "} else {" << endl;
868 indent_up();
869 indent(out) << "TProtocolUtil.skip(iprot, field.type);" << endl;
870 indent(out) << "return null;" << endl;
871 indent_down();
872 indent(out) << "}" << endl;
873
874 indent_down();
875 indent(out) << "}" << endl;
876}
877
878void t_javame_generator::generate_write_value(ostream& out, t_struct* tstruct) {
879 indent(out) << "protected void writeValue(TProtocol oprot) throws TException {" << endl;
880
881 indent_up();
882
883 indent(out) << "switch (setField_) {" << endl;
884 indent_up();
885
886 const vector<t_field*>& members = tstruct->get_members();
887 vector<t_field*>::const_iterator m_iter;
888
889 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
890 t_field* field = (*m_iter);
891
892 indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
893 indent_up();
894 indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << " = ("
895 << type_name(field->get_type(), true, false) << ")value_;" << endl;
896 generate_serialize_field(out, field, "");
897 indent(out) << "return;" << endl;
898 indent_down();
899 }
900
901 indent(out) << "default:" << endl;
902 indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + "
903 "setField_);" << endl;
904
905 indent_down();
906 indent(out) << "}" << endl;
907
908 indent_down();
909
910 indent(out) << "}" << endl;
911}
912
913void t_javame_generator::generate_get_field_desc(ostream& out, t_struct* tstruct) {
914 indent(out) << "protected TField getFieldDesc(_Fields setField) {" << endl;
915 indent_up();
916
917 const vector<t_field*>& members = tstruct->get_members();
918 vector<t_field*>::const_iterator m_iter;
919
920 indent(out) << "switch (setField) {" << endl;
921 indent_up();
922
923 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
924 t_field* field = (*m_iter);
925 indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
926 indent(out) << " return " << constant_name(field->get_name()) << "_FIELD_DESC;" << endl;
927 }
928
929 indent(out) << "default:" << endl;
930 indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl;
931
932 indent_down();
933 indent(out) << "}" << endl;
934
935 indent_down();
936 indent(out) << "}" << endl;
937}
938
939void t_javame_generator::generate_get_struct_desc(ostream& out, t_struct* tstruct) {
940 (void)tstruct;
941 indent(out) << "protected TStruct getStructDesc() {" << endl;
942 indent(out) << " return STRUCT_DESC;" << endl;
943 indent(out) << "}" << endl;
944}
945
946void t_javame_generator::generate_union_comparisons(ostream& out, t_struct* tstruct) {
947 // equality
948 indent(out) << "public boolean equals(Object other) {" << endl;
949 indent(out) << " if (other instanceof " << tstruct->get_name() << ") {" << endl;
950 indent(out) << " return equals((" << tstruct->get_name() << ")other);" << endl;
951 indent(out) << " } else {" << endl;
952 indent(out) << " return false;" << endl;
953 indent(out) << " }" << endl;
954 indent(out) << "}" << endl;
955
956 out << endl;
957
958 indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl;
959 indent(out) << " return other != null && getSetField() == other.getSetField() && "
960 "getFieldValue().equals(other.getFieldValue());" << endl;
961 indent(out) << "}" << endl;
962 out << endl;
963
964 indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
965 indent(out) << " int lastComparison = TBaseHelper.compareTo(getSetField(), other.getSetField());"
966 << endl;
967 indent(out) << " if (lastComparison == 0) {" << endl;
968 indent(out) << " return TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());"
969 << endl;
970 indent(out) << " }" << endl;
971 indent(out) << " return lastComparison;" << endl;
972 indent(out) << "}" << endl;
973 out << endl;
974}
975
976void t_javame_generator::generate_union_hashcode(ostream& out, t_struct* tstruct) {
977 (void)tstruct;
978 indent(out) << "/**" << endl;
979 indent(out)
980 << " * If you'd like this to perform more respectably, use the hashcode generator option."
981 << endl;
982 indent(out) << " */" << endl;
983 indent(out) << "public int hashCode() {" << endl;
984 indent(out) << " return 0;" << endl;
985 indent(out) << "}" << endl;
986}
987
988/**
989 * Java struct definition. This has various parameters, as it could be
990 * generated standalone or inside another class as a helper. If it
991 * is a helper than it is a static class.
992 *
993 * @param tstruct The struct definition
994 * @param is_exception Is this an exception?
995 * @param in_class If inside a class, needs to be static class
996 * @param is_result If this is a result it needs a different writer
997 */
998void t_javame_generator::generate_java_struct_definition(ostream& out,
999 t_struct* tstruct,
1000 bool is_exception,
1001 bool in_class,
1002 bool is_result) {
1003 generate_java_doc(out, tstruct);
1004
1005 bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
1006
1007 indent(out) << "public " << (is_final ? "final " : "") << (in_class ? "static " : "") << "class "
1008 << tstruct->get_name() << " ";
1009
1010 if (is_exception) {
1011 out << "extends Exception ";
1012 }
1013 out << "implements TBase ";
1014
1015 scope_up(out);
1016
1017 generate_struct_desc(out, tstruct);
1018
1019 // Members are public for -java, private for -javabean
1020 const vector<t_field*>& members = tstruct->get_members();
1021 vector<t_field*>::const_iterator m_iter;
1022
1023 out << endl;
1024
1025 generate_field_descs(out, tstruct);
1026
1027 out << endl;
1028
1029 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1030 indent(out) << "private ";
1031 out << declare_field(*m_iter, false) << endl;
1032 }
1033
1034 // isset data
1035 if (members.size() > 0) {
1036 out << endl;
1037
1038 indent(out) << "// isset id assignments" << endl;
1039
1040 int i = 0;
1041 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1042 if (!type_can_be_null((*m_iter)->get_type())) {
1043 indent(out) << "private static final int " << isset_field_id(*m_iter) << " = " << i << ";"
1044 << endl;
1045 i++;
1046 }
1047 }
1048
1049 if (i > 0) {
1050 indent(out) << "private boolean[] __isset_vector = new boolean[" << i << "];" << endl;
1051 }
1052
1053 out << endl;
1054 }
1055
1056 bool all_optional_members = true;
1057
1058 // Default constructor
1059 indent(out) << "public " << tstruct->get_name() << "() {" << endl;
1060 indent_up();
1061 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1062 t_type* t = get_true_type((*m_iter)->get_type());
1063 if ((*m_iter)->get_value() != NULL) {
1064 print_const_value(out,
1065 "this." + (*m_iter)->get_name(),
1066 t,
1067 (*m_iter)->get_value(),
1068 true,
1069 true);
1070 }
1071 if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
1072 all_optional_members = false;
1073 }
1074 }
1075 indent_down();
1076 indent(out) << "}" << endl << endl;
1077
1078 if (!members.empty() && !all_optional_members) {
1079 // Full constructor for all fields
1080 indent(out) << "public " << tstruct->get_name() << "(" << endl;
1081 indent_up();
1082 bool first = true;
1083 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1084 if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
1085 if (!first) {
1086 out << "," << endl;
1087 }
1088 first = false;
1089 indent(out) << type_name((*m_iter)->get_type()) << " " << (*m_iter)->get_name();
1090 }
1091 }
1092 out << ")" << endl;
1093 indent_down();
1094 indent(out) << "{" << endl;
1095 indent_up();
1096 indent(out) << "this();" << endl;
1097 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1098 if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
1099 indent(out) << "this." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << ";"
1100 << endl;
1101 generate_isset_set(out, (*m_iter));
1102 }
1103 }
1104 indent_down();
1105 indent(out) << "}" << endl << endl;
1106 }
1107
1108 // copy constructor
1109 indent(out) << "/**" << endl;
1110 indent(out) << " * Performs a deep copy on <i>other</i>." << endl;
1111 indent(out) << " */" << endl;
1112 indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {"
1113 << endl;
1114 indent_up();
1115
1116 if (has_bit_vector(tstruct)) {
1117 indent(out) << "System.arraycopy(other.__isset_vector, 0, __isset_vector, 0, "
1118 "other.__isset_vector.length);" << endl;
1119 }
1120
1121 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1122 t_field* field = (*m_iter);
1123 std::string field_name = field->get_name();
1124 t_type* type = field->get_type();
1125 bool can_be_null = type_can_be_null(type);
1126
1127 if (can_be_null) {
1128 indent(out) << "if (other." << generate_isset_check(field) << ") {" << endl;
1129 indent_up();
1130 }
1131
1132 if (type->is_container()) {
1133 generate_deep_copy_container(out, "other", field_name, "__this__" + field_name, type);
1134 indent(out) << "this." << field_name << " = __this__" << field_name << ";" << endl;
1135 } else {
1136 indent(out) << "this." << field_name << " = ";
1137 generate_deep_copy_non_container(out, "other." + field_name, field_name, type);
1138 out << ";" << endl;
1139 }
1140
1141 if (can_be_null) {
1142 indent_down();
1143 indent(out) << "}" << endl;
1144 }
1145 }
1146
1147 indent_down();
1148 indent(out) << "}" << endl << endl;
1149
1150 // clone method, so that you can deep copy an object when you don't know its class.
1151 indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl;
1152 indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
1153 indent(out) << "}" << endl << endl;
1154
1155 generate_java_struct_clear(out, tstruct);
1156
1157 generate_java_bean_boilerplate(out, tstruct);
1158 generate_generic_field_getters_setters(out, tstruct);
1159
1160 generate_java_struct_equality(out, tstruct);
1161 generate_java_struct_compare_to(out, tstruct);
1162
1163 generate_java_struct_reader(out, tstruct);
1164 if (is_result) {
1165 generate_java_struct_result_writer(out, tstruct);
1166 } else {
1167 generate_java_struct_writer(out, tstruct);
1168 }
1169 generate_java_struct_tostring(out, tstruct);
1170 generate_java_validator(out, tstruct);
1171 scope_down(out);
1172 out << endl;
1173}
1174
1175/**
1176 * Generates equals methods and a hashCode method for a structure.
1177 *
1178 * @param tstruct The struct definition
1179 */
1180void t_javame_generator::generate_java_struct_equality(ostream& out, t_struct* tstruct) {
1181 out << indent() << "public boolean equals(Object that) {" << endl;
1182 indent_up();
1183 out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl
1184 << indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl << indent()
1185 << " return this.equals((" << tstruct->get_name() << ")that);" << endl << indent()
1186 << "return false;" << endl;
1187 scope_down(out);
1188 out << endl;
1189
1190 out << indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl;
1191 indent_up();
1192 out << indent() << "if (that == null)" << endl << indent() << " return false;" << endl
1193 << indent() << "if (this == that)" << endl << indent() << " return true;" << endl;
1194
1195 const vector<t_field*>& members = tstruct->get_members();
1196 vector<t_field*>::const_iterator m_iter;
1197 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1198 out << endl;
1199
1200 t_type* t = get_true_type((*m_iter)->get_type());
1201 // Most existing Thrift code does not use isset or optional/required,
1202 // so we treat "default" fields as required.
1203 bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL;
1204 bool can_be_null = type_can_be_null(t);
1205 string name = (*m_iter)->get_name();
1206
1207 string this_present = "true";
1208 string that_present = "true";
1209 string unequal;
1210
1211 if (is_optional || can_be_null) {
1212 this_present += " && this." + generate_isset_check(*m_iter);
1213 that_present += " && that." + generate_isset_check(*m_iter);
1214 }
1215
1216 out << indent() << "boolean this_present_" << name << " = " << this_present << ";" << endl
1217 << indent() << "boolean that_present_" << name << " = " << that_present << ";" << endl
1218 << indent() << "if ("
1219 << "this_present_" << name << " || that_present_" << name << ") {" << endl;
1220 indent_up();
1221 out << indent() << "if (!("
1222 << "this_present_" << name << " && that_present_" << name << "))" << endl << indent()
1223 << " return false;" << endl;
1224
1225 if (t->is_binary()) {
1226 unequal = "TBaseHelper.compareTo(this." + name + ", that." + name + ") != 0";
1227 } else if (can_be_null) {
1228 unequal = "!this." + name + ".equals(that." + name + ")";
1229 } else {
1230 unequal = "this." + name + " != that." + name;
1231 }
1232
1233 out << indent() << "if (" << unequal << ")" << endl << indent() << " return false;" << endl;
1234
1235 scope_down(out);
1236 }
1237 out << endl;
1238 indent(out) << "return true;" << endl;
1239 scope_down(out);
1240 out << endl;
1241
1242 out << indent() << "public int hashCode() {" << endl;
1243 indent_up();
1244 indent(out) << "return 0;" << endl;
1245 indent_down();
1246 indent(out) << "}" << endl << endl;
1247}
1248
1249void t_javame_generator::generate_java_struct_compare_to(ostream& out, t_struct* tstruct) {
1250 indent(out) << "public int compareTo(Object otherObject) {" << endl;
1251 // indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl;
1252 indent_up();
1253
1254 indent(out) << "if (!getClass().equals(otherObject.getClass())) {" << endl;
1255 indent(out) << " return getClass().getName().compareTo(otherObject.getClass().getName());"
1256 << endl;
1257 indent(out) << "}" << endl;
1258 out << endl;
1259 indent(out) << type_name(tstruct) << " other = (" << type_name(tstruct) << ")otherObject;";
1260
1261 indent(out) << "int lastComparison = 0;" << endl;
1262 out << endl;
1263
1264 const vector<t_field*>& members = tstruct->get_members();
1265 vector<t_field*>::const_iterator m_iter;
1266 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1267 t_field* field = *m_iter;
1268 indent(out) << "lastComparison = TBaseHelper.compareTo(" << generate_isset_check(field)
1269 << ", other." << generate_isset_check(field) << ");" << endl;
1270 indent(out) << "if (lastComparison != 0) {" << endl;
1271 indent(out) << " return lastComparison;" << endl;
1272 indent(out) << "}" << endl;
1273
1274 indent(out) << "if (" << generate_isset_check(field) << ") {" << endl;
1275 if (field->get_type()->is_struct() || field->get_type()->is_xception()) {
1276 indent(out) << " lastComparison = this." << field->get_name() << ".compareTo(other."
1277 << field->get_name() << ");" << endl;
1278 } else {
1279 indent(out) << " lastComparison = TBaseHelper.compareTo(this." << field->get_name()
1280 << ", other." << field->get_name() << ");" << endl;
1281 }
1282
1283 indent(out) << " if (lastComparison != 0) {" << endl;
1284 indent(out) << " return lastComparison;" << endl;
1285 indent(out) << " }" << endl;
1286 indent(out) << "}" << endl;
1287 }
1288
1289 indent(out) << "return 0;" << endl;
1290
1291 indent_down();
1292 indent(out) << "}" << endl << endl;
1293}
1294
1295/**
1296 * Generates a function to read all the fields of the struct.
1297 *
1298 * @param tstruct The struct definition
1299 */
1300void t_javame_generator::generate_java_struct_reader(ostream& out, t_struct* tstruct) {
1301 out << indent() << "public void read(TProtocol iprot) throws TException {" << endl;
1302 indent_up();
1303
1304 const vector<t_field*>& fields = tstruct->get_members();
1305 vector<t_field*>::const_iterator f_iter;
1306
1307 // Declare stack tmp variables and read struct header
1308 out << indent() << "TField field;" << endl << indent() << "iprot.readStructBegin();" << endl;
1309
1310 // Loop over reading in fields
1311 indent(out) << "while (true)" << endl;
1312 scope_up(out);
1313
1314 // Read beginning field marker
1315 indent(out) << "field = iprot.readFieldBegin();" << endl;
1316
1317 // Check for field STOP marker and break
1318 indent(out) << "if (field.type == TType.STOP) { " << endl;
1319 indent_up();
1320 indent(out) << "break;" << endl;
1321 indent_down();
1322 indent(out) << "}" << endl;
1323
1324 // Switch statement on the field we are reading
1325 indent(out) << "switch (field.id) {" << endl;
1326
1327 indent_up();
1328
1329 // Generate deserialization code for known cases
1330 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1331 indent(out) << "case " << (*f_iter)->get_key() << ": // "
1332 << constant_name((*f_iter)->get_name()) << endl;
1333 indent_up();
1334 indent(out) << "if (field.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
1335 indent_up();
1336
1337 generate_deserialize_field(out, *f_iter, "this.");
1338 generate_isset_set(out, *f_iter);
1339 indent_down();
1340 out << indent() << "} else { " << endl << indent() << " TProtocolUtil.skip(iprot, field.type);"
1341 << endl << indent() << "}" << endl << indent() << "break;" << endl;
1342 indent_down();
1343 }
1344
1345 indent(out) << "default:" << endl;
1346 indent(out) << " TProtocolUtil.skip(iprot, field.type);" << endl;
1347
1348 indent_down();
1349 indent(out) << "}" << endl;
1350
1351 // Read field end marker
1352 indent(out) << "iprot.readFieldEnd();" << endl;
1353
1354 indent_down();
1355 indent(out) << "}" << endl;
1356
1357 out << indent() << "iprot.readStructEnd();" << endl;
1358
1359 // performs various checks (e.g. check that all required fields are set)
1360 indent(out) << "validate();" << endl;
1361
1362 indent_down();
1363 out << indent() << "}" << endl << endl;
1364}
1365
1366// generates java method to perform various checks
1367// (e.g. check that all required fields are set)
1368void t_javame_generator::generate_java_validator(ostream& out, t_struct* tstruct) {
1369 indent(out) << "public void validate() throws TException {" << endl;
1370 indent_up();
1371
1372 const vector<t_field*>& fields = tstruct->get_members();
1373 vector<t_field*>::const_iterator f_iter;
1374
1375 out << indent() << "// check for required fields" << endl;
1376 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1377 if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
1378 out << indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl << indent()
1379 << " throw new TProtocolException(\"Required field '" << (*f_iter)->get_name()
1380 << "' is unset! Struct:\" + toString());" << endl << indent() << "}" << endl << endl;
1381 }
1382 }
1383
1384 indent_down();
1385 indent(out) << "}" << endl << endl;
1386}
1387
1388/**
1389 * Generates a function to write all the fields of the struct
1390 *
1391 * @param tstruct The struct definition
1392 */
1393void t_javame_generator::generate_java_struct_writer(ostream& out, t_struct* tstruct) {
1394 out << indent() << "public void write(TProtocol oprot) throws TException {" << endl;
1395 indent_up();
1396
1397 string name = tstruct->get_name();
1398 const vector<t_field*>& fields = tstruct->get_sorted_members();
1399 vector<t_field*>::const_iterator f_iter;
1400
1401 // performs various checks (e.g. check that all required fields are set)
1402 indent(out) << "validate();" << endl << endl;
1403
1404 indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
1405
1406 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1407 bool null_allowed = type_can_be_null((*f_iter)->get_type());
1408 if (null_allowed) {
1409 out << indent() << "if (this." << (*f_iter)->get_name() << " != null) {" << endl;
1410 indent_up();
1411 }
1412 bool optional = (*f_iter)->get_req() == t_field::T_OPTIONAL;
1413 if (optional) {
1414 indent(out) << "if (" << generate_isset_check((*f_iter)) << ") {" << endl;
1415 indent_up();
1416 }
1417
1418 indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
1419 << "_FIELD_DESC);" << endl;
1420
1421 // Write field contents
1422 generate_serialize_field(out, *f_iter, "this.");
1423
1424 // Write field closer
1425 indent(out) << "oprot.writeFieldEnd();" << endl;
1426
1427 if (optional) {
1428 indent_down();
1429 indent(out) << "}" << endl;
1430 }
1431 if (null_allowed) {
1432 indent_down();
1433 indent(out) << "}" << endl;
1434 }
1435 }
1436 // Write the struct map
1437 out << indent() << "oprot.writeFieldStop();" << endl << indent() << "oprot.writeStructEnd();"
1438 << endl;
1439
1440 indent_down();
1441 out << indent() << "}" << endl << endl;
1442}
1443
1444/**
1445 * Generates a function to write all the fields of the struct,
1446 * which is a function result. These fields are only written
1447 * if they are set in the Isset array, and only one of them
1448 * can be set at a time.
1449 *
1450 * @param tstruct The struct definition
1451 */
1452void t_javame_generator::generate_java_struct_result_writer(ostream& out, t_struct* tstruct) {
1453 out << indent() << "public void write(TProtocol oprot) throws TException {" << endl;
1454 indent_up();
1455
1456 string name = tstruct->get_name();
1457 const vector<t_field*>& fields = tstruct->get_sorted_members();
1458 vector<t_field*>::const_iterator f_iter;
1459
1460 indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl;
1461
1462 bool first = true;
1463 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1464 if (first) {
1465 first = false;
1466 out << endl << indent() << "if ";
1467 } else {
1468 out << " else if ";
1469 }
1470
1471 out << "(this." << generate_isset_check(*f_iter) << ") {" << endl;
1472
1473 indent_up();
1474
1475 indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name())
1476 << "_FIELD_DESC);" << endl;
1477
1478 // Write field contents
1479 generate_serialize_field(out, *f_iter, "this.");
1480
1481 // Write field closer
1482 indent(out) << "oprot.writeFieldEnd();" << endl;
1483
1484 indent_down();
1485 indent(out) << "}";
1486 }
1487 // Write the struct map
1488 out << endl << indent() << "oprot.writeFieldStop();" << endl << indent()
1489 << "oprot.writeStructEnd();" << endl;
1490
1491 indent_down();
1492 out << indent() << "}" << endl << endl;
1493}
1494
1495void t_javame_generator::generate_reflection_getters(ostringstream& out,
1496 t_type* type,
1497 string field_name,
1498 string cap_name) {
1499 indent(out) << "case " << constant_name(field_name) << ":" << endl;
1500 indent_up();
1501
1502 if (type->is_base_type() && !type->is_string()) {
1503 t_base_type* base_type = (t_base_type*)type;
1504
1505 indent(out) << "return new " << type_name(type, true, false) << "("
1506 << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl;
1507 } else {
1508 indent(out) << "return get" << cap_name << "();" << endl << endl;
1509 }
1510
1511 indent_down();
1512}
1513
1514void t_javame_generator::generate_reflection_setters(ostringstream& out,
1515 t_type* type,
1516 string field_name,
1517 string cap_name) {
1518 indent(out) << "case " << constant_name(field_name) << ":" << endl;
1519 indent_up();
1520 indent(out) << "if (value == null) {" << endl;
1521 indent(out) << " unset" << get_cap_name(field_name) << "();" << endl;
1522 indent(out) << "} else {" << endl;
1523 indent(out) << " set" << cap_name << "((" << type_name(type, true, false) << ")value);" << endl;
1524 indent(out) << "}" << endl;
1525 indent(out) << "break;" << endl << endl;
1526
1527 indent_down();
1528}
1529
1530void t_javame_generator::generate_generic_field_getters_setters(std::ostream& out,
1531 t_struct* tstruct) {
1532 (void)out;
1533 std::ostringstream getter_stream;
1534 std::ostringstream setter_stream;
1535
1536 // build up the bodies of both the getter and setter at once
1537 const vector<t_field*>& fields = tstruct->get_members();
1538 vector<t_field*>::const_iterator f_iter;
1539 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1540 t_field* field = *f_iter;
1541 t_type* type = get_true_type(field->get_type());
1542 std::string field_name = field->get_name();
1543 std::string cap_name = get_cap_name(field_name);
1544
1545 indent_up();
1546 generate_reflection_setters(setter_stream, type, field_name, cap_name);
1547 generate_reflection_getters(getter_stream, type, field_name, cap_name);
1548 indent_down();
1549 }
1550}
1551
1552/**
1553 * Generates a set of Java Bean boilerplate functions (setters, getters, etc.)
1554 * for the given struct.
1555 *
1556 * @param tstruct The struct definition
1557 */
1558void t_javame_generator::generate_java_bean_boilerplate(ostream& out, t_struct* tstruct) {
1559 const vector<t_field*>& fields = tstruct->get_members();
1560 vector<t_field*>::const_iterator f_iter;
1561 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1562 t_field* field = *f_iter;
1563 t_type* type = get_true_type(field->get_type());
1564 std::string field_name = field->get_name();
1565 std::string cap_name = get_cap_name(field_name);
1566
1567 if (type->is_container()) {
1568 // Method to return the size of the collection
1569 indent(out) << "public int get" << cap_name;
1570 out << get_cap_name("size() {") << endl;
1571
1572 indent_up();
1573 indent(out) << "return (this." << field_name << " == null) ? 0 : "
1574 << "this." << field_name << ".size();" << endl;
1575 indent_down();
1576 indent(out) << "}" << endl << endl;
1577 }
1578
1579 if (type->is_set() || type->is_list()) {
1580
1581 t_type* element_type;
1582 if (type->is_set()) {
1583 element_type = ((t_set*)type)->get_elem_type();
1584 } else {
1585 element_type = ((t_list*)type)->get_elem_type();
1586 }
1587
1588 // Iterator getter for sets and lists
1589 indent(out) << "public Enumeration get" << cap_name;
1590 out << get_cap_name("Enumeration() {") << endl;
1591
1592 indent_up();
1593 indent(out) << "return (this." << field_name << " == null) ? null : "
1594 << "this." << field_name << ".elements();" << endl;
1595 indent_down();
1596 indent(out) << "}" << endl << endl;
1597
1598 // Add to set or list, create if the set/list is null
1599 indent(out);
1600 out << "public void add" << get_cap_name("to");
1601 out << cap_name << "(" << type_name(element_type) << " elem) {" << endl;
1602
1603 indent_up();
1604 indent(out) << "if (this." << field_name << " == null) {" << endl;
1605 indent_up();
1606 indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
1607 << endl;
1608 indent_down();
1609 indent(out) << "}" << endl;
1610 if (type->is_set()) {
1611 indent(out) << "this." << field_name << ".put(" << box_type(element_type, "elem") << ", "
1612 << box_type(element_type, "elem") << ");" << endl;
1613 } else {
1614 indent(out) << "this." << field_name << ".addElement(" << box_type(element_type, "elem")
1615 << ");" << endl;
1616 }
1617 indent_down();
1618 indent(out) << "}" << endl << endl;
1619
1620 } else if (type->is_map()) {
1621 // Put to map
1622 t_type* key_type = ((t_map*)type)->get_key_type();
1623 t_type* val_type = ((t_map*)type)->get_val_type();
1624
1625 indent(out);
1626 out << "public void putTo" << cap_name << "(" << type_name(key_type, true) << " key, "
1627 << type_name(val_type, true) << " val) {" << endl;
1628
1629 indent_up();
1630 indent(out) << "if (this." << field_name << " == null) {" << endl;
1631 indent_up();
1632 indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << "();"
1633 << endl;
1634 indent_down();
1635 indent(out) << "}" << endl;
1636 indent(out) << "this." << field_name << ".put(key, val);" << endl;
1637 indent_down();
1638 indent(out) << "}" << endl << endl;
1639 }
1640
1641 // Simple getter
1642 generate_java_doc(out, field);
1643 indent(out) << "public " << type_name(type);
1644 if (type->is_base_type() && ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) {
1645 out << " is";
1646 } else {
1647 out << " get";
1648 }
1649 out << cap_name << "() {" << endl;
1650 indent_up();
1651 indent(out) << "return this." << field_name << ";" << endl;
1652 indent_down();
1653 indent(out) << "}" << endl << endl;
1654
1655 // Simple setter
1656 generate_java_doc(out, field);
1657 indent(out) << "public ";
1658 out << "void";
1659 out << " set" << cap_name << "(" << type_name(type) << " " << field_name << ") {" << endl;
1660 indent_up();
1661 indent(out) << "this." << field_name << " = " << field_name << ";" << endl;
1662 generate_isset_set(out, field);
1663
1664 indent_down();
1665 indent(out) << "}" << endl << endl;
1666
1667 // Unsetter
1668 indent(out) << "public void unset" << cap_name << "() {" << endl;
1669 indent_up();
1670 if (type_can_be_null(type)) {
1671 indent(out) << "this." << field_name << " = null;" << endl;
1672 } else {
1673 indent(out) << "__isset_vector[" << isset_field_id(field) << "] = false;" << endl;
1674 }
1675 indent_down();
1676 indent(out) << "}" << endl << endl;
1677
1678 // isSet method
1679 indent(out) << "/** Returns true if field " << field_name
1680 << " is set (has been assigned a value) and false otherwise */" << endl;
1681 indent(out) << "public boolean is" << get_cap_name("set") << cap_name << "() {" << endl;
1682 indent_up();
1683 if (type_can_be_null(type)) {
1684 indent(out) << "return this." << field_name << " != null;" << endl;
1685 } else {
1686 indent(out) << "return __isset_vector[" << isset_field_id(field) << "];" << endl;
1687 }
1688 indent_down();
1689 indent(out) << "}" << endl << endl;
1690
1691 indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {"
1692 << endl;
1693 indent_up();
1694 if (type_can_be_null(type)) {
1695 indent(out) << "if (!value) {" << endl;
1696 indent(out) << " this." << field_name << " = null;" << endl;
1697 indent(out) << "}" << endl;
1698 } else {
1699 indent(out) << "__isset_vector[" << isset_field_id(field) << "] = value;" << endl;
1700 }
1701 indent_down();
1702 indent(out) << "}" << endl << endl;
1703 }
1704}
1705
1706/**
1707 * Generates a toString() method for the given struct
1708 *
1709 * @param tstruct The struct definition
1710 */
1711void t_javame_generator::generate_java_struct_tostring(ostream& out, t_struct* tstruct) {
1712 out << indent() << "public String toString() {" << endl;
1713 indent_up();
1714
1715 out << indent() << "StringBuffer sb = new StringBuffer(\"" << tstruct->get_name() << "(\");"
1716 << endl;
1717 out << indent() << "boolean first = true;" << endl << endl;
1718
1719 const vector<t_field*>& fields = tstruct->get_members();
1720 vector<t_field*>::const_iterator f_iter;
1721 bool first = true;
1722 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1723 bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL;
1724 if (could_be_unset) {
1725 indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl;
1726 indent_up();
1727 }
1728
1729 t_field* field = (*f_iter);
1730
1731 if (!first) {
1732 indent(out) << "if (!first) sb.append(\", \");" << endl;
1733 }
1734 indent(out) << "sb.append(\"" << (*f_iter)->get_name() << ":\");" << endl;
1735 bool can_be_null = type_can_be_null(field->get_type());
1736 if (can_be_null) {
1737 indent(out) << "if (this." << (*f_iter)->get_name() << " == null) {" << endl;
1738 indent(out) << " sb.append(\"null\");" << endl;
1739 indent(out) << "} else {" << endl;
1740 indent_up();
1741 }
1742
1743 if (field->get_type()->is_binary()) {
1744 indent(out) << "TBaseHelper.toString(this." << field->get_name() << ", sb);" << endl;
1745 } else {
1746 indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
1747 }
1748
1749 if (can_be_null) {
1750 indent_down();
1751 indent(out) << "}" << endl;
1752 }
1753 indent(out) << "first = false;" << endl;
1754
1755 if (could_be_unset) {
1756 indent_down();
1757 indent(out) << "}" << endl;
1758 }
1759 first = false;
1760 }
1761 out << indent() << "sb.append(\")\");" << endl << indent() << "return sb.toString();" << endl;
1762
1763 indent_down();
1764 indent(out) << "}" << endl << endl;
1765}
1766
1767/**
1768 * Returns a string with the java representation of the given thrift type
1769 * (e.g. for the type struct it returns "TType.STRUCT")
1770 */
1771std::string t_javame_generator::get_java_type_string(t_type* type) {
1772 if (type->is_list()) {
1773 return "TType.LIST";
1774 } else if (type->is_map()) {
1775 return "TType.MAP";
1776 } else if (type->is_set()) {
1777 return "TType.SET";
1778 } else if (type->is_struct() || type->is_xception()) {
1779 return "TType.STRUCT";
1780 } else if (type->is_enum()) {
1781 return "TType.ENUM";
1782 } else if (type->is_typedef()) {
1783 return get_java_type_string(((t_typedef*)type)->get_type());
1784 } else if (type->is_base_type()) {
1785 switch (((t_base_type*)type)->get_base()) {
1786 case t_base_type::TYPE_VOID:
1787 return "TType.VOID";
1788 break;
1789 case t_base_type::TYPE_STRING:
1790 return "TType.STRING";
1791 break;
1792 case t_base_type::TYPE_BOOL:
1793 return "TType.BOOL";
1794 break;
1795 case t_base_type::TYPE_I8:
1796 return "TType.BYTE";
1797 break;
1798 case t_base_type::TYPE_I16:
1799 return "TType.I16";
1800 break;
1801 case t_base_type::TYPE_I32:
1802 return "TType.I32";
1803 break;
1804 case t_base_type::TYPE_I64:
1805 return "TType.I64";
1806 break;
1807 case t_base_type::TYPE_DOUBLE:
1808 return "TType.DOUBLE";
1809 break;
1810 default:
1811 throw std::runtime_error("Unknown thrift type \"" + type->get_name()
1812 + "\" passed to t_javame_generator::get_java_type_string!");
1813 break; // This should never happen!
1814 }
1815 } else {
1816 throw std::runtime_error(
1817 "Unknown thrift type \"" + type->get_name()
1818 + "\" passed to t_javame_generator::get_java_type_string!"); // This should never happen!
1819 }
1820}
1821
1822void t_javame_generator::generate_field_value_meta_data(std::ostream& out, t_type* type) {
1823 out << endl;
1824 indent_up();
1825 indent_up();
1826 if (type->is_struct() || type->is_xception()) {
1827 indent(out) << "new StructMetaData(TType.STRUCT, " << type_name(type) << ".class";
1828 } else if (type->is_container()) {
1829 if (type->is_list()) {
1830 indent(out) << "new ListMetaData(TType.LIST, ";
1831 t_type* elem_type = ((t_list*)type)->get_elem_type();
1832 generate_field_value_meta_data(out, elem_type);
1833 } else if (type->is_set()) {
1834 indent(out) << "new SetMetaData(TType.SET, ";
1835 t_type* elem_type = ((t_list*)type)->get_elem_type();
1836 generate_field_value_meta_data(out, elem_type);
1837 } else { // map
1838 indent(out) << "new MapMetaData(TType.MAP, ";
1839 t_type* key_type = ((t_map*)type)->get_key_type();
1840 t_type* val_type = ((t_map*)type)->get_val_type();
1841 generate_field_value_meta_data(out, key_type);
1842 out << ", ";
1843 generate_field_value_meta_data(out, val_type);
1844 }
1845 } else if (type->is_enum()) {
1846 indent(out) << "new EnumMetaData(TType.ENUM, " << type_name(type) << ".class";
1847 } else {
1848 indent(out) << "new FieldValueMetaData(" << get_java_type_string(type);
1849 if (type->is_typedef()) {
1850 indent(out) << ", \"" << ((t_typedef*)type)->get_symbolic() << "\"";
1851 }
1852 }
1853 out << ")";
1854 indent_down();
1855 indent_down();
1856}
1857
1858/**
1859 * Generates a thrift service. In C++, this comprises an entirely separate
1860 * header and source file. The header file defines the methods and includes
1861 * the data types defined in the main header file, and the implementation
1862 * file contains implementations of the basic printer and default interfaces.
1863 *
1864 * @param tservice The service definition
1865 */
1866void t_javame_generator::generate_service(t_service* tservice) {
1867 // Make output file
1868 string f_service_name = package_dir_ + "/" + service_name_ + ".java";
1869 f_service_.open(f_service_name.c_str());
1870
1871 f_service_ << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
1872
1873 f_service_ << "public class " << service_name_ << " {" << endl << endl;
1874 indent_up();
1875
1876 // Generate the three main parts of the service
1877 generate_service_interface(tservice);
1878 generate_service_client(tservice);
1879 generate_service_server(tservice);
1880 generate_service_helpers(tservice);
1881
1882 indent_down();
1883 f_service_ << "}" << endl;
1884 f_service_.close();
1885}
1886
1887/**
1888 * Generates a service interface definition.
1889 *
1890 * @param tservice The service to generate a header definition for
1891 */
1892void t_javame_generator::generate_primitive_service_interface(t_service* tservice) {
1893 f_service_ << indent() << "public interface Iface extends " << service_name_ << "Iface { }"
1894 << endl << endl;
1895
1896 string f_interface_name = package_dir_ + "/" + service_name_ + "Iface.java";
1897 ofstream_with_content_based_conditional_update f_iface;
1898 f_iface.open(f_interface_name.c_str());
1899
1900 string extends_iface = "";
1901 if (tservice->get_extends() != NULL) {
1902 extends_iface = " extends " + type_name(tservice->get_extends()) + "Iface";
1903 }
1904
1905 f_iface << autogen_comment() << java_package() << java_type_imports() << java_thrift_imports();
1906 generate_java_doc(f_iface, tservice);
1907 f_iface << "public interface " << service_name_ << "Iface" << extends_iface << " {" << endl
1908 << endl;
1909 vector<t_function*> functions = tservice->get_functions();
1910 vector<t_function*>::iterator f_iter;
1911 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1912 generate_java_doc(f_iface, *f_iter);
1913 f_iface << " public " << function_signature(*f_iter) << ";" << endl << endl;
1914 }
1915 f_iface << "}" << endl << endl;
1916}
1917
1918/**
1919 * Generates a service interface definition.
1920 *
1921 * @param tservice The service to generate a header definition for
1922 */
1923void t_javame_generator::generate_service_interface(t_service* tservice) {
1924 string extends = "";
1925 string extends_iface = "";
1926 if (tservice->get_extends() != NULL) {
1927 extends = type_name(tservice->get_extends());
1928 extends_iface = " extends " + extends + ".Iface";
1929 }
1930
1931 generate_java_doc(f_service_, tservice);
1932 f_service_ << indent() << "public interface Iface" << extends_iface << " {" << endl << endl;
1933 indent_up();
1934 vector<t_function*> functions = tservice->get_functions();
1935 vector<t_function*>::iterator f_iter;
1936 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1937 generate_java_doc(f_service_, *f_iter);
1938 indent(f_service_) << "public " << function_signature(*f_iter) << ";" << endl << endl;
1939 }
1940 indent_down();
1941 f_service_ << indent() << "}" << endl << endl;
1942}
1943
1944/**
1945 * Generates structs for all the service args and return types
1946 *
1947 * @param tservice The service
1948 */
1949void t_javame_generator::generate_service_helpers(t_service* tservice) {
1950 vector<t_function*> functions = tservice->get_functions();
1951 vector<t_function*>::iterator f_iter;
1952 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1953 t_struct* ts = (*f_iter)->get_arglist();
1954 generate_java_struct_definition(f_service_, ts, false, true);
1955 generate_function_helpers(*f_iter);
1956 }
1957}
1958
1959/**
1960 * Generates a service client definition.
1961 *
1962 * @param tservice The service to generate a server for.
1963 */
1964void t_javame_generator::generate_service_client(t_service* tservice) {
1965 string extends = "";
1966 string extends_client = "";
1967 if (tservice->get_extends() != NULL) {
1968 extends = type_name(tservice->get_extends());
1969 extends_client = " extends " + extends + ".Client";
1970 }
1971
1972 indent(f_service_) << "public static class Client" << extends_client
1973 << " implements TServiceClient, Iface {" << endl;
1974 indent_up();
1975
1976 indent(f_service_) << "public Client(TProtocol prot)" << endl;
1977 scope_up(f_service_);
1978 indent(f_service_) << "this(prot, prot);" << endl;
1979 scope_down(f_service_);
1980 f_service_ << endl;
1981
1982 indent(f_service_) << "public Client(TProtocol iprot, TProtocol oprot)" << endl;
1983 scope_up(f_service_);
1984 if (extends.empty()) {
1985 f_service_ << indent() << "iprot_ = iprot;" << endl << indent() << "oprot_ = oprot;" << endl;
1986 } else {
1987 f_service_ << indent() << "super(iprot, oprot);" << endl;
1988 }
1989 scope_down(f_service_);
1990 f_service_ << endl;
1991
1992 if (extends.empty()) {
1993 f_service_ << indent() << "protected TProtocol iprot_;" << endl << indent()
1994 << "protected TProtocol oprot_;" << endl << endl << indent()
1995 << "protected int seqid_;" << endl << endl;
1996
1997 indent(f_service_) << "public TProtocol getInputProtocol()" << endl;
1998 scope_up(f_service_);
1999 indent(f_service_) << "return this.iprot_;" << endl;
2000 scope_down(f_service_);
2001 f_service_ << endl;
2002
2003 indent(f_service_) << "public TProtocol getOutputProtocol()" << endl;
2004 scope_up(f_service_);
2005 indent(f_service_) << "return this.oprot_;" << endl;
2006 scope_down(f_service_);
2007 f_service_ << endl;
2008 }
2009
2010 // Generate client method implementations
2011 vector<t_function*> functions = tservice->get_functions();
2012 vector<t_function*>::const_iterator f_iter;
2013 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
2014 string funname = (*f_iter)->get_name();
2015
2016 // Open function
2017 indent(f_service_) << "public " << function_signature(*f_iter) << endl;
2018 scope_up(f_service_);
2019 indent(f_service_) << "send_" << funname << "(";
2020
2021 // Get the struct of function call params
2022 t_struct* arg_struct = (*f_iter)->get_arglist();
2023
2024 // Declare the function arguments
2025 const vector<t_field*>& fields = arg_struct->get_members();
2026 vector<t_field*>::const_iterator fld_iter;
2027 bool first = true;
2028 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
2029 if (first) {
2030 first = false;
2031 } else {
2032 f_service_ << ", ";
2033 }
2034 f_service_ << (*fld_iter)->get_name();
2035 }
2036 f_service_ << ");" << endl;
2037
2038 if (!(*f_iter)->is_oneway()) {
2039 f_service_ << indent();
2040 if (!(*f_iter)->get_returntype()->is_void()) {
2041 f_service_ << "return ";
2042 }
2043 f_service_ << "recv_" << funname << "();" << endl;
2044 }
2045 scope_down(f_service_);
2046 f_service_ << endl;
2047
2048 t_function send_function(g_type_void,
2049 string("send_") + (*f_iter)->get_name(),
2050 (*f_iter)->get_arglist());
2051
2052 string argsname = (*f_iter)->get_name() + "_args";
2053
2054 // Open function
2055 indent(f_service_) << "public " << function_signature(&send_function) << endl;
2056 scope_up(f_service_);
2057
2058 // Serialize the request
2059 f_service_ << indent() << "oprot_.writeMessageBegin(new TMessage(\"" << funname << "\", "
2060 << ((*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL")
2061 << ", ++seqid_));" << endl << indent() << argsname << " args = new " << argsname
2062 << "();" << endl;
2063
2064 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
2065 f_service_ << indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "("
2066 << (*fld_iter)->get_name() << ");" << endl;
2067 }
2068
2069 f_service_ << indent() << "args.write(oprot_);" << endl << indent()
2070 << "oprot_.writeMessageEnd();" << endl << indent()
2071 << "oprot_.getTransport().flush();" << endl;
2072
2073 scope_down(f_service_);
2074 f_service_ << endl;
2075
2076 if (!(*f_iter)->is_oneway()) {
2077 string resultname = (*f_iter)->get_name() + "_result";
2078
2079 t_struct noargs(program_);
2080 t_function recv_function((*f_iter)->get_returntype(),
2081 string("recv_") + (*f_iter)->get_name(),
2082 &noargs,
2083 (*f_iter)->get_xceptions());
2084 // Open function
2085 indent(f_service_) << "public " << function_signature(&recv_function) << endl;
2086 scope_up(f_service_);
2087
2088 f_service_ << indent() << "TMessage msg = iprot_.readMessageBegin();" << endl << indent()
2089 << "if (msg.type == TMessageType.EXCEPTION) {" << endl << indent()
2090 << " TApplicationException x = TApplicationException.read(iprot_);" << endl
2091 << indent() << " iprot_.readMessageEnd();" << endl << indent() << " throw x;"
2092 << endl << indent() << "}" << endl << indent() << "if (msg.seqid != seqid_) {"
2093 << endl << indent()
2094 << " throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, \""
2095 << (*f_iter)->get_name() << " failed: out of sequence response\");" << endl
2096 << indent() << "}" << endl << indent() << resultname << " result = new "
2097 << resultname << "();" << endl << indent() << "result.read(iprot_);" << endl
2098 << indent() << "iprot_.readMessageEnd();" << endl;
2099
2100 // Careful, only return _result if not a void function
2101 if (!(*f_iter)->get_returntype()->is_void()) {
2102 f_service_ << indent() << "if (result." << generate_isset_check("success") << ") {" << endl
2103 << indent() << " return result.success;" << endl << indent() << "}" << endl;
2104 }
2105
2106 t_struct* xs = (*f_iter)->get_xceptions();
2107 const std::vector<t_field*>& xceptions = xs->get_members();
2108 vector<t_field*>::const_iterator x_iter;
2109 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
2110 f_service_ << indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl
2111 << indent() << " throw result." << (*x_iter)->get_name() << ";" << endl
2112 << indent() << "}" << endl;
2113 }
2114
2115 // If you get here it's an exception, unless a void function
2116 if ((*f_iter)->get_returntype()->is_void()) {
2117 indent(f_service_) << "return;" << endl;
2118 } else {
2119 f_service_ << indent()
2120 << "throw new TApplicationException(TApplicationException.MISSING_RESULT, \""
2121 << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
2122 }
2123
2124 // Close function
2125 scope_down(f_service_);
2126 f_service_ << endl;
2127 }
2128 }
2129
2130 indent_down();
2131 indent(f_service_) << "}" << endl;
2132}
2133
2134/**
2135 * Generates a service server definition.
2136 *
2137 * @param tservice The service to generate a server for.
2138 */
2139void t_javame_generator::generate_service_server(t_service* tservice) {
2140 // Generate the dispatch methods
2141 vector<t_function*> functions = tservice->get_functions();
2142 vector<t_function*>::iterator f_iter;
2143
2144 // Extends stuff
2145 string extends = "";
2146 string extends_processor = "";
2147 if (tservice->get_extends() != NULL) {
2148 extends = type_name(tservice->get_extends());
2149 extends_processor = " extends " + extends + ".Processor";
2150 }
2151
2152 // Generate the header portion
2153 indent(f_service_) << "public static class Processor" << extends_processor
2154 << " implements TProcessor {" << endl;
2155 indent_up();
2156
2157 indent(f_service_) << "public Processor(Iface iface)" << endl;
2158 scope_up(f_service_);
2159 if (!extends.empty()) {
2160 f_service_ << indent() << "super(iface);" << endl;
2161 }
2162 f_service_ << indent() << "iface_ = iface;" << endl;
2163
2164 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
2165 f_service_ << indent() << "processMap_.put(\"" << (*f_iter)->get_name() << "\", new "
2166 << (*f_iter)->get_name() << "());" << endl;
2167 }
2168
2169 scope_down(f_service_);
2170 f_service_ << endl;
2171
2172 if (extends.empty()) {
2173 f_service_
2174 << indent() << "protected static interface ProcessFunction {" << endl << indent()
2175 << " public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException;"
2176 << endl << indent() << "}" << endl << endl;
2177 }
2178
2179 f_service_ << indent() << "private Iface iface_;" << endl;
2180
2181 if (extends.empty()) {
2182 f_service_ << indent() << "protected final Hashtable processMap_ = new Hashtable();" << endl;
2183 }
2184
2185 f_service_ << endl;
2186
2187 // Generate the server implementation
2188 indent(f_service_) << "public boolean process(TProtocol iprot, TProtocol oprot) throws TException"
2189 << endl;
2190 scope_up(f_service_);
2191
2192 f_service_ << indent() << "TMessage msg = iprot.readMessageBegin();" << endl;
2193
2194 // TODO(mcslee): validate message, was the seqid etc. legit?
2195
2196 f_service_
2197 << indent() << "ProcessFunction fn = (ProcessFunction)processMap_.get(msg.name);" << endl
2198 << indent() << "if (fn == null) {" << endl << indent()
2199 << " TProtocolUtil.skip(iprot, TType.STRUCT);" << endl << indent()
2200 << " iprot.readMessageEnd();" << endl << indent()
2201 << " TApplicationException x = new "
2202 "TApplicationException(TApplicationException.UNKNOWN_METHOD, \"Invalid method name: "
2203 "'\"+msg.name+\"'\");" << endl << indent()
2204 << " oprot.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));"
2205 << endl << indent() << " x.write(oprot);" << endl << indent() << " oprot.writeMessageEnd();"
2206 << endl << indent() << " oprot.getTransport().flush();" << endl << indent()
2207 << " return true;" << endl << indent() << "}" << endl << indent()
2208 << "fn.process(msg.seqid, iprot, oprot);" << endl;
2209
2210 f_service_ << indent() << "return true;" << endl;
2211
2212 scope_down(f_service_);
2213 f_service_ << endl;
2214
2215 // Generate the process subfunctions
2216 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
2217 generate_process_function(tservice, *f_iter);
2218 }
2219
2220 indent_down();
2221 indent(f_service_) << "}" << endl << endl;
2222}
2223
2224/**
2225 * Generates a struct and helpers for a function.
2226 *
2227 * @param tfunction The function
2228 */
2229void t_javame_generator::generate_function_helpers(t_function* tfunction) {
2230 if (tfunction->is_oneway()) {
2231 return;
2232 }
2233
2234 t_struct result(program_, tfunction->get_name() + "_result");
2235 t_field success(tfunction->get_returntype(), "success", 0);
2236 if (!tfunction->get_returntype()->is_void()) {
2237 result.append(&success);
2238 }
2239
2240 t_struct* xs = tfunction->get_xceptions();
2241 const vector<t_field*>& fields = xs->get_members();
2242 vector<t_field*>::const_iterator f_iter;
2243 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
2244 result.append(*f_iter);
2245 }
2246
2247 generate_java_struct_definition(f_service_, &result, false, true, true);
2248}
2249
2250/**
2251 * Generates a process function definition.
2252 *
2253 * @param tfunction The function to write a dispatcher for
2254 */
2255void t_javame_generator::generate_process_function(t_service* tservice, t_function* tfunction) {
2256 (void)tservice;
2257 // Open class
2258 indent(f_service_) << "private class " << tfunction->get_name() << " implements ProcessFunction {"
2259 << endl;
2260 indent_up();
2261
2262 // Open function
2263 indent(f_service_)
2264 << "public void process(int seqid, TProtocol iprot, TProtocol oprot) throws TException"
2265 << endl;
2266 scope_up(f_service_);
2267
2268 string argsname = tfunction->get_name() + "_args";
2269 string resultname = tfunction->get_name() + "_result";
2270
2271 f_service_ << indent() << argsname << " args = new " << argsname << "();" << endl << indent()
2272 << "try {" << endl;
2273 indent_up();
2274 f_service_ << indent() << "args.read(iprot);" << endl;
2275 indent_down();
2276 f_service_ << indent() << "} catch (TProtocolException e) {" << endl;
2277 indent_up();
2278 f_service_ << indent() << "iprot.readMessageEnd();" << endl << indent()
2279 << "TApplicationException x = new "
2280 "TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());"
2281 << endl << indent() << "oprot.writeMessageBegin(new TMessage(\""
2282 << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl << indent()
2283 << "x.write(oprot);" << endl << indent() << "oprot.writeMessageEnd();" << endl
2284 << indent() << "oprot.getTransport().flush();" << endl << indent() << "return;"
2285 << endl;
2286 indent_down();
2287 f_service_ << indent() << "}" << endl;
2288 f_service_ << indent() << "iprot.readMessageEnd();" << endl;
2289
2290 t_struct* xs = tfunction->get_xceptions();
2291 const std::vector<t_field*>& xceptions = xs->get_members();
2292 vector<t_field*>::const_iterator x_iter;
2293
2294 // Declare result for non oneway function
2295 if (!tfunction->is_oneway()) {
2296 f_service_ << indent() << resultname << " result = new " << resultname << "();" << endl;
2297 }
2298
2299 // Try block for a function with exceptions
2300 if (xceptions.size() > 0) {
2301 f_service_ << indent() << "try {" << endl;
2302 indent_up();
2303 }
2304
2305 // Generate the function call
2306 t_struct* arg_struct = tfunction->get_arglist();
2307 const std::vector<t_field*>& fields = arg_struct->get_members();
2308 vector<t_field*>::const_iterator f_iter;
2309
2310 f_service_ << indent();
2311 if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
2312 f_service_ << "result.success = ";
2313 }
2314 f_service_ << "iface_." << tfunction->get_name() << "(";
2315 bool first = true;
2316 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
2317 if (first) {
2318 first = false;
2319 } else {
2320 f_service_ << ", ";
2321 }
2322 f_service_ << "args." << (*f_iter)->get_name();
2323 }
2324 f_service_ << ");" << endl;
2325
2326 // Set isset on success field
2327 if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()
2328 && !type_can_be_null(tfunction->get_returntype())) {
2329 f_service_ << indent() << "result.set" << get_cap_name("success") << get_cap_name("isSet")
2330 << "(true);" << endl;
2331 }
2332
2333 if (!tfunction->is_oneway() && xceptions.size() > 0) {
2334 indent_down();
2335 f_service_ << indent() << "}";
2336 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
2337 f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " "
2338 << (*x_iter)->get_name() << ") {" << endl;
2339 if (!tfunction->is_oneway()) {
2340 indent_up();
2341 f_service_ << indent() << "result." << (*x_iter)->get_name() << " = "
2342 << (*x_iter)->get_name() << ";" << endl;
2343 indent_down();
2344 f_service_ << indent() << "}";
2345 } else {
2346 f_service_ << "}";
2347 }
2348 }
2349 f_service_ << " catch (Throwable th) {" << endl;
2350 indent_up();
2351 f_service_ << indent() << "TApplicationException x = new "
2352 "TApplicationException(TApplicationException.INTERNAL_ERROR, "
2353 "\"Internal error processing " << tfunction->get_name() << "\");"
2354 << endl << indent() << "oprot.writeMessageBegin(new TMessage(\""
2355 << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl
2356 << indent() << "x.write(oprot);" << endl << indent() << "oprot.writeMessageEnd();"
2357 << endl << indent() << "oprot.getTransport().flush();" << endl << indent()
2358 << "return;" << endl;
2359 indent_down();
2360 f_service_ << indent() << "}" << endl;
2361 }
2362
2363 // Shortcut out here for oneway functions
2364 if (tfunction->is_oneway()) {
2365 f_service_ << indent() << "return;" << endl;
2366 scope_down(f_service_);
2367
2368 // Close class
2369 indent_down();
2370 f_service_ << indent() << "}" << endl << endl;
2371 return;
2372 }
2373
2374 f_service_ << indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name()
2375 << "\", TMessageType.REPLY, seqid));" << endl << indent() << "result.write(oprot);"
2376 << endl << indent() << "oprot.writeMessageEnd();" << endl << indent()
2377 << "oprot.getTransport().flush();" << endl;
2378
2379 // Close function
2380 scope_down(f_service_);
2381 f_service_ << endl;
2382
2383 // Close class
2384 indent_down();
2385 f_service_ << indent() << "}" << endl << endl;
2386}
2387
2388/**
2389 * Deserializes a field of any type.
2390 *
2391 * @param tfield The field
2392 * @param prefix The variable name or container for this field
2393 */
2394void t_javame_generator::generate_deserialize_field(ostream& out, t_field* tfield, string prefix) {
2395 t_type* type = get_true_type(tfield->get_type());
2396
2397 if (type->is_void()) {
2398 throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
2399 }
2400
2401 string name = prefix + tfield->get_name();
2402
2403 if (type->is_struct() || type->is_xception()) {
2404 generate_deserialize_struct(out, (t_struct*)type, name);
2405 } else if (type->is_container()) {
2406 generate_deserialize_container(out, type, name);
2407 } else if (type->is_base_type()) {
2408 indent(out) << name << " = iprot.";
2409
2410 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
2411 switch (tbase) {
2412 case t_base_type::TYPE_VOID:
2413 throw "compiler error: cannot serialize void field in a struct: " + name;
2414 break;
2415 case t_base_type::TYPE_STRING:
2416 if (!type->is_binary()) {
2417 out << "readString();";
2418 } else {
2419 out << "readBinary();";
2420 }
2421 break;
2422 case t_base_type::TYPE_BOOL:
2423 out << "readBool();";
2424 break;
2425 case t_base_type::TYPE_I8:
2426 out << "readByte();";
2427 break;
2428 case t_base_type::TYPE_I16:
2429 out << "readI16();";
2430 break;
2431 case t_base_type::TYPE_I32:
2432 out << "readI32();";
2433 break;
2434 case t_base_type::TYPE_I64:
2435 out << "readI64();";
2436 break;
2437 case t_base_type::TYPE_DOUBLE:
2438 out << "readDouble();";
2439 break;
2440 default:
2441 throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
2442 }
2443 out << endl;
2444 } else if (type->is_enum()) {
2445 indent(out) << name << " = "
2446 << type_name(tfield->get_type(), true, false) + ".findByValue(iprot.readI32());"
2447 << endl;
2448 } else {
2449 printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
2450 tfield->get_name().c_str(),
2451 type_name(type).c_str());
2452 }
2453}
2454
2455/**
2456 * Generates an unserializer for a struct, invokes read()
2457 */
2458void t_javame_generator::generate_deserialize_struct(ostream& out,
2459 t_struct* tstruct,
2460 string prefix) {
2461 out << indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << indent()
2462 << prefix << ".read(iprot);" << endl;
2463}
2464
2465/**
2466 * Deserializes a container by reading its size and then iterating
2467 */
2468void t_javame_generator::generate_deserialize_container(ostream& out,
2469 t_type* ttype,
2470 string prefix) {
2471 scope_up(out);
2472
2473 string obj;
2474
2475 if (ttype->is_map()) {
2476 obj = tmp("_map");
2477 } else if (ttype->is_set()) {
2478 obj = tmp("_set");
2479 } else if (ttype->is_list()) {
2480 obj = tmp("_list");
2481 }
2482
2483 // Declare variables, read header
2484 if (ttype->is_map()) {
2485 indent(out) << "TMap " << obj << " = iprot.readMapBegin();" << endl;
2486 } else if (ttype->is_set()) {
2487 indent(out) << "TSet " << obj << " = iprot.readSetBegin();" << endl;
2488 } else if (ttype->is_list()) {
2489 indent(out) << "TList " << obj << " = iprot.readListBegin();" << endl;
2490 }
2491
2492 indent(out) << prefix << " = new " << type_name(ttype, false, true)
2493 // size the collection correctly
2494 << "(" << (ttype->is_list() ? "" : "2*") << obj << ".size"
2495 << ");" << endl;
2496
2497 // For loop iterates over elements
2498 string i = tmp("_i");
2499 indent(out) << "for (int " << i << " = 0; " << i << " < " << obj << ".size"
2500 << "; "
2501 << "++" << i << ")" << endl;
2502
2503 scope_up(out);
2504
2505 if (ttype->is_map()) {
2506 generate_deserialize_map_element(out, (t_map*)ttype, prefix);
2507 } else if (ttype->is_set()) {
2508 generate_deserialize_set_element(out, (t_set*)ttype, prefix);
2509 } else if (ttype->is_list()) {
2510 generate_deserialize_list_element(out, (t_list*)ttype, prefix);
2511 }
2512
2513 scope_down(out);
2514
2515 // Read container end
2516 if (ttype->is_map()) {
2517 indent(out) << "iprot.readMapEnd();" << endl;
2518 } else if (ttype->is_set()) {
2519 indent(out) << "iprot.readSetEnd();" << endl;
2520 } else if (ttype->is_list()) {
2521 indent(out) << "iprot.readListEnd();" << endl;
2522 }
2523
2524 scope_down(out);
2525}
2526
2527/**
2528 * Generates code to deserialize a map
2529 */
2530void t_javame_generator::generate_deserialize_map_element(ostream& out,
2531 t_map* tmap,
2532 string prefix) {
2533 string key = tmp("_key");
2534 string val = tmp("_val");
2535 t_field fkey(tmap->get_key_type(), key);
2536 t_field fval(tmap->get_val_type(), val);
2537
2538 indent(out) << declare_field(&fkey) << endl;
2539 indent(out) << declare_field(&fval) << endl;
2540
2541 generate_deserialize_field(out, &fkey);
2542 generate_deserialize_field(out, &fval);
2543
2544 indent(out) << prefix << ".put(" << box_type(tmap->get_key_type(), key) << ", "
2545 << box_type(tmap->get_val_type(), val) << ");" << endl;
2546}
2547
2548/**
2549 * Deserializes a set element
2550 */
2551void t_javame_generator::generate_deserialize_set_element(ostream& out,
2552 t_set* tset,
2553 string prefix) {
2554 string elem = tmp("_elem");
2555 t_field felem(tset->get_elem_type(), elem);
2556
2557 indent(out) << declare_field(&felem) << endl;
2558
2559 generate_deserialize_field(out, &felem);
2560
2561 indent(out) << prefix << ".put(" << box_type(tset->get_elem_type(), elem) << ", "
2562 << box_type(tset->get_elem_type(), elem) << ");" << endl;
2563}
2564
2565/**
2566 * Deserializes a list element
2567 */
2568void t_javame_generator::generate_deserialize_list_element(ostream& out,
2569 t_list* tlist,
2570 string prefix) {
2571 string elem = tmp("_elem");
2572 t_field felem(tlist->get_elem_type(), elem);
2573
2574 indent(out) << declare_field(&felem) << endl;
2575
2576 generate_deserialize_field(out, &felem);
2577
2578 indent(out) << prefix << ".addElement(" << box_type(tlist->get_elem_type(), elem) << ");" << endl;
2579}
2580
2581/**
2582 * Serializes a field of any type.
2583 *
2584 * @param tfield The field to serialize
2585 * @param prefix Name to prepend to field name
2586 */
2587void t_javame_generator::generate_serialize_field(ostream& out, t_field* tfield, string prefix) {
2588 t_type* type = get_true_type(tfield->get_type());
2589
2590 // Do nothing for void types
2591 if (type->is_void()) {
2592 throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
2593 }
2594
2595 if (type->is_struct() || type->is_xception()) {
2596 generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name());
2597 } else if (type->is_container()) {
2598 generate_serialize_container(out, type, prefix + tfield->get_name());
2599 } else if (type->is_enum()) {
2600 indent(out) << "oprot.writeI32(" << prefix + tfield->get_name() << ".getValue());" << endl;
2601 } else if (type->is_base_type()) {
2602 string name = prefix + tfield->get_name();
2603 indent(out) << "oprot.";
2604
2605 if (type->is_base_type()) {
2606 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
2607 switch (tbase) {
2608 case t_base_type::TYPE_VOID:
2609 throw "compiler error: cannot serialize void field in a struct: " + name;
2610 break;
2611 case t_base_type::TYPE_STRING:
2612 if (type->is_binary()) {
2613 out << "writeBinary(" << name << ");";
2614 } else {
2615 out << "writeString(" << name << ");";
2616 }
2617 break;
2618 case t_base_type::TYPE_BOOL:
2619 out << "writeBool(" << name << ");";
2620 break;
2621 case t_base_type::TYPE_I8:
2622 out << "writeByte(" << name << ");";
2623 break;
2624 case t_base_type::TYPE_I16:
2625 out << "writeI16(" << name << ");";
2626 break;
2627 case t_base_type::TYPE_I32:
2628 out << "writeI32(" << name << ");";
2629 break;
2630 case t_base_type::TYPE_I64:
2631 out << "writeI64(" << name << ");";
2632 break;
2633 case t_base_type::TYPE_DOUBLE:
2634 out << "writeDouble(" << name << ");";
2635 break;
2636 default:
2637 throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
2638 }
2639 } else if (type->is_enum()) {
2640 out << "writeI32(" << name << ");";
2641 }
2642 out << endl;
2643 } else {
2644 printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
2645 prefix.c_str(),
2646 tfield->get_name().c_str(),
2647 type_name(type).c_str());
2648 }
2649}
2650
2651/**
2652 * Serializes all the members of a struct.
2653 *
2654 * @param tstruct The struct to serialize
2655 * @param prefix String prefix to attach to all fields
2656 */
2657void t_javame_generator::generate_serialize_struct(ostream& out,
2658 t_struct* tstruct,
2659 string prefix) {
2660 (void)tstruct;
2661 out << indent() << prefix << ".write(oprot);" << endl;
2662}
2663
2664/**
2665 * Serializes a container by writing its size then the elements.
2666 *
2667 * @param ttype The type of container
2668 * @param prefix String prefix for fields
2669 */
2670void t_javame_generator::generate_serialize_container(ostream& out, t_type* ttype, string prefix) {
2671 scope_up(out);
2672
2673 if (ttype->is_map()) {
2674 indent(out) << "oprot.writeMapBegin(new TMap(" << type_to_enum(((t_map*)ttype)->get_key_type())
2675 << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << prefix
2676 << ".size()));" << endl;
2677 } else if (ttype->is_set()) {
2678 indent(out) << "oprot.writeSetBegin(new TSet(" << type_to_enum(((t_set*)ttype)->get_elem_type())
2679 << ", " << prefix << ".size()));" << endl;
2680 } else if (ttype->is_list()) {
2681 indent(out) << "oprot.writeListBegin(new TList("
2682 << type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << prefix << ".size()));"
2683 << endl;
2684 }
2685
2686 string iter = tmp("_iter");
2687 if (ttype->is_map()) {
2688 string enumer = iter + "_enum";
2689 string key_type = type_name(((t_map*)ttype)->get_key_type(), true, false);
2690 indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer
2691 << ".hasMoreElements(); ) ";
2692 scope_up(out);
2693 indent(out) << key_type << " " << iter << " = (" << key_type << ")" << enumer
2694 << ".nextElement();" << endl;
2695 } else if (ttype->is_set()) {
2696 string enumer = iter + "_enum";
2697 string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
2698 indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".keys(); " << enumer
2699 << ".hasMoreElements(); ) ";
2700 scope_up(out);
2701 indent(out) << ele_type << " " << iter << " = (" << ele_type << ")" << enumer
2702 << ".nextElement();" << endl;
2703 } else if (ttype->is_list()) {
2704 string enumer = iter + "_enum";
2705 indent(out) << "for (Enumeration " << enumer << " = " << prefix << ".elements(); " << enumer
2706 << ".hasMoreElements(); ) ";
2707 scope_up(out);
2708 string ele_type = type_name(((t_list*)ttype)->get_elem_type(), true);
2709 indent(out) << ele_type << " " << iter << " = (" << ele_type << ")" << enumer
2710 << ".nextElement();" << endl;
2711 }
2712
2713 if (ttype->is_map()) {
2714 generate_serialize_map_element(out, (t_map*)ttype, iter, prefix);
2715 } else if (ttype->is_set()) {
2716 generate_serialize_set_element(out, (t_set*)ttype, iter);
2717 } else if (ttype->is_list()) {
2718 generate_serialize_list_element(out, (t_list*)ttype, iter);
2719 }
2720 scope_down(out);
2721
2722 if (ttype->is_map()) {
2723 indent(out) << "oprot.writeMapEnd();" << endl;
2724 } else if (ttype->is_set()) {
2725 indent(out) << "oprot.writeSetEnd();" << endl;
2726 } else if (ttype->is_list()) {
2727 indent(out) << "oprot.writeListEnd();" << endl;
2728 }
2729
2730 scope_down(out);
2731}
2732
2733/**
2734 * Serializes the members of a map.
2735 */
2736void t_javame_generator::generate_serialize_map_element(ostream& out,
2737 t_map* tmap,
2738 string iter,
2739 string map) {
2740 t_field kfield(tmap->get_key_type(), iter);
2741 generate_serialize_field(out, &kfield, "");
2742 string val_type = type_name(tmap->get_val_type(), true, false);
2743 t_field vfield(tmap->get_val_type(), "((" + val_type + ")" + map + ".get(" + iter + "))");
2744 generate_serialize_field(out, &vfield, "");
2745}
2746
2747/**
2748 * Serializes the members of a set.
2749 */
2750void t_javame_generator::generate_serialize_set_element(ostream& out, t_set* tset, string iter) {
2751 t_field efield(tset->get_elem_type(), iter);
2752 generate_serialize_field(out, &efield, "");
2753}
2754
2755/**
2756 * Serializes the members of a list.
2757 */
2758void t_javame_generator::generate_serialize_list_element(ostream& out,
2759 t_list* tlist,
2760 string iter) {
2761 t_field efield(tlist->get_elem_type(), iter);
2762 generate_serialize_field(out, &efield, "");
2763}
2764
2765/**
2766 * Returns a Java type name
2767 *
2768 * @param ttype The type
2769 * @param container Is the type going inside a container?
2770 * @return Java type name, i.e. Vector
2771 */
2772string t_javame_generator::type_name(t_type* ttype,
2773 bool in_container,
2774 bool in_init,
2775 bool skip_generic) {
2776 (void)in_init;
2777 (void)skip_generic;
2778 // In Java typedefs are just resolved to their real type
2779 ttype = get_true_type(ttype);
2780 string prefix;
2781
2782 if (ttype->is_base_type()) {
2783 return base_type_name((t_base_type*)ttype, in_container);
2784 } else if (ttype->is_map()) {
2785 return "Hashtable";
2786 } else if (ttype->is_set()) {
2787 return "Hashtable";
2788 } else if (ttype->is_list()) {
2789 return "Vector";
2790 }
2791
2792 // Check for namespacing
2793 t_program* program = ttype->get_program();
2794 if (program != NULL && program != program_) {
2795 string package = program->get_namespace("java");
2796 if (!package.empty()) {
2797 return package + "." + ttype->get_name();
2798 }
2799 }
2800
2801 return ttype->get_name();
2802}
2803
2804/**
2805 * Returns the C++ type that corresponds to the thrift type.
2806 *
2807 * @param tbase The base type
2808 * @param container Is it going in a Java container?
2809 */
2810string t_javame_generator::base_type_name(t_base_type* type, bool in_container) {
2811 t_base_type::t_base tbase = type->get_base();
2812
2813 switch (tbase) {
2814 case t_base_type::TYPE_VOID:
2815 return "void";
2816 case t_base_type::TYPE_STRING:
2817 if (!type->is_binary()) {
2818 return "String";
2819 } else {
2820 return "byte[]";
2821 }
2822 case t_base_type::TYPE_BOOL:
2823 return (in_container ? "Boolean" : "boolean");
2824 case t_base_type::TYPE_I8:
2825 return (in_container ? "Byte" : "byte");
2826 case t_base_type::TYPE_I16:
2827 return (in_container ? "Short" : "short");
2828 case t_base_type::TYPE_I32:
2829 return (in_container ? "Integer" : "int");
2830 case t_base_type::TYPE_I64:
2831 return (in_container ? "Long" : "long");
2832 case t_base_type::TYPE_DOUBLE:
2833 return (in_container ? "Double" : "double");
2834 default:
2835 throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase);
2836 }
2837}
2838
2839/**
2840 * Declares a field, which may include initialization as necessary.
2841 *
2842 * @param ttype The type
2843 */
2844string t_javame_generator::declare_field(t_field* tfield, bool init) {
2845 // TODO(mcslee): do we ever need to initialize the field?
2846 string result = type_name(tfield->get_type()) + " " + tfield->get_name();
2847 if (init) {
2848 t_type* ttype = get_true_type(tfield->get_type());
2849 if (ttype->is_base_type() && tfield->get_value() != NULL) {
2850 std::ofstream dummy;
2851 result += " = " + render_const_value(dummy, tfield->get_name(), ttype, tfield->get_value());
2852 } else if (ttype->is_base_type()) {
2853 t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
2854 switch (tbase) {
2855 case t_base_type::TYPE_VOID:
2856 throw "NO T_VOID CONSTRUCT";
2857 case t_base_type::TYPE_STRING:
2858 result += " = null";
2859 break;
2860 case t_base_type::TYPE_BOOL:
2861 result += " = false";
2862 break;
2863 case t_base_type::TYPE_I8:
2864 case t_base_type::TYPE_I16:
2865 case t_base_type::TYPE_I32:
2866 case t_base_type::TYPE_I64:
2867 result += " = 0";
2868 break;
2869 case t_base_type::TYPE_DOUBLE:
2870 result += " = (double)0";
2871 break;
2872 }
2873
2874 } else if (ttype->is_enum()) {
2875 result += " = 0";
2876 } else if (ttype->is_container()) {
2877 result += " = new " + type_name(ttype, false, true) + "()";
2878 } else {
2879 result += " = new " + type_name(ttype, false, true) + "()";
2880 ;
2881 }
2882 }
2883 return result + ";";
2884}
2885
2886/**
2887 * Renders a function signature of the form 'type name(args)'
2888 *
2889 * @param tfunction Function definition
2890 * @return String of rendered function definition
2891 */
2892string t_javame_generator::function_signature(t_function* tfunction, string prefix) {
2893 t_type* ttype = tfunction->get_returntype();
2894 std::string result = type_name(ttype) + " " + prefix + tfunction->get_name() + "("
2895 + argument_list(tfunction->get_arglist()) + ") throws ";
2896 t_struct* xs = tfunction->get_xceptions();
2897 const std::vector<t_field*>& xceptions = xs->get_members();
2898 vector<t_field*>::const_iterator x_iter;
2899 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
2900 result += type_name((*x_iter)->get_type(), false, false) + ", ";
2901 }
2902 result += "TException";
2903 return result;
2904}
2905
2906/**
2907 * Renders a comma separated field list, with type names
2908 */
2909string t_javame_generator::argument_list(t_struct* tstruct, bool include_types) {
2910 string result = "";
2911
2912 const vector<t_field*>& fields = tstruct->get_members();
2913 vector<t_field*>::const_iterator f_iter;
2914 bool first = true;
2915 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
2916 if (first) {
2917 first = false;
2918 } else {
2919 result += ", ";
2920 }
2921 if (include_types) {
2922 result += type_name((*f_iter)->get_type()) + " ";
2923 }
2924 result += (*f_iter)->get_name();
2925 }
2926 return result;
2927}
2928
2929/**
2930 * Converts the parse type to a C++ enum string for the given type.
2931 */
2932string t_javame_generator::type_to_enum(t_type* type) {
2933 type = get_true_type(type);
2934
2935 if (type->is_base_type()) {
2936 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
2937 switch (tbase) {
2938 case t_base_type::TYPE_VOID:
2939 throw "NO T_VOID CONSTRUCT";
2940 case t_base_type::TYPE_STRING:
2941 return "TType.STRING";
2942 case t_base_type::TYPE_BOOL:
2943 return "TType.BOOL";
2944 case t_base_type::TYPE_I8:
2945 return "TType.BYTE";
2946 case t_base_type::TYPE_I16:
2947 return "TType.I16";
2948 case t_base_type::TYPE_I32:
2949 return "TType.I32";
2950 case t_base_type::TYPE_I64:
2951 return "TType.I64";
2952 case t_base_type::TYPE_DOUBLE:
2953 return "TType.DOUBLE";
2954 }
2955 } else if (type->is_enum()) {
2956 return "TType.I32";
2957 } else if (type->is_struct() || type->is_xception()) {
2958 return "TType.STRUCT";
2959 } else if (type->is_map()) {
2960 return "TType.MAP";
2961 } else if (type->is_set()) {
2962 return "TType.SET";
2963 } else if (type->is_list()) {
2964 return "TType.LIST";
2965 }
2966
2967 throw "INVALID TYPE IN type_to_enum: " + type->get_name();
2968}
2969
2970/**
2971 * Applies the correct style to a string based on the value of nocamel_style_
2972 */
2973std::string t_javame_generator::get_cap_name(std::string name) {
2974 name[0] = toupper(name[0]);
2975 return name;
2976}
2977
2978string t_javame_generator::constant_name(string name) {
2979 string constant_name;
2980
2981 bool is_first = true;
2982 bool was_previous_char_upper = false;
2983 for (char character : name) {
2984 bool is_upper = isupper(character);
2985
2986 if (is_upper && !is_first && !was_previous_char_upper) {
2987 constant_name += '_';
2988 }
2989 constant_name += toupper(character);
2990
2991 is_first = false;
2992 was_previous_char_upper = is_upper;
2993 }
2994
2995 return constant_name;
2996}
2997
2998void t_javame_generator::generate_java_docstring_comment(ostream& out, string contents) {
2999 generate_docstring_comment(out, "/**\n", " * ", contents, " */\n");
3000}
3001
3002void t_javame_generator::generate_java_doc(ostream& out, t_field* field) {
3003 if (field->get_type()->is_enum()) {
3004 string combined_message = field->get_doc() + "\n@see " + get_enum_class_name(field->get_type());
3005 generate_java_docstring_comment(out, combined_message);
3006 } else {
3007 generate_java_doc(out, (t_doc*)field);
3008 }
3009}
3010
3011/**
3012 * Emits a JavaDoc comment if the provided object has a doc in Thrift
3013 */
3014void t_javame_generator::generate_java_doc(ostream& out, t_doc* tdoc) {
3015 if (tdoc->has_doc()) {
3016 generate_java_docstring_comment(out, tdoc->get_doc());
3017 }
3018}
3019
3020/**
3021 * Emits a JavaDoc comment if the provided function object has a doc in Thrift
3022 */
3023void t_javame_generator::generate_java_doc(ostream& out, t_function* tfunction) {
3024 if (tfunction->has_doc()) {
3025 stringstream ss;
3026 ss << tfunction->get_doc();
3027 const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
3028 vector<t_field*>::const_iterator p_iter;
3029 for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
3030 t_field* p = *p_iter;
3031 ss << "\n@param " << p->get_name();
3032 if (p->has_doc()) {
3033 ss << " " << p->get_doc();
3034 }
3035 }
3036 generate_docstring_comment(out, "/**\n", " * ", ss.str(), " */\n");
3037 }
3038}
3039
3040void t_javame_generator::generate_deep_copy_container(ostream& out,
3041 std::string source_name_p1,
3042 std::string source_name_p2,
3043 std::string result_name,
3044 t_type* type) {
3045
3046 t_container* container = (t_container*)type;
3047 std::string source_name;
3048 if (source_name_p2 == "")
3049 source_name = source_name_p1;
3050 else
3051 source_name = source_name_p1 + "." + source_name_p2;
3052
3053 indent(out) << type_name(type, true, false) << " " << result_name << " = new "
3054 << type_name(container, false, true) << "();" << endl;
3055
3056 std::string iterator_element_name = source_name_p1 + "_element";
3057 std::string enumeration_name = source_name_p1 + "_enum";
3058 std::string result_element_name = result_name + "_copy";
3059
3060 if (container->is_map()) {
3061 t_type* key_type = ((t_map*)container)->get_key_type();
3062 t_type* val_type = ((t_map*)container)->get_val_type();
3063
3064 indent(out) << "for (Enumeration " << enumeration_name << " = " << source_name << ".keys(); "
3065 << enumeration_name << ".hasMoreElements(); ) {" << endl;
3066 indent_up();
3067
3068 out << endl;
3069
3070 indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = ("
3071 << type_name(key_type, true, false) << ")" << enumeration_name << ".nextElement();"
3072 << endl;
3073 indent(out) << type_name(val_type, true, false) << " " << iterator_element_name << "_value = ("
3074 << type_name(val_type, true, false) << ")" << source_name << ".get("
3075 << iterator_element_name << "_key);" << endl;
3076
3077 out << endl;
3078
3079 if (key_type->is_container()) {
3080 generate_deep_copy_container(out,
3081 iterator_element_name + "_key",
3082 "",
3083 result_element_name + "_key",
3084 key_type);
3085 } else {
3086 indent(out) << type_name(key_type, true, false) << " " << result_element_name << "_key = ";
3087 generate_deep_copy_non_container(out,
3088 iterator_element_name + "_key",
3089 result_element_name + "_key",
3090 key_type);
3091 out << ";" << endl;
3092 }
3093
3094 out << endl;
3095
3096 if (val_type->is_container()) {
3097 generate_deep_copy_container(out,
3098 iterator_element_name + "_value",
3099 "",
3100 result_element_name + "_value",
3101 val_type);
3102 } else {
3103 indent(out) << type_name(val_type, true, false) << " " << result_element_name << "_value = ";
3104 generate_deep_copy_non_container(out,
3105 iterator_element_name + "_value",
3106 result_element_name + "_value",
3107 val_type);
3108 out << ";" << endl;
3109 }
3110
3111 out << endl;
3112
3113 indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name
3114 << "_value);" << endl;
3115
3116 indent_down();
3117 indent(out) << "}" << endl;
3118
3119 } else {
3120 t_type* elem_type;
3121
3122 if (container->is_set()) {
3123 elem_type = ((t_set*)container)->get_elem_type();
3124 } else {
3125 elem_type = ((t_list*)container)->get_elem_type();
3126 }
3127
3128 indent(out) << "for (Enumeration " << enumeration_name << " = " << source_name
3129 << ".elements(); " << enumeration_name << ".hasMoreElements(); ) {" << endl;
3130 indent_up();
3131 indent(out) << type_name(elem_type, true, false) << " " << iterator_element_name << " = ("
3132 << type_name(elem_type, true, false) << ")" << enumeration_name << ".nextElement();"
3133 << endl;
3134 if (elem_type->is_container()) {
3135 // recursive deep copy
3136 generate_deep_copy_container(out, iterator_element_name, "", result_element_name, elem_type);
3137 if (elem_type->is_list()) {
3138 indent(out) << result_name << ".addElement(" << result_element_name << ");" << endl;
3139 } else {
3140 indent(out) << result_name << ".put(" << result_element_name << ", " << result_element_name
3141 << ");" << endl;
3142 }
3143 } else {
3144 // iterative copy
3145 if (elem_type->is_binary()) {
3146 indent(out) << type_name(elem_type, true, false) << " temp_binary_element = ";
3147 generate_deep_copy_non_container(out,
3148 iterator_element_name,
3149 "temp_binary_element",
3150 elem_type);
3151 out << ";" << endl;
3152 if (elem_type->is_list()) {
3153 indent(out) << result_name << ".addElement(temp_binary_element);" << endl;
3154 } else {
3155 indent(out) << result_name << ".put(temp_binary_element, temp_binary_element);" << endl;
3156 }
3157 } else {
3158 indent(out) << result_name << ".addElement(";
3159 generate_deep_copy_non_container(out, iterator_element_name, result_name, elem_type);
3160 out << ");" << endl;
3161 }
3162 }
3163
3164 indent_down();
3165
3166 indent(out) << "}" << endl;
3167 }
3168}
3169
3170void t_javame_generator::generate_deep_copy_non_container(ostream& out,
3171 std::string source_name,
3172 std::string dest_name,
3173 t_type* type) {
3174 if (type->is_base_type() || type->is_enum() || type->is_typedef()) {
3175 // binary fields need to be copied with System.arraycopy
3176 if (type->is_binary()) {
3177 out << "new byte[" << source_name << ".length];" << endl;
3178 indent(out) << "System.arraycopy(" << source_name << ", 0, " << dest_name << ", 0, "
3179 << source_name << ".length)";
3180 }
3181 // everything else can be copied directly
3182 else
3183 out << source_name;
3184 } else {
3185 out << "new " << type_name(type, true, true) << "(" << source_name << ")";
3186 }
3187}
3188
3189std::string t_javame_generator::generate_isset_check(t_field* field) {
3190 return generate_isset_check(field->get_name());
3191}
3192
3193std::string t_javame_generator::isset_field_id(t_field* field) {
3194 return "__" + upcase_string(field->get_name() + "_isset_id");
3195}
3196
3197std::string t_javame_generator::generate_isset_check(std::string field_name) {
3198 return "is" + get_cap_name("set") + get_cap_name(field_name) + "()";
3199}
3200
3201void t_javame_generator::generate_isset_set(ostream& out, t_field* field) {
3202 if (!type_can_be_null(field->get_type())) {
3203 indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);"
3204 << endl;
3205 }
3206}
3207
3208std::string t_javame_generator::get_enum_class_name(t_type* type) {
3209 string package = "";
3210 t_program* program = type->get_program();
3211 if (program != NULL && program != program_) {
3212 package = program->get_namespace("java") + ".";
3213 }
3214 return package + type->get_name();
3215}
3216
3217void t_javame_generator::generate_struct_desc(ostream& out, t_struct* tstruct) {
3218 indent(out) << "private static final TStruct STRUCT_DESC = new TStruct(\"" << tstruct->get_name()
3219 << "\");" << endl;
3220}
3221
3222void t_javame_generator::generate_field_descs(ostream& out, t_struct* tstruct) {
3223 const vector<t_field*>& members = tstruct->get_members();
3224 vector<t_field*>::const_iterator m_iter;
3225
3226 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3227 indent(out) << "private static final TField " << constant_name((*m_iter)->get_name())
3228 << "_FIELD_DESC = new TField(\"" << (*m_iter)->get_name() << "\", "
3229 << type_to_enum((*m_iter)->get_type()) << ", "
3230 << "(short)" << (*m_iter)->get_key() << ");" << endl;
3231 }
3232}
3233
3234bool t_javame_generator::has_bit_vector(t_struct* tstruct) {
3235 const vector<t_field*>& members = tstruct->get_members();
3236 vector<t_field*>::const_iterator m_iter;
3237
3238 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3239 if (!type_can_be_null(get_true_type((*m_iter)->get_type()))) {
3240 return true;
3241 }
3242 }
3243 return false;
3244}
3245
3246void t_javame_generator::generate_java_struct_clear(std::ostream& out, t_struct* tstruct) {
3247 indent(out) << "public void clear() {" << endl;
3248
3249 const vector<t_field*>& members = tstruct->get_members();
3250 vector<t_field*>::const_iterator m_iter;
3251
3252 indent_up();
3253 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3254 t_type* t = get_true_type((*m_iter)->get_type());
3255 if ((*m_iter)->get_value() != NULL) {
3256 print_const_value(out,
3257 "this." + (*m_iter)->get_name(),
3258 t,
3259 (*m_iter)->get_value(),
3260 true,
3261 true);
3262 } else {
3263 if (type_can_be_null(t)) {
3264 indent(out) << "this." << (*m_iter)->get_name() << " = null;" << endl;
3265 } else {
3266 // must be a base type
3267 // means it also needs to be explicitly unset
3268 indent(out) << "set" << get_cap_name((*m_iter)->get_name()) << get_cap_name("isSet")
3269 << "(false);" << endl;
3270 switch (((t_base_type*)t)->get_base()) {
3271 case t_base_type::TYPE_I8:
3272 case t_base_type::TYPE_I16:
3273 case t_base_type::TYPE_I32:
3274 case t_base_type::TYPE_I64:
3275 indent(out) << "this." << (*m_iter)->get_name() << " = 0;" << endl;
3276 break;
3277 case t_base_type::TYPE_DOUBLE:
3278 indent(out) << "this." << (*m_iter)->get_name() << " = 0.0;" << endl;
3279 break;
3280 case t_base_type::TYPE_BOOL:
3281 indent(out) << "this." << (*m_iter)->get_name() << " = false;" << endl;
3282 break;
3283 default: // prevent gcc compiler warning
3284 break;
3285 }
3286 }
3287 }
3288 }
3289 indent_down();
3290
3291 indent(out) << "}" << endl << endl;
3292}
3293
3294THRIFT_REGISTER_GENERATOR(javame, "Java ME", "")