forked from apache/thrift
/
t_java_generator.cc
4006 lines (3414 loc) · 141 KB
/
t_java_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#include <sys/stat.h>
#include <stdexcept>
#include "platform.h"
#include "t_oop_generator.h"
using namespace std;
/**
* Java code generator.
*
*/
class t_java_generator : public t_oop_generator {
public:
t_java_generator(
t_program* program,
const std::map<std::string, std::string>& parsed_options,
const std::string& option_string)
: t_oop_generator(program)
{
(void) option_string;
std::map<std::string, std::string>::const_iterator iter;
iter = parsed_options.find("beans");
bean_style_ = (iter != parsed_options.end());
iter = parsed_options.find("private-members");
private_members_ = (iter != parsed_options.end());
iter = parsed_options.find("nocamel");
nocamel_style_ = (iter != parsed_options.end());
iter = parsed_options.find("hashcode");
gen_hash_code_ = (iter != parsed_options.end());
out_dir_base_ = (bean_style_ ? "gen-javabean" : "gen-java");
}
/**
* Init and close methods
*/
void init_generator();
void close_generator();
void generate_consts(std::vector<t_const*> consts);
/**
* Program-level generation functions
*/
void generate_typedef (t_typedef* ttypedef);
void generate_enum (t_enum* tenum);
void generate_struct (t_struct* tstruct);
void generate_union (t_struct* tunion);
void generate_xception(t_struct* txception);
void generate_service (t_service* tservice);
void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false);
std::string render_const_value(std::ofstream& out, t_type* type, t_const_value* value);
/**
* Service-level generation functions
*/
void generate_java_struct(t_struct* tstruct, bool is_exception);
void generate_java_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false);
void generate_java_struct_equality(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_compare_to(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_java_validator(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_result_writer(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_tostring(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_clear(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_write_object(std::ofstream& out, t_struct* tstruct);
void generate_java_struct_read_object(std::ofstream& out, t_struct* tstruct);
void generate_java_meta_data_map(std::ofstream& out, t_struct* tstruct);
void generate_field_value_meta_data(std::ofstream& out, t_type* type);
std::string get_java_type_string(t_type* type);
void generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct);
void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
void generate_generic_isset_method(std::ofstream& out, t_struct* tstruct);
void generate_java_bean_boilerplate(std::ofstream& out, t_struct* tstruct);
void generate_function_helpers(t_function* tfunction);
std::string get_cap_name(std::string name);
std::string generate_isset_check(t_field* field);
std::string generate_isset_check(std::string field);
void generate_isset_set(ofstream& out, t_field* field);
std::string isset_field_id(t_field* field);
void generate_service_interface (t_service* tservice);
void generate_service_async_interface(t_service* tservice);
void generate_service_helpers (t_service* tservice);
void generate_service_client (t_service* tservice);
void generate_service_async_client(t_service* tservice);
void generate_service_server (t_service* tservice);
void generate_process_function (t_service* tservice, t_function* tfunction);
void generate_java_union(t_struct* tstruct);
void generate_union_constructor(ofstream& out, t_struct* tstruct);
void generate_union_getters_and_setters(ofstream& out, t_struct* tstruct);
void generate_union_abstract_methods(ofstream& out, t_struct* tstruct);
void generate_check_type(ofstream& out, t_struct* tstruct);
void generate_read_value(ofstream& out, t_struct* tstruct);
void generate_write_value(ofstream& out, t_struct* tstruct);
void generate_get_field_desc(ofstream& out, t_struct* tstruct);
void generate_get_struct_desc(ofstream& out, t_struct* tstruct);
void generate_get_field_name(ofstream& out, t_struct* tstruct);
void generate_union_comparisons(ofstream& out, t_struct* tstruct);
void generate_union_hashcode(ofstream& out, t_struct* tstruct);
/**
* Serialization constructs
*/
void generate_deserialize_field (std::ofstream& out,
t_field* tfield,
std::string prefix="");
void generate_deserialize_struct (std::ofstream& out,
t_struct* tstruct,
std::string prefix="");
void generate_deserialize_container (std::ofstream& out,
t_type* ttype,
std::string prefix="");
void generate_deserialize_set_element (std::ofstream& out,
t_set* tset,
std::string prefix="");
void generate_deserialize_map_element (std::ofstream& out,
t_map* tmap,
std::string prefix="");
void generate_deserialize_list_element (std::ofstream& out,
t_list* tlist,
std::string prefix="");
void generate_serialize_field (std::ofstream& out,
t_field* tfield,
std::string prefix="");
void generate_serialize_struct (std::ofstream& out,
t_struct* tstruct,
std::string prefix="");
void generate_serialize_container (std::ofstream& out,
t_type* ttype,
std::string prefix="");
void generate_serialize_map_element (std::ofstream& out,
t_map* tmap,
std::string iter,
std::string map);
void generate_serialize_set_element (std::ofstream& out,
t_set* tmap,
std::string iter);
void generate_serialize_list_element (std::ofstream& out,
t_list* tlist,
std::string iter);
void generate_java_doc (std::ofstream& out,
t_field* field);
void generate_java_doc (std::ofstream& out,
t_doc* tdoc);
void generate_java_doc (std::ofstream& out,
t_function* tdoc);
void generate_java_docstring_comment (std::ofstream &out,
string contents);
void generate_deep_copy_container(std::ofstream& out, std::string source_name_p1, std::string source_name_p2, std::string result_name, t_type* type);
void generate_deep_copy_non_container(std::ofstream& out, std::string source_name, std::string dest_name, t_type* type);
bool has_bit_vector(t_struct* tstruct);
/**
* Helper rendering functions
*/
std::string java_package();
std::string java_type_imports();
std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false, bool skip_generic=false);
std::string base_type_name(t_base_type* tbase, bool in_container=false);
std::string declare_field(t_field* tfield, bool init=false);
std::string function_signature(t_function* tfunction, std::string prefix="");
std::string function_signature_async(t_function* tfunction, bool use_base_method = false, std::string prefix="");
std::string argument_list(t_struct* tstruct, bool include_types = true);
std::string async_function_call_arglist(t_function* tfunc, bool use_base_method = true, bool include_types = true);
std::string async_argument_list(t_function* tfunct, t_struct* tstruct, t_type* ttype, bool include_types=false);
std::string type_to_enum(t_type* ttype);
std::string get_enum_class_name(t_type* type);
void generate_struct_desc(ofstream& out, t_struct* tstruct);
void generate_field_descs(ofstream& out, t_struct* tstruct);
void generate_field_name_constants(ofstream& out, t_struct* tstruct);
bool type_can_be_null(t_type* ttype) {
ttype = get_true_type(ttype);
return
ttype->is_container() ||
ttype->is_struct() ||
ttype->is_xception() ||
ttype->is_string() ||
ttype->is_enum();
}
std::string constant_name(std::string name);
private:
/**
* File streams
*/
std::string package_name_;
std::ofstream f_service_;
std::string package_dir_;
bool bean_style_;
bool private_members_;
bool nocamel_style_;
bool gen_hash_code_;
};
/**
* Prepares for file generation by opening up the necessary file output
* streams.
*
* @param tprogram The program to generate
*/
void t_java_generator::init_generator() {
// Make output directory
MKDIR(get_out_dir().c_str());
package_name_ = program_->get_namespace("java");
string dir = package_name_;
string subdir = get_out_dir();
string::size_type loc;
while ((loc = dir.find(".")) != string::npos) {
subdir = subdir + "/" + dir.substr(0, loc);
MKDIR(subdir.c_str());
dir = dir.substr(loc+1);
}
if (dir.size() > 0) {
subdir = subdir + "/" + dir;
MKDIR(subdir.c_str());
}
package_dir_ = subdir;
}
/**
* Packages the generated file
*
* @return String of the package, i.e. "package org.apache.thriftdemo;"
*/
string t_java_generator::java_package() {
if (!package_name_.empty()) {
return string("package ") + package_name_ + ";\n\n";
}
return "";
}
/**
* Prints standard java imports
*
* @return List of imports for Java types that are used in here
*/
string t_java_generator::java_type_imports() {
string hash_builder;
if (gen_hash_code_) {
hash_builder = "import org.apache.commons.lang.builder.HashCodeBuilder;\n";
}
return
string() +
hash_builder +
"import java.util.List;\n" +
"import java.util.ArrayList;\n" +
"import java.util.Map;\n" +
"import java.util.HashMap;\n" +
"import java.util.EnumMap;\n" +
"import java.util.Set;\n" +
"import java.util.HashSet;\n" +
"import java.util.EnumSet;\n" +
"import java.util.Collections;\n" +
"import java.util.BitSet;\n" +
"import java.nio.ByteBuffer;\n"
"import java.util.Arrays;\n" +
"import org.slf4j.Logger;\n" +
"import org.slf4j.LoggerFactory;\n\n";
}
/**
* Nothing in Java
*/
void t_java_generator::close_generator() {}
/**
* Generates a typedef. This is not done in Java, since it does
* not support arbitrary name replacements, and it'd be a wacky waste
* of overhead to make wrapper classes.
*
* @param ttypedef The type definition
*/
void t_java_generator::generate_typedef(t_typedef* ttypedef) {
(void) ttypedef;
}
/**
* Enums are a class with a set of static constants.
*
* @param tenum The enumeration
*/
void t_java_generator::generate_enum(t_enum* tenum) {
// Make output file
string f_enum_name = package_dir_+"/"+(tenum->get_name())+".java";
ofstream f_enum;
f_enum.open(f_enum_name.c_str());
// Comment and package it
f_enum <<
autogen_comment() <<
java_package() << endl;
// Add java imports
f_enum << string() +
"import java.util.Map;\n" +
"import java.util.HashMap;\n" +
"import org.apache.thrift.TEnum;" << endl << endl;
generate_java_doc(f_enum, tenum);
indent(f_enum) <<
"public enum " << tenum->get_name() << " implements org.apache.thrift.TEnum ";
scope_up(f_enum);
vector<t_enum_value*> constants = tenum->get_constants();
vector<t_enum_value*>::iterator c_iter;
bool first = true;
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
if (first) {
first = false;
} else {
f_enum << "," << endl;
}
generate_java_doc(f_enum, *c_iter);
indent(f_enum) << (*c_iter)->get_name() << "(" << value << ")";
}
f_enum << ";" << endl << endl;
// Field for thriftCode
indent(f_enum) << "private final int value;" << endl << endl;
indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl;
indent(f_enum) << " this.value = value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " */" << endl;
indent(f_enum) << "public int getValue() {" << endl;
indent(f_enum) << " return value;" <<endl;
indent(f_enum) << "}" << endl << endl;
indent(f_enum) << "/**" << endl;
indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL." << endl;
indent(f_enum) << " * @return null if the value is not found." << endl;
indent(f_enum) << " */" << endl;
indent(f_enum) << "public static "+ tenum->get_name() + " findByValue(int value) { " << endl;
indent_up();
indent(f_enum) << "switch (value) {" << endl;
indent_up();
for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
int value = (*c_iter)->get_value();
indent(f_enum) << "case " << value << ":" << endl;
indent(f_enum) << " return " << (*c_iter)->get_name() << ";" << endl;
}
indent(f_enum) << "default:" << endl;
indent(f_enum) << " return null;" << endl;
indent_down();
indent(f_enum) << "}" << endl;
indent_down();
indent(f_enum) << "}" << endl;
scope_down(f_enum);
f_enum.close();
}
/**
* Generates a class that holds all the constants.
*/
void t_java_generator::generate_consts(std::vector<t_const*> consts) {
if (consts.empty()) {
return;
}
string f_consts_name = package_dir_+"/Constants.java";
ofstream f_consts;
f_consts.open(f_consts_name.c_str());
// Print header
f_consts <<
autogen_comment() <<
java_package() <<
java_type_imports();
f_consts <<
"public class Constants {" << endl <<
endl;
indent_up();
vector<t_const*>::iterator c_iter;
for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
generate_java_doc(f_consts, (*c_iter));
print_const_value(f_consts,
(*c_iter)->get_name(),
(*c_iter)->get_type(),
(*c_iter)->get_value(),
false);
}
indent_down();
indent(f_consts) <<
"}" << endl;
f_consts.close();
}
/**
* Prints the value of a constant with the given type. Note that type checking
* is NOT performed in this function as it is always run beforehand using the
* validate_types method in main.cc
*/
void t_java_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) {
type = get_true_type(type);
indent(out);
if (!defval) {
out <<
(in_static ? "" : "public static final ") <<
type_name(type) << " ";
}
if (type->is_base_type()) {
string v2 = render_const_value(out, type, value);
out << name << " = " << v2 << ";" << endl << endl;
} else if (type->is_enum()) {
out << name << " = " << render_const_value(out, type, value) << ";" << endl << endl;
} else if (type->is_struct() || type->is_xception()) {
const vector<t_field*>& fields = ((t_struct*)type)->get_members();
vector<t_field*>::const_iterator f_iter;
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
out << name << " = new " << type_name(type, false, true) << "();" << endl;
if (!in_static) {
indent(out) << "static {" << endl;
indent_up();
}
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
t_type* field_type = NULL;
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
if ((*f_iter)->get_name() == v_iter->first->get_string()) {
field_type = (*f_iter)->get_type();
}
}
if (field_type == NULL) {
throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
}
string val = render_const_value(out, field_type, v_iter->second);
indent(out) << name << ".";
std::string cap_name = get_cap_name(v_iter->first->get_string());
out << "set" << cap_name << "(" << val << ");" << endl;
}
if (!in_static) {
indent_down();
indent(out) << "}" << endl;
}
out << endl;
} else if (type->is_map()) {
out << name << " = new " << type_name(type, false, true) << "();" << endl;
if (!in_static) {
indent(out) << "static {" << endl;
indent_up();
}
t_type* ktype = ((t_map*)type)->get_key_type();
t_type* vtype = ((t_map*)type)->get_val_type();
const map<t_const_value*, t_const_value*>& val = value->get_map();
map<t_const_value*, t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string key = render_const_value(out, ktype, v_iter->first);
string val = render_const_value(out, vtype, v_iter->second);
indent(out) << name << ".put(" << key << ", " << val << ");" << endl;
}
if (!in_static) {
indent_down();
indent(out) << "}" << endl;
}
out << endl;
} else if (type->is_list() || type->is_set()) {
out << name << " = new " << type_name(type, false, true) << "();" << endl;
if (!in_static) {
indent(out) << "static {" << endl;
indent_up();
}
t_type* etype;
if (type->is_list()) {
etype = ((t_list*)type)->get_elem_type();
} else {
etype = ((t_set*)type)->get_elem_type();
}
const vector<t_const_value*>& val = value->get_list();
vector<t_const_value*>::const_iterator v_iter;
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
string val = render_const_value(out, etype, *v_iter);
indent(out) << name << ".add(" << val << ");" << endl;
}
if (!in_static) {
indent_down();
indent(out) << "}" << endl;
}
out << endl;
} else {
throw "compiler error: no const of type " + type->get_name();
}
}
string t_java_generator::render_const_value(ofstream& out, t_type* type, t_const_value* value) {
type = get_true_type(type);
std::ostringstream render;
if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
switch (tbase) {
case t_base_type::TYPE_STRING:
render << '"' << get_escaped_string(value) << '"';
break;
case t_base_type::TYPE_BOOL:
render << ((value->get_integer() > 0) ? "true" : "false");
break;
case t_base_type::TYPE_BYTE:
render << "(byte)" << value->get_integer();
break;
case t_base_type::TYPE_I16:
render << "(short)" << value->get_integer();
break;
case t_base_type::TYPE_I32:
render << value->get_integer();
break;
case t_base_type::TYPE_I64:
render << value->get_integer() << "L";
break;
case t_base_type::TYPE_DOUBLE:
if (value->get_type() == t_const_value::CV_INTEGER) {
render << "(double)" << value->get_integer();
} else {
render << value->get_double();
}
break;
default:
throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
}
} else if (type->is_enum()) {
render << type->get_program()->get_namespace("java") << "." << value->get_identifier_with_parent();
} else {
string t = tmp("tmp");
print_const_value(out, t, type, value, true);
render << t;
}
return render.str();
}
/**
* Generates a struct definition for a thrift data type. This will be a org.apache.thrift.TBase
* implementor.
*
* @param tstruct The struct definition
*/
void t_java_generator::generate_struct(t_struct* tstruct) {
if (tstruct->is_union()) {
generate_java_union(tstruct);
} else {
generate_java_struct(tstruct, false);
}
}
/**
* Exceptions are structs, but they inherit from Exception
*
* @param tstruct The struct definition
*/
void t_java_generator::generate_xception(t_struct* txception) {
generate_java_struct(txception, true);
}
/**
* Java struct definition.
*
* @param tstruct The struct definition
*/
void t_java_generator::generate_java_struct(t_struct* tstruct,
bool is_exception) {
// Make output file
string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
f_struct <<
autogen_comment() <<
java_package() <<
java_type_imports();
generate_java_struct_definition(f_struct,
tstruct,
is_exception);
f_struct.close();
}
/**
* Java union definition.
*
* @param tstruct The struct definition
*/
void t_java_generator::generate_java_union(t_struct* tstruct) {
// Make output file
string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java";
ofstream f_struct;
f_struct.open(f_struct_name.c_str());
f_struct <<
autogen_comment() <<
java_package() <<
java_type_imports();
generate_java_doc(f_struct, tstruct);
bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end());
indent(f_struct) <<
"public " << (is_final ? "final " : "") << "class " << tstruct->get_name()
<< " extends org.apache.thrift.TUnion<" << tstruct->get_name() << ", " << tstruct->get_name() << "._Fields> ";
scope_up(f_struct);
generate_struct_desc(f_struct, tstruct);
generate_field_descs(f_struct, tstruct);
f_struct << endl;
generate_field_name_constants(f_struct, tstruct);
f_struct << endl;
generate_java_meta_data_map(f_struct, tstruct);
generate_union_constructor(f_struct, tstruct);
f_struct << endl;
generate_union_abstract_methods(f_struct, tstruct);
f_struct << endl;
generate_java_struct_field_by_id(f_struct, tstruct);
f_struct << endl;
generate_union_getters_and_setters(f_struct, tstruct);
f_struct << endl;
generate_union_comparisons(f_struct, tstruct);
f_struct << endl;
generate_union_hashcode(f_struct, tstruct);
f_struct << endl;
generate_java_struct_write_object(f_struct, tstruct);
f_struct << endl;
generate_java_struct_read_object(f_struct, tstruct);
f_struct << endl;
scope_down(f_struct);
f_struct.close();
}
void t_java_generator::generate_union_constructor(ofstream& out, t_struct* tstruct) {
indent(out) << "public " << type_name(tstruct) << "() {" << endl;
indent(out) << " super();" << endl;
indent(out) << "}" << endl << endl;
indent(out) << "public " << type_name(tstruct) << "(_Fields setField, Object value) {" << endl;
indent(out) << " super(setField, value);" << endl;
indent(out) << "}" << endl << endl;
indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {" << endl;
indent(out) << " super(other);" << endl;
indent(out) << "}" << endl;
indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl;
indent(out) << " return new " << tstruct->get_name() << "(this);" << endl;
indent(out) << "}" << endl << endl;
// generate "constructors" for each field
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_type* type = (*m_iter)->get_type();
indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(" << type_name(type) << " value) {" << endl;
indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl;
indent(out) << " return x;" << endl;
indent(out) << "}" << endl << endl;
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(byte[] value) {" << endl;
indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl;
indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(ByteBuffer.wrap(value));" << endl;
indent(out) << " return x;" << endl;
indent(out) << "}" << endl << endl;
}
}
}
void t_java_generator::generate_union_getters_and_setters(ofstream& out, t_struct* tstruct) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
bool first = true;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (first) {
first = false;
} else {
out << endl;
}
t_field* field = (*m_iter);
t_type* type = field->get_type();
std::string cap_name = get_cap_name(field->get_name());
generate_java_doc(out, field);
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public byte[] get" << cap_name << "() {" << endl;
indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(buffer" << get_cap_name("for") << cap_name << "()));" << endl;
indent(out) << " ByteBuffer b = buffer" << get_cap_name("for") << cap_name << "();" << endl;
indent(out) << " return b == null ? null : b.array();" << endl;
indent(out) << "}" << endl;
out << endl;
indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << get_cap_name(field->get_name()) << "() {" << endl;
indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
indent(out) << " return (ByteBuffer)getFieldValue();" << endl;
indent(out) << " } else {" << endl;
indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
<< "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl;
} else {
indent(out) << "public " << type_name(field->get_type()) << " get" << get_cap_name(field->get_name()) << "() {" << endl;
indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl;
indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();" << endl;
indent(out) << " } else {" << endl;
indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name()
<< "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl;
indent(out) << " }" << endl;
indent(out) << "}" << endl;
}
out << endl;
generate_java_doc(out, field);
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public void set" << get_cap_name(field->get_name()) << "(byte[] value) {" << endl;
indent(out) << " set" << get_cap_name(field->get_name()) << "(ByteBuffer.wrap(value));" << endl;
indent(out) << "}" << endl;
out << endl;
}
indent(out) << "public void set" << get_cap_name(field->get_name()) << "(" << type_name(field->get_type()) << " value) {" << endl;
if (type_can_be_null(field->get_type())) {
indent(out) << " if (value == null) throw new NullPointerException();" << endl;
}
indent(out) << " setField_ = _Fields." << constant_name(field->get_name()) << ";" << endl;
indent(out) << " value_ = value;" << endl;
indent(out) << "}" << endl;
}
}
void t_java_generator::generate_union_abstract_methods(ofstream& out, t_struct* tstruct) {
generate_check_type(out, tstruct);
out << endl;
generate_read_value(out, tstruct);
out << endl;
generate_write_value(out, tstruct);
out << endl;
generate_get_field_desc(out, tstruct);
out << endl;
generate_get_struct_desc(out, tstruct);
out << endl;
indent(out) << "@Override" << endl;
indent(out) << "protected _Fields enumForId(short id) {" << endl;
indent(out) << " return _Fields.findByThriftIdOrThrow(id);" << endl;
indent(out) << "}" << endl;
}
void t_java_generator::generate_check_type(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
indent(out) << "protected void checkType(_Fields setField, Object value) throws ClassCastException {" << endl;
indent_up();
indent(out) << "switch (setField) {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true) << ") {" << endl;
indent(out) << " break;" << endl;
indent(out) << " }" << endl;
indent(out) << " throw new ClassCastException(\"Was expecting value of type "
<< type_name(field->get_type(), true, false) << " for field '" << field->get_name()
<< "', but got \" + value.getClass().getSimpleName());" << endl;
// do the real check here
}
indent(out) << "default:" << endl;
indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
indent(out) << "}" << endl;
}
void t_java_generator::generate_read_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
indent(out) << "protected Object readValue(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TField field) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "_Fields setField = _Fields.findByThriftId(field.id);" << endl;
indent(out) << "if (setField != null) {" << endl;
indent_up();
indent(out) << "switch (setField) {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {" << endl;
indent_up();
indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl;
generate_deserialize_field(out, field, "");
indent(out) << "return " << field->get_name() << ";" << endl;
indent_down();
indent(out) << "} else {" << endl;
indent(out) << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type);" << endl;
indent(out) << " return null;" << endl;
indent(out) << "}" << endl;
indent_down();
}
indent(out) << "default:" << endl;
indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
indent(out) << "} else {" << endl;
indent_up();
indent(out) << "org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type);" << endl;
indent(out) << "return null;" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
indent(out) << "}" << endl;
}
void t_java_generator::generate_write_value(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
indent(out) << "protected void writeValue(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl;
indent_up();
indent(out) << "switch (setField_) {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent_up();
indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name()
<< " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl;
generate_serialize_field(out, field, "");
indent(out) << "return;" << endl;
indent_down();
}
indent(out) << "default:" << endl;
indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl;
indent_down();
indent(out) << "}" << endl;
indent_down();
indent(out) << "}" << endl;
}
void t_java_generator::generate_get_field_desc(ofstream& out, t_struct* tstruct) {
indent(out) << "@Override" << endl;
indent(out) << "protected org.apache.thrift.protocol.TField getFieldDesc(_Fields setField) {" << endl;
indent_up();
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
indent(out) << "switch (setField) {" << endl;
indent_up();
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
t_field* field = (*m_iter);
indent(out) << "case " << constant_name(field->get_name()) << ":" << endl;
indent(out) << " return " << constant_name(field->get_name()) << "_FIELD_DESC;" << endl;
}