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