David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 20 | #include <sstream> |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <vector> |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 25 | #include <cctype> |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 26 | |
| 27 | #include <sys/stat.h> |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 28 | #include <stdexcept> |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 29 | |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 30 | #include "platform.h" |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 31 | #include "t_oop_generator.h" |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 32 | using namespace std; |
| 33 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 34 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Java code generator. |
| 37 | * |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 38 | */ |
| 39 | class t_java_generator : public t_oop_generator { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 40 | public: |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 41 | t_java_generator( |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 42 | t_program* program, |
| 43 | const std::map<std::string, std::string>& parsed_options, |
| 44 | const std::string& option_string) |
| 45 | : t_oop_generator(program) |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 46 | { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 47 | (void) option_string; |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 48 | std::map<std::string, std::string>::const_iterator iter; |
| 49 | |
| 50 | iter = parsed_options.find("beans"); |
| 51 | bean_style_ = (iter != parsed_options.end()); |
| 52 | |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 53 | iter = parsed_options.find("private-members"); |
| 54 | private_members_ = (iter != parsed_options.end()); |
| 55 | |
David Reiss | 70e329f | 2008-07-14 19:35:50 +0000 | [diff] [blame] | 56 | iter = parsed_options.find("nocamel"); |
| 57 | nocamel_style_ = (iter != parsed_options.end()); |
| 58 | |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 59 | iter = parsed_options.find("hashcode"); |
| 60 | gen_hash_code_ = (iter != parsed_options.end()); |
| 61 | |
Roger Meier | 6b38cf6 | 2011-04-16 20:05:51 +0000 | [diff] [blame] | 62 | iter = parsed_options.find("android_legacy"); |
| 63 | android_legacy_ = (iter != parsed_options.end()); |
| 64 | |
Bryan Duxbury | 9a42239 | 2011-05-23 21:41:02 +0000 | [diff] [blame] | 65 | iter = parsed_options.find("java5"); |
| 66 | java5_ = (iter != parsed_options.end()); |
| 67 | if (java5_) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 68 | android_legacy_ = true; |
Bryan Duxbury | 9a42239 | 2011-05-23 21:41:02 +0000 | [diff] [blame] | 69 | } |
| 70 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 71 | out_dir_base_ = (bean_style_ ? "gen-javabean" : "gen-java"); |
| 72 | } |
| 73 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 74 | /** |
| 75 | * Init and close methods |
| 76 | */ |
| 77 | |
| 78 | void init_generator(); |
| 79 | void close_generator(); |
| 80 | |
| 81 | void generate_consts(std::vector<t_const*> consts); |
| 82 | |
| 83 | /** |
| 84 | * Program-level generation functions |
| 85 | */ |
| 86 | |
| 87 | void generate_typedef (t_typedef* ttypedef); |
| 88 | void generate_enum (t_enum* tenum); |
| 89 | void generate_struct (t_struct* tstruct); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 90 | void generate_union (t_struct* tunion); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 91 | void generate_xception(t_struct* txception); |
| 92 | void generate_service (t_service* tservice); |
| 93 | |
| 94 | void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value, bool in_static, bool defval=false); |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 95 | std::string render_const_value(std::ofstream& out, t_type* type, t_const_value* value); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Service-level generation functions |
| 99 | */ |
| 100 | |
| 101 | void generate_java_struct(t_struct* tstruct, bool is_exception); |
| 102 | |
| 103 | void generate_java_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool in_class=false, bool is_result=false); |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 104 | void generate_java_struct_equality(std::ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 684b4f9 | 2009-07-31 00:12:21 +0000 | [diff] [blame] | 105 | void generate_java_struct_compare_to(std::ofstream& out, t_struct* tstruct); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 106 | void generate_java_struct_reader(std::ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 107 | void generate_java_validator(std::ofstream& out, t_struct* tstruct); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 108 | void generate_java_struct_result_writer(std::ofstream& out, t_struct* tstruct); |
| 109 | void generate_java_struct_writer(std::ofstream& out, t_struct* tstruct); |
| 110 | void generate_java_struct_tostring(std::ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 111 | void generate_java_struct_clear(std::ofstream& out, t_struct* tstruct); |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 112 | void generate_java_struct_write_object(std::ofstream& out, t_struct* tstruct); |
| 113 | void generate_java_struct_read_object(std::ofstream& out, t_struct* tstruct); |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 114 | void generate_java_meta_data_map(std::ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 115 | void generate_field_value_meta_data(std::ofstream& out, t_type* type); |
| 116 | std::string get_java_type_string(t_type* type); |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 117 | void generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct); |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 118 | void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name); |
| 119 | void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name); |
| 120 | void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct); |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 121 | void generate_generic_isset_method(std::ofstream& out, t_struct* tstruct); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 122 | void generate_java_bean_boilerplate(std::ofstream& out, t_struct* tstruct); |
| 123 | |
| 124 | void generate_function_helpers(t_function* tfunction); |
David Reiss | 2db737d | 2009-01-05 21:02:55 +0000 | [diff] [blame] | 125 | std::string get_cap_name(std::string name); |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 126 | std::string generate_isset_check(t_field* field); |
Bryan Duxbury | e3c3d19 | 2009-03-07 03:08:37 +0000 | [diff] [blame] | 127 | std::string generate_isset_check(std::string field); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 128 | void generate_isset_set(ofstream& out, t_field* field, std::string prefix); |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 129 | std::string isset_field_id(t_field* field); |
| 130 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 131 | void generate_service_interface (t_service* tservice); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 132 | void generate_service_async_interface(t_service* tservice); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 133 | void generate_service_helpers (t_service* tservice); |
| 134 | void generate_service_client (t_service* tservice); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 135 | void generate_service_async_client(t_service* tservice); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 136 | void generate_service_server (t_service* tservice); |
| 137 | void generate_process_function (t_service* tservice, t_function* tfunction); |
| 138 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 139 | void generate_java_union(t_struct* tstruct); |
| 140 | void generate_union_constructor(ofstream& out, t_struct* tstruct); |
| 141 | void generate_union_getters_and_setters(ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 63c2d37 | 2011-06-08 17:46:55 +0000 | [diff] [blame] | 142 | void generate_union_is_set_methods(ofstream& out, t_struct* tstruct); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 143 | void generate_union_abstract_methods(ofstream& out, t_struct* tstruct); |
| 144 | void generate_check_type(ofstream& out, t_struct* tstruct); |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 145 | void generate_standard_scheme_read_value(ofstream& out, t_struct* tstruct); |
| 146 | void generate_standard_scheme_write_value(ofstream& out, t_struct* tstruct); |
| 147 | void generate_tuple_scheme_read_value(ofstream& out, t_struct* tstruct); |
| 148 | void generate_tuple_scheme_write_value(ofstream& out, t_struct* tstruct); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 149 | void generate_get_field_desc(ofstream& out, t_struct* tstruct); |
| 150 | void generate_get_struct_desc(ofstream& out, t_struct* tstruct); |
| 151 | void generate_get_field_name(ofstream& out, t_struct* tstruct); |
| 152 | |
| 153 | void generate_union_comparisons(ofstream& out, t_struct* tstruct); |
| 154 | void generate_union_hashcode(ofstream& out, t_struct* tstruct); |
| 155 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 156 | void generate_scheme_map(ofstream& out, t_struct* tstruct); |
| 157 | void generate_standard_writer(ofstream& out, t_struct* tstruct); |
| 158 | void generate_standard_reader(ofstream& out, t_struct* tstruct); |
| 159 | void generate_java_struct_standard_scheme(ofstream& out, t_struct* tstruct); |
| 160 | |
| 161 | void generate_java_struct_tuple_scheme(ofstream& out, t_struct* tstruct); |
| 162 | void generate_java_struct_tuple_reader(ofstream& out, t_struct* tstruct); |
| 163 | void generate_java_struct_tuple_writer(ofstream& out, t_struct* tstruct); |
| 164 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 165 | /** |
| 166 | * Serialization constructs |
| 167 | */ |
| 168 | |
| 169 | void generate_deserialize_field (std::ofstream& out, |
| 170 | t_field* tfield, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 171 | std::string prefix="", |
| 172 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 173 | |
| 174 | void generate_deserialize_struct (std::ofstream& out, |
| 175 | t_struct* tstruct, |
| 176 | std::string prefix=""); |
| 177 | |
| 178 | void generate_deserialize_container (std::ofstream& out, |
| 179 | t_type* ttype, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 180 | std::string prefix="", |
| 181 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 182 | |
| 183 | void generate_deserialize_set_element (std::ofstream& out, |
| 184 | t_set* tset, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 185 | std::string prefix="", |
| 186 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 187 | |
| 188 | void generate_deserialize_map_element (std::ofstream& out, |
| 189 | t_map* tmap, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 190 | std::string prefix="", |
| 191 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 192 | |
| 193 | void generate_deserialize_list_element (std::ofstream& out, |
| 194 | t_list* tlist, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 195 | std::string prefix="", |
| 196 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 197 | |
| 198 | void generate_serialize_field (std::ofstream& out, |
| 199 | t_field* tfield, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 200 | std::string prefix="", |
| 201 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 202 | |
| 203 | void generate_serialize_struct (std::ofstream& out, |
| 204 | t_struct* tstruct, |
| 205 | std::string prefix=""); |
| 206 | |
| 207 | void generate_serialize_container (std::ofstream& out, |
| 208 | t_type* ttype, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 209 | std::string prefix="", |
| 210 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 211 | |
| 212 | void generate_serialize_map_element (std::ofstream& out, |
| 213 | t_map* tmap, |
| 214 | std::string iter, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 215 | std::string map, |
| 216 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 217 | |
| 218 | void generate_serialize_set_element (std::ofstream& out, |
| 219 | t_set* tmap, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 220 | std::string iter, |
| 221 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 222 | |
| 223 | void generate_serialize_list_element (std::ofstream& out, |
| 224 | t_list* tlist, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 225 | std::string iter, |
| 226 | bool has_metadata = true); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 227 | |
| 228 | void generate_java_doc (std::ofstream& out, |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 229 | t_field* field); |
| 230 | |
| 231 | void generate_java_doc (std::ofstream& out, |
| 232 | t_doc* tdoc); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 233 | |
David Reiss | 7bcf866 | 2009-01-06 20:54:27 +0000 | [diff] [blame] | 234 | void generate_java_doc (std::ofstream& out, |
| 235 | t_function* tdoc); |
| 236 | |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 237 | void generate_java_docstring_comment (std::ofstream &out, |
| 238 | string contents); |
| 239 | |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 240 | 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); |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 241 | void generate_deep_copy_non_container(std::ofstream& out, std::string source_name, std::string dest_name, t_type* type); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 242 | |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 243 | bool has_bit_vector(t_struct* tstruct); |
| 244 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 245 | /** |
| 246 | * Helper rendering functions |
| 247 | */ |
| 248 | |
| 249 | std::string java_package(); |
| 250 | std::string java_type_imports(); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 251 | std::string type_name(t_type* ttype, bool in_container=false, bool in_init=false, bool skip_generic=false); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 252 | std::string base_type_name(t_base_type* tbase, bool in_container=false); |
| 253 | std::string declare_field(t_field* tfield, bool init=false); |
| 254 | std::string function_signature(t_function* tfunction, std::string prefix=""); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 255 | std::string function_signature_async(t_function* tfunction, bool use_base_method = false, std::string prefix=""); |
| 256 | std::string argument_list(t_struct* tstruct, bool include_types = true); |
| 257 | std::string async_function_call_arglist(t_function* tfunc, bool use_base_method = true, bool include_types = true); |
| 258 | std::string async_argument_list(t_function* tfunct, t_struct* tstruct, t_type* ttype, bool include_types=false); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 259 | std::string type_to_enum(t_type* ttype); |
Bryan Duxbury | 3696d64 | 2009-03-18 03:14:56 +0000 | [diff] [blame] | 260 | std::string get_enum_class_name(t_type* type); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 261 | void generate_struct_desc(ofstream& out, t_struct* tstruct); |
| 262 | void generate_field_descs(ofstream& out, t_struct* tstruct); |
| 263 | void generate_field_name_constants(ofstream& out, t_struct* tstruct); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 264 | |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 265 | bool type_can_be_null(t_type* ttype) { |
| 266 | ttype = get_true_type(ttype); |
| 267 | |
| 268 | return |
| 269 | ttype->is_container() || |
| 270 | ttype->is_struct() || |
| 271 | ttype->is_xception() || |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 272 | ttype->is_string() || |
| 273 | ttype->is_enum(); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 276 | std::string constant_name(std::string name); |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 277 | |
| 278 | private: |
| 279 | |
| 280 | /** |
| 281 | * File streams |
| 282 | */ |
| 283 | |
| 284 | std::string package_name_; |
| 285 | std::ofstream f_service_; |
| 286 | std::string package_dir_; |
| 287 | |
| 288 | bool bean_style_; |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 289 | bool private_members_; |
David Reiss | 70e329f | 2008-07-14 19:35:50 +0000 | [diff] [blame] | 290 | bool nocamel_style_; |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 291 | bool gen_hash_code_; |
Roger Meier | 6b38cf6 | 2011-04-16 20:05:51 +0000 | [diff] [blame] | 292 | bool android_legacy_; |
Bryan Duxbury | 9a42239 | 2011-05-23 21:41:02 +0000 | [diff] [blame] | 293 | bool java5_; |
David Reiss | dc61be9 | 2008-02-27 01:55:09 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 297 | /** |
| 298 | * Prepares for file generation by opening up the necessary file output |
| 299 | * streams. |
| 300 | * |
| 301 | * @param tprogram The program to generate |
| 302 | */ |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 303 | void t_java_generator::init_generator() { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 304 | // Make output directory |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 305 | MKDIR(get_out_dir().c_str()); |
David Reiss | 771f8c7 | 2008-02-27 01:55:25 +0000 | [diff] [blame] | 306 | package_name_ = program_->get_namespace("java"); |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 307 | |
| 308 | string dir = package_name_; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 309 | string subdir = get_out_dir(); |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 310 | string::size_type loc; |
| 311 | while ((loc = dir.find(".")) != string::npos) { |
| 312 | subdir = subdir + "/" + dir.substr(0, loc); |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 313 | MKDIR(subdir.c_str()); |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 314 | dir = dir.substr(loc+1); |
| 315 | } |
| 316 | if (dir.size() > 0) { |
| 317 | subdir = subdir + "/" + dir; |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 318 | MKDIR(subdir.c_str()); |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | package_dir_ = subdir; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Packages the generated file |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 326 | * |
Bryan Duxbury | afa80ea | 2009-01-15 23:47:51 +0000 | [diff] [blame] | 327 | * @return String of the package, i.e. "package org.apache.thriftdemo;" |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 328 | */ |
| 329 | string t_java_generator::java_package() { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 330 | if (!package_name_.empty()) { |
| 331 | return string("package ") + package_name_ + ";\n\n"; |
| 332 | } |
| 333 | return ""; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Prints standard java imports |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 338 | * |
| 339 | * @return List of imports for Java types that are used in here |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 340 | */ |
| 341 | string t_java_generator::java_type_imports() { |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 342 | string hash_builder; |
| 343 | if (gen_hash_code_) { |
| 344 | hash_builder = "import org.apache.commons.lang.builder.HashCodeBuilder;\n"; |
| 345 | } |
| 346 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 347 | return |
| 348 | string() + |
Bryan Duxbury | 2ad9eb7 | 2009-03-24 00:43:33 +0000 | [diff] [blame] | 349 | hash_builder + |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 350 | "import org.apache.thrift.scheme.IScheme;\n" + |
| 351 | "import org.apache.thrift.scheme.SchemeFactory;\n" + |
| 352 | "import org.apache.thrift.scheme.StandardScheme;\n\n" + |
| 353 | "import org.apache.thrift.scheme.TupleScheme;\n" + |
| 354 | "import org.apache.thrift.protocol.TTupleProtocol;\n" + |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 355 | "import java.util.List;\n" + |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 356 | "import java.util.ArrayList;\n" + |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 357 | "import java.util.Map;\n" + |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 358 | "import java.util.HashMap;\n" + |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 359 | "import java.util.EnumMap;\n" + |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 360 | "import java.util.Set;\n" + |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 361 | "import java.util.HashSet;\n" + |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 362 | "import java.util.EnumSet;\n" + |
Bryan Duxbury | ef73b0e | 2009-06-17 16:43:25 +0000 | [diff] [blame] | 363 | "import java.util.Collections;\n" + |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 364 | "import java.util.BitSet;\n" + |
Bryan Duxbury | f5abd26 | 2010-08-06 00:18:25 +0000 | [diff] [blame] | 365 | "import java.nio.ByteBuffer;\n" |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 366 | "import java.util.Arrays;\n" + |
Bryan Duxbury | c27cda5 | 2009-08-14 20:04:15 +0000 | [diff] [blame] | 367 | "import org.slf4j.Logger;\n" + |
| 368 | "import org.slf4j.LoggerFactory;\n\n"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 372 | * Nothing in Java |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 373 | */ |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 374 | void t_java_generator::close_generator() {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 375 | |
| 376 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 377 | * Generates a typedef. This is not done in Java, since it does |
| 378 | * not support arbitrary name replacements, and it'd be a wacky waste |
| 379 | * of overhead to make wrapper classes. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 380 | * |
| 381 | * @param ttypedef The type definition |
| 382 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 383 | void t_java_generator::generate_typedef(t_typedef* ttypedef) { |
| 384 | (void) ttypedef; |
| 385 | } |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 386 | |
| 387 | /** |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 388 | * Enums are a class with a set of static constants. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 389 | * |
| 390 | * @param tenum The enumeration |
| 391 | */ |
| 392 | void t_java_generator::generate_enum(t_enum* tenum) { |
| 393 | // Make output file |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 394 | string f_enum_name = package_dir_+"/"+(tenum->get_name())+".java"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 395 | ofstream f_enum; |
| 396 | f_enum.open(f_enum_name.c_str()); |
| 397 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 398 | // Comment and package it |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 399 | f_enum << |
| 400 | autogen_comment() << |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 401 | java_package() << endl; |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 402 | |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 403 | // Add java imports |
| 404 | f_enum << string() + |
Bryan Duxbury | 3696d64 | 2009-03-18 03:14:56 +0000 | [diff] [blame] | 405 | "import java.util.Map;\n" + |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 406 | "import java.util.HashMap;\n" + |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 407 | "import org.apache.thrift.TEnum;" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 408 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 409 | generate_java_doc(f_enum, tenum); |
| 410 | indent(f_enum) << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 411 | "public enum " << tenum->get_name() << " implements org.apache.thrift.TEnum "; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 412 | scope_up(f_enum); |
| 413 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 414 | vector<t_enum_value*> constants = tenum->get_constants(); |
| 415 | vector<t_enum_value*>::iterator c_iter; |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 416 | bool first = true; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 417 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
Bryan Duxbury | a406b90 | 2010-09-27 23:37:44 +0000 | [diff] [blame] | 418 | int value = (*c_iter)->get_value(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 419 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 420 | if (first) { |
| 421 | first = false; |
| 422 | } else { |
| 423 | f_enum << "," << endl; |
| 424 | } |
| 425 | |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 426 | generate_java_doc(f_enum, *c_iter); |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 427 | indent(f_enum) << (*c_iter)->get_name() << "(" << value << ")"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 428 | } |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 429 | f_enum << ";" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 430 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 431 | // Field for thriftCode |
| 432 | indent(f_enum) << "private final int value;" << endl << endl; |
| 433 | |
| 434 | indent(f_enum) << "private " << tenum->get_name() << "(int value) {" << endl; |
| 435 | indent(f_enum) << " this.value = value;" <<endl; |
| 436 | indent(f_enum) << "}" << endl << endl; |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 437 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 438 | indent(f_enum) << "/**" << endl; |
| 439 | indent(f_enum) << " * Get the integer value of this enum value, as defined in the Thrift IDL." << endl; |
| 440 | indent(f_enum) << " */" << endl; |
| 441 | indent(f_enum) << "public int getValue() {" << endl; |
| 442 | indent(f_enum) << " return value;" <<endl; |
| 443 | indent(f_enum) << "}" << endl << endl; |
| 444 | |
| 445 | indent(f_enum) << "/**" << endl; |
| 446 | indent(f_enum) << " * Find a the enum type by its integer value, as defined in the Thrift IDL." << endl; |
| 447 | indent(f_enum) << " * @return null if the value is not found." << endl; |
| 448 | indent(f_enum) << " */" << endl; |
| 449 | indent(f_enum) << "public static "+ tenum->get_name() + " findByValue(int value) { " << endl; |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 450 | |
| 451 | indent_up(); |
| 452 | |
| 453 | indent(f_enum) << "switch (value) {" << endl; |
| 454 | indent_up(); |
| 455 | |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 456 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { |
Bryan Duxbury | a406b90 | 2010-09-27 23:37:44 +0000 | [diff] [blame] | 457 | int value = (*c_iter)->get_value(); |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 458 | indent(f_enum) << "case " << value << ":" << endl; |
| 459 | indent(f_enum) << " return " << (*c_iter)->get_name() << ";" << endl; |
| 460 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 461 | |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 462 | indent(f_enum) << "default:" << endl; |
| 463 | indent(f_enum) << " return null;" << endl; |
| 464 | |
| 465 | indent_down(); |
| 466 | |
| 467 | indent(f_enum) << "}" << endl; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 468 | |
Bryan Duxbury | c4ad9be | 2010-01-16 09:13:20 +0000 | [diff] [blame] | 469 | indent_down(); |
| 470 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 471 | indent(f_enum) << "}" << endl; |
| 472 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 473 | scope_down(f_enum); |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 474 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 475 | f_enum.close(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 479 | * Generates a class that holds all the constants. |
| 480 | */ |
| 481 | void t_java_generator::generate_consts(std::vector<t_const*> consts) { |
David Reiss | a1e0a6f | 2008-06-10 22:56:43 +0000 | [diff] [blame] | 482 | if (consts.empty()) { |
| 483 | return; |
| 484 | } |
| 485 | |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 486 | string f_consts_name = package_dir_+"/Constants.java"; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 487 | ofstream f_consts; |
| 488 | f_consts.open(f_consts_name.c_str()); |
| 489 | |
| 490 | // Print header |
| 491 | f_consts << |
| 492 | autogen_comment() << |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 493 | java_package() << |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 494 | java_type_imports(); |
| 495 | |
| 496 | f_consts << |
| 497 | "public class Constants {" << endl << |
| 498 | endl; |
| 499 | indent_up(); |
| 500 | vector<t_const*>::iterator c_iter; |
| 501 | for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { |
Bryan Duxbury | d4235e0 | 2010-09-13 16:28:53 +0000 | [diff] [blame] | 502 | generate_java_doc(f_consts, (*c_iter)); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 503 | print_const_value(f_consts, |
| 504 | (*c_iter)->get_name(), |
| 505 | (*c_iter)->get_type(), |
| 506 | (*c_iter)->get_value(), |
| 507 | false); |
| 508 | } |
| 509 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 510 | indent(f_consts) << "}" << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 511 | f_consts.close(); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /** |
| 516 | * Prints the value of a constant with the given type. Note that type checking |
| 517 | * is NOT performed in this function as it is always run beforehand using the |
| 518 | * validate_types method in main.cc |
| 519 | */ |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 520 | void t_java_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) { |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 521 | type = get_true_type(type); |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 522 | |
| 523 | indent(out); |
| 524 | if (!defval) { |
| 525 | out << |
| 526 | (in_static ? "" : "public static final ") << |
| 527 | type_name(type) << " "; |
| 528 | } |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 529 | if (type->is_base_type()) { |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 530 | string v2 = render_const_value(out, type, value); |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 531 | out << name << " = " << v2 << ";" << endl << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 532 | } else if (type->is_enum()) { |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 533 | out << name << " = " << render_const_value(out, type, value) << ";" << endl << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 534 | } else if (type->is_struct() || type->is_xception()) { |
| 535 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 536 | vector<t_field*>::const_iterator f_iter; |
| 537 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 538 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
Mark Slee | dd43eb8 | 2008-02-13 22:03:15 +0000 | [diff] [blame] | 539 | out << name << " = new " << type_name(type, false, true) << "();" << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 540 | if (!in_static) { |
| 541 | indent(out) << "static {" << endl; |
| 542 | indent_up(); |
| 543 | } |
| 544 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 545 | t_type* field_type = NULL; |
| 546 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 547 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 548 | field_type = (*f_iter)->get_type(); |
| 549 | } |
| 550 | } |
| 551 | if (field_type == NULL) { |
| 552 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 553 | } |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 554 | string val = render_const_value(out, field_type, v_iter->second); |
David Reiss | 87e9ac6 | 2008-06-10 22:56:34 +0000 | [diff] [blame] | 555 | indent(out) << name << "."; |
David Reiss | 2bcf399 | 2009-04-07 20:51:37 +0000 | [diff] [blame] | 556 | std::string cap_name = get_cap_name(v_iter->first->get_string()); |
| 557 | out << "set" << cap_name << "(" << val << ");" << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 558 | } |
| 559 | if (!in_static) { |
| 560 | indent_down(); |
| 561 | indent(out) << "}" << endl; |
| 562 | } |
| 563 | out << endl; |
| 564 | } else if (type->is_map()) { |
Mark Slee | dd43eb8 | 2008-02-13 22:03:15 +0000 | [diff] [blame] | 565 | out << name << " = new " << type_name(type, false, true) << "();" << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 566 | if (!in_static) { |
| 567 | indent(out) << "static {" << endl; |
| 568 | indent_up(); |
| 569 | } |
| 570 | t_type* ktype = ((t_map*)type)->get_key_type(); |
| 571 | t_type* vtype = ((t_map*)type)->get_val_type(); |
| 572 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 573 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 574 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 575 | string key = render_const_value(out, ktype, v_iter->first); |
| 576 | string val = render_const_value(out, vtype, v_iter->second); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 577 | indent(out) << name << ".put(" << key << ", " << val << ");" << endl; |
| 578 | } |
| 579 | if (!in_static) { |
| 580 | indent_down(); |
| 581 | indent(out) << "}" << endl; |
| 582 | } |
| 583 | out << endl; |
| 584 | } else if (type->is_list() || type->is_set()) { |
Mark Slee | dd43eb8 | 2008-02-13 22:03:15 +0000 | [diff] [blame] | 585 | out << name << " = new " << type_name(type, false, true) << "();" << endl; |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 586 | if (!in_static) { |
| 587 | indent(out) << "static {" << endl; |
| 588 | indent_up(); |
| 589 | } |
| 590 | t_type* etype; |
| 591 | if (type->is_list()) { |
| 592 | etype = ((t_list*)type)->get_elem_type(); |
| 593 | } else { |
| 594 | etype = ((t_set*)type)->get_elem_type(); |
| 595 | } |
| 596 | const vector<t_const_value*>& val = value->get_list(); |
| 597 | vector<t_const_value*>::const_iterator v_iter; |
| 598 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 599 | string val = render_const_value(out, etype, *v_iter); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 600 | indent(out) << name << ".add(" << val << ");" << endl; |
| 601 | } |
| 602 | if (!in_static) { |
| 603 | indent_down(); |
| 604 | indent(out) << "}" << endl; |
| 605 | } |
| 606 | out << endl; |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 607 | } else { |
| 608 | throw "compiler error: no const of type " + type->get_name(); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 612 | string t_java_generator::render_const_value(ofstream& out, t_type* type, t_const_value* value) { |
David Reiss | 9a4edfa | 2008-05-01 05:52:50 +0000 | [diff] [blame] | 613 | type = get_true_type(type); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 614 | std::ostringstream render; |
| 615 | |
| 616 | if (type->is_base_type()) { |
| 617 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 618 | switch (tbase) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 619 | case t_base_type::TYPE_STRING: |
| 620 | render << '"' << get_escaped_string(value) << '"'; |
| 621 | break; |
| 622 | case t_base_type::TYPE_BOOL: |
| 623 | render << ((value->get_integer() > 0) ? "true" : "false"); |
| 624 | break; |
| 625 | case t_base_type::TYPE_BYTE: |
| 626 | render << "(byte)" << value->get_integer(); |
| 627 | break; |
| 628 | case t_base_type::TYPE_I16: |
| 629 | render << "(short)" << value->get_integer(); |
| 630 | break; |
| 631 | case t_base_type::TYPE_I32: |
| 632 | render << value->get_integer(); |
| 633 | break; |
| 634 | case t_base_type::TYPE_I64: |
| 635 | render << value->get_integer() << "L"; |
| 636 | break; |
| 637 | case t_base_type::TYPE_DOUBLE: |
| 638 | if (value->get_type() == t_const_value::CV_INTEGER) { |
| 639 | render << "(double)" << value->get_integer(); |
| 640 | } else { |
| 641 | render << value->get_double(); |
| 642 | } |
| 643 | break; |
| 644 | default: |
| 645 | throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 646 | } |
| 647 | } else if (type->is_enum()) { |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 648 | render << type->get_program()->get_namespace("java") << "." << value->get_identifier_with_parent(); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame] | 649 | } else { |
| 650 | string t = tmp("tmp"); |
| 651 | print_const_value(out, t, type, value, true); |
| 652 | render << t; |
| 653 | } |
| 654 | |
| 655 | return render.str(); |
| 656 | } |
| 657 | |
| 658 | /** |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 659 | * Generates a struct definition for a thrift data type. This will be a org.apache.thrift.TBase |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 660 | * implementor. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 661 | * |
| 662 | * @param tstruct The struct definition |
| 663 | */ |
| 664 | void t_java_generator::generate_struct(t_struct* tstruct) { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 665 | if (tstruct->is_union()) { |
| 666 | generate_java_union(tstruct); |
| 667 | } else { |
| 668 | generate_java_struct(tstruct, false); |
| 669 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 672 | /** |
| 673 | * Exceptions are structs, but they inherit from Exception |
| 674 | * |
| 675 | * @param tstruct The struct definition |
| 676 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 677 | void t_java_generator::generate_xception(t_struct* txception) { |
| 678 | generate_java_struct(txception, true); |
| 679 | } |
| 680 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 681 | |
| 682 | /** |
| 683 | * Java struct definition. |
| 684 | * |
| 685 | * @param tstruct The struct definition |
| 686 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 687 | void t_java_generator::generate_java_struct(t_struct* tstruct, |
| 688 | bool is_exception) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 689 | // Make output file |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 690 | string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 691 | ofstream f_struct; |
| 692 | f_struct.open(f_struct_name.c_str()); |
| 693 | |
| 694 | f_struct << |
| 695 | autogen_comment() << |
| 696 | java_package() << |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 697 | java_type_imports(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 698 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 699 | generate_java_struct_definition(f_struct, |
| 700 | tstruct, |
| 701 | is_exception); |
| 702 | f_struct.close(); |
| 703 | } |
| 704 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 705 | /** |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 706 | * Java union definition. |
| 707 | * |
| 708 | * @param tstruct The struct definition |
| 709 | */ |
| 710 | void t_java_generator::generate_java_union(t_struct* tstruct) { |
| 711 | // Make output file |
| 712 | string f_struct_name = package_dir_+"/"+(tstruct->get_name())+".java"; |
| 713 | ofstream f_struct; |
| 714 | f_struct.open(f_struct_name.c_str()); |
| 715 | |
| 716 | f_struct << |
| 717 | autogen_comment() << |
| 718 | java_package() << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 719 | java_type_imports(); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 720 | |
| 721 | generate_java_doc(f_struct, tstruct); |
| 722 | |
| 723 | bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end()); |
| 724 | |
| 725 | indent(f_struct) << |
Bryan Duxbury | 0fab12e | 2010-04-22 00:22:34 +0000 | [diff] [blame] | 726 | "public " << (is_final ? "final " : "") << "class " << tstruct->get_name() |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 727 | << " extends org.apache.thrift.TUnion<" << tstruct->get_name() << ", " << tstruct->get_name() << "._Fields> "; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 728 | |
| 729 | scope_up(f_struct); |
| 730 | |
| 731 | generate_struct_desc(f_struct, tstruct); |
| 732 | generate_field_descs(f_struct, tstruct); |
| 733 | |
| 734 | f_struct << endl; |
| 735 | |
| 736 | generate_field_name_constants(f_struct, tstruct); |
| 737 | |
| 738 | f_struct << endl; |
| 739 | |
| 740 | generate_java_meta_data_map(f_struct, tstruct); |
| 741 | |
| 742 | generate_union_constructor(f_struct, tstruct); |
| 743 | |
| 744 | f_struct << endl; |
| 745 | |
| 746 | generate_union_abstract_methods(f_struct, tstruct); |
| 747 | |
| 748 | f_struct << endl; |
| 749 | |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 750 | generate_java_struct_field_by_id(f_struct, tstruct); |
| 751 | |
| 752 | f_struct << endl; |
| 753 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 754 | generate_union_getters_and_setters(f_struct, tstruct); |
Bryan Duxbury | 63c2d37 | 2011-06-08 17:46:55 +0000 | [diff] [blame] | 755 | |
| 756 | f_struct << endl; |
| 757 | |
| 758 | generate_union_is_set_methods(f_struct, tstruct); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 759 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 760 | f_struct << endl; |
| 761 | |
| 762 | generate_union_comparisons(f_struct, tstruct); |
| 763 | |
| 764 | f_struct << endl; |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 765 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 766 | generate_union_hashcode(f_struct, tstruct); |
| 767 | |
| 768 | f_struct << endl; |
| 769 | |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 770 | generate_java_struct_write_object(f_struct, tstruct); |
| 771 | |
| 772 | f_struct << endl; |
| 773 | |
| 774 | generate_java_struct_read_object(f_struct, tstruct); |
| 775 | |
| 776 | f_struct << endl; |
| 777 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 778 | scope_down(f_struct); |
| 779 | |
| 780 | f_struct.close(); |
| 781 | } |
| 782 | |
| 783 | void t_java_generator::generate_union_constructor(ofstream& out, t_struct* tstruct) { |
| 784 | indent(out) << "public " << type_name(tstruct) << "() {" << endl; |
| 785 | indent(out) << " super();" << endl; |
| 786 | indent(out) << "}" << endl << endl; |
| 787 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 788 | indent(out) << "public " << type_name(tstruct) << "(_Fields setField, Object value) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 789 | indent(out) << " super(setField, value);" << endl; |
| 790 | indent(out) << "}" << endl << endl; |
| 791 | |
| 792 | indent(out) << "public " << type_name(tstruct) << "(" << type_name(tstruct) << " other) {" << endl; |
| 793 | indent(out) << " super(other);" << endl; |
| 794 | indent(out) << "}" << endl; |
| 795 | |
| 796 | indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl; |
| 797 | indent(out) << " return new " << tstruct->get_name() << "(this);" << endl; |
| 798 | indent(out) << "}" << endl << endl; |
| 799 | |
| 800 | // generate "constructors" for each field |
| 801 | const vector<t_field*>& members = tstruct->get_members(); |
| 802 | vector<t_field*>::const_iterator m_iter; |
| 803 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 804 | t_type* type = (*m_iter)->get_type(); |
| 805 | indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(" << type_name(type) << " value) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 806 | indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl; |
| 807 | indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(value);" << endl; |
| 808 | indent(out) << " return x;" << endl; |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 809 | indent(out) << "}" << endl << endl; |
| 810 | |
| 811 | if (type->is_base_type() && ((t_base_type*)type)->is_binary()) { |
| 812 | indent(out) << "public static " << type_name(tstruct) << " " << (*m_iter)->get_name() << "(byte[] value) {" << endl; |
| 813 | indent(out) << " " << type_name(tstruct) << " x = new " << type_name(tstruct) << "();" << endl; |
| 814 | indent(out) << " x.set" << get_cap_name((*m_iter)->get_name()) << "(ByteBuffer.wrap(value));" << endl; |
| 815 | indent(out) << " return x;" << endl; |
| 816 | indent(out) << "}" << endl << endl; |
| 817 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | void t_java_generator::generate_union_getters_and_setters(ofstream& out, t_struct* tstruct) { |
| 822 | const vector<t_field*>& members = tstruct->get_members(); |
| 823 | vector<t_field*>::const_iterator m_iter; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 824 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 825 | bool first = true; |
| 826 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 827 | if (first) { |
| 828 | first = false; |
| 829 | } else { |
| 830 | out << endl; |
| 831 | } |
| 832 | |
| 833 | t_field* field = (*m_iter); |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 834 | t_type* type = field->get_type(); |
| 835 | std::string cap_name = get_cap_name(field->get_name()); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 836 | |
| 837 | generate_java_doc(out, field); |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 838 | if (type->is_base_type() && ((t_base_type*)type)->is_binary()) { |
| 839 | indent(out) << "public byte[] get" << cap_name << "() {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 840 | indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(buffer" << get_cap_name("for") << cap_name << "()));" << endl; |
Bryan Duxbury | a5eb848 | 2010-11-24 22:30:38 +0000 | [diff] [blame] | 841 | indent(out) << " ByteBuffer b = buffer" << get_cap_name("for") << cap_name << "();" << endl; |
| 842 | indent(out) << " return b == null ? null : b.array();" << endl; |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 843 | indent(out) << "}" << endl; |
| 844 | |
| 845 | out << endl; |
| 846 | |
| 847 | indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << get_cap_name(field->get_name()) << "() {" << endl; |
| 848 | indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl; |
| 849 | indent(out) << " return (ByteBuffer)getFieldValue();" << endl; |
| 850 | indent(out) << " } else {" << endl; |
| 851 | indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name() |
| 852 | << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl; |
| 853 | indent(out) << " }" << endl; |
| 854 | indent(out) << "}" << endl; |
| 855 | } else { |
| 856 | indent(out) << "public " << type_name(field->get_type()) << " get" << get_cap_name(field->get_name()) << "() {" << endl; |
| 857 | indent(out) << " if (getSetField() == _Fields." << constant_name(field->get_name()) << ") {" << endl; |
| 858 | indent(out) << " return (" << type_name(field->get_type(), true) << ")getFieldValue();" << endl; |
| 859 | indent(out) << " } else {" << endl; |
| 860 | indent(out) << " throw new RuntimeException(\"Cannot get field '" << field->get_name() |
| 861 | << "' because union is currently set to \" + getFieldDesc(getSetField()).name);" << endl; |
| 862 | indent(out) << " }" << endl; |
| 863 | indent(out) << "}" << endl; |
| 864 | } |
| 865 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 866 | out << endl; |
| 867 | |
| 868 | generate_java_doc(out, field); |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 869 | if (type->is_base_type() && ((t_base_type*)type)->is_binary()) { |
| 870 | indent(out) << "public void set" << get_cap_name(field->get_name()) << "(byte[] value) {" << endl; |
| 871 | indent(out) << " set" << get_cap_name(field->get_name()) << "(ByteBuffer.wrap(value));" << endl; |
| 872 | indent(out) << "}" << endl; |
| 873 | |
| 874 | out << endl; |
| 875 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 876 | indent(out) << "public void set" << get_cap_name(field->get_name()) << "(" << type_name(field->get_type()) << " value) {" << endl; |
| 877 | if (type_can_be_null(field->get_type())) { |
| 878 | indent(out) << " if (value == null) throw new NullPointerException();" << endl; |
| 879 | } |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 880 | indent(out) << " setField_ = _Fields." << constant_name(field->get_name()) << ";" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 881 | indent(out) << " value_ = value;" << endl; |
| 882 | indent(out) << "}" << endl; |
| 883 | } |
| 884 | } |
| 885 | |
Bryan Duxbury | 63c2d37 | 2011-06-08 17:46:55 +0000 | [diff] [blame] | 886 | void t_java_generator::generate_union_is_set_methods(ofstream& out, t_struct* tstruct) { |
| 887 | const vector<t_field*>& members = tstruct->get_members(); |
| 888 | vector<t_field*>::const_iterator m_iter; |
| 889 | |
| 890 | bool first = true; |
| 891 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 892 | if (first) { |
| 893 | first = false; |
| 894 | } else { |
| 895 | out << endl; |
| 896 | } |
| 897 | |
| 898 | std::string field_name = (*m_iter)->get_name(); |
| 899 | |
| 900 | indent(out) << "public boolean is" << get_cap_name("set") << get_cap_name(field_name) << "() {" << endl; |
| 901 | indent_up(); |
| 902 | indent(out) << "return setField_ == _Fields." << constant_name(field_name) << ";" << endl; |
| 903 | indent_down(); |
| 904 | indent(out) << "}" << endl << endl; |
| 905 | } |
| 906 | } |
| 907 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 908 | void t_java_generator::generate_union_abstract_methods(ofstream& out, t_struct* tstruct) { |
| 909 | generate_check_type(out, tstruct); |
| 910 | out << endl; |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 911 | generate_standard_scheme_read_value(out, tstruct); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 912 | out << endl; |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 913 | generate_standard_scheme_write_value(out, tstruct); |
| 914 | out << endl; |
| 915 | generate_tuple_scheme_read_value(out, tstruct); |
| 916 | out << endl; |
| 917 | generate_tuple_scheme_write_value(out, tstruct); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 918 | out << endl; |
| 919 | generate_get_field_desc(out, tstruct); |
| 920 | out << endl; |
| 921 | generate_get_struct_desc(out, tstruct); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 922 | out << endl; |
| 923 | indent(out) << "@Override" << endl; |
| 924 | indent(out) << "protected _Fields enumForId(short id) {" << endl; |
| 925 | indent(out) << " return _Fields.findByThriftIdOrThrow(id);" << endl; |
| 926 | indent(out) << "}" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | void t_java_generator::generate_check_type(ofstream& out, t_struct* tstruct) { |
| 930 | indent(out) << "@Override" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 931 | indent(out) << "protected void checkType(_Fields setField, Object value) throws ClassCastException {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 932 | indent_up(); |
| 933 | |
| 934 | indent(out) << "switch (setField) {" << endl; |
| 935 | indent_up(); |
| 936 | |
| 937 | const vector<t_field*>& members = tstruct->get_members(); |
| 938 | vector<t_field*>::const_iterator m_iter; |
| 939 | |
| 940 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 941 | t_field* field = (*m_iter); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 942 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 943 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 944 | indent(out) << " if (value instanceof " << type_name(field->get_type(), true, false, true) << ") {" << endl; |
| 945 | indent(out) << " break;" << endl; |
| 946 | indent(out) << " }" << endl; |
| 947 | indent(out) << " throw new ClassCastException(\"Was expecting value of type " |
| 948 | << type_name(field->get_type(), true, false) << " for field '" << field->get_name() |
| 949 | << "', but got \" + value.getClass().getSimpleName());" << endl; |
| 950 | // do the real check here |
| 951 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 952 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 953 | indent(out) << "default:" << endl; |
| 954 | indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl; |
| 955 | |
| 956 | indent_down(); |
| 957 | indent(out) << "}" << endl; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 958 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 959 | indent_down(); |
| 960 | indent(out) << "}" << endl; |
| 961 | } |
| 962 | |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 963 | void t_java_generator::generate_standard_scheme_read_value(ofstream& out, t_struct* tstruct) { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 964 | indent(out) << "@Override" << endl; |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 965 | indent(out) << "protected Object standardSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TField field) throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 966 | |
| 967 | indent_up(); |
| 968 | |
Bryan Duxbury | c2ec7ca | 2009-12-31 18:59:15 +0000 | [diff] [blame] | 969 | indent(out) << "_Fields setField = _Fields.findByThriftId(field.id);" << endl; |
| 970 | indent(out) << "if (setField != null) {" << endl; |
| 971 | indent_up(); |
| 972 | indent(out) << "switch (setField) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 973 | indent_up(); |
| 974 | |
| 975 | const vector<t_field*>& members = tstruct->get_members(); |
| 976 | vector<t_field*>::const_iterator m_iter; |
| 977 | |
| 978 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 979 | t_field* field = (*m_iter); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 980 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 981 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 982 | indent_up(); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 983 | indent(out) << "if (field.type == " << constant_name(field->get_name()) << "_FIELD_DESC.type) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 984 | indent_up(); |
| 985 | indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl; |
| 986 | generate_deserialize_field(out, field, ""); |
| 987 | indent(out) << "return " << field->get_name() << ";" << endl; |
| 988 | indent_down(); |
| 989 | indent(out) << "} else {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 990 | indent(out) << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type);" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 991 | indent(out) << " return null;" << endl; |
| 992 | indent(out) << "}" << endl; |
| 993 | indent_down(); |
| 994 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 995 | |
Bryan Duxbury | c2ec7ca | 2009-12-31 18:59:15 +0000 | [diff] [blame] | 996 | indent(out) << "default:" << endl; |
| 997 | indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl; |
| 998 | |
| 999 | indent_down(); |
| 1000 | indent(out) << "}" << endl; |
| 1001 | |
| 1002 | indent_down(); |
| 1003 | indent(out) << "} else {" << endl; |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 1004 | indent_up(); |
Bryan Duxbury | c2ec7ca | 2009-12-31 18:59:15 +0000 | [diff] [blame] | 1005 | indent(out) << "return null;" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1006 | indent_down(); |
| 1007 | indent(out) << "}" << endl; |
| 1008 | |
| 1009 | indent_down(); |
| 1010 | indent(out) << "}" << endl; |
| 1011 | } |
| 1012 | |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 1013 | void t_java_generator::generate_standard_scheme_write_value(ofstream& out, t_struct* tstruct) { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1014 | indent(out) << "@Override" << endl; |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 1015 | indent(out) << "protected void standardSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1016 | |
| 1017 | indent_up(); |
| 1018 | |
Bryan Duxbury | 4e7cf25 | 2010-02-28 05:19:38 +0000 | [diff] [blame] | 1019 | indent(out) << "switch (setField_) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1020 | indent_up(); |
| 1021 | |
| 1022 | const vector<t_field*>& members = tstruct->get_members(); |
| 1023 | vector<t_field*>::const_iterator m_iter; |
| 1024 | |
| 1025 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1026 | t_field* field = (*m_iter); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1027 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1028 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1029 | indent_up(); |
| 1030 | indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() |
Bryan Duxbury | 4e7cf25 | 2010-02-28 05:19:38 +0000 | [diff] [blame] | 1031 | << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl; |
Bryan Duxbury | adaf826 | 2011-09-01 18:54:07 +0000 | [diff] [blame] | 1032 | generate_serialize_field(out, field, ""); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1033 | indent(out) << "return;" << endl; |
| 1034 | indent_down(); |
| 1035 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1036 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1037 | indent(out) << "default:" << endl; |
Bryan Duxbury | 4e7cf25 | 2010-02-28 05:19:38 +0000 | [diff] [blame] | 1038 | indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1039 | |
| 1040 | indent_down(); |
| 1041 | indent(out) << "}" << endl; |
| 1042 | |
| 1043 | indent_down(); |
| 1044 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1045 | indent(out) << "}" << endl; |
| 1046 | } |
| 1047 | |
Bryan Duxbury | 18784d7 | 2011-09-20 22:53:31 +0000 | [diff] [blame] | 1048 | void t_java_generator::generate_tuple_scheme_read_value(ofstream& out, t_struct* tstruct) { |
| 1049 | indent(out) << "@Override" << endl; |
| 1050 | indent(out) << "protected Object tupleSchemeReadValue(org.apache.thrift.protocol.TProtocol iprot, short fieldID) throws org.apache.thrift.TException {" << endl; |
| 1051 | |
| 1052 | indent_up(); |
| 1053 | |
| 1054 | indent(out) << "_Fields setField = _Fields.findByThriftId(fieldID);" << endl; |
| 1055 | indent(out) << "if (setField != null) {" << endl; |
| 1056 | indent_up(); |
| 1057 | indent(out) << "switch (setField) {" << endl; |
| 1058 | indent_up(); |
| 1059 | |
| 1060 | const vector<t_field*>& members = tstruct->get_members(); |
| 1061 | vector<t_field*>::const_iterator m_iter; |
| 1062 | |
| 1063 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1064 | t_field* field = (*m_iter); |
| 1065 | |
| 1066 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
| 1067 | indent_up(); |
| 1068 | indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() << ";" << endl; |
| 1069 | generate_deserialize_field(out, field, ""); |
| 1070 | indent(out) << "return " << field->get_name() << ";" << endl; |
| 1071 | indent_down(); |
| 1072 | } |
| 1073 | |
| 1074 | indent(out) << "default:" << endl; |
| 1075 | indent(out) << " throw new IllegalStateException(\"setField wasn't null, but didn't match any of the case statements!\");" << endl; |
| 1076 | |
| 1077 | indent_down(); |
| 1078 | indent(out) << "}" << endl; |
| 1079 | |
| 1080 | indent_down(); |
| 1081 | indent(out) << "} else {" << endl; |
| 1082 | indent_up(); |
| 1083 | indent(out) << "return null;" << endl; |
| 1084 | indent_down(); |
| 1085 | indent(out) << "}" << endl; |
| 1086 | indent_down(); |
| 1087 | indent(out) << "}" << endl; |
| 1088 | } |
| 1089 | |
| 1090 | void t_java_generator::generate_tuple_scheme_write_value(ofstream& out, t_struct* tstruct) { |
| 1091 | indent(out) << "@Override" << endl; |
| 1092 | indent(out) << "protected void tupleSchemeWriteValue(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl; |
| 1093 | |
| 1094 | indent_up(); |
| 1095 | |
| 1096 | indent(out) << "switch (setField_) {" << endl; |
| 1097 | indent_up(); |
| 1098 | |
| 1099 | const vector<t_field*>& members = tstruct->get_members(); |
| 1100 | vector<t_field*>::const_iterator m_iter; |
| 1101 | |
| 1102 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1103 | t_field* field = (*m_iter); |
| 1104 | |
| 1105 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
| 1106 | indent_up(); |
| 1107 | indent(out) << type_name(field->get_type(), true, false) << " " << field->get_name() |
| 1108 | << " = (" << type_name(field->get_type(), true, false) << ")value_;" << endl; |
| 1109 | generate_serialize_field(out, field, ""); |
| 1110 | indent(out) << "return;" << endl; |
| 1111 | indent_down(); |
| 1112 | } |
| 1113 | |
| 1114 | indent(out) << "default:" << endl; |
| 1115 | indent(out) << " throw new IllegalStateException(\"Cannot write union with unknown field \" + setField_);" << endl; |
| 1116 | |
| 1117 | indent_down(); |
| 1118 | indent(out) << "}" << endl; |
| 1119 | |
| 1120 | indent_down(); |
| 1121 | |
| 1122 | indent(out) << "}" << endl; |
| 1123 | } |
| 1124 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1125 | void t_java_generator::generate_get_field_desc(ofstream& out, t_struct* tstruct) { |
| 1126 | indent(out) << "@Override" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1127 | indent(out) << "protected org.apache.thrift.protocol.TField getFieldDesc(_Fields setField) {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1128 | indent_up(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1129 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1130 | const vector<t_field*>& members = tstruct->get_members(); |
| 1131 | vector<t_field*>::const_iterator m_iter; |
| 1132 | |
| 1133 | indent(out) << "switch (setField) {" << endl; |
| 1134 | indent_up(); |
| 1135 | |
| 1136 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1137 | t_field* field = (*m_iter); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1138 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
| 1139 | indent(out) << " return " << constant_name(field->get_name()) << "_FIELD_DESC;" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | indent(out) << "default:" << endl; |
| 1143 | indent(out) << " throw new IllegalArgumentException(\"Unknown field id \" + setField);" << endl; |
| 1144 | |
| 1145 | indent_down(); |
| 1146 | indent(out) << "}" << endl; |
| 1147 | |
| 1148 | indent_down(); |
| 1149 | indent(out) << "}" << endl; |
| 1150 | } |
| 1151 | |
| 1152 | void t_java_generator::generate_get_struct_desc(ofstream& out, t_struct* tstruct) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 1153 | (void) tstruct; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1154 | indent(out) << "@Override" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1155 | indent(out) << "protected org.apache.thrift.protocol.TStruct getStructDesc() {" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1156 | indent(out) << " return STRUCT_DESC;" << endl; |
| 1157 | indent(out) << "}" << endl; |
| 1158 | } |
| 1159 | |
| 1160 | void t_java_generator::generate_union_comparisons(ofstream& out, t_struct* tstruct) { |
| 1161 | // equality |
| 1162 | indent(out) << "public boolean equals(Object other) {" << endl; |
| 1163 | indent(out) << " if (other instanceof " << tstruct->get_name() << ") {" << endl; |
| 1164 | indent(out) << " return equals((" << tstruct->get_name() << ")other);" << endl; |
| 1165 | indent(out) << " } else {" << endl; |
| 1166 | indent(out) << " return false;" << endl; |
| 1167 | indent(out) << " }" << endl; |
| 1168 | indent(out) << "}" << endl; |
| 1169 | |
| 1170 | out << endl; |
| 1171 | |
| 1172 | indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl; |
Bryan Duxbury | f5abd26 | 2010-08-06 00:18:25 +0000 | [diff] [blame] | 1173 | indent(out) << " return other != null && getSetField() == other.getSetField() && getFieldValue().equals(other.getFieldValue());" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1174 | indent(out) << "}" << endl; |
| 1175 | out << endl; |
| 1176 | |
Bryan Duxbury | 5557bef | 2010-03-29 23:57:09 +0000 | [diff] [blame] | 1177 | indent(out) << "@Override" << endl; |
| 1178 | indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1179 | indent(out) << " int lastComparison = org.apache.thrift.TBaseHelper.compareTo(getSetField(), other.getSetField());" << endl; |
Bryan Duxbury | 5557bef | 2010-03-29 23:57:09 +0000 | [diff] [blame] | 1180 | indent(out) << " if (lastComparison == 0) {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1181 | indent(out) << " return org.apache.thrift.TBaseHelper.compareTo(getFieldValue(), other.getFieldValue());" << endl; |
Bryan Duxbury | 5557bef | 2010-03-29 23:57:09 +0000 | [diff] [blame] | 1182 | indent(out) << " }" << endl; |
| 1183 | indent(out) << " return lastComparison;" << endl; |
| 1184 | indent(out) << "}" << endl; |
| 1185 | out << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | void t_java_generator::generate_union_hashcode(ofstream& out, t_struct* tstruct) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 1189 | (void) tstruct; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1190 | if (gen_hash_code_) { |
| 1191 | indent(out) << "@Override" << endl; |
| 1192 | indent(out) << "public int hashCode() {" << endl; |
Bryan Duxbury | cd43a06 | 2010-02-17 20:01:29 +0000 | [diff] [blame] | 1193 | indent(out) << " HashCodeBuilder hcb = new HashCodeBuilder();" << endl; |
| 1194 | indent(out) << " hcb.append(this.getClass().getName());" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1195 | indent(out) << " org.apache.thrift.TFieldIdEnum setField = getSetField();" << endl; |
Bryan Duxbury | cd43a06 | 2010-02-17 20:01:29 +0000 | [diff] [blame] | 1196 | indent(out) << " if (setField != null) {" << endl; |
| 1197 | indent(out) << " hcb.append(setField.getThriftFieldId());" << endl; |
| 1198 | indent(out) << " Object value = getFieldValue();" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1199 | indent(out) << " if (value instanceof org.apache.thrift.TEnum) {" << endl; |
| 1200 | indent(out) << " hcb.append(((org.apache.thrift.TEnum)getFieldValue()).getValue());" << endl; |
Bryan Duxbury | cd43a06 | 2010-02-17 20:01:29 +0000 | [diff] [blame] | 1201 | indent(out) << " } else {" << endl; |
| 1202 | indent(out) << " hcb.append(value);" << endl; |
| 1203 | indent(out) << " }" << endl; |
| 1204 | indent(out) << " }" << endl; |
| 1205 | indent(out) << " return hcb.toHashCode();" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1206 | indent(out) << "}"; |
| 1207 | } else { |
| 1208 | indent(out) << "/**" << endl; |
| 1209 | indent(out) << " * If you'd like this to perform more respectably, use the hashcode generator option." << endl; |
| 1210 | indent(out) << " */" << endl; |
| 1211 | indent(out) << "@Override" << endl; |
| 1212 | indent(out) << "public int hashCode() {" << endl; |
| 1213 | indent(out) << " return 0;" << endl; |
| 1214 | indent(out) << "}" << endl; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1219 | * Java struct definition. This has various parameters, as it could be |
| 1220 | * generated standalone or inside another class as a helper. If it |
| 1221 | * is a helper than it is a static class. |
| 1222 | * |
| 1223 | * @param tstruct The struct definition |
| 1224 | * @param is_exception Is this an exception? |
| 1225 | * @param in_class If inside a class, needs to be static class |
| 1226 | * @param is_result If this is a result it needs a different writer |
| 1227 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1228 | void t_java_generator::generate_java_struct_definition(ofstream &out, |
| 1229 | t_struct* tstruct, |
| 1230 | bool is_exception, |
| 1231 | bool in_class, |
| 1232 | bool is_result) { |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 1233 | generate_java_doc(out, tstruct); |
| 1234 | |
Bryan Duxbury | 056bcb6 | 2009-02-01 16:56:29 +0000 | [diff] [blame] | 1235 | bool is_final = (tstruct->annotations_.find("final") != tstruct->annotations_.end()); |
| 1236 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1237 | indent(out) << |
Bryan Duxbury | 056bcb6 | 2009-02-01 16:56:29 +0000 | [diff] [blame] | 1238 | "public " << (is_final ? "final " : "") << |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1239 | (in_class ? "static " : "") << "class " << tstruct->get_name() << " "; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 1240 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1241 | if (is_exception) { |
| 1242 | out << "extends Exception "; |
| 1243 | } |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1244 | out << "implements org.apache.thrift.TBase<" << tstruct->get_name() << ", " << tstruct->get_name() << "._Fields>, java.io.Serializable, Cloneable"; |
Bryan Duxbury | 684b4f9 | 2009-07-31 00:12:21 +0000 | [diff] [blame] | 1245 | |
| 1246 | out << " "; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 1247 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1248 | scope_up(out); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1249 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1250 | generate_struct_desc(out, tstruct); |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 1251 | |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1252 | // Members are public for -java, private for -javabean |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1253 | const vector<t_field*>& members = tstruct->get_members(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 1254 | vector<t_field*>::const_iterator m_iter; |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 1255 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1256 | out << endl; |
| 1257 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1258 | generate_field_descs(out, tstruct); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1259 | |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 1260 | out << endl; |
| 1261 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1262 | generate_scheme_map(out, tstruct); |
| 1263 | |
| 1264 | out << endl; |
| 1265 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1266 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 1267 | if (bean_style_ || private_members_) { |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1268 | indent(out) << "private "; |
| 1269 | } else { |
David Reiss | f33e03c | 2008-10-29 00:07:49 +0000 | [diff] [blame] | 1270 | generate_java_doc(out, *m_iter); |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1271 | indent(out) << "public "; |
| 1272 | } |
| 1273 | out << declare_field(*m_iter, false) << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1274 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1275 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1276 | out << endl; |
| 1277 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1278 | generate_field_name_constants(out, tstruct); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1279 | |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1280 | // isset data |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1281 | if (members.size() > 0) { |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1282 | out << endl; |
| 1283 | |
| 1284 | indent(out) << "// isset id assignments" << endl; |
| 1285 | |
| 1286 | int i = 0; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1287 | int optionals = 0; |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1288 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1289 | if ((*m_iter)->get_req() == t_field::T_OPTIONAL) { |
| 1290 | optionals++; |
| 1291 | } |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1292 | if (!type_can_be_null((*m_iter)->get_type())) { |
| 1293 | indent(out) << "private static final int " << isset_field_id(*m_iter) |
| 1294 | << " = " << i << ";" << endl; |
| 1295 | i++; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1296 | } |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | if (i > 0) { |
| 1300 | indent(out) << "private BitSet __isset_bit_vector = new BitSet(" << i << ");" << endl; |
| 1301 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1302 | if (optionals > 0) { |
| 1303 | std::string output_string = "private _Fields optionals[] = {"; |
| 1304 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1305 | if ((*m_iter)->get_req() == t_field::T_OPTIONAL) { |
| 1306 | output_string = output_string + "_Fields." + constant_name((*m_iter)->get_name()) + ","; |
| 1307 | } |
| 1308 | } |
| 1309 | indent(out) << output_string.substr(0, output_string.length() - 1) << "};" << endl; |
| 1310 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1311 | } |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 1312 | |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 1313 | generate_java_meta_data_map(out, tstruct); |
| 1314 | |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1315 | bool all_optional_members = true; |
| 1316 | |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 1317 | // Default constructor |
| 1318 | indent(out) << |
| 1319 | "public " << tstruct->get_name() << "() {" << endl; |
| 1320 | indent_up(); |
| 1321 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 1322 | t_type* t = get_true_type((*m_iter)->get_type()); |
David Reiss | 80a702f | 2007-12-18 02:54:06 +0000 | [diff] [blame] | 1323 | if ((*m_iter)->get_value() != NULL) { |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 1324 | print_const_value(out, "this." + (*m_iter)->get_name(), t, (*m_iter)->get_value(), true, true); |
| 1325 | } |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1326 | if ((*m_iter)->get_req() != t_field::T_OPTIONAL) { |
| 1327 | all_optional_members = false; |
| 1328 | } |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 1329 | } |
| 1330 | indent_down(); |
| 1331 | indent(out) << "}" << endl << endl; |
| 1332 | |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1333 | if (!members.empty() && !all_optional_members) { |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1334 | // Full constructor for all fields |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 1335 | indent(out) << |
| 1336 | "public " << tstruct->get_name() << "(" << endl; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 1337 | indent_up(); |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1338 | bool first = true; |
| 1339 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1340 | if ((*m_iter)->get_req() != t_field::T_OPTIONAL) { |
| 1341 | if (!first) { |
| 1342 | out << "," << endl; |
| 1343 | } |
| 1344 | first = false; |
| 1345 | indent(out) << type_name((*m_iter)->get_type()) << " " << |
| 1346 | (*m_iter)->get_name(); |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | out << ")" << endl; |
| 1350 | indent_down(); |
| 1351 | indent(out) << "{" << endl; |
| 1352 | indent_up(); |
| 1353 | indent(out) << "this();" << endl; |
| 1354 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1355 | if ((*m_iter)->get_req() != t_field::T_OPTIONAL) { |
| 1356 | indent(out) << "this." << (*m_iter)->get_name() << " = " << |
| 1357 | (*m_iter)->get_name() << ";" << endl; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1358 | generate_isset_set(out, (*m_iter), ""); |
Bryan Duxbury | 364902e | 2009-10-02 00:56:53 +0000 | [diff] [blame] | 1359 | } |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 1360 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1361 | |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 1362 | indent_down(); |
| 1363 | indent(out) << "}" << endl << endl; |
| 1364 | } |
| 1365 | |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1366 | // copy constructor |
| 1367 | indent(out) << "/**" << endl; |
| 1368 | indent(out) << " * Performs a deep copy on <i>other</i>." << endl; |
| 1369 | indent(out) << " */" << endl; |
| 1370 | indent(out) << "public " << tstruct->get_name() << "(" << tstruct->get_name() << " other) {" << endl; |
| 1371 | indent_up(); |
| 1372 | |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1373 | if (has_bit_vector(tstruct)) { |
| 1374 | indent(out) << "__isset_bit_vector.clear();" << endl; |
| 1375 | indent(out) << "__isset_bit_vector.or(other.__isset_bit_vector);" << endl; |
| 1376 | } |
| 1377 | |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1378 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1379 | t_field* field = (*m_iter); |
| 1380 | std::string field_name = field->get_name(); |
| 1381 | t_type* type = field->get_type(); |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1382 | bool can_be_null = type_can_be_null(type); |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1383 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1384 | if (can_be_null) { |
| 1385 | indent(out) << "if (other." << generate_isset_check(field) << ") {" << endl; |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1386 | indent_up(); |
| 1387 | } |
| 1388 | |
| 1389 | if (type->is_container()) { |
| 1390 | generate_deep_copy_container(out, "other", field_name, "__this__" + field_name, type); |
| 1391 | indent(out) << "this." << field_name << " = __this__" << field_name << ";" << endl; |
| 1392 | } else { |
| 1393 | indent(out) << "this." << field_name << " = "; |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 1394 | generate_deep_copy_non_container(out, "other." + field_name, field_name, type); |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1395 | out << ";" << endl; |
| 1396 | } |
| 1397 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1398 | if (can_be_null) { |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 1399 | indent_down(); |
| 1400 | indent(out) << "}" << endl; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | indent_down(); |
| 1405 | indent(out) << "}" << endl << endl; |
| 1406 | |
| 1407 | // clone method, so that you can deep copy an object when you don't know its class. |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 1408 | indent(out) << "public " << tstruct->get_name() << " deepCopy() {" << endl; |
| 1409 | indent(out) << " return new " << tstruct->get_name() << "(this);" << endl; |
| 1410 | indent(out) << "}" << endl << endl; |
| 1411 | |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 1412 | generate_java_struct_clear(out, tstruct); |
| 1413 | |
Bryan Duxbury | f5383b6 | 2009-02-03 00:49:25 +0000 | [diff] [blame] | 1414 | generate_java_bean_boilerplate(out, tstruct); |
| 1415 | generate_generic_field_getters_setters(out, tstruct); |
| 1416 | generate_generic_isset_method(out, tstruct); |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1417 | |
| 1418 | generate_java_struct_equality(out, tstruct); |
Bryan Duxbury | 5557bef | 2010-03-29 23:57:09 +0000 | [diff] [blame] | 1419 | generate_java_struct_compare_to(out, tstruct); |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 1420 | generate_java_struct_field_by_id(out, tstruct); |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1421 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1422 | generate_java_struct_reader(out, tstruct); |
| 1423 | if (is_result) { |
| 1424 | generate_java_struct_result_writer(out, tstruct); |
| 1425 | } else { |
| 1426 | generate_java_struct_writer(out, tstruct); |
| 1427 | } |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1428 | generate_java_struct_tostring(out, tstruct); |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1429 | generate_java_validator(out, tstruct); |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 1430 | |
| 1431 | generate_java_struct_write_object(out, tstruct); |
| 1432 | generate_java_struct_read_object(out, tstruct); |
| 1433 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1434 | generate_java_struct_standard_scheme(out, tstruct); |
| 1435 | generate_java_struct_tuple_scheme(out, tstruct); |
| 1436 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1437 | scope_down(out); |
Mark Slee | b7f58ff | 2006-09-02 21:59:28 +0000 | [diff] [blame] | 1438 | out << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1441 | /** |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1442 | * Generates equals methods and a hashCode method for a structure. |
| 1443 | * |
| 1444 | * @param tstruct The struct definition |
| 1445 | */ |
| 1446 | void t_java_generator::generate_java_struct_equality(ofstream& out, |
| 1447 | t_struct* tstruct) { |
Bryan Duxbury | 369417a | 2009-01-27 22:09:33 +0000 | [diff] [blame] | 1448 | out << indent() << "@Override" << endl << |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1449 | indent() << "public boolean equals(Object that) {" << endl; |
| 1450 | indent_up(); |
| 1451 | out << |
| 1452 | indent() << "if (that == null)" << endl << |
| 1453 | indent() << " return false;" << endl << |
| 1454 | indent() << "if (that instanceof " << tstruct->get_name() << ")" << endl << |
| 1455 | indent() << " return this.equals((" << tstruct->get_name() << ")that);" << endl << |
| 1456 | indent() << "return false;" << endl; |
| 1457 | scope_down(out); |
| 1458 | out << endl; |
| 1459 | |
| 1460 | out << |
| 1461 | indent() << "public boolean equals(" << tstruct->get_name() << " that) {" << endl; |
| 1462 | indent_up(); |
| 1463 | out << |
| 1464 | indent() << "if (that == null)" << endl << |
| 1465 | indent() << " return false;" << endl; |
| 1466 | |
| 1467 | const vector<t_field*>& members = tstruct->get_members(); |
| 1468 | vector<t_field*>::const_iterator m_iter; |
| 1469 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1470 | out << endl; |
| 1471 | |
| 1472 | t_type* t = get_true_type((*m_iter)->get_type()); |
| 1473 | // Most existing Thrift code does not use isset or optional/required, |
| 1474 | // so we treat "default" fields as required. |
| 1475 | bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL; |
| 1476 | bool can_be_null = type_can_be_null(t); |
| 1477 | string name = (*m_iter)->get_name(); |
| 1478 | |
| 1479 | string this_present = "true"; |
| 1480 | string that_present = "true"; |
| 1481 | string unequal; |
| 1482 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1483 | if (is_optional || can_be_null) { |
| 1484 | this_present += " && this." + generate_isset_check(*m_iter); |
| 1485 | that_present += " && that." + generate_isset_check(*m_iter); |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | out << |
| 1489 | indent() << "boolean this_present_" << name << " = " |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1490 | << this_present << ";" << endl << |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1491 | indent() << "boolean that_present_" << name << " = " |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1492 | << that_present << ";" << endl << |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1493 | indent() << "if (" << "this_present_" << name |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1494 | << " || that_present_" << name << ") {" << endl; |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1495 | indent_up(); |
| 1496 | out << |
| 1497 | indent() << "if (!(" << "this_present_" << name |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1498 | << " && that_present_" << name << "))" << endl << |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1499 | indent() << " return false;" << endl; |
| 1500 | |
| 1501 | if (t->is_base_type() && ((t_base_type*)t)->is_binary()) { |
Bryan Duxbury | f5abd26 | 2010-08-06 00:18:25 +0000 | [diff] [blame] | 1502 | unequal = "!this." + name + ".equals(that." + name + ")"; |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1503 | } else if (can_be_null) { |
| 1504 | unequal = "!this." + name + ".equals(that." + name + ")"; |
| 1505 | } else { |
| 1506 | unequal = "this." + name + " != that." + name; |
| 1507 | } |
| 1508 | |
| 1509 | out << |
| 1510 | indent() << "if (" << unequal << ")" << endl << |
| 1511 | indent() << " return false;" << endl; |
| 1512 | |
| 1513 | scope_down(out); |
| 1514 | } |
| 1515 | out << endl; |
| 1516 | indent(out) << "return true;" << endl; |
| 1517 | scope_down(out); |
| 1518 | out << endl; |
| 1519 | |
Bryan Duxbury | 30e1de9 | 2009-02-10 18:36:56 +0000 | [diff] [blame] | 1520 | out << indent() << "@Override" << endl << |
| 1521 | indent() << "public int hashCode() {" << endl; |
| 1522 | indent_up(); |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1523 | if (gen_hash_code_) { |
Bryan Duxbury | 30e1de9 | 2009-02-10 18:36:56 +0000 | [diff] [blame] | 1524 | indent(out) << "HashCodeBuilder builder = new HashCodeBuilder();" << endl; |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1525 | |
| 1526 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1527 | out << endl; |
| 1528 | |
| 1529 | t_type* t = get_true_type((*m_iter)->get_type()); |
| 1530 | bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL; |
| 1531 | bool can_be_null = type_can_be_null(t); |
| 1532 | string name = (*m_iter)->get_name(); |
| 1533 | |
| 1534 | string present = "true"; |
| 1535 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1536 | if (is_optional || can_be_null) { |
| 1537 | present += " && (" + generate_isset_check(*m_iter) + ")"; |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 1540 | indent(out) << "boolean present_" << name << " = " << present << ";" << endl; |
| 1541 | indent(out) << "builder.append(present_" << name << ");" << endl; |
| 1542 | indent(out) << "if (present_" << name << ")" << endl; |
| 1543 | if (t->is_enum()) { |
| 1544 | indent(out) << " builder.append(" << name << ".getValue());" << endl; |
| 1545 | } else { |
| 1546 | indent(out) << " builder.append(" << name << ");" << endl; |
| 1547 | } |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | out << endl; |
Bryan Duxbury | 30e1de9 | 2009-02-10 18:36:56 +0000 | [diff] [blame] | 1551 | indent(out) << "return builder.toHashCode();" << endl; |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1552 | } else { |
Bryan Duxbury | 30e1de9 | 2009-02-10 18:36:56 +0000 | [diff] [blame] | 1553 | indent(out) << "return 0;" << endl; |
David Reiss | 14feb41 | 2008-03-02 06:29:23 +0000 | [diff] [blame] | 1554 | } |
Bryan Duxbury | 30e1de9 | 2009-02-10 18:36:56 +0000 | [diff] [blame] | 1555 | indent_down(); |
| 1556 | indent(out) << "}" << endl << endl; |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Bryan Duxbury | 684b4f9 | 2009-07-31 00:12:21 +0000 | [diff] [blame] | 1559 | void t_java_generator::generate_java_struct_compare_to(ofstream& out, t_struct* tstruct) { |
| 1560 | indent(out) << "public int compareTo(" << type_name(tstruct) << " other) {" << endl; |
| 1561 | indent_up(); |
| 1562 | |
| 1563 | indent(out) << "if (!getClass().equals(other.getClass())) {" << endl; |
| 1564 | indent(out) << " return getClass().getName().compareTo(other.getClass().getName());" << endl; |
| 1565 | indent(out) << "}" << endl; |
| 1566 | out << endl; |
| 1567 | |
| 1568 | indent(out) << "int lastComparison = 0;" << endl; |
| 1569 | indent(out) << type_name(tstruct) << " typedOther = (" << type_name(tstruct) << ")other;" << endl; |
| 1570 | out << endl; |
| 1571 | |
| 1572 | const vector<t_field*>& members = tstruct->get_members(); |
| 1573 | vector<t_field*>::const_iterator m_iter; |
| 1574 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 1575 | t_field* field = *m_iter; |
Bryan Duxbury | dee6d42 | 2010-02-23 19:06:25 +0000 | [diff] [blame] | 1576 | indent(out) << "lastComparison = Boolean.valueOf(" << generate_isset_check(field) << ").compareTo(typedOther." << generate_isset_check(field) << ");" << endl; |
Bryan Duxbury | 684b4f9 | 2009-07-31 00:12:21 +0000 | [diff] [blame] | 1577 | indent(out) << "if (lastComparison != 0) {" << endl; |
| 1578 | indent(out) << " return lastComparison;" << endl; |
| 1579 | indent(out) << "}" << endl; |
| 1580 | |
Bryan Duxbury | d920765 | 2010-09-17 19:27:36 +0000 | [diff] [blame] | 1581 | indent(out) << "if (" << generate_isset_check(field) << ") {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1582 | indent(out) << " lastComparison = org.apache.thrift.TBaseHelper.compareTo(this." << field->get_name() << ", typedOther." << field->get_name() << ");" << endl; |
Bryan Duxbury | dee6d42 | 2010-02-23 19:06:25 +0000 | [diff] [blame] | 1583 | indent(out) << " if (lastComparison != 0) {" << endl; |
| 1584 | indent(out) << " return lastComparison;" << endl; |
| 1585 | indent(out) << " }" << endl; |
Bryan Duxbury | 684b4f9 | 2009-07-31 00:12:21 +0000 | [diff] [blame] | 1586 | indent(out) << "}" << endl; |
| 1587 | } |
| 1588 | |
| 1589 | indent(out) << "return 0;" << endl; |
| 1590 | |
| 1591 | indent_down(); |
| 1592 | indent(out) << "}" << endl << endl; |
| 1593 | } |
| 1594 | |
David Reiss | 3b15ebc | 2008-03-02 06:29:19 +0000 | [diff] [blame] | 1595 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1596 | * Generates a function to read all the fields of the struct. |
| 1597 | * |
| 1598 | * @param tstruct The struct definition |
| 1599 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1600 | void t_java_generator::generate_java_struct_reader(ofstream& out, |
| 1601 | t_struct* tstruct) { |
Roger Meier | b6af6db | 2011-08-18 20:31:16 +0000 | [diff] [blame] | 1602 | (void) tstruct; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1603 | indent(out) << "public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1604 | indent_up(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1605 | indent(out) << "schemes.get(iprot.getScheme()).getScheme().read(iprot, this);" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1606 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1607 | indent(out) << "}" << endl << |
| 1608 | endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1611 | // generates java method to perform various checks |
| 1612 | // (e.g. check that all required fields are set) |
| 1613 | void t_java_generator::generate_java_validator(ofstream& out, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1614 | t_struct* tstruct){ |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1615 | indent(out) << "public void validate() throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 1616 | indent_up(); |
| 1617 | |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1618 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1619 | vector<t_field*>::const_iterator f_iter; |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 1620 | |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1621 | out << indent() << "// check for required fields" << endl; |
| 1622 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
David Reiss | af81de0 | 2009-08-27 20:27:09 +0000 | [diff] [blame] | 1623 | if ((*f_iter)->get_req() == t_field::T_REQUIRED) { |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1624 | if (bean_style_) { |
| 1625 | out << |
Bryan Duxbury | f4c71c7 | 2009-02-23 17:49:24 +0000 | [diff] [blame] | 1626 | indent() << "if (!" << generate_isset_check(*f_iter) << ") {" << endl << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1627 | indent() << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' is unset! Struct:\" + toString());" << endl << |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1628 | indent() << "}" << endl << endl; |
| 1629 | } else{ |
| 1630 | if (type_can_be_null((*f_iter)->get_type())) { |
| 1631 | indent(out) << "if (" << (*f_iter)->get_name() << " == null) {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1632 | indent(out) << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' was not present! Struct: \" + toString());" << endl; |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1633 | indent(out) << "}" << endl; |
| 1634 | } else { |
David Reiss | af81de0 | 2009-08-27 20:27:09 +0000 | [diff] [blame] | 1635 | indent(out) << "// alas, we cannot check '" << (*f_iter)->get_name() << "' because it's a primitive and you chose the non-beans generator." << endl; |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1636 | } |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 1637 | } |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1638 | } |
| 1639 | } |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 1640 | |
Bryan Duxbury | 8bbd844 | 2009-01-20 01:54:14 +0000 | [diff] [blame] | 1641 | indent_down(); |
| 1642 | indent(out) << "}" << endl << endl; |
| 1643 | } |
| 1644 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1645 | /** |
| 1646 | * Generates a function to write all the fields of the struct |
| 1647 | * |
| 1648 | * @param tstruct The struct definition |
| 1649 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1650 | void t_java_generator::generate_java_struct_writer(ofstream& out, |
| 1651 | t_struct* tstruct) { |
Roger Meier | b6af6db | 2011-08-18 20:31:16 +0000 | [diff] [blame] | 1652 | (void) tstruct; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1653 | indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1654 | indent_up(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1655 | indent(out) << "schemes.get(oprot.getScheme()).getScheme().write(oprot, this);" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1656 | |
| 1657 | indent_down(); |
Bryan Duxbury | f357417 | 2011-09-26 20:32:59 +0000 | [diff] [blame] | 1658 | indent(out) << "}" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1661 | /** |
| 1662 | * Generates a function to write all the fields of the struct, |
| 1663 | * which is a function result. These fields are only written |
| 1664 | * if they are set in the Isset array, and only one of them |
| 1665 | * can be set at a time. |
| 1666 | * |
| 1667 | * @param tstruct The struct definition |
| 1668 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1669 | void t_java_generator::generate_java_struct_result_writer(ofstream& out, |
| 1670 | t_struct* tstruct) { |
Roger Meier | b6af6db | 2011-08-18 20:31:16 +0000 | [diff] [blame] | 1671 | (void) tstruct; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1672 | indent(out) << "public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1673 | indent_up(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1674 | indent(out) << "schemes.get(oprot.getScheme()).getScheme().write(oprot, this);" << endl; |
| 1675 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1676 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1677 | indent(out) << " }" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 1680 | void t_java_generator::generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 1681 | (void) tstruct; |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 1682 | indent(out) << "public _Fields fieldForId(int fieldId) {" << endl; |
| 1683 | indent(out) << " return _Fields.findByThriftId(fieldId);" << endl; |
| 1684 | indent(out) << "}" << endl << endl; |
| 1685 | } |
| 1686 | |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1687 | void t_java_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) { |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1688 | indent(out) << "case " << constant_name(field_name) << ":" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1689 | indent_up(); |
| 1690 | |
| 1691 | if (type->is_base_type() && !type->is_string()) { |
| 1692 | t_base_type* base_type = (t_base_type*)type; |
| 1693 | |
Bryan Duxbury | d94cb08 | 2011-01-26 18:29:33 +0000 | [diff] [blame] | 1694 | indent(out) << "return " << type_name(type, true, false) << ".valueOf(" << (base_type->is_bool() ? "is" : "get") << cap_name << "());" << endl << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1695 | } else { |
| 1696 | indent(out) << "return get" << cap_name << "();" << endl << endl; |
| 1697 | } |
| 1698 | |
| 1699 | indent_down(); |
| 1700 | } |
| 1701 | |
| 1702 | void t_java_generator::generate_reflection_setters(ostringstream& out, t_type* type, string field_name, string cap_name) { |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1703 | indent(out) << "case " << constant_name(field_name) << ":" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1704 | indent_up(); |
Bryan Duxbury | 42336c1 | 2009-03-13 18:28:40 +0000 | [diff] [blame] | 1705 | indent(out) << "if (value == null) {" << endl; |
| 1706 | indent(out) << " unset" << get_cap_name(field_name) << "();" << endl; |
| 1707 | indent(out) << "} else {" << endl; |
| 1708 | indent(out) << " set" << cap_name << "((" << type_name(type, true, false) << ")value);" << endl; |
| 1709 | indent(out) << "}" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1710 | indent(out) << "break;" << endl << endl; |
| 1711 | |
| 1712 | indent_down(); |
| 1713 | } |
| 1714 | |
| 1715 | void t_java_generator::generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct) { |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1716 | std::ostringstream getter_stream; |
| 1717 | std::ostringstream setter_stream; |
| 1718 | |
| 1719 | // build up the bodies of both the getter and setter at once |
| 1720 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1721 | vector<t_field*>::const_iterator f_iter; |
| 1722 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1723 | t_field* field = *f_iter; |
| 1724 | t_type* type = get_true_type(field->get_type()); |
| 1725 | std::string field_name = field->get_name(); |
David Reiss | 2db737d | 2009-01-05 21:02:55 +0000 | [diff] [blame] | 1726 | std::string cap_name = get_cap_name(field_name); |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1727 | |
| 1728 | indent_up(); |
| 1729 | generate_reflection_setters(setter_stream, type, field_name, cap_name); |
| 1730 | generate_reflection_getters(getter_stream, type, field_name, cap_name); |
| 1731 | indent_down(); |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | // create the setter |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 1736 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1737 | indent(out) << "public void setFieldValue(_Fields field, Object value) {" << endl; |
| 1738 | indent(out) << " switch (field) {" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1739 | out << setter_stream.str(); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1740 | indent(out) << " }" << endl; |
| 1741 | indent(out) << "}" << endl << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1742 | |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1743 | // create the getter |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1744 | indent(out) << "public Object getFieldValue(_Fields field) {" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1745 | indent_up(); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1746 | indent(out) << "switch (field) {" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1747 | out << getter_stream.str(); |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1748 | indent(out) << "}" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1749 | indent(out) << "throw new IllegalStateException();" << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1750 | indent_down(); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1751 | indent(out) << "}" << endl << endl; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1754 | // Creates a generic isSet method that takes the field number as argument |
| 1755 | void t_java_generator::generate_generic_isset_method(std::ofstream& out, t_struct* tstruct){ |
| 1756 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1757 | vector<t_field*>::const_iterator f_iter; |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1758 | |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1759 | // create the isSet method |
Roger Meier | 26f817a | 2010-11-30 19:46:56 +0000 | [diff] [blame] | 1760 | indent(out) << "/** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1761 | indent(out) << "public boolean isSet(_Fields field) {" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1762 | indent_up(); |
Bryan Duxbury | 66467a7 | 2010-08-21 17:48:18 +0000 | [diff] [blame] | 1763 | indent(out) << "if (field == null) {" << endl; |
| 1764 | indent(out) << " throw new IllegalArgumentException();" << endl; |
| 1765 | indent(out) << "}" << endl << endl; |
| 1766 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1767 | indent(out) << "switch (field) {" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1768 | |
| 1769 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1770 | t_field* field = *f_iter; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1771 | indent(out) << "case " << constant_name(field->get_name()) << ":" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1772 | indent_up(); |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1773 | indent(out) << "return " << generate_isset_check(field) << ";" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1774 | indent_down(); |
| 1775 | } |
| 1776 | |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1777 | indent(out) << "}" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 1778 | indent(out) << "throw new IllegalStateException();" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1779 | indent_down(); |
| 1780 | indent(out) << "}" << endl << endl; |
| 1781 | } |
David Reiss | 31997ca | 2008-10-09 00:08:46 +0000 | [diff] [blame] | 1782 | |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1783 | /** |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1784 | * Generates a set of Java Bean boilerplate functions (setters, getters, etc.) |
| 1785 | * for the given struct. |
| 1786 | * |
| 1787 | * @param tstruct The struct definition |
| 1788 | */ |
| 1789 | void t_java_generator::generate_java_bean_boilerplate(ofstream& out, |
| 1790 | t_struct* tstruct) { |
| 1791 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1792 | vector<t_field*>::const_iterator f_iter; |
| 1793 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 1794 | t_field* field = *f_iter; |
| 1795 | t_type* type = get_true_type(field->get_type()); |
| 1796 | std::string field_name = field->get_name(); |
David Reiss | 2db737d | 2009-01-05 21:02:55 +0000 | [diff] [blame] | 1797 | std::string cap_name = get_cap_name(field_name); |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1798 | |
Bryan Duxbury | f18202f | 2009-10-01 22:52:25 +0000 | [diff] [blame] | 1799 | if (type->is_container()) { |
| 1800 | // Method to return the size of the collection |
| 1801 | indent(out) << "public int get" << cap_name; |
| 1802 | out << get_cap_name("size() {") << endl; |
| 1803 | |
| 1804 | indent_up(); |
| 1805 | indent(out) << "return (this." << field_name << " == null) ? 0 : " << |
| 1806 | "this." << field_name << ".size();" << endl; |
| 1807 | indent_down(); |
| 1808 | indent(out) << "}" << endl << endl; |
| 1809 | } |
| 1810 | |
| 1811 | if (type->is_set() || type->is_list()) { |
Bryan Duxbury | f18202f | 2009-10-01 22:52:25 +0000 | [diff] [blame] | 1812 | t_type* element_type; |
| 1813 | if (type->is_set()) { |
| 1814 | element_type = ((t_set*)type)->get_elem_type(); |
| 1815 | } else { |
| 1816 | element_type = ((t_list*)type)->get_elem_type(); |
| 1817 | } |
| 1818 | |
| 1819 | // Iterator getter for sets and lists |
| 1820 | indent(out) << "public java.util.Iterator<" << |
| 1821 | type_name(element_type, true, false) << "> get" << cap_name; |
| 1822 | out << get_cap_name("iterator() {") << endl; |
| 1823 | |
| 1824 | indent_up(); |
| 1825 | indent(out) << "return (this." << field_name << " == null) ? null : " << |
| 1826 | "this." << field_name << ".iterator();" << endl; |
| 1827 | indent_down(); |
| 1828 | indent(out) << "}" << endl << endl; |
| 1829 | |
| 1830 | // Add to set or list, create if the set/list is null |
| 1831 | indent(out); |
| 1832 | out << "public void add" << get_cap_name("to"); |
| 1833 | out << cap_name << "(" << type_name(element_type) << " elem) {" << endl; |
| 1834 | |
| 1835 | indent_up(); |
| 1836 | indent(out) << "if (this." << field_name << " == null) {" << endl; |
| 1837 | indent_up(); |
| 1838 | indent(out) << "this." << field_name << " = new " << type_name(type, false, true) << |
| 1839 | "();" << endl; |
| 1840 | indent_down(); |
| 1841 | indent(out) << "}" << endl; |
| 1842 | indent(out) << "this." << field_name << ".add(elem);" << endl; |
| 1843 | indent_down(); |
| 1844 | indent(out) << "}" << endl << endl; |
Bryan Duxbury | f18202f | 2009-10-01 22:52:25 +0000 | [diff] [blame] | 1845 | } else if (type->is_map()) { |
| 1846 | // Put to map |
| 1847 | t_type* key_type = ((t_map*)type)->get_key_type(); |
| 1848 | t_type* val_type = ((t_map*)type)->get_val_type(); |
| 1849 | |
| 1850 | indent(out); |
| 1851 | out << "public void put" << get_cap_name("to"); |
| 1852 | out << cap_name << "(" << type_name(key_type) << " key, " |
| 1853 | << type_name(val_type) << " val) {" << endl; |
| 1854 | |
| 1855 | indent_up(); |
| 1856 | indent(out) << "if (this." << field_name << " == null) {" << endl; |
| 1857 | indent_up(); |
| 1858 | indent(out) << "this." << field_name << " = new " << |
| 1859 | type_name(type, false, true) << "();" << endl; |
| 1860 | indent_down(); |
| 1861 | indent(out) << "}" << endl; |
| 1862 | indent(out) << "this." << field_name << ".put(key, val);" << endl; |
| 1863 | indent_down(); |
| 1864 | indent(out) << "}" << endl << endl; |
| 1865 | } |
| 1866 | |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1867 | // Simple getter |
David Reiss | f33e03c | 2008-10-29 00:07:49 +0000 | [diff] [blame] | 1868 | generate_java_doc(out, field); |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1869 | if (type->is_base_type() && ((t_base_type*)type)->is_binary()) { |
| 1870 | indent(out) << "public byte[] get" << cap_name << "() {" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 1871 | indent(out) << " set" << cap_name << "(org.apache.thrift.TBaseHelper.rightSize(" << field_name << "));" << endl; |
Bryan Duxbury | a5eb848 | 2010-11-24 22:30:38 +0000 | [diff] [blame] | 1872 | indent(out) << " return " << field_name << " == null ? null : " << field_name << ".array();" << endl; |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1873 | indent(out) << "}" << endl << endl; |
| 1874 | |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 1875 | indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << cap_name << "() {" << endl; |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1876 | indent(out) << " return " << field_name << ";" << endl; |
| 1877 | indent(out) << "}" << endl << endl; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1878 | } else { |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1879 | indent(out) << "public " << type_name(type); |
| 1880 | if (type->is_base_type() && |
| 1881 | ((t_base_type*)type)->get_base() == t_base_type::TYPE_BOOL) { |
| 1882 | out << " is"; |
| 1883 | } else { |
| 1884 | out << " get"; |
| 1885 | } |
| 1886 | out << cap_name << "() {" << endl; |
| 1887 | indent_up(); |
| 1888 | indent(out) << "return this." << field_name << ";" << endl; |
| 1889 | indent_down(); |
| 1890 | indent(out) << "}" << endl << endl; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1891 | } |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1892 | |
| 1893 | // Simple setter |
David Reiss | f33e03c | 2008-10-29 00:07:49 +0000 | [diff] [blame] | 1894 | generate_java_doc(out, field); |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1895 | if (type->is_base_type() && ((t_base_type*)type)->is_binary()) { |
| 1896 | indent(out) << "public "; |
| 1897 | if (bean_style_) { |
| 1898 | out << "void"; |
| 1899 | } else { |
| 1900 | out << type_name(tstruct); |
| 1901 | } |
| 1902 | out << " set" << cap_name << "(byte[] " << field_name << ") {" << endl; |
Bryan Duxbury | a5eb848 | 2010-11-24 22:30:38 +0000 | [diff] [blame] | 1903 | indent(out) << " set" << cap_name << "(" << field_name << " == null ? (ByteBuffer)null : ByteBuffer.wrap(" << field_name << "));" << endl; |
Bryan Duxbury | a7420b6 | 2010-09-13 15:42:36 +0000 | [diff] [blame] | 1904 | if (!bean_style_) { |
| 1905 | indent(out) << " return this;" << endl; |
| 1906 | } |
| 1907 | indent(out) << "}" << endl << endl; |
| 1908 | } |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 1909 | indent(out) << "public "; |
| 1910 | if (bean_style_) { |
| 1911 | out << "void"; |
| 1912 | } else { |
| 1913 | out << type_name(tstruct); |
| 1914 | } |
| 1915 | out << " set" << cap_name << "(" << type_name(type) << " " << field_name << ") {" << endl; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1916 | indent_up(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 1917 | indent(out) << "this." << field_name << " = " << field_name << ";" << endl; |
| 1918 | generate_isset_set(out, field, ""); |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 1919 | if (!bean_style_) { |
| 1920 | indent(out) << "return this;" << endl; |
| 1921 | } |
David Reiss | 5af01bc | 2008-10-01 03:24:50 +0000 | [diff] [blame] | 1922 | |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1923 | indent_down(); |
| 1924 | indent(out) << "}" << endl << endl; |
| 1925 | |
| 1926 | // Unsetter |
| 1927 | indent(out) << "public void unset" << cap_name << "() {" << endl; |
| 1928 | indent_up(); |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1929 | if (type_can_be_null(type)) { |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1930 | indent(out) << "this." << field_name << " = null;" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1931 | } else { |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1932 | indent(out) << "__isset_bit_vector.clear(" << isset_field_id(field) << ");" << endl; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1933 | } |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1934 | indent_down(); |
| 1935 | indent(out) << "}" << endl << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1936 | |
| 1937 | // isSet method |
Roger Meier | 26f817a | 2010-11-30 19:46:56 +0000 | [diff] [blame] | 1938 | indent(out) << "/** Returns true if field " << field_name << " is set (has been assigned a value) and false otherwise */" << endl; |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1939 | indent(out) << "public boolean is" << get_cap_name("set") << cap_name << "() {" << endl; |
| 1940 | indent_up(); |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1941 | if (type_can_be_null(type)) { |
Bryan Duxbury | c8ee358 | 2010-05-04 14:01:14 +0000 | [diff] [blame] | 1942 | indent(out) << "return this." << field_name << " != null;" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1943 | } else { |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1944 | indent(out) << "return __isset_bit_vector.get(" << isset_field_id(field) << ");" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1945 | } |
David Reiss | 25be92b | 2009-01-05 21:02:58 +0000 | [diff] [blame] | 1946 | indent_down(); |
| 1947 | indent(out) << "}" << endl << endl; |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1948 | |
| 1949 | indent(out) << "public void set" << cap_name << get_cap_name("isSet") << "(boolean value) {" << endl; |
| 1950 | indent_up(); |
| 1951 | if (type_can_be_null(type)) { |
| 1952 | indent(out) << "if (!value) {" << endl; |
| 1953 | indent(out) << " this." << field_name << " = null;" << endl; |
| 1954 | indent(out) << "}" << endl; |
| 1955 | } else { |
| 1956 | indent(out) << "__isset_bit_vector.set(" << isset_field_id(field) << ", value);" << endl; |
Bryan Duxbury | d20a1d3 | 2009-02-07 01:08:58 +0000 | [diff] [blame] | 1957 | } |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 1958 | indent_down(); |
| 1959 | indent(out) << "}" << endl << endl; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | /** |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1964 | * Generates a toString() method for the given struct |
| 1965 | * |
| 1966 | * @param tstruct The struct definition |
| 1967 | */ |
| 1968 | void t_java_generator::generate_java_struct_tostring(ofstream& out, |
| 1969 | t_struct* tstruct) { |
Bryan Duxbury | 369417a | 2009-01-27 22:09:33 +0000 | [diff] [blame] | 1970 | out << indent() << "@Override" << endl << |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1971 | indent() << "public String toString() {" << endl; |
| 1972 | indent_up(); |
| 1973 | |
| 1974 | out << |
eletuchy | 447d7d23 | 2007-06-13 04:24:11 +0000 | [diff] [blame] | 1975 | indent() << "StringBuilder sb = new StringBuilder(\"" << tstruct->get_name() << "(\");" << endl; |
David Reiss | 8684554 | 2008-10-01 18:32:47 +0000 | [diff] [blame] | 1976 | out << indent() << "boolean first = true;" << endl << endl; |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1977 | |
| 1978 | const vector<t_field*>& fields = tstruct->get_members(); |
| 1979 | vector<t_field*>::const_iterator f_iter; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1980 | bool first = true; |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1981 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1982 | bool could_be_unset = (*f_iter)->get_req() == t_field::T_OPTIONAL; |
| 1983 | if(could_be_unset) { |
| 1984 | indent(out) << "if (" << generate_isset_check(*f_iter) << ") {" << endl; |
David Reiss | 8684554 | 2008-10-01 18:32:47 +0000 | [diff] [blame] | 1985 | indent_up(); |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 1986 | } |
David Reiss | 8684554 | 2008-10-01 18:32:47 +0000 | [diff] [blame] | 1987 | |
David Reiss | 30a9360 | 2008-11-21 21:31:36 +0000 | [diff] [blame] | 1988 | t_field* field = (*f_iter); |
| 1989 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1990 | if (!first) { |
| 1991 | indent(out) << "if (!first) sb.append(\", \");" << endl; |
| 1992 | } |
David Reiss | 8684554 | 2008-10-01 18:32:47 +0000 | [diff] [blame] | 1993 | indent(out) << "sb.append(\"" << (*f_iter)->get_name() << ":\");" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1994 | bool can_be_null = type_can_be_null(field->get_type()); |
| 1995 | if (can_be_null) { |
| 1996 | indent(out) << "if (this." << (*f_iter)->get_name() << " == null) {" << endl; |
David Reiss | 30a9360 | 2008-11-21 21:31:36 +0000 | [diff] [blame] | 1997 | indent(out) << " sb.append(\"null\");" << endl; |
| 1998 | indent(out) << "} else {" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 1999 | indent_up(); |
| 2000 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2001 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 2002 | if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2003 | indent(out) << "org.apache.thrift.TBaseHelper.toString(this." << field->get_name() << ", sb);" << endl; |
David Reiss | 30a9360 | 2008-11-21 21:31:36 +0000 | [diff] [blame] | 2004 | } else { |
| 2005 | indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl; |
| 2006 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2007 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 2008 | if (can_be_null) { |
David Reiss | 8684554 | 2008-10-01 18:32:47 +0000 | [diff] [blame] | 2009 | indent_down(); |
| 2010 | indent(out) << "}" << endl; |
| 2011 | } |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 2012 | indent(out) << "first = false;" << endl; |
| 2013 | |
| 2014 | if(could_be_unset) { |
| 2015 | indent_down(); |
| 2016 | indent(out) << "}" << endl; |
| 2017 | } |
| 2018 | first = false; |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 2019 | } |
| 2020 | out << |
| 2021 | indent() << "sb.append(\")\");" << endl << |
| 2022 | indent() << "return sb.toString();" << endl; |
| 2023 | |
| 2024 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2025 | indent(out) << "}" << endl << endl; |
Mark Slee | 8109757 | 2007-03-14 03:07:00 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 2028 | /** |
| 2029 | * Generates a static map with meta data to store information such as fieldID to |
| 2030 | * fieldName mapping |
| 2031 | * |
| 2032 | * @param tstruct The struct definition |
| 2033 | */ |
| 2034 | void t_java_generator::generate_java_meta_data_map(ofstream& out, |
| 2035 | t_struct* tstruct) { |
| 2036 | const vector<t_field*>& fields = tstruct->get_members(); |
| 2037 | vector<t_field*>::const_iterator f_iter; |
| 2038 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2039 | // Static Map with fieldID -> org.apache.thrift.meta_data.FieldMetaData mappings |
| 2040 | indent(out) << "public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;" << endl; |
Bryan Duxbury | 15b7b28 | 2010-03-23 05:39:18 +0000 | [diff] [blame] | 2041 | indent(out) << "static {" << endl; |
| 2042 | indent_up(); |
| 2043 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2044 | indent(out) << "Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);" << endl; |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Populate map |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 2047 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 2048 | t_field* field = *f_iter; |
| 2049 | std::string field_name = field->get_name(); |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2050 | indent(out) << "tmpMap.put(_Fields." << constant_name(field_name) << ", new org.apache.thrift.meta_data.FieldMetaData(\"" << field_name << "\", "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Set field requirement type (required, optional, etc.) |
| 2053 | if (field->get_req() == t_field::T_REQUIRED) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2054 | out << "org.apache.thrift.TFieldRequirementType.REQUIRED, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2055 | } else if (field->get_req() == t_field::T_OPTIONAL) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2056 | out << "org.apache.thrift.TFieldRequirementType.OPTIONAL, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2057 | } else { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2058 | out << "org.apache.thrift.TFieldRequirementType.DEFAULT, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 2061 | // Create value meta data |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2062 | generate_field_value_meta_data(out, field->get_type()); |
| 2063 | out << "));" << endl; |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 2064 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 2065 | |
Bryan Duxbury | 15b7b28 | 2010-03-23 05:39:18 +0000 | [diff] [blame] | 2066 | indent(out) << "metaDataMap = Collections.unmodifiableMap(tmpMap);" << endl; |
| 2067 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2068 | indent(out) << "org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(" << type_name(tstruct) << ".class, metaDataMap);" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 2069 | indent_down(); |
| 2070 | indent(out) << "}" << endl << endl; |
David Reiss | b936ffd | 2009-01-05 21:02:52 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2073 | /** |
| 2074 | * Returns a string with the java representation of the given thrift type |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2075 | * (e.g. for the type struct it returns "org.apache.thrift.protocol.TType.STRUCT") |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2076 | */ |
| 2077 | std::string t_java_generator::get_java_type_string(t_type* type) { |
| 2078 | if (type->is_list()){ |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2079 | return "org.apache.thrift.protocol.TType.LIST"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2080 | } else if (type->is_map()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2081 | return "org.apache.thrift.protocol.TType.MAP"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2082 | } else if (type->is_set()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2083 | return "org.apache.thrift.protocol.TType.SET"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2084 | } else if (type->is_struct() || type->is_xception()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2085 | return "org.apache.thrift.protocol.TType.STRUCT"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2086 | } else if (type->is_enum()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2087 | return "org.apache.thrift.protocol.TType.ENUM"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2088 | } else if (type->is_typedef()) { |
| 2089 | return get_java_type_string(((t_typedef*)type)->get_type()); |
| 2090 | } else if (type->is_base_type()) { |
| 2091 | switch (((t_base_type*)type)->get_base()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2092 | case t_base_type::TYPE_VOID : return "org.apache.thrift.protocol.TType.VOID"; break; |
| 2093 | case t_base_type::TYPE_STRING : return "org.apache.thrift.protocol.TType.STRING"; break; |
| 2094 | case t_base_type::TYPE_BOOL : return "org.apache.thrift.protocol.TType.BOOL"; break; |
| 2095 | case t_base_type::TYPE_BYTE : return "org.apache.thrift.protocol.TType.BYTE"; break; |
| 2096 | case t_base_type::TYPE_I16 : return "org.apache.thrift.protocol.TType.I16"; break; |
| 2097 | case t_base_type::TYPE_I32 : return "org.apache.thrift.protocol.TType.I32"; break; |
| 2098 | case t_base_type::TYPE_I64 : return "org.apache.thrift.protocol.TType.I64"; break; |
| 2099 | case t_base_type::TYPE_DOUBLE : return "org.apache.thrift.protocol.TType.DOUBLE"; break; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2100 | default : throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!"); break; // This should never happen! |
| 2101 | } |
| 2102 | } else { |
| 2103 | throw std::runtime_error("Unknown thrift type \"" + type->get_name() + "\" passed to t_java_generator::get_java_type_string!"); // This should never happen! |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | void t_java_generator::generate_field_value_meta_data(std::ofstream& out, t_type* type){ |
| 2108 | out << endl; |
| 2109 | indent_up(); |
| 2110 | indent_up(); |
| 2111 | if (type->is_struct()){ |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2112 | indent(out) << "new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, " << type_name(type) << ".class"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2113 | } else if (type->is_container()){ |
| 2114 | if (type->is_list()){ |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2115 | indent(out) << "new org.apache.thrift.meta_data.ListMetaData(org.apache.thrift.protocol.TType.LIST, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2116 | t_type* elem_type = ((t_list*)type)->get_elem_type(); |
| 2117 | generate_field_value_meta_data(out, elem_type); |
| 2118 | } else if (type->is_set()){ |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2119 | indent(out) << "new org.apache.thrift.meta_data.SetMetaData(org.apache.thrift.protocol.TType.SET, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2120 | t_type* elem_type = ((t_list*)type)->get_elem_type(); |
| 2121 | generate_field_value_meta_data(out, elem_type); |
| 2122 | } else{ // map |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2123 | indent(out) << "new org.apache.thrift.meta_data.MapMetaData(org.apache.thrift.protocol.TType.MAP, "; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2124 | t_type* key_type = ((t_map*)type)->get_key_type(); |
| 2125 | t_type* val_type = ((t_map*)type)->get_val_type(); |
| 2126 | generate_field_value_meta_data(out, key_type); |
| 2127 | out << ", "; |
| 2128 | generate_field_value_meta_data(out, val_type); |
| 2129 | } |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 2130 | } else if (type->is_enum()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2131 | indent(out) << "new org.apache.thrift.meta_data.EnumMetaData(org.apache.thrift.protocol.TType.ENUM, " << type_name(type) << ".class"; |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2132 | } else { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2133 | indent(out) << "new org.apache.thrift.meta_data.FieldValueMetaData(" << get_java_type_string(type); |
Bryan Duxbury | 15e2930 | 2010-04-22 01:05:02 +0000 | [diff] [blame] | 2134 | if (type->is_typedef()) { |
| 2135 | indent(out) << ", \"" << ((t_typedef*)type)->get_symbolic() << "\""; |
Bryan Duxbury | b3d0aa0 | 2010-10-06 20:00:03 +0000 | [diff] [blame] | 2136 | } else if (((t_base_type*)type)->is_binary()) { |
| 2137 | indent(out) << ", true"; |
Bryan Duxbury | 15e2930 | 2010-04-22 01:05:02 +0000 | [diff] [blame] | 2138 | } |
Bryan Duxbury | 986d705 | 2009-01-29 01:51:08 +0000 | [diff] [blame] | 2139 | } |
| 2140 | out << ")"; |
| 2141 | indent_down(); |
| 2142 | indent_down(); |
| 2143 | } |
| 2144 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2145 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2146 | /** |
| 2147 | * Generates a thrift service. In C++, this comprises an entirely separate |
| 2148 | * header and source file. The header file defines the methods and includes |
| 2149 | * the data types defined in the main header file, and the implementation |
| 2150 | * file contains implementations of the basic printer and default interfaces. |
| 2151 | * |
| 2152 | * @param tservice The service definition |
| 2153 | */ |
| 2154 | void t_java_generator::generate_service(t_service* tservice) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2155 | // Make output file |
Mark Slee | 98e962b | 2007-02-20 18:44:05 +0000 | [diff] [blame] | 2156 | string f_service_name = package_dir_+"/"+service_name_+".java"; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2157 | f_service_.open(f_service_name.c_str()); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2158 | |
| 2159 | f_service_ << |
| 2160 | autogen_comment() << |
| 2161 | java_package() << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2162 | java_type_imports(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2163 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2164 | f_service_ << "public class " << service_name_ << " {" << endl << endl; |
Mark Slee | dafa3cf | 2006-09-02 23:56:49 +0000 | [diff] [blame] | 2165 | indent_up(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Generate the three main parts of the service |
| 2168 | generate_service_interface(tservice); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2169 | generate_service_async_interface(tservice); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2170 | generate_service_client(tservice); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2171 | generate_service_async_client(tservice); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2172 | generate_service_server(tservice); |
| 2173 | generate_service_helpers(tservice); |
| 2174 | |
Mark Slee | dafa3cf | 2006-09-02 23:56:49 +0000 | [diff] [blame] | 2175 | indent_down(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2176 | f_service_ << |
| 2177 | "}" << endl; |
| 2178 | f_service_.close(); |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * Generates a service interface definition. |
| 2183 | * |
| 2184 | * @param tservice The service to generate a header definition for |
| 2185 | */ |
| 2186 | void t_java_generator::generate_service_interface(t_service* tservice) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2187 | string extends = ""; |
| 2188 | string extends_iface = ""; |
| 2189 | if (tservice->get_extends() != NULL) { |
| 2190 | extends = type_name(tservice->get_extends()); |
| 2191 | extends_iface = " extends " + extends + ".Iface"; |
| 2192 | } |
| 2193 | |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 2194 | generate_java_doc(f_service_, tservice); |
| 2195 | f_service_ << indent() << "public interface Iface" << extends_iface << |
| 2196 | " {" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2197 | indent_up(); |
| 2198 | vector<t_function*> functions = tservice->get_functions(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2199 | vector<t_function*>::iterator f_iter; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2200 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 2201 | generate_java_doc(f_service_, *f_iter); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2202 | indent(f_service_) << "public " << function_signature(*f_iter) << ";" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2203 | } |
| 2204 | indent_down(); |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2205 | f_service_ << indent() << "}" << endl << endl; |
| 2206 | } |
| 2207 | |
| 2208 | void t_java_generator::generate_service_async_interface(t_service* tservice) { |
| 2209 | string extends = ""; |
| 2210 | string extends_iface = ""; |
| 2211 | if (tservice->get_extends() != NULL) { |
| 2212 | extends = type_name(tservice->get_extends()); |
| 2213 | extends_iface = " extends " + extends + " .AsyncIface"; |
| 2214 | } |
| 2215 | |
| 2216 | f_service_ << indent() << "public interface AsyncIface" << extends_iface << " {" << endl << endl; |
| 2217 | indent_up(); |
| 2218 | vector<t_function*> functions = tservice->get_functions(); |
| 2219 | vector<t_function*>::iterator f_iter; |
| 2220 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2221 | indent(f_service_) << "public " << function_signature_async(*f_iter, true) << " throws org.apache.thrift.TException;" << endl << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2222 | } |
| 2223 | indent_down(); |
| 2224 | f_service_ << indent() << "}" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2227 | /** |
| 2228 | * Generates structs for all the service args and return types |
| 2229 | * |
| 2230 | * @param tservice The service |
| 2231 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2232 | void t_java_generator::generate_service_helpers(t_service* tservice) { |
| 2233 | vector<t_function*> functions = tservice->get_functions(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2234 | vector<t_function*>::iterator f_iter; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2235 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 2236 | t_struct* ts = (*f_iter)->get_arglist(); |
| 2237 | generate_java_struct_definition(f_service_, ts, false, true); |
| 2238 | generate_function_helpers(*f_iter); |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | /** |
| 2243 | * Generates a service client definition. |
| 2244 | * |
| 2245 | * @param tservice The service to generate a server for. |
| 2246 | */ |
| 2247 | void t_java_generator::generate_service_client(t_service* tservice) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2248 | string extends = ""; |
| 2249 | string extends_client = ""; |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2250 | if (tservice->get_extends() == NULL) { |
| 2251 | extends_client = "org.apache.thrift.TServiceClient"; |
| 2252 | } else { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2253 | extends = type_name(tservice->get_extends()); |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2254 | extends_client = extends + ".Client"; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | indent(f_service_) << |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2258 | "public static class Client extends " << extends_client << " implements Iface {" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2259 | indent_up(); |
| 2260 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2261 | indent(f_service_) << "public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {" << endl; |
Bryan Duxbury | b1f7f79 | 2010-04-03 23:19:52 +0000 | [diff] [blame] | 2262 | indent_up(); |
| 2263 | indent(f_service_) << "public Factory() {}" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2264 | indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol prot) {" << endl; |
Bryan Duxbury | b1f7f79 | 2010-04-03 23:19:52 +0000 | [diff] [blame] | 2265 | indent_up(); |
| 2266 | indent(f_service_) << "return new Client(prot);" << endl; |
| 2267 | indent_down(); |
| 2268 | indent(f_service_) << "}" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2269 | indent(f_service_) << "public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {" << endl; |
Bryan Duxbury | b1f7f79 | 2010-04-03 23:19:52 +0000 | [diff] [blame] | 2270 | indent_up(); |
| 2271 | indent(f_service_) << "return new Client(iprot, oprot);" << endl; |
| 2272 | indent_down(); |
| 2273 | indent(f_service_) << "}" << endl; |
| 2274 | indent_down(); |
| 2275 | indent(f_service_) << "}" << endl << endl; |
| 2276 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2277 | indent(f_service_) << "public Client(org.apache.thrift.protocol.TProtocol prot)" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2278 | scope_up(f_service_); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2279 | indent(f_service_) << "super(prot, prot);" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2280 | scope_down(f_service_); |
| 2281 | f_service_ << endl; |
| 2282 | |
| 2283 | indent(f_service_) << |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2284 | "public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {" << endl; |
| 2285 | indent(f_service_) << " super(iprot, oprot);" << endl; |
| 2286 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Generate client method implementations |
| 2289 | vector<t_function*> functions = tservice->get_functions(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2290 | vector<t_function*>::const_iterator f_iter; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2291 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 2292 | string funname = (*f_iter)->get_name(); |
| 2293 | |
| 2294 | // Open function |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2295 | indent(f_service_) << "public " << function_signature(*f_iter) << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2296 | scope_up(f_service_); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2297 | indent(f_service_) << "send_" << funname << "("; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2298 | |
| 2299 | // Get the struct of function call params |
| 2300 | t_struct* arg_struct = (*f_iter)->get_arglist(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2301 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2302 | // Declare the function arguments |
| 2303 | const vector<t_field*>& fields = arg_struct->get_members(); |
| 2304 | vector<t_field*>::const_iterator fld_iter; |
| 2305 | bool first = true; |
| 2306 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 2307 | if (first) { |
| 2308 | first = false; |
| 2309 | } else { |
| 2310 | f_service_ << ", "; |
| 2311 | } |
| 2312 | f_service_ << (*fld_iter)->get_name(); |
| 2313 | } |
| 2314 | f_service_ << ");" << endl; |
| 2315 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2316 | if (!(*f_iter)->is_oneway()) { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2317 | f_service_ << indent(); |
| 2318 | if (!(*f_iter)->get_returntype()->is_void()) { |
| 2319 | f_service_ << "return "; |
| 2320 | } |
| 2321 | f_service_ << |
| 2322 | "recv_" << funname << "();" << endl; |
| 2323 | } |
| 2324 | scope_down(f_service_); |
| 2325 | f_service_ << endl; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2326 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2327 | t_function send_function(g_type_void, |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2328 | string("send_") + (*f_iter)->get_name(), |
| 2329 | (*f_iter)->get_arglist()); |
| 2330 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2331 | string argsname = (*f_iter)->get_name() + "_args"; |
| 2332 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2333 | // Open function |
| 2334 | indent(f_service_) << |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2335 | "public " << function_signature(&send_function) << endl; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2336 | scope_up(f_service_); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2337 | |
| 2338 | // Serialize the request |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2339 | indent(f_service_) << argsname << " args = new " << argsname << "();" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2340 | |
| 2341 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2342 | indent(f_service_) << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2345 | indent(f_service_) << "sendBase(\"" << funname << "\", args);" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2346 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2347 | scope_down(f_service_); |
| 2348 | f_service_ << endl; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2349 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2350 | if (!(*f_iter)->is_oneway()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2351 | string resultname = (*f_iter)->get_name() + "_result"; |
| 2352 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2353 | t_struct noargs(program_); |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2354 | t_function recv_function((*f_iter)->get_returntype(), |
| 2355 | string("recv_") + (*f_iter)->get_name(), |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2356 | &noargs, |
| 2357 | (*f_iter)->get_xceptions()); |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2358 | // Open function |
| 2359 | indent(f_service_) << |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2360 | "public " << function_signature(&recv_function) << endl; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2361 | scope_up(f_service_); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2362 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2363 | f_service_ << |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2364 | indent() << resultname << " result = new " << resultname << "();" << endl << |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2365 | indent() << "receiveBase(result, \"" << funname << "\");" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2366 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2367 | // Careful, only return _result if not a void function |
| 2368 | if (!(*f_iter)->get_returntype()->is_void()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2369 | f_service_ << |
Bryan Duxbury | e3c3d19 | 2009-03-07 03:08:37 +0000 | [diff] [blame] | 2370 | indent() << "if (result." << generate_isset_check("success") << ") {" << endl << |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2371 | indent() << " return result.success;" << endl << |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2372 | indent() << "}" << endl; |
| 2373 | } |
| 2374 | |
| 2375 | t_struct* xs = (*f_iter)->get_xceptions(); |
| 2376 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 2377 | vector<t_field*>::const_iterator x_iter; |
| 2378 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 2379 | f_service_ << |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 2380 | indent() << "if (result." << (*x_iter)->get_name() << " != null) {" << endl << |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2381 | indent() << " throw result." << (*x_iter)->get_name() << ";" << endl << |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2382 | indent() << "}" << endl; |
| 2383 | } |
| 2384 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2385 | // If you get here it's an exception, unless a void function |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2386 | if ((*f_iter)->get_returntype()->is_void()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2387 | indent(f_service_) << "return;" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2388 | } else { |
| 2389 | f_service_ << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2390 | indent() << "throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2391 | } |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2392 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2393 | // Close function |
| 2394 | scope_down(f_service_); |
| 2395 | f_service_ << endl; |
| 2396 | } |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2400 | indent(f_service_) << "}" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2403 | void t_java_generator::generate_service_async_client(t_service* tservice) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2404 | string extends = "org.apache.thrift.async.TAsyncClient"; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2405 | string extends_client = ""; |
| 2406 | if (tservice->get_extends() != NULL) { |
| 2407 | extends = type_name(tservice->get_extends()) + ".AsyncClient"; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | indent(f_service_) << |
| 2411 | "public static class AsyncClient extends " << extends << " implements AsyncIface {" << endl; |
| 2412 | indent_up(); |
| 2413 | |
| 2414 | // Factory method |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2415 | indent(f_service_) << "public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {" << endl; |
| 2416 | indent(f_service_) << " private org.apache.thrift.async.TAsyncClientManager clientManager;" << endl; |
| 2417 | indent(f_service_) << " private org.apache.thrift.protocol.TProtocolFactory protocolFactory;" << endl; |
| 2418 | indent(f_service_) << " public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2419 | indent(f_service_) << " this.clientManager = clientManager;" << endl; |
| 2420 | indent(f_service_) << " this.protocolFactory = protocolFactory;" << endl; |
| 2421 | indent(f_service_) << " }" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2422 | indent(f_service_) << " public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2423 | indent(f_service_) << " return new AsyncClient(protocolFactory, clientManager, transport);" << endl; |
| 2424 | indent(f_service_) << " }" << endl; |
| 2425 | indent(f_service_) << "}" << endl << endl; |
| 2426 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2427 | indent(f_service_) << "public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2428 | indent(f_service_) << " super(protocolFactory, clientManager, transport);" << endl; |
| 2429 | indent(f_service_) << "}" << endl << endl; |
| 2430 | |
| 2431 | // Generate client method implementations |
| 2432 | vector<t_function*> functions = tservice->get_functions(); |
| 2433 | vector<t_function*>::const_iterator f_iter; |
| 2434 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 2435 | string funname = (*f_iter)->get_name(); |
| 2436 | t_type* ret_type = (*f_iter)->get_returntype(); |
| 2437 | t_struct* arg_struct = (*f_iter)->get_arglist(); |
| 2438 | string funclassname = funname + "_call"; |
| 2439 | const vector<t_field*>& fields = arg_struct->get_members(); |
| 2440 | const std::vector<t_field*>& xceptions = (*f_iter)->get_xceptions()->get_members(); |
| 2441 | vector<t_field*>::const_iterator fld_iter; |
| 2442 | string args_name = (*f_iter)->get_name() + "_args"; |
| 2443 | string result_name = (*f_iter)->get_name() + "_result"; |
| 2444 | |
| 2445 | // Main method body |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2446 | indent(f_service_) << "public " << function_signature_async(*f_iter, false) << " throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2447 | indent(f_service_) << " checkReady();" << endl; |
Bryan Duxbury | ee7c7bb | 2011-03-15 17:25:23 +0000 | [diff] [blame] | 2448 | indent(f_service_) << " " << funclassname << " method_call = new " + funclassname + "(" << async_argument_list(*f_iter, arg_struct, ret_type) << ", this, ___protocolFactory, ___transport);" << endl; |
| 2449 | indent(f_service_) << " this.___currentMethod = method_call;" << endl; |
| 2450 | indent(f_service_) << " ___manager.call(method_call);" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2451 | indent(f_service_) << "}" << endl; |
| 2452 | |
| 2453 | f_service_ << endl; |
| 2454 | |
| 2455 | // TAsyncMethod object for this function call |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2456 | indent(f_service_) << "public static class " + funclassname + " extends org.apache.thrift.async.TAsyncMethodCall {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2457 | indent_up(); |
| 2458 | |
| 2459 | // Member variables |
| 2460 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 2461 | indent(f_service_) << "private " + type_name((*fld_iter)->get_type()) + " " + (*fld_iter)->get_name() + ";" << endl; |
| 2462 | } |
| 2463 | |
| 2464 | // NOTE since we use a new Client instance to deserialize, let's keep seqid to 0 for now |
| 2465 | // indent(f_service_) << "private int seqid;" << endl << endl; |
| 2466 | |
| 2467 | // Constructor |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2468 | indent(f_service_) << "public " + funclassname + "(" + async_argument_list(*f_iter, arg_struct, ret_type, true) << ", org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2469 | indent(f_service_) << " super(client, protocolFactory, transport, resultHandler, " << ((*f_iter)->is_oneway() ? "true" : "false") << ");" << endl; |
| 2470 | |
| 2471 | // Assign member variables |
| 2472 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 2473 | indent(f_service_) << " this." + (*fld_iter)->get_name() + " = " + (*fld_iter)->get_name() + ";" << endl; |
| 2474 | } |
| 2475 | |
| 2476 | indent(f_service_) << "}" << endl << endl; |
| 2477 | |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2478 | indent(f_service_) << "public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2479 | indent_up(); |
| 2480 | |
| 2481 | // Serialize request |
| 2482 | // NOTE we are leaving seqid as 0, for now (see above) |
| 2483 | f_service_ << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2484 | indent() << "prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage(\"" << funname << "\", org.apache.thrift.protocol.TMessageType.CALL, 0));" << endl << |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2485 | indent() << args_name << " args = new " << args_name << "();" << endl; |
| 2486 | |
| 2487 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { |
| 2488 | f_service_ << indent() << "args.set" << get_cap_name((*fld_iter)->get_name()) << "(" << (*fld_iter)->get_name() << ");" << endl; |
| 2489 | } |
| 2490 | |
| 2491 | f_service_ << |
| 2492 | indent() << "args.write(prot);" << endl << |
| 2493 | indent() << "prot.writeMessageEnd();" << endl; |
| 2494 | |
| 2495 | indent_down(); |
| 2496 | indent(f_service_) << "}" << endl << endl; |
| 2497 | |
| 2498 | // Return method |
| 2499 | indent(f_service_) << "public " + type_name(ret_type) + " getResult() throws "; |
| 2500 | vector<t_field*>::const_iterator x_iter; |
| 2501 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 2502 | f_service_ << type_name((*x_iter)->get_type(), false, false) + ", "; |
| 2503 | } |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2504 | f_service_ << "org.apache.thrift.TException {" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2505 | |
| 2506 | indent_up(); |
| 2507 | f_service_ << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2508 | indent() << "if (getState() != org.apache.thrift.async.TAsyncMethodCall.State.RESPONSE_READ) {" << endl << |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2509 | indent() << " throw new IllegalStateException(\"Method call not finished!\");" << endl << |
| 2510 | indent() << "}" << endl << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 2511 | indent() << "org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array());" << endl << |
| 2512 | indent() << "org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport);" << endl; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 2513 | if (!(*f_iter)->is_oneway()) { |
| 2514 | indent(f_service_); |
| 2515 | if (!ret_type->is_void()) { |
| 2516 | f_service_ << "return "; |
| 2517 | } |
| 2518 | f_service_ << "(new Client(prot)).recv_" + funname + "();" << endl; |
| 2519 | } |
| 2520 | |
| 2521 | // Close function |
| 2522 | indent_down(); |
| 2523 | indent(f_service_) << "}" << endl; |
| 2524 | |
| 2525 | // Close class |
| 2526 | indent_down(); |
| 2527 | indent(f_service_) << "}" << endl << endl; |
| 2528 | } |
| 2529 | |
| 2530 | // Close AsyncClient |
| 2531 | scope_down(f_service_); |
| 2532 | f_service_ << endl; |
| 2533 | } |
| 2534 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2535 | /** |
| 2536 | * Generates a service server definition. |
| 2537 | * |
| 2538 | * @param tservice The service to generate a server for. |
| 2539 | */ |
| 2540 | void t_java_generator::generate_service_server(t_service* tservice) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2541 | // Generate the dispatch methods |
| 2542 | vector<t_function*> functions = tservice->get_functions(); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2543 | vector<t_function*>::iterator f_iter; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2544 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2545 | // Extends stuff |
| 2546 | string extends = ""; |
| 2547 | string extends_processor = ""; |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2548 | if (tservice->get_extends() == NULL) { |
Bryan Duxbury | 02357f2 | 2011-11-14 23:12:20 +0000 | [diff] [blame] | 2549 | extends_processor = "org.apache.thrift.TBaseProcessor<I>"; |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2550 | } else { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2551 | extends = type_name(tservice->get_extends()); |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2552 | extends_processor = extends + ".Processor"; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2555 | // Generate the header portion |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2556 | indent(f_service_) << |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2557 | "public static class Processor<I extends Iface> extends " << extends_processor << " implements org.apache.thrift.TProcessor {" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2558 | indent_up(); |
| 2559 | |
Bryan Duxbury | c27cda5 | 2009-08-14 20:04:15 +0000 | [diff] [blame] | 2560 | indent(f_service_) << "private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());" << endl; |
Bryan Duxbury | ef73b0e | 2009-06-17 16:43:25 +0000 | [diff] [blame] | 2561 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2562 | indent(f_service_) << "public Processor(I iface) {" << endl; |
| 2563 | indent(f_service_) << " super(iface, getProcessMap(new HashMap<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>>()));" << endl; |
| 2564 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2565 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2566 | indent(f_service_) << "protected Processor(I iface, Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {" << endl; |
| 2567 | indent(f_service_) << " super(iface, getProcessMap(processMap));" << endl; |
| 2568 | indent(f_service_) << "}" << endl << endl; |
| 2569 | |
| 2570 | indent(f_service_) << "private static <I extends Iface> Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> getProcessMap(Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {" << endl; |
| 2571 | indent_up(); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2572 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2573 | indent(f_service_) << "processMap.put(\"" << (*f_iter)->get_name() << "\", new " << (*f_iter)->get_name() << "());" << endl; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2574 | } |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2575 | indent(f_service_) << "return processMap;" << endl; |
| 2576 | indent_down(); |
| 2577 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2578 | |
| 2579 | // Generate the process subfunctions |
| 2580 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { |
| 2581 | generate_process_function(tservice, *f_iter); |
| 2582 | } |
| 2583 | |
| 2584 | indent_down(); |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2585 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2586 | } |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2587 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2588 | /** |
| 2589 | * Generates a struct and helpers for a function. |
| 2590 | * |
| 2591 | * @param tfunction The function |
| 2592 | */ |
| 2593 | void t_java_generator::generate_function_helpers(t_function* tfunction) { |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2594 | if (tfunction->is_oneway()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2595 | return; |
| 2596 | } |
| 2597 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2598 | t_struct result(program_, tfunction->get_name() + "_result"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2599 | t_field success(tfunction->get_returntype(), "success", 0); |
| 2600 | if (!tfunction->get_returntype()->is_void()) { |
| 2601 | result.append(&success); |
| 2602 | } |
| 2603 | |
| 2604 | t_struct* xs = tfunction->get_xceptions(); |
| 2605 | const vector<t_field*>& fields = xs->get_members(); |
| 2606 | vector<t_field*>::const_iterator f_iter; |
| 2607 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 2608 | result.append(*f_iter); |
| 2609 | } |
| 2610 | |
| 2611 | generate_java_struct_definition(f_service_, &result, false, true, true); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /** |
| 2615 | * Generates a process function definition. |
| 2616 | * |
| 2617 | * @param tfunction The function to write a dispatcher for |
| 2618 | */ |
| 2619 | void t_java_generator::generate_process_function(t_service* tservice, |
| 2620 | t_function* tfunction) { |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2621 | string argsname = tfunction->get_name() + "_args"; |
| 2622 | string resultname = tfunction->get_name() + "_result"; |
| 2623 | if (tfunction->is_oneway()) { |
| 2624 | resultname = "org.apache.thrift.TBase"; |
| 2625 | } |
| 2626 | |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 2627 | (void) tservice; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2628 | // Open class |
| 2629 | indent(f_service_) << |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2630 | "private static class " << tfunction->get_name() << "<I extends Iface> extends org.apache.thrift.ProcessFunction<I, " << argsname << "> {" << endl; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2631 | indent_up(); |
| 2632 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2633 | indent(f_service_) << "public " << tfunction->get_name() << "() {" << endl; |
| 2634 | indent(f_service_) << " super(\"" << tfunction->get_name() << "\");" << endl; |
| 2635 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2636 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2637 | indent(f_service_) << "protected " << argsname << " getEmptyArgsInstance() {" << endl; |
| 2638 | indent(f_service_) << " return new " << argsname << "();" << endl; |
| 2639 | indent(f_service_) << "}" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2640 | |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2641 | indent(f_service_) << "protected " << resultname << " getResult(I iface, " << argsname << " args) throws org.apache.thrift.TException {" << endl; |
Bryan Duxbury | 53cba3c | 2010-02-26 21:38:42 +0000 | [diff] [blame] | 2642 | indent_up(); |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2643 | if (!tfunction->is_oneway()) { |
| 2644 | indent(f_service_) << resultname << " result = new " << resultname << "();" << endl; |
| 2645 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2646 | |
| 2647 | t_struct* xs = tfunction->get_xceptions(); |
| 2648 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 2649 | vector<t_field*>::const_iterator x_iter; |
| 2650 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2651 | // Try block for a function with exceptions |
| 2652 | if (xceptions.size() > 0) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2653 | f_service_ << indent() << "try {" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2654 | indent_up(); |
| 2655 | } |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2656 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2657 | // Generate the function call |
| 2658 | t_struct* arg_struct = tfunction->get_arglist(); |
| 2659 | const std::vector<t_field*>& fields = arg_struct->get_members(); |
| 2660 | vector<t_field*>::const_iterator f_iter; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2661 | f_service_ << indent(); |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2662 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2663 | if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) { |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2664 | f_service_ << "result.success = "; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2665 | } |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2666 | f_service_ << "iface." << tfunction->get_name() << "("; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2667 | bool first = true; |
| 2668 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 2669 | if (first) { |
| 2670 | first = false; |
| 2671 | } else { |
| 2672 | f_service_ << ", "; |
| 2673 | } |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2674 | f_service_ << "args." << (*f_iter)->get_name(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2675 | } |
| 2676 | f_service_ << ");" << endl; |
| 2677 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2678 | // Set isset on success field |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2679 | if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void() && !type_can_be_null(tfunction->get_returntype())) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2680 | indent(f_service_) << "result.set" << get_cap_name("success") << get_cap_name("isSet") << "(true);" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2681 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2682 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2683 | if (!tfunction->is_oneway() && xceptions.size() > 0) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2684 | indent_down(); |
| 2685 | f_service_ << indent() << "}"; |
| 2686 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 2687 | f_service_ << " catch (" << type_name((*x_iter)->get_type(), false, false) << " " << (*x_iter)->get_name() << ") {" << endl; |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2688 | if (!tfunction->is_oneway()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2689 | indent_up(); |
| 2690 | f_service_ << |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 2691 | indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << ";" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2692 | indent_down(); |
| 2693 | f_service_ << indent() << "}"; |
| 2694 | } else { |
| 2695 | f_service_ << "}"; |
| 2696 | } |
| 2697 | } |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2698 | f_service_ << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
David Reiss | 4732925 | 2009-03-24 20:01:02 +0000 | [diff] [blame] | 2701 | if (tfunction->is_oneway()) { |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2702 | indent(f_service_) << "return null;" << endl; |
| 2703 | } else { |
| 2704 | indent(f_service_) << "return result;" << endl; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2705 | } |
Bryan Duxbury | 0fd37f0 | 2011-02-08 17:26:37 +0000 | [diff] [blame] | 2706 | indent_down(); |
| 2707 | indent(f_service_) << "}"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2708 | |
| 2709 | // Close function |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2710 | f_service_ << endl; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2711 | |
| 2712 | // Close class |
| 2713 | indent_down(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2714 | f_service_ << indent() << "}" << endl << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | /** |
| 2718 | * Deserializes a field of any type. |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2719 | * |
| 2720 | * @param tfield The field |
| 2721 | * @param prefix The variable name or container for this field |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2722 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2723 | void t_java_generator::generate_deserialize_field(ofstream& out, |
| 2724 | t_field* tfield, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2725 | string prefix, bool has_metadata) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 2726 | t_type* type = get_true_type(tfield->get_type()); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (type->is_void()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2729 | throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | string name = prefix + tfield->get_name(); |
| 2733 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2734 | if (type->is_struct() || type->is_xception()) { |
| 2735 | generate_deserialize_struct(out, |
Mark Slee | 216e7d6 | 2006-11-21 00:44:23 +0000 | [diff] [blame] | 2736 | (t_struct*)type, |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2737 | name); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2738 | } else if (type->is_container()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2739 | generate_deserialize_container(out, type, name, has_metadata); |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 2740 | } else if (type->is_base_type()) { |
| 2741 | indent(out) << name << " = iprot."; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2742 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2743 | |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 2744 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 2745 | switch (tbase) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2746 | case t_base_type::TYPE_VOID: |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2747 | throw "compiler error: cannot serialize void field in a struct: " + name; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2748 | break; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2749 | case t_base_type::TYPE_STRING: |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 2750 | if (((t_base_type*)type)->is_binary()) { |
| 2751 | out << "readBinary();"; |
| 2752 | } else { |
| 2753 | out << "readString();"; |
| 2754 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2755 | break; |
| 2756 | case t_base_type::TYPE_BOOL: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2757 | out << "readBool();"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2758 | break; |
| 2759 | case t_base_type::TYPE_BYTE: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2760 | out << "readByte();"; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | case t_base_type::TYPE_I16: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2763 | out << "readI16();"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2764 | break; |
| 2765 | case t_base_type::TYPE_I32: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2766 | out << "readI32();"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2767 | break; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2768 | case t_base_type::TYPE_I64: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2769 | out << "readI64();"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2770 | break; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 2771 | case t_base_type::TYPE_DOUBLE: |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2772 | out << "readDouble();"; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 2773 | break; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2774 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 2775 | throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2776 | } |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 2777 | out << endl; |
| 2778 | } else if (type->is_enum()) { |
| 2779 | indent(out) << name << " = " << type_name(tfield->get_type(), true, false) + ".findByValue(iprot.readI32());" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2780 | } else { |
| 2781 | printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", |
| 2782 | tfield->get_name().c_str(), type_name(type).c_str()); |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2787 | * Generates an unserializer for a struct, invokes read() |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2788 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2789 | void t_java_generator::generate_deserialize_struct(ofstream& out, |
| 2790 | t_struct* tstruct, |
| 2791 | string prefix) { |
| 2792 | out << |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 2793 | indent() << prefix << " = new " << type_name(tstruct) << "();" << endl << |
Mark Slee | 575fa9a | 2006-10-25 20:54:10 +0000 | [diff] [blame] | 2794 | indent() << prefix << ".read(iprot);" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2797 | /** |
| 2798 | * Deserializes a container by reading its size and then iterating |
| 2799 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2800 | void t_java_generator::generate_deserialize_container(ofstream& out, |
| 2801 | t_type* ttype, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2802 | string prefix, bool has_metadata) { |
| 2803 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2804 | scope_up(out); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2805 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2806 | string obj; |
| 2807 | |
| 2808 | if (ttype->is_map()) { |
| 2809 | obj = tmp("_map"); |
| 2810 | } else if (ttype->is_set()) { |
| 2811 | obj = tmp("_set"); |
| 2812 | } else if (ttype->is_list()) { |
| 2813 | obj = tmp("_list"); |
| 2814 | } |
| 2815 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2816 | if (has_metadata) { |
| 2817 | // Declare variables, read header |
| 2818 | if (ttype->is_map()) { |
| 2819 | indent(out) << "org.apache.thrift.protocol.TMap " << obj << " = iprot.readMapBegin();" << endl; |
| 2820 | } else if (ttype->is_set()) { |
| 2821 | indent(out) << "org.apache.thrift.protocol.TSet " << obj << " = iprot.readSetBegin();" << endl; |
| 2822 | } else if (ttype->is_list()) { |
| 2823 | indent(out) << "org.apache.thrift.protocol.TList " << obj << " = iprot.readListBegin();" << endl; |
| 2824 | } |
| 2825 | } else { |
| 2826 | // Declare variables, read header |
| 2827 | if (ttype->is_map()) { |
| 2828 | indent(out) << "org.apache.thrift.protocol.TMap " << obj << " = new org.apache.thrift.protocol.TMap(" << |
| 2829 | type_to_enum(((t_map*)ttype)->get_key_type()) << ", " << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << "iprot.readI32());" << endl; |
| 2830 | } else if (ttype->is_set()) { |
| 2831 | indent(out) << "org.apache.thrift.protocol.TSet " << obj << " = new org.apache.thrift.protocol.TSet(" << |
| 2832 | type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());" << endl; |
| 2833 | } else if (ttype->is_list()) { |
| 2834 | indent(out) << "org.apache.thrift.protocol.TList " << obj << " = new org.apache.thrift.protocol.TList(" << |
| 2835 | type_to_enum(((t_set*)ttype)->get_elem_type()) << ", iprot.readI32());" << endl; |
| 2836 | } |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2839 | indent(out) << prefix << " = new " << type_name(ttype, false, true); |
| 2840 | // size the collection correctly |
| 2841 | out << "(" |
eletuchy | 447d7d23 | 2007-06-13 04:24:11 +0000 | [diff] [blame] | 2842 | << (ttype->is_list() ? "" : "2*" ) |
| 2843 | << obj << ".size" |
| 2844 | << ");" << endl; |
| 2845 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2846 | // For loop iterates over elements |
| 2847 | string i = tmp("_i"); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2848 | indent(out) << |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2849 | "for (int " << i << " = 0; " << |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 2850 | i << " < " << obj << ".size" << "; " << |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2851 | "++" << i << ")" << endl; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2852 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2853 | scope_up(out); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2854 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2855 | if (ttype->is_map()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2856 | generate_deserialize_map_element(out, (t_map*)ttype, prefix, has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2857 | } else if (ttype->is_set()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2858 | generate_deserialize_set_element(out, (t_set*)ttype, prefix, has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2859 | } else if (ttype->is_list()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2860 | generate_deserialize_list_element(out, (t_list*)ttype, prefix, has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2863 | scope_down(out); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2864 | |
| 2865 | if (has_metadata) { |
| 2866 | // Read container end |
| 2867 | if (ttype->is_map()) { |
| 2868 | indent(out) << "iprot.readMapEnd();" << endl; |
| 2869 | } else if (ttype->is_set()) { |
| 2870 | indent(out) << "iprot.readSetEnd();" << endl; |
| 2871 | } else if (ttype->is_list()) { |
| 2872 | indent(out) << "iprot.readListEnd();" << endl; |
| 2873 | } |
| 2874 | } |
| 2875 | scope_down(out); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | |
| 2879 | /** |
| 2880 | * Generates code to deserialize a map |
| 2881 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2882 | void t_java_generator::generate_deserialize_map_element(ofstream& out, |
| 2883 | t_map* tmap, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2884 | string prefix, bool has_metadata) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2885 | string key = tmp("_key"); |
| 2886 | string val = tmp("_val"); |
| 2887 | t_field fkey(tmap->get_key_type(), key); |
| 2888 | t_field fval(tmap->get_val_type(), val); |
| 2889 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2890 | indent(out) << declare_field(&fkey) << endl; |
| 2891 | indent(out) << declare_field(&fval) << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2892 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2893 | generate_deserialize_field(out, &fkey, "", has_metadata); |
| 2894 | generate_deserialize_field(out, &fval, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2895 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2896 | indent(out) << prefix << ".put(" << key << ", " << val << ");" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2899 | /** |
| 2900 | * Deserializes a set element |
| 2901 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2902 | void t_java_generator::generate_deserialize_set_element(ofstream& out, |
| 2903 | t_set* tset, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2904 | string prefix, bool has_metadata) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2905 | string elem = tmp("_elem"); |
| 2906 | t_field felem(tset->get_elem_type(), elem); |
| 2907 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2908 | indent(out) << declare_field(&felem) << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2909 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2910 | generate_deserialize_field(out, &felem, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2911 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2912 | indent(out) << prefix << ".add(" << elem << ");" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 2915 | /** |
| 2916 | * Deserializes a list element |
| 2917 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2918 | void t_java_generator::generate_deserialize_list_element(ofstream& out, |
| 2919 | t_list* tlist, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2920 | string prefix, bool has_metadata) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2921 | string elem = tmp("_elem"); |
| 2922 | t_field felem(tlist->get_elem_type(), elem); |
| 2923 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2924 | indent(out) << declare_field(&felem) << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2925 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2926 | generate_deserialize_field(out, &felem, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2927 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2928 | indent(out) << prefix << ".add(" << elem << ");" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2931 | /** |
| 2932 | * Serializes a field of any type. |
| 2933 | * |
| 2934 | * @param tfield The field to serialize |
| 2935 | * @param prefix Name to prepend to field name |
| 2936 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2937 | void t_java_generator::generate_serialize_field(ofstream& out, |
| 2938 | t_field* tfield, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2939 | string prefix, bool has_metadata) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 2940 | t_type* type = get_true_type(tfield->get_type()); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2941 | |
| 2942 | // Do nothing for void types |
| 2943 | if (type->is_void()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2944 | throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2945 | } |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2946 | |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2947 | if (type->is_struct() || type->is_xception()) { |
| 2948 | generate_serialize_struct(out, |
Mark Slee | 216e7d6 | 2006-11-21 00:44:23 +0000 | [diff] [blame] | 2949 | (t_struct*)type, |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2950 | prefix + tfield->get_name()); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2951 | } else if (type->is_container()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2952 | generate_serialize_container(out, |
Mark Slee | 216e7d6 | 2006-11-21 00:44:23 +0000 | [diff] [blame] | 2953 | type, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2954 | prefix + tfield->get_name(), has_metadata); |
Bryan Duxbury | 9af23d9 | 2009-11-19 17:26:38 +0000 | [diff] [blame] | 2955 | } else if (type->is_enum()){ |
| 2956 | indent(out) << "oprot.writeI32(" << prefix + tfield->get_name() << ".getValue());" << endl; |
| 2957 | } else if (type->is_base_type()) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2958 | string name = prefix + tfield->get_name(); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2959 | indent(out) << "oprot."; |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 2960 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2961 | if (type->is_base_type()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2962 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2963 | switch (tbase) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2964 | case t_base_type::TYPE_VOID: |
| 2965 | throw "compiler error: cannot serialize void field in a struct: " + name; |
| 2966 | break; |
| 2967 | case t_base_type::TYPE_STRING: |
| 2968 | if (((t_base_type*)type)->is_binary()) { |
| 2969 | out << "writeBinary(" << name << ");"; |
| 2970 | } else { |
| 2971 | out << "writeString(" << name << ");"; |
| 2972 | } |
| 2973 | break; |
| 2974 | case t_base_type::TYPE_BOOL: |
| 2975 | out << "writeBool(" << name << ");"; |
| 2976 | break; |
| 2977 | case t_base_type::TYPE_BYTE: |
| 2978 | out << "writeByte(" << name << ");"; |
| 2979 | break; |
| 2980 | case t_base_type::TYPE_I16: |
| 2981 | out << "writeI16(" << name << ");"; |
| 2982 | break; |
| 2983 | case t_base_type::TYPE_I32: |
| 2984 | out << "writeI32(" << name << ");"; |
| 2985 | break; |
| 2986 | case t_base_type::TYPE_I64: |
| 2987 | out << "writeI64(" << name << ");"; |
| 2988 | break; |
| 2989 | case t_base_type::TYPE_DOUBLE: |
| 2990 | out << "writeDouble(" << name << ");"; |
| 2991 | break; |
| 2992 | default: |
| 2993 | throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } else if (type->is_enum()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 2996 | out << "writeI32(struct." << name << ");"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2997 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 2998 | out << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 2999 | } else { |
| 3000 | printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n", |
| 3001 | prefix.c_str(), |
| 3002 | tfield->get_name().c_str(), |
| 3003 | type_name(type).c_str()); |
| 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | /** |
| 3008 | * Serializes all the members of a struct. |
| 3009 | * |
| 3010 | * @param tstruct The struct to serialize |
| 3011 | * @param prefix String prefix to attach to all fields |
| 3012 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3013 | void t_java_generator::generate_serialize_struct(ofstream& out, |
| 3014 | t_struct* tstruct, |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3015 | string prefix) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 3016 | (void) tstruct; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3017 | out << indent() << prefix << ".write(oprot);" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 3020 | /** |
| 3021 | * Serializes a container by writing its size then the elements. |
| 3022 | * |
| 3023 | * @param ttype The type of container |
| 3024 | * @param prefix String prefix for fields |
| 3025 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3026 | void t_java_generator::generate_serialize_container(ofstream& out, |
| 3027 | t_type* ttype, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3028 | string prefix, bool has_metadata) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3029 | scope_up(out); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 3030 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3031 | if (has_metadata) { |
| 3032 | if (ttype->is_map()) { |
| 3033 | indent(out) << |
| 3034 | "oprot.writeMapBegin(new org.apache.thrift.protocol.TMap(" << |
| 3035 | type_to_enum(((t_map*)ttype)->get_key_type()) << ", " << |
| 3036 | type_to_enum(((t_map*)ttype)->get_val_type()) << ", " << |
| 3037 | prefix << ".size()));" << endl; |
| 3038 | } else if (ttype->is_set()) { |
| 3039 | indent(out) << |
| 3040 | "oprot.writeSetBegin(new org.apache.thrift.protocol.TSet(" << |
| 3041 | type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " << |
| 3042 | prefix << ".size()));" << endl; |
| 3043 | } else if (ttype->is_list()) { |
| 3044 | indent(out) << |
| 3045 | "oprot.writeListBegin(new org.apache.thrift.protocol.TList(" << |
| 3046 | type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " << |
| 3047 | prefix << ".size()));" << endl; |
| 3048 | } |
| 3049 | } else { |
| 3050 | indent(out) << "oprot.writeI32(" << prefix << ".size());" << endl; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | string iter = tmp("_iter"); |
| 3054 | if (ttype->is_map()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3055 | indent(out) << |
Bryan Duxbury | 9f427ee | 2009-01-29 05:47:21 +0000 | [diff] [blame] | 3056 | "for (Map.Entry<" << |
| 3057 | type_name(((t_map*)ttype)->get_key_type(), true, false) << ", " << |
| 3058 | type_name(((t_map*)ttype)->get_val_type(), true, false) << "> " << iter << |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3059 | " : " << |
Bryan Duxbury | 9f427ee | 2009-01-29 05:47:21 +0000 | [diff] [blame] | 3060 | prefix << ".entrySet())"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3061 | } else if (ttype->is_set()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3062 | indent(out) << |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3063 | "for (" << |
| 3064 | type_name(((t_set*)ttype)->get_elem_type()) << " " << iter << |
| 3065 | " : " << |
| 3066 | prefix << ")"; |
| 3067 | } else if (ttype->is_list()) { |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3068 | indent(out) << |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3069 | "for (" << |
| 3070 | type_name(((t_list*)ttype)->get_elem_type()) << " " << iter << |
| 3071 | " : " << |
| 3072 | prefix << ")"; |
| 3073 | } |
| 3074 | |
David Reiss | db5d589 | 2009-09-01 21:36:46 +0000 | [diff] [blame] | 3075 | out << endl; |
| 3076 | scope_up(out); |
| 3077 | if (ttype->is_map()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3078 | generate_serialize_map_element(out, (t_map*)ttype, iter, prefix, has_metadata); |
David Reiss | db5d589 | 2009-09-01 21:36:46 +0000 | [diff] [blame] | 3079 | } else if (ttype->is_set()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3080 | generate_serialize_set_element(out, (t_set*)ttype, iter, has_metadata); |
David Reiss | db5d589 | 2009-09-01 21:36:46 +0000 | [diff] [blame] | 3081 | } else if (ttype->is_list()) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3082 | generate_serialize_list_element(out, (t_list*)ttype, iter, has_metadata); |
David Reiss | db5d589 | 2009-09-01 21:36:46 +0000 | [diff] [blame] | 3083 | } |
| 3084 | scope_down(out); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 3085 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3086 | if (has_metadata) { |
| 3087 | if (ttype->is_map()) { |
| 3088 | indent(out) << "oprot.writeMapEnd();" << endl; |
| 3089 | } else if (ttype->is_set()) { |
| 3090 | indent(out) << "oprot.writeSetEnd();" << endl; |
| 3091 | } else if (ttype->is_list()) { |
| 3092 | indent(out) << "oprot.writeListEnd();" << endl; |
| 3093 | } |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | scope_down(out); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /** |
| 3100 | * Serializes the members of a map. |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 3101 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3102 | void t_java_generator::generate_serialize_map_element(ofstream& out, |
| 3103 | t_map* tmap, |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3104 | string iter, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3105 | string map, bool has_metadata) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 3106 | (void) map; |
Bryan Duxbury | 9f427ee | 2009-01-29 05:47:21 +0000 | [diff] [blame] | 3107 | t_field kfield(tmap->get_key_type(), iter + ".getKey()"); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3108 | generate_serialize_field(out, &kfield, "", has_metadata); |
Bryan Duxbury | 9f427ee | 2009-01-29 05:47:21 +0000 | [diff] [blame] | 3109 | t_field vfield(tmap->get_val_type(), iter + ".getValue()"); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3110 | generate_serialize_field(out, &vfield, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /** |
| 3114 | * Serializes the members of a set. |
| 3115 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3116 | void t_java_generator::generate_serialize_set_element(ofstream& out, |
| 3117 | t_set* tset, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3118 | string iter, |
| 3119 | bool has_metadata) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3120 | t_field efield(tset->get_elem_type(), iter); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3121 | generate_serialize_field(out, &efield, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | /** |
| 3125 | * Serializes the members of a list. |
| 3126 | */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3127 | void t_java_generator::generate_serialize_list_element(ofstream& out, |
| 3128 | t_list* tlist, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3129 | string iter, |
| 3130 | bool has_metadata) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3131 | t_field efield(tlist->get_elem_type(), iter); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3132 | generate_serialize_field(out, &efield, "", has_metadata); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | /** |
| 3136 | * Returns a Java type name |
| 3137 | * |
| 3138 | * @param ttype The type |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3139 | * @param container Is the type going inside a container? |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 3140 | * @return Java type name, i.e. HashMap<Key,Value> |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3141 | */ |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3142 | string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_init, bool skip_generic) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3143 | // In Java typedefs are just resolved to their real type |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 3144 | ttype = get_true_type(ttype); |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3145 | string prefix; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3146 | |
| 3147 | if (ttype->is_base_type()) { |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 3148 | return base_type_name((t_base_type*)ttype, in_container); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3149 | } else if (ttype->is_map()) { |
| 3150 | t_map* tmap = (t_map*) ttype; |
Mark Slee | 725f9e6 | 2006-10-31 05:08:33 +0000 | [diff] [blame] | 3151 | if (in_init) { |
| 3152 | prefix = "HashMap"; |
| 3153 | } else { |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3154 | prefix = "Map"; |
Mark Slee | 725f9e6 | 2006-10-31 05:08:33 +0000 | [diff] [blame] | 3155 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3156 | return prefix + (skip_generic ? "" : "<" + |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3157 | type_name(tmap->get_key_type(), true) + "," + |
| 3158 | type_name(tmap->get_val_type(), true) + ">"); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3159 | } else if (ttype->is_set()) { |
| 3160 | t_set* tset = (t_set*) ttype; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3161 | if (in_init) { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3162 | prefix = "HashSet"; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3163 | } else { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3164 | prefix = "Set"; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3165 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3166 | return prefix + (skip_generic ? "" : "<" + type_name(tset->get_elem_type(), true) + ">"); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3167 | } else if (ttype->is_list()) { |
| 3168 | t_list* tlist = (t_list*) ttype; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3169 | if (in_init) { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3170 | prefix = "ArrayList"; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3171 | } else { |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3172 | prefix = "List"; |
Mark Slee | 22360b2 | 2008-02-09 00:18:32 +0000 | [diff] [blame] | 3173 | } |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3174 | return prefix + (skip_generic ? "" : "<" + type_name(tlist->get_elem_type(), true) + ">"); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3175 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 3176 | |
| 3177 | // Check for namespacing |
| 3178 | t_program* program = ttype->get_program(); |
| 3179 | if (program != NULL && program != program_) { |
David Reiss | 771f8c7 | 2008-02-27 01:55:25 +0000 | [diff] [blame] | 3180 | string package = program->get_namespace("java"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 3181 | if (!package.empty()) { |
| 3182 | return package + "." + ttype->get_name(); |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | return ttype->get_name(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | /** |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 3190 | * Returns the Java type that corresponds to the thrift type. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3191 | * |
| 3192 | * @param tbase The base type |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3193 | * @param container Is it going in a Java container? |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3194 | */ |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 3195 | string t_java_generator::base_type_name(t_base_type* type, |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3196 | bool in_container) { |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 3197 | t_base_type::t_base tbase = type->get_base(); |
| 3198 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3199 | switch (tbase) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3200 | case t_base_type::TYPE_VOID: |
| 3201 | return "void"; |
| 3202 | case t_base_type::TYPE_STRING: |
| 3203 | if (type->is_binary()) { |
| 3204 | return "ByteBuffer"; |
| 3205 | } else { |
| 3206 | return "String"; |
| 3207 | } |
| 3208 | case t_base_type::TYPE_BOOL: |
| 3209 | return (in_container ? "Boolean" : "boolean"); |
| 3210 | case t_base_type::TYPE_BYTE: |
| 3211 | return (in_container ? "Byte" : "byte"); |
| 3212 | case t_base_type::TYPE_I16: |
| 3213 | return (in_container ? "Short" : "short"); |
| 3214 | case t_base_type::TYPE_I32: |
| 3215 | return (in_container ? "Integer" : "int"); |
| 3216 | case t_base_type::TYPE_I64: |
| 3217 | return (in_container ? "Long" : "long"); |
| 3218 | case t_base_type::TYPE_DOUBLE: |
| 3219 | return (in_container ? "Double" : "double"); |
| 3220 | default: |
| 3221 | throw "compiler error: no Java name for base type " + t_base_type::t_base_name(tbase); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | /** |
| 3226 | * Declares a field, which may include initialization as necessary. |
| 3227 | * |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 3228 | * @param tfield The field |
| 3229 | * @param init Whether to initialize the field |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3230 | */ |
| 3231 | string t_java_generator::declare_field(t_field* tfield, bool init) { |
| 3232 | // TODO(mcslee): do we ever need to initialize the field? |
| 3233 | string result = type_name(tfield->get_type()) + " " + tfield->get_name(); |
| 3234 | if (init) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 3235 | t_type* ttype = get_true_type(tfield->get_type()); |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 3236 | if (ttype->is_base_type() && tfield->get_value() != NULL) { |
| 3237 | ofstream dummy; |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 3238 | result += " = " + render_const_value(dummy, ttype, tfield->get_value()); |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 3239 | } else if (ttype->is_base_type()) { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3240 | t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base(); |
| 3241 | switch (tbase) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3242 | case t_base_type::TYPE_VOID: |
| 3243 | throw "NO T_VOID CONSTRUCT"; |
| 3244 | case t_base_type::TYPE_STRING: |
| 3245 | result += " = null"; |
| 3246 | break; |
| 3247 | case t_base_type::TYPE_BOOL: |
| 3248 | result += " = false"; |
| 3249 | break; |
| 3250 | case t_base_type::TYPE_BYTE: |
| 3251 | case t_base_type::TYPE_I16: |
| 3252 | case t_base_type::TYPE_I32: |
| 3253 | case t_base_type::TYPE_I64: |
| 3254 | result += " = 0"; |
| 3255 | break; |
| 3256 | case t_base_type::TYPE_DOUBLE: |
| 3257 | result += " = (double)0"; |
| 3258 | break; |
| 3259 | } |
Mark Slee | b7f58ff | 2006-09-02 21:59:28 +0000 | [diff] [blame] | 3260 | } else if (ttype->is_enum()) { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3261 | result += " = 0"; |
Mark Slee | b7f58ff | 2006-09-02 21:59:28 +0000 | [diff] [blame] | 3262 | } else if (ttype->is_container()) { |
Mark Slee | 725f9e6 | 2006-10-31 05:08:33 +0000 | [diff] [blame] | 3263 | result += " = new " + type_name(ttype, false, true) + "()"; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3264 | } else { |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 3265 | result += " = new " + type_name(ttype, false, true) + "()";; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 3266 | } |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3267 | } |
Bryan Duxbury | b342ddf | 2011-08-02 23:10:32 +0000 | [diff] [blame] | 3268 | result += "; // "; |
Jake Farrell | 605c760 | 2011-11-06 15:02:30 +0000 | [diff] [blame] | 3269 | if (tfield->get_req() == t_field::T_OPTIONAL) { |
Bryan Duxbury | b342ddf | 2011-08-02 23:10:32 +0000 | [diff] [blame] | 3270 | result += "optional"; |
| 3271 | } else { |
| 3272 | result += "required"; |
| 3273 | } |
| 3274 | return result; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | /** |
| 3278 | * Renders a function signature of the form 'type name(args)' |
| 3279 | * |
| 3280 | * @param tfunction Function definition |
| 3281 | * @return String of rendered function definition |
| 3282 | */ |
| 3283 | string t_java_generator::function_signature(t_function* tfunction, |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3284 | string prefix) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3285 | t_type* ttype = tfunction->get_returntype(); |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3286 | std::string result = |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 3287 | type_name(ttype) + " " + prefix + tfunction->get_name() + "(" + argument_list(tfunction->get_arglist()) + ") throws "; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3288 | t_struct* xs = tfunction->get_xceptions(); |
| 3289 | const std::vector<t_field*>& xceptions = xs->get_members(); |
| 3290 | vector<t_field*>::const_iterator x_iter; |
| 3291 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
Mark Slee | a0f8bdc | 2007-08-29 04:36:28 +0000 | [diff] [blame] | 3292 | result += type_name((*x_iter)->get_type(), false, false) + ", "; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3293 | } |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3294 | result += "org.apache.thrift.TException"; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3295 | return result; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | /** |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3299 | * Renders a function signature of the form 'void name(args, resultHandler)' |
| 3300 | * |
| 3301 | * @params tfunction Function definition |
| 3302 | * @return String of rendered function definition |
| 3303 | */ |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3304 | string t_java_generator::function_signature_async(t_function* tfunction, bool use_base_method, string prefix) { |
| 3305 | std::string arglist = async_function_call_arglist(tfunction, use_base_method, true); |
| 3306 | |
| 3307 | std::string ret_type = ""; |
| 3308 | if (use_base_method) { |
| 3309 | ret_type += "AsyncClient."; |
| 3310 | } |
| 3311 | ret_type += tfunction->get_name() + "_call"; |
| 3312 | |
| 3313 | std::string result = prefix + "void " + tfunction->get_name() + "(" + arglist + ")"; |
| 3314 | return result; |
| 3315 | } |
| 3316 | |
| 3317 | string t_java_generator::async_function_call_arglist(t_function* tfunc, bool use_base_method, bool include_types) { |
| 3318 | std::string arglist = ""; |
| 3319 | if (tfunc->get_arglist()->get_members().size() > 0) { |
| 3320 | arglist = argument_list(tfunc->get_arglist(), include_types) + ", "; |
| 3321 | } |
| 3322 | |
| 3323 | std::string ret_type = ""; |
| 3324 | if (use_base_method) { |
| 3325 | ret_type += "AsyncClient."; |
| 3326 | } |
| 3327 | ret_type += tfunc->get_name() + "_call"; |
| 3328 | |
| 3329 | if (include_types) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3330 | arglist += "org.apache.thrift.async.AsyncMethodCallback<" + ret_type + "> "; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3331 | } |
| 3332 | arglist += "resultHandler"; |
| 3333 | |
| 3334 | return arglist; |
| 3335 | } |
| 3336 | |
| 3337 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 3338 | * Renders a comma separated field list, with type names |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3339 | */ |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3340 | string t_java_generator::argument_list(t_struct* tstruct, bool include_types) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3341 | string result = ""; |
| 3342 | |
| 3343 | const vector<t_field*>& fields = tstruct->get_members(); |
| 3344 | vector<t_field*>::const_iterator f_iter; |
| 3345 | bool first = true; |
| 3346 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 3347 | if (first) { |
| 3348 | first = false; |
| 3349 | } else { |
| 3350 | result += ", "; |
| 3351 | } |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3352 | if (include_types) { |
| 3353 | result += type_name((*f_iter)->get_type()) + " "; |
| 3354 | } |
| 3355 | result += (*f_iter)->get_name(); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3356 | } |
| 3357 | return result; |
| 3358 | } |
| 3359 | |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3360 | string t_java_generator::async_argument_list(t_function* tfunct, t_struct* tstruct, t_type* ttype, bool include_types) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 3361 | (void) ttype; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3362 | string result = ""; |
| 3363 | const vector<t_field*>& fields = tstruct->get_members(); |
| 3364 | vector<t_field*>::const_iterator f_iter; |
| 3365 | bool first = true; |
| 3366 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 3367 | if (first) { |
| 3368 | first = false; |
| 3369 | } else { |
| 3370 | result += ", "; |
| 3371 | } |
| 3372 | if (include_types) { |
| 3373 | result += type_name((*f_iter)->get_type()) + " "; |
| 3374 | } |
| 3375 | result += (*f_iter)->get_name(); |
| 3376 | } |
| 3377 | if (!first) { |
| 3378 | result += ", "; |
| 3379 | } |
| 3380 | if (include_types) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3381 | result += "org.apache.thrift.async.AsyncMethodCallback<" + tfunct->get_name() + "_call" + "> "; |
Bryan Duxbury | d58ccec | 2010-05-26 16:34:48 +0000 | [diff] [blame] | 3382 | } |
| 3383 | result += "resultHandler"; |
| 3384 | return result; |
| 3385 | } |
| 3386 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3387 | /** |
Roger Meier | e0cac98 | 2010-12-16 13:15:49 +0000 | [diff] [blame] | 3388 | * Converts the parse type to a Java enum string for the given type. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3389 | */ |
| 3390 | string t_java_generator::type_to_enum(t_type* type) { |
David Reiss | e087a30 | 2007-08-23 21:43:25 +0000 | [diff] [blame] | 3391 | type = get_true_type(type); |
Mark Slee | 5ab5705 | 2007-11-27 08:38:16 +0000 | [diff] [blame] | 3392 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3393 | if (type->is_base_type()) { |
| 3394 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 3395 | switch (tbase) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3396 | case t_base_type::TYPE_VOID: |
| 3397 | throw "NO T_VOID CONSTRUCT"; |
| 3398 | case t_base_type::TYPE_STRING: |
| 3399 | return "org.apache.thrift.protocol.TType.STRING"; |
| 3400 | case t_base_type::TYPE_BOOL: |
| 3401 | return "org.apache.thrift.protocol.TType.BOOL"; |
| 3402 | case t_base_type::TYPE_BYTE: |
| 3403 | return "org.apache.thrift.protocol.TType.BYTE"; |
| 3404 | case t_base_type::TYPE_I16: |
| 3405 | return "org.apache.thrift.protocol.TType.I16"; |
| 3406 | case t_base_type::TYPE_I32: |
| 3407 | return "org.apache.thrift.protocol.TType.I32"; |
| 3408 | case t_base_type::TYPE_I64: |
| 3409 | return "org.apache.thrift.protocol.TType.I64"; |
| 3410 | case t_base_type::TYPE_DOUBLE: |
| 3411 | return "org.apache.thrift.protocol.TType.DOUBLE"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3412 | } |
| 3413 | } else if (type->is_enum()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3414 | return "org.apache.thrift.protocol.TType.I32"; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 3415 | } else if (type->is_struct() || type->is_xception()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3416 | return "org.apache.thrift.protocol.TType.STRUCT"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3417 | } else if (type->is_map()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3418 | return "org.apache.thrift.protocol.TType.MAP"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3419 | } else if (type->is_set()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3420 | return "org.apache.thrift.protocol.TType.SET"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3421 | } else if (type->is_list()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3422 | return "org.apache.thrift.protocol.TType.LIST"; |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | throw "INVALID TYPE IN type_to_enum: " + type->get_name(); |
| 3426 | } |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 3427 | |
| 3428 | /** |
David Reiss | 2db737d | 2009-01-05 21:02:55 +0000 | [diff] [blame] | 3429 | * Applies the correct style to a string based on the value of nocamel_style_ |
| 3430 | */ |
| 3431 | std::string t_java_generator::get_cap_name(std::string name){ |
| 3432 | if (nocamel_style_) { |
| 3433 | return "_" + name; |
| 3434 | } else { |
| 3435 | name[0] = toupper(name[0]); |
| 3436 | return name; |
| 3437 | } |
| 3438 | } |
| 3439 | |
Bryan Duxbury | bb7826d | 2009-02-08 00:12:38 +0000 | [diff] [blame] | 3440 | string t_java_generator::constant_name(string name) { |
| 3441 | string constant_name; |
| 3442 | |
| 3443 | bool is_first = true; |
| 3444 | bool was_previous_char_upper = false; |
| 3445 | for (string::iterator iter = name.begin(); iter != name.end(); ++iter) { |
| 3446 | string::value_type character = (*iter); |
| 3447 | |
| 3448 | bool is_upper = isupper(character); |
| 3449 | |
| 3450 | if (is_upper && !is_first && !was_previous_char_upper) { |
| 3451 | constant_name += '_'; |
| 3452 | } |
| 3453 | constant_name += toupper(character); |
| 3454 | |
| 3455 | is_first = false; |
| 3456 | was_previous_char_upper = is_upper; |
| 3457 | } |
| 3458 | |
| 3459 | return constant_name; |
| 3460 | } |
| 3461 | |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 3462 | void t_java_generator::generate_java_docstring_comment(ofstream &out, string contents) { |
| 3463 | generate_docstring_comment(out, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3464 | "/**\n", |
| 3465 | " * ", contents, |
| 3466 | " */\n"); |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | void t_java_generator::generate_java_doc(ofstream &out, |
| 3470 | t_field* field) { |
| 3471 | if (field->get_type()->is_enum()) { |
| 3472 | string combined_message = field->get_doc() + "\n@see " + get_enum_class_name(field->get_type()); |
| 3473 | generate_java_docstring_comment(out, combined_message); |
| 3474 | } else { |
| 3475 | generate_java_doc(out, (t_doc*)field); |
| 3476 | } |
| 3477 | } |
| 3478 | |
David Reiss | 2db737d | 2009-01-05 21:02:55 +0000 | [diff] [blame] | 3479 | /** |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 3480 | * Emits a JavaDoc comment if the provided object has a doc in Thrift |
| 3481 | */ |
| 3482 | void t_java_generator::generate_java_doc(ofstream &out, |
| 3483 | t_doc* tdoc) { |
| 3484 | if (tdoc->has_doc()) { |
Bryan Duxbury | 3d5db20 | 2009-07-29 23:44:44 +0000 | [diff] [blame] | 3485 | generate_java_docstring_comment(out, tdoc->get_doc()); |
Mark Slee | 8775c73 | 2007-09-10 22:33:05 +0000 | [diff] [blame] | 3486 | } |
| 3487 | } |
David Reiss | e1404d2 | 2008-02-27 01:55:05 +0000 | [diff] [blame] | 3488 | |
David Reiss | 7bcf866 | 2009-01-06 20:54:27 +0000 | [diff] [blame] | 3489 | /** |
| 3490 | * Emits a JavaDoc comment if the provided function object has a doc in Thrift |
| 3491 | */ |
| 3492 | void t_java_generator::generate_java_doc(ofstream &out, |
| 3493 | t_function* tfunction) { |
| 3494 | if (tfunction->has_doc()) { |
| 3495 | stringstream ss; |
| 3496 | ss << tfunction->get_doc(); |
| 3497 | const vector<t_field*>& fields = tfunction->get_arglist()->get_members(); |
| 3498 | vector<t_field*>::const_iterator p_iter; |
| 3499 | for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) { |
| 3500 | t_field* p = *p_iter; |
| 3501 | ss << "\n@param " << p->get_name(); |
| 3502 | if (p->has_doc()) { |
| 3503 | ss << " " << p->get_doc(); |
| 3504 | } |
| 3505 | } |
| 3506 | generate_docstring_comment(out, |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3507 | "/**\n", |
| 3508 | " * ", ss.str(), |
| 3509 | " */\n"); |
David Reiss | 7bcf866 | 2009-01-06 20:54:27 +0000 | [diff] [blame] | 3510 | } |
| 3511 | } |
| 3512 | |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3513 | void t_java_generator::generate_deep_copy_container(ofstream &out, std::string source_name_p1, std::string source_name_p2, |
| 3514 | std::string result_name, t_type* type) { |
| 3515 | |
| 3516 | t_container* container = (t_container*)type; |
| 3517 | std::string source_name; |
| 3518 | if (source_name_p2 == "") |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3519 | source_name = source_name_p1; |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3520 | else |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3521 | source_name = source_name_p1 + "." + source_name_p2; |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3522 | |
| 3523 | indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "();" << endl; |
| 3524 | |
| 3525 | std::string iterator_element_name = source_name_p1 + "_element"; |
| 3526 | std::string result_element_name = result_name + "_copy"; |
| 3527 | |
| 3528 | if(container->is_map()) { |
| 3529 | t_type* key_type = ((t_map*)container)->get_key_type(); |
| 3530 | t_type* val_type = ((t_map*)container)->get_val_type(); |
| 3531 | |
| 3532 | indent(out) << |
| 3533 | "for (Map.Entry<" << type_name(key_type, true, false) << ", " << type_name(val_type, true, false) << "> " << iterator_element_name << " : " << source_name << ".entrySet()) {" << endl; |
| 3534 | indent_up(); |
| 3535 | |
| 3536 | out << endl; |
| 3537 | |
| 3538 | indent(out) << type_name(key_type, true, false) << " " << iterator_element_name << "_key = " << iterator_element_name << ".getKey();" << endl; |
| 3539 | indent(out) << type_name(val_type, true, false) << " " << iterator_element_name << "_value = " << iterator_element_name << ".getValue();" << endl; |
| 3540 | |
| 3541 | out << endl; |
| 3542 | |
| 3543 | if (key_type->is_container()) { |
| 3544 | generate_deep_copy_container(out, iterator_element_name + "_key", "", result_element_name + "_key", key_type); |
| 3545 | } else { |
| 3546 | indent(out) << type_name(key_type, true, false) << " " << result_element_name << "_key = "; |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3547 | generate_deep_copy_non_container(out, iterator_element_name + "_key", result_element_name + "_key", key_type); |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3548 | out << ";" << endl; |
| 3549 | } |
| 3550 | |
| 3551 | out << endl; |
| 3552 | |
| 3553 | if (val_type->is_container()) { |
| 3554 | generate_deep_copy_container(out, iterator_element_name + "_value", "", result_element_name + "_value", val_type); |
| 3555 | } else { |
| 3556 | indent(out) << type_name(val_type, true, false) << " " << result_element_name << "_value = "; |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3557 | generate_deep_copy_non_container(out, iterator_element_name + "_value", result_element_name + "_value", val_type); |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3558 | out << ";" << endl; |
| 3559 | } |
| 3560 | |
| 3561 | out << endl; |
| 3562 | |
| 3563 | indent(out) << result_name << ".put(" << result_element_name << "_key, " << result_element_name << "_value);" << endl; |
| 3564 | |
| 3565 | indent_down(); |
| 3566 | indent(out) << "}" << endl; |
| 3567 | |
| 3568 | } else { |
| 3569 | t_type* elem_type; |
| 3570 | |
| 3571 | if (container->is_set()) { |
| 3572 | elem_type = ((t_set*)container)->get_elem_type(); |
| 3573 | } else { |
| 3574 | elem_type = ((t_list*)container)->get_elem_type(); |
| 3575 | } |
| 3576 | |
| 3577 | indent(out) |
| 3578 | << "for (" << type_name(elem_type, true, false) << " " << iterator_element_name << " : " << source_name << ") {" << endl; |
| 3579 | |
| 3580 | indent_up(); |
| 3581 | |
| 3582 | if (elem_type->is_container()) { |
| 3583 | // recursive deep copy |
| 3584 | generate_deep_copy_container(out, iterator_element_name, "", result_element_name, elem_type); |
| 3585 | indent(out) << result_name << ".add(" << result_element_name << ");" << endl; |
| 3586 | } else { |
| 3587 | // iterative copy |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3588 | if(((t_base_type*)elem_type)->is_binary()){ |
Bryan Duxbury | f5abd26 | 2010-08-06 00:18:25 +0000 | [diff] [blame] | 3589 | indent(out) << "ByteBuffer temp_binary_element = "; |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3590 | generate_deep_copy_non_container(out, iterator_element_name, "temp_binary_element", elem_type); |
| 3591 | out << ";" << endl; |
| 3592 | indent(out) << result_name << ".add(temp_binary_element);" << endl; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3593 | } else{ |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3594 | indent(out) << result_name << ".add("; |
| 3595 | generate_deep_copy_non_container(out, iterator_element_name, result_name, elem_type); |
| 3596 | out << ");" << endl; |
| 3597 | } |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | indent_down(); |
| 3601 | |
| 3602 | indent(out) << "}" << endl; |
| 3603 | |
| 3604 | } |
| 3605 | } |
| 3606 | |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3607 | void t_java_generator::generate_deep_copy_non_container(ofstream& out, std::string source_name, std::string dest_name, t_type* type) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 3608 | (void) dest_name; |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3609 | if (type->is_base_type() || type->is_enum() || type->is_typedef()) { |
Bryan Duxbury | d920765 | 2010-09-17 19:27:36 +0000 | [diff] [blame] | 3610 | if (((t_base_type*)type)->is_binary()) { |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3611 | out << "org.apache.thrift.TBaseHelper.copyBinary(" << source_name << ");" << endl; |
Bryan Duxbury | d920765 | 2010-09-17 19:27:36 +0000 | [diff] [blame] | 3612 | } else { |
| 3613 | // everything else can be copied directly |
David Reiss | 5455f00 | 2009-01-05 21:02:48 +0000 | [diff] [blame] | 3614 | out << source_name; |
Bryan Duxbury | d920765 | 2010-09-17 19:27:36 +0000 | [diff] [blame] | 3615 | } |
David Reiss | 603d504 | 2008-12-02 02:06:31 +0000 | [diff] [blame] | 3616 | } else { |
| 3617 | out << "new " << type_name(type, true, true) << "(" << source_name << ")"; |
| 3618 | } |
| 3619 | } |
| 3620 | |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 3621 | std::string t_java_generator::generate_isset_check(t_field* field) { |
Bryan Duxbury | e3c3d19 | 2009-03-07 03:08:37 +0000 | [diff] [blame] | 3622 | return generate_isset_check(field->get_name()); |
| 3623 | } |
| 3624 | |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 3625 | std::string t_java_generator::isset_field_id(t_field* field) { |
| 3626 | return "__" + upcase_string(field->get_name() + "_isset_id"); |
| 3627 | } |
| 3628 | |
Bryan Duxbury | e3c3d19 | 2009-03-07 03:08:37 +0000 | [diff] [blame] | 3629 | std::string t_java_generator::generate_isset_check(std::string field_name) { |
| 3630 | return "is" + get_cap_name("set") + get_cap_name(field_name) + "()"; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3633 | void t_java_generator::generate_isset_set(ofstream& out, t_field* field, string prefix) { |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 3634 | if (!type_can_be_null(field->get_type())) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3635 | indent(out) << prefix << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(true);" << endl; |
Bryan Duxbury | d11f241 | 2009-02-19 21:21:44 +0000 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
Bryan Duxbury | 3696d64 | 2009-03-18 03:14:56 +0000 | [diff] [blame] | 3639 | std::string t_java_generator::get_enum_class_name(t_type* type) { |
| 3640 | string package = ""; |
| 3641 | t_program* program = type->get_program(); |
| 3642 | if (program != NULL && program != program_) { |
| 3643 | package = program->get_namespace("java") + "."; |
| 3644 | } |
| 3645 | return package + type->get_name(); |
| 3646 | } |
David Reiss | e1404d2 | 2008-02-27 01:55:05 +0000 | [diff] [blame] | 3647 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3648 | void t_java_generator::generate_struct_desc(ofstream& out, t_struct* tstruct) { |
| 3649 | indent(out) << |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3650 | "private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct(\"" << tstruct->get_name() << "\");" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | void t_java_generator::generate_field_descs(ofstream& out, t_struct* tstruct) { |
| 3654 | const vector<t_field*>& members = tstruct->get_members(); |
| 3655 | vector<t_field*>::const_iterator m_iter; |
| 3656 | |
| 3657 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 3658 | indent(out) << |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3659 | "private static final org.apache.thrift.protocol.TField " << constant_name((*m_iter)->get_name()) << |
| 3660 | "_FIELD_DESC = new org.apache.thrift.protocol.TField(\"" << (*m_iter)->get_name() << "\", " << |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3661 | type_to_enum((*m_iter)->get_type()) << ", " << |
| 3662 | "(short)" << (*m_iter)->get_key() << ");" << endl; |
| 3663 | } |
| 3664 | } |
| 3665 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3666 | void t_java_generator::generate_scheme_map(ofstream& out, t_struct* tstruct) { |
| 3667 | indent(out) << "private static final Map<Class<? extends IScheme>, SchemeFactory> schemes = new HashMap<Class<? extends IScheme>, SchemeFactory>();" << endl; |
| 3668 | indent(out) << "static {" << endl; |
| 3669 | indent(out) << " schemes.put(StandardScheme.class, new " << tstruct->get_name() << "StandardSchemeFactory());" << endl; |
| 3670 | indent(out) << " schemes.put(TupleScheme.class, new " << tstruct->get_name() << "TupleSchemeFactory());" << endl; |
| 3671 | indent(out) << "}" << endl; |
| 3672 | } |
| 3673 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3674 | void t_java_generator::generate_field_name_constants(ofstream& out, t_struct* tstruct) { |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3675 | indent(out) << "/** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */" << endl; |
Bryan Duxbury | 932ce2b | 2010-12-07 18:28:48 +0000 | [diff] [blame] | 3676 | indent(out) << "public enum _Fields implements org.apache.thrift.TFieldIdEnum {" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3677 | |
| 3678 | indent_up(); |
| 3679 | bool first = true; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3680 | const vector<t_field*>& members = tstruct->get_members(); |
| 3681 | vector<t_field*>::const_iterator m_iter; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3682 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3683 | if (!first) { |
| 3684 | out << "," << endl; |
| 3685 | } |
| 3686 | first = false; |
| 3687 | generate_java_doc(out, *m_iter); |
| 3688 | indent(out) << constant_name((*m_iter)->get_name()) << "((short)" << (*m_iter)->get_key() << ", \"" << (*m_iter)->get_name() << "\")"; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3689 | } |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3690 | |
| 3691 | out << ";" << endl << endl; |
| 3692 | |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3693 | indent(out) << "private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();" << endl; |
| 3694 | out << endl; |
| 3695 | |
| 3696 | indent(out) << "static {" << endl; |
| 3697 | indent(out) << " for (_Fields field : EnumSet.allOf(_Fields.class)) {" << endl; |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3698 | indent(out) << " byName.put(field.getFieldName(), field);" << endl; |
| 3699 | indent(out) << " }" << endl; |
| 3700 | indent(out) << "}" << endl << endl; |
| 3701 | |
| 3702 | indent(out) << "/**" << endl; |
| 3703 | indent(out) << " * Find the _Fields constant that matches fieldId, or null if its not found." << endl; |
| 3704 | indent(out) << " */" << endl; |
| 3705 | indent(out) << "public static _Fields findByThriftId(int fieldId) {" << endl; |
Bryan Duxbury | 49b3801 | 2010-04-04 04:01:07 +0000 | [diff] [blame] | 3706 | indent_up(); |
| 3707 | indent(out) << "switch(fieldId) {" << endl; |
| 3708 | indent_up(); |
| 3709 | |
| 3710 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
| 3711 | indent(out) << "case " << (*m_iter)->get_key() << ": // " << constant_name((*m_iter)->get_name()) << endl; |
| 3712 | indent(out) << " return " << constant_name((*m_iter)->get_name()) << ";" << endl; |
| 3713 | } |
| 3714 | |
| 3715 | indent(out) << "default:" << endl; |
| 3716 | indent(out) << " return null;" << endl; |
| 3717 | |
| 3718 | indent_down(); |
| 3719 | indent(out) << "}" << endl; |
| 3720 | |
| 3721 | indent_down(); |
Bryan Duxbury | aa9fb5d | 2009-11-12 20:52:25 +0000 | [diff] [blame] | 3722 | indent(out) << "}" << endl << endl; |
| 3723 | |
| 3724 | indent(out) << "/**" << endl; |
| 3725 | indent(out) << " * Find the _Fields constant that matches fieldId, throwing an exception" << endl; |
| 3726 | indent(out) << " * if it is not found." << endl; |
| 3727 | indent(out) << " */" << endl; |
| 3728 | indent(out) << "public static _Fields findByThriftIdOrThrow(int fieldId) {" << endl; |
| 3729 | indent(out) << " _Fields fields = findByThriftId(fieldId);" << endl; |
| 3730 | indent(out) << " if (fields == null) throw new IllegalArgumentException(\"Field \" + fieldId + \" doesn't exist!\");" << endl; |
| 3731 | indent(out) << " return fields;" << endl; |
| 3732 | indent(out) << "}" << endl << endl; |
| 3733 | |
| 3734 | indent(out) << "/**" << endl; |
| 3735 | indent(out) << " * Find the _Fields constant that matches name, or null if its not found." << endl; |
| 3736 | indent(out) << " */" << endl; |
| 3737 | indent(out) << "public static _Fields findByName(String name) {" << endl; |
| 3738 | indent(out) << " return byName.get(name);" << endl; |
| 3739 | indent(out) << "}" << endl << endl; |
| 3740 | |
| 3741 | indent(out) << "private final short _thriftId;" << endl; |
| 3742 | indent(out) << "private final String _fieldName;" << endl << endl; |
| 3743 | |
| 3744 | indent(out) << "_Fields(short thriftId, String fieldName) {" << endl; |
| 3745 | indent(out) << " _thriftId = thriftId;" << endl; |
| 3746 | indent(out) << " _fieldName = fieldName;" << endl; |
| 3747 | indent(out) << "}" << endl << endl; |
| 3748 | |
| 3749 | indent(out) << "public short getThriftFieldId() {" << endl; |
| 3750 | indent(out) << " return _thriftId;" << endl; |
| 3751 | indent(out) << "}" << endl << endl; |
| 3752 | |
| 3753 | indent(out) << "public String getFieldName() {" << endl; |
| 3754 | indent(out) << " return _fieldName;" << endl; |
| 3755 | indent(out) << "}" << endl; |
| 3756 | |
| 3757 | indent_down(); |
| 3758 | |
| 3759 | indent(out) << "}" << endl; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 3762 | bool t_java_generator::has_bit_vector(t_struct* tstruct) { |
| 3763 | const vector<t_field*>& members = tstruct->get_members(); |
| 3764 | vector<t_field*>::const_iterator m_iter; |
| 3765 | |
| 3766 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | 2845b16 | 2009-11-09 15:55:22 +0000 | [diff] [blame] | 3767 | if (!type_can_be_null(get_true_type((*m_iter)->get_type()))) { |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 3768 | return true; |
| 3769 | } |
| 3770 | } |
Bryan Duxbury | 2845b16 | 2009-11-09 15:55:22 +0000 | [diff] [blame] | 3771 | return false; |
Bryan Duxbury | 3efce23 | 2009-08-03 19:37:26 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 3774 | void t_java_generator::generate_java_struct_clear(std::ofstream& out, t_struct* tstruct) { |
Bryan Duxbury | 9a42239 | 2011-05-23 21:41:02 +0000 | [diff] [blame] | 3775 | if (!java5_) { |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3776 | indent(out) << "@Override" << endl; |
Bryan Duxbury | 9a42239 | 2011-05-23 21:41:02 +0000 | [diff] [blame] | 3777 | } |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 3778 | indent(out) << "public void clear() {" << endl; |
| 3779 | |
| 3780 | const vector<t_field*>& members = tstruct->get_members(); |
| 3781 | vector<t_field*>::const_iterator m_iter; |
| 3782 | |
| 3783 | indent_up(); |
| 3784 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Roger Meier | 5e64d51 | 2010-10-16 15:54:36 +0000 | [diff] [blame] | 3785 | t_field* field = *m_iter; |
| 3786 | t_type* t = get_true_type(field->get_type()); |
| 3787 | |
| 3788 | if (field->get_value() != NULL) { |
| 3789 | print_const_value(out, "this." + field->get_name(), t, field->get_value(), true, true); |
| 3790 | continue; |
| 3791 | } |
| 3792 | |
| 3793 | if (type_can_be_null(t)) { |
| 3794 | indent(out) << "this." << field->get_name() << " = null;" << endl; |
| 3795 | continue; |
| 3796 | } |
| 3797 | |
| 3798 | // must be a base type |
| 3799 | // means it also needs to be explicitly unset |
| 3800 | indent(out) << "set" << get_cap_name(field->get_name()) << get_cap_name("isSet") << "(false);" << endl; |
| 3801 | t_base_type* base_type = (t_base_type*) t; |
| 3802 | |
| 3803 | switch (base_type->get_base()) { |
| 3804 | case t_base_type::TYPE_BYTE: |
| 3805 | case t_base_type::TYPE_I16: |
| 3806 | case t_base_type::TYPE_I32: |
| 3807 | case t_base_type::TYPE_I64: |
| 3808 | indent(out) << "this." << field->get_name() << " = 0;" << endl; |
| 3809 | break; |
| 3810 | case t_base_type::TYPE_DOUBLE: |
| 3811 | indent(out) << "this." << field->get_name() << " = 0.0;" << endl; |
| 3812 | break; |
| 3813 | case t_base_type::TYPE_BOOL: |
| 3814 | indent(out) << "this." << field->get_name() << " = false;" << endl; |
| 3815 | break; |
| 3816 | default: |
| 3817 | throw "unsupported type: " + base_type->get_name() + " for field " + field->get_name(); |
Bryan Duxbury | 5226eea | 2010-08-05 20:44:53 +0000 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | indent_down(); |
| 3821 | |
| 3822 | indent(out) << "}" << endl << endl; |
| 3823 | } |
| 3824 | |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 3825 | // generates java method to serialize (in the Java sense) the object |
| 3826 | void t_java_generator::generate_java_struct_write_object(ofstream& out, t_struct* tstruct) { |
Roger Meier | a8cef6e | 2011-07-17 18:55:59 +0000 | [diff] [blame] | 3827 | (void) tstruct; |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 3828 | indent(out) << "private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {" << endl; |
| 3829 | indent(out) << " try {" << endl; |
| 3830 | indent(out) << " write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));" << endl; |
| 3831 | indent(out) << " } catch (org.apache.thrift.TException te) {" << endl; |
Roger Meier | 6b38cf6 | 2011-04-16 20:05:51 +0000 | [diff] [blame] | 3832 | indent(out) << " throw new java.io.IOException(te" << (android_legacy_? ".getMessage()" : "") << ");" << endl; |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 3833 | indent(out) << " }" << endl; |
| 3834 | indent(out) << "}" << endl << endl; |
| 3835 | } |
| 3836 | |
| 3837 | // generates java method to serialize (in the Java sense) the object |
| 3838 | void t_java_generator::generate_java_struct_read_object(ofstream& out, t_struct* tstruct) { |
| 3839 | indent(out) << "private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {" << endl; |
| 3840 | indent(out) << " try {" << endl; |
| 3841 | if (!tstruct->is_union() && has_bit_vector(tstruct)) { |
| 3842 | indent(out) << " // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor." << endl; |
| 3843 | indent(out) << " __isset_bit_vector = new BitSet(1);" << endl; |
| 3844 | } |
| 3845 | indent(out) << " read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));" << endl; |
| 3846 | indent(out) << " } catch (org.apache.thrift.TException te) {" << endl; |
Roger Meier | 6b38cf6 | 2011-04-16 20:05:51 +0000 | [diff] [blame] | 3847 | indent(out) << " throw new java.io.IOException(te" << (android_legacy_? ".getMessage()" : "") << ");" << endl; |
Bryan Duxbury | c8d533b | 2011-01-26 22:42:02 +0000 | [diff] [blame] | 3848 | indent(out) << " }" << endl; |
| 3849 | indent(out) << "}" << endl << endl; |
| 3850 | } |
| 3851 | |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3852 | void t_java_generator::generate_standard_reader(ofstream& out, t_struct* tstruct) { |
| 3853 | out << |
| 3854 | indent() << "public void read(org.apache.thrift.protocol.TProtocol iprot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl; |
| 3855 | indent_up(); |
| 3856 | |
| 3857 | const vector<t_field*>& fields = tstruct->get_members(); |
| 3858 | vector<t_field*>::const_iterator f_iter; |
| 3859 | |
| 3860 | // Declare stack tmp variables and read struct header |
| 3861 | out << |
| 3862 | indent() << "org.apache.thrift.protocol.TField schemeField;" << endl << |
| 3863 | indent() << "iprot.readStructBegin();" << endl; |
| 3864 | |
| 3865 | // Loop over reading in fields |
| 3866 | indent(out) << "while (true)" << endl; |
| 3867 | scope_up(out); |
| 3868 | |
| 3869 | // Read beginning field marker |
| 3870 | indent(out) << "schemeField = iprot.readFieldBegin();" << endl; |
| 3871 | |
| 3872 | // Check for field STOP marker and break |
| 3873 | indent(out) << "if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { " << endl; |
| 3874 | indent_up(); |
| 3875 | indent(out) << "break;" << endl; |
| 3876 | indent_down(); |
| 3877 | indent(out) << "}" << endl; |
| 3878 | |
| 3879 | // Switch statement on the field we are reading |
| 3880 | indent(out) << "switch (schemeField.id) {" << endl; |
| 3881 | |
| 3882 | indent_up(); |
| 3883 | |
| 3884 | // Generate deserialization code for known cases |
| 3885 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 3886 | indent(out) << |
| 3887 | "case " << (*f_iter)->get_key() << ": // " << constant_name((*f_iter)->get_name()) << endl; |
| 3888 | indent_up(); |
| 3889 | indent(out) << |
| 3890 | "if (schemeField.type == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl; |
| 3891 | indent_up(); |
| 3892 | |
Bryan Duxbury | b8c8e0b | 2011-08-29 17:50:31 +0000 | [diff] [blame] | 3893 | generate_deserialize_field(out, *f_iter, "struct.", true); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3894 | indent(out) << "struct." << "set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl; |
| 3895 | indent_down(); |
| 3896 | out << |
| 3897 | indent() << "} else { " << endl << |
| 3898 | indent() << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);" << endl << |
| 3899 | indent() << "}" << endl << |
| 3900 | indent() << "break;" << endl; |
| 3901 | indent_down(); |
| 3902 | } |
| 3903 | |
| 3904 | indent(out) << "default:" << endl; |
| 3905 | indent(out) << " org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);" << endl; |
| 3906 | |
| 3907 | indent_down(); |
| 3908 | indent(out) << "}" << endl; |
| 3909 | |
| 3910 | // Read field end marker |
| 3911 | indent(out) << "iprot.readFieldEnd();" << endl; |
| 3912 | |
| 3913 | indent_down(); |
| 3914 | indent(out) << "}" << endl; |
| 3915 | |
| 3916 | out << |
| 3917 | indent() << "iprot.readStructEnd();" << endl; |
| 3918 | |
| 3919 | // in non-beans style, check for required fields of primitive type |
| 3920 | // (which can be checked here but not in the general validate method) |
| 3921 | if (!bean_style_){ |
| 3922 | out << endl << indent() << "// check for required fields of primitive type, which can't be checked in the validate method" << endl; |
| 3923 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 3924 | if ((*f_iter)->get_req() == t_field::T_REQUIRED && !type_can_be_null((*f_iter)->get_type())) { |
| 3925 | out << |
| 3926 | indent() << "if (!struct." << generate_isset_check(*f_iter) << ") {" << endl << |
| 3927 | indent() << " throw new org.apache.thrift.protocol.TProtocolException(\"Required field '" << (*f_iter)->get_name() << "' was not found in serialized data! Struct: \" + toString());" << endl << |
| 3928 | indent() << "}" << endl; |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
| 3933 | // performs various checks (e.g. check that all required fields are set) |
| 3934 | indent(out) << "struct.validate();" << endl; |
| 3935 | |
| 3936 | indent_down(); |
| 3937 | out << indent() << "}" << endl; |
| 3938 | } |
| 3939 | |
| 3940 | void t_java_generator::generate_standard_writer(ofstream& out, t_struct* tstruct) { |
| 3941 | indent_up(); |
| 3942 | out << |
| 3943 | indent() << "public void write(org.apache.thrift.protocol.TProtocol oprot, " << |
| 3944 | tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl; |
| 3945 | indent_up(); |
| 3946 | const vector<t_field*>& fields = tstruct->get_sorted_members(); |
| 3947 | vector<t_field*>::const_iterator f_iter; |
| 3948 | |
| 3949 | // performs various checks (e.g. check that all required fields are set) |
| 3950 | indent(out) << "struct.validate();" << endl << endl; |
| 3951 | |
| 3952 | indent(out) << "oprot.writeStructBegin(STRUCT_DESC);" << endl; |
| 3953 | |
| 3954 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 3955 | bool null_allowed = type_can_be_null((*f_iter)->get_type()); |
| 3956 | if (null_allowed) { |
| 3957 | out << |
| 3958 | indent() << "if (struct." << (*f_iter)->get_name() << " != null) {" << endl; |
| 3959 | indent_up(); |
| 3960 | } |
| 3961 | bool optional = (*f_iter)->get_req() == t_field::T_OPTIONAL; |
| 3962 | if (optional) { |
| 3963 | indent(out) << "if (" << "struct." << generate_isset_check((*f_iter)) << ") {" << endl; |
| 3964 | indent_up(); |
| 3965 | } |
| 3966 | |
| 3967 | indent(out) << "oprot.writeFieldBegin(" << constant_name((*f_iter)->get_name()) << "_FIELD_DESC);" << endl; |
| 3968 | |
| 3969 | // Write field contents |
Bryan Duxbury | b8c8e0b | 2011-08-29 17:50:31 +0000 | [diff] [blame] | 3970 | generate_serialize_field(out, *f_iter, "struct.", true); |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 3971 | |
| 3972 | // Write field closer |
| 3973 | indent(out) << "oprot.writeFieldEnd();" << endl; |
| 3974 | |
| 3975 | if (optional) { |
| 3976 | indent_down(); |
| 3977 | indent(out) << "}" << endl; |
| 3978 | } |
| 3979 | if (null_allowed) { |
| 3980 | indent_down(); |
| 3981 | indent(out) << "}" << endl; |
| 3982 | } |
| 3983 | } |
| 3984 | // Write the struct map |
| 3985 | out << |
| 3986 | indent() << "oprot.writeFieldStop();" << endl << |
| 3987 | indent() << "oprot.writeStructEnd();" << endl; |
| 3988 | |
| 3989 | indent_down(); |
| 3990 | out << indent() << "}" << endl << endl; |
| 3991 | indent_down(); |
| 3992 | } |
| 3993 | |
| 3994 | void t_java_generator::generate_java_struct_standard_scheme(ofstream& out, t_struct* tstruct){ |
| 3995 | indent(out) << "private static class " << tstruct->get_name() << "StandardSchemeFactory implements SchemeFactory {" << endl; |
| 3996 | indent_up(); |
| 3997 | indent(out) << "public " << tstruct->get_name() << "StandardScheme getScheme() {" << endl; |
| 3998 | indent_up(); |
| 3999 | indent(out) << "return new " << tstruct->get_name() << "StandardScheme();" << endl; |
| 4000 | indent_down(); |
| 4001 | indent(out) << "}" << endl; |
| 4002 | indent_down(); |
| 4003 | indent(out) << "}" << endl << endl; |
| 4004 | |
| 4005 | out << indent() << "private static class " << tstruct->get_name() << "StandardScheme extends StandardScheme<" << tstruct->get_name() << "> {" << endl << endl; |
| 4006 | indent_up(); |
| 4007 | generate_standard_reader(out, tstruct); |
| 4008 | indent_down(); |
| 4009 | out << endl; |
| 4010 | generate_standard_writer(out, tstruct); |
| 4011 | |
| 4012 | out << |
| 4013 | indent() << "}" << endl << |
| 4014 | endl; |
| 4015 | } |
| 4016 | |
| 4017 | void t_java_generator::generate_java_struct_tuple_reader(ofstream& out, t_struct* tstruct) { |
| 4018 | indent(out) << "@Override" << endl; |
| 4019 | indent(out) << "public void read(org.apache.thrift.protocol.TProtocol prot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl; |
| 4020 | indent_up(); |
| 4021 | indent(out) << "TTupleProtocol iprot = (TTupleProtocol) prot;" << endl; |
| 4022 | int optional_count = 0; |
| 4023 | const vector<t_field*>& fields = tstruct->get_members(); |
| 4024 | vector<t_field*>::const_iterator f_iter; |
| 4025 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 4026 | if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) { |
| 4027 | optional_count++; |
| 4028 | } |
| 4029 | if ((*f_iter)->get_req() == t_field::T_REQUIRED) { |
| 4030 | generate_deserialize_field(out, (*f_iter), "struct.", false); |
| 4031 | indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl; |
| 4032 | } |
| 4033 | } |
| 4034 | if (optional_count > 0) { |
| 4035 | indent(out) << "BitSet incoming = iprot.readBitSet(" << optional_count << ");" << endl; |
| 4036 | int i = 0; |
| 4037 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 4038 | if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) { |
| 4039 | indent(out) << "if (incoming.get(" << i << ")) {" << endl; |
| 4040 | indent_up(); |
| 4041 | generate_deserialize_field(out, (*f_iter), "struct.", false); |
| 4042 | indent(out) << "struct.set" << get_cap_name((*f_iter)->get_name()) << get_cap_name("isSet") << "(true);" << endl; |
| 4043 | indent_down(); |
| 4044 | indent(out) << "}" << endl; |
| 4045 | i++; |
| 4046 | } |
| 4047 | } |
| 4048 | } |
| 4049 | indent_down(); |
| 4050 | indent(out) << "}" << endl; |
| 4051 | } |
| 4052 | |
| 4053 | void t_java_generator::generate_java_struct_tuple_writer(ofstream& out, t_struct* tstruct) { |
| 4054 | indent(out) << "@Override" << endl; |
| 4055 | indent(out) << "public void write(org.apache.thrift.protocol.TProtocol prot, " << tstruct->get_name() << " struct) throws org.apache.thrift.TException {" << endl; |
| 4056 | indent_up(); |
| 4057 | indent(out) << "TTupleProtocol oprot = (TTupleProtocol) prot;" << endl; |
| 4058 | |
| 4059 | const vector<t_field*>& fields = tstruct->get_members(); |
| 4060 | vector<t_field*>::const_iterator f_iter; |
| 4061 | bool has_optional = false; |
Bryan Duxbury | 40d51a2 | 2011-09-26 21:29:15 +0000 | [diff] [blame] | 4062 | int optional_count = 0; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 4063 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 4064 | if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) { |
Bryan Duxbury | 40d51a2 | 2011-09-26 21:29:15 +0000 | [diff] [blame] | 4065 | optional_count++; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 4066 | has_optional = true; |
| 4067 | } |
| 4068 | if ((*f_iter)->get_req() == t_field::T_REQUIRED) { |
| 4069 | generate_serialize_field(out, (*f_iter), "struct.", false); |
| 4070 | } |
| 4071 | } |
| 4072 | if (has_optional) { |
| 4073 | indent(out) << "BitSet optionals = new BitSet();" << endl; |
| 4074 | int i = 0; |
| 4075 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 4076 | if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) { |
| 4077 | indent(out) << "if (struct." << generate_isset_check((*f_iter)) << ") {" << endl; |
| 4078 | indent_up(); |
| 4079 | indent(out) << "optionals.set(" << i << ");" << endl; |
| 4080 | indent_down(); |
| 4081 | indent(out) << "}" << endl; |
| 4082 | i++; |
| 4083 | } |
| 4084 | } |
| 4085 | |
Bryan Duxbury | 40d51a2 | 2011-09-26 21:29:15 +0000 | [diff] [blame] | 4086 | indent(out) << "oprot.writeBitSet(optionals, " << optional_count << ");" << endl; |
Bryan Duxbury | 1121b47 | 2011-08-11 18:50:58 +0000 | [diff] [blame] | 4087 | int j = 0; |
| 4088 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 4089 | if ((*f_iter)->get_req() == t_field::T_OPTIONAL || (*f_iter)->get_req() == t_field::T_OPT_IN_REQ_OUT) { |
| 4090 | indent(out) << "if (struct." << generate_isset_check(*f_iter) << ") {" << endl; |
| 4091 | indent_up(); |
| 4092 | generate_serialize_field(out, (*f_iter), "struct.", false); |
| 4093 | indent_down(); |
| 4094 | indent(out) << "}" << endl; |
| 4095 | j++; |
| 4096 | } |
| 4097 | } |
| 4098 | } |
| 4099 | indent_down(); |
| 4100 | indent(out) << "}" << endl; |
| 4101 | } |
| 4102 | |
| 4103 | void t_java_generator::generate_java_struct_tuple_scheme(ofstream& out, t_struct* tstruct){ |
| 4104 | indent(out) << "private static class " << tstruct->get_name() << "TupleSchemeFactory implements SchemeFactory {" << endl; |
| 4105 | indent_up(); |
| 4106 | indent(out) << "public " << tstruct->get_name() << "TupleScheme getScheme() {" << endl; |
| 4107 | indent_up(); |
| 4108 | indent(out) << "return new " << tstruct->get_name() << "TupleScheme();" << endl; |
| 4109 | indent_down(); |
| 4110 | indent(out) << "}" << endl; |
| 4111 | indent_down(); |
| 4112 | indent(out) << "}" << endl << endl; |
| 4113 | out << indent() << "private static class " << tstruct->get_name() << "TupleScheme extends TupleScheme<" << tstruct->get_name() << "> {" << endl << endl; |
| 4114 | indent_up(); |
| 4115 | generate_java_struct_tuple_writer(out, tstruct); |
| 4116 | out << endl; |
| 4117 | generate_java_struct_tuple_reader(out, tstruct); |
| 4118 | indent_down(); |
| 4119 | out << indent() << "}" << endl << endl; |
| 4120 | } |
| 4121 | |
David Reiss | e1404d2 | 2008-02-27 01:55:05 +0000 | [diff] [blame] | 4122 | THRIFT_REGISTER_GENERATOR(java, "Java", |
Bryan Duxbury | ea7910f | 2010-04-03 05:04:48 +0000 | [diff] [blame] | 4123 | " beans: Members will be private, and setter methods will return void.\n" |
| 4124 | " private-members: Members will be private, but setter methods will return 'this' like usual.\n" |
David Reiss | 70e329f | 2008-07-14 19:35:50 +0000 | [diff] [blame] | 4125 | " nocamel: Do not use CamelCase field accessors with beans.\n" |
David Reiss | 6987535 | 2008-03-04 21:10:41 +0000 | [diff] [blame] | 4126 | " hashcode: Generate quality hashCode methods.\n" |
Roger Meier | 6b38cf6 | 2011-04-16 20:05:51 +0000 | [diff] [blame] | 4127 | " android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).\n" |
Jake Farrell | fae04ea | 2011-10-07 23:15:03 +0000 | [diff] [blame] | 4128 | " java5: Generate Java 1.5 compliant code (includes android_legacy flag).\n" |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 4129 | ) |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 4130 | |