THRIFT-225. java/cpp: Handle non-i32 integer constants properly
Java generates a warning when converting from an integer constant
to a byte or short without an explicit cast. This change adds a
cast to byte and short literals in Java.
Both Java and C++ fail to compile integer literals larger than 2^31
unless they are decorated with "L" (for Java) or "LL" (for g++).
This change adds the appropriate decorations.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@727130 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index 6366497..2bb24c4 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -560,9 +560,11 @@
case t_base_type::TYPE_BYTE:
case t_base_type::TYPE_I16:
case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
render << value->get_integer();
break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "LL";
+ break;
case t_base_type::TYPE_DOUBLE:
if (value->get_type() == t_const_value::CV_INTEGER) {
render << value->get_integer();
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 6da8e3c..887fc7a 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -478,11 +478,17 @@
render << ((value->get_integer() > 0) ? "true" : "false");
break;
case t_base_type::TYPE_BYTE:
+ render << "(byte)" << value->get_integer();
+ break;
case t_base_type::TYPE_I16:
+ render << "(short)" << value->get_integer();
+ break;
case t_base_type::TYPE_I32:
- case t_base_type::TYPE_I64:
render << value->get_integer();
break;
+ case t_base_type::TYPE_I64:
+ render << value->get_integer() << "L";
+ break;
case t_base_type::TYPE_DOUBLE:
if (value->get_type() == t_const_value::CV_INTEGER) {
render << value->get_integer();
diff --git a/test/DebugProtoTest.thrift b/test/DebugProtoTest.thrift
index 515635e..e8eb9a3 100644
--- a/test/DebugProtoTest.thrift
+++ b/test/DebugProtoTest.thrift
@@ -15,15 +15,18 @@
struct OneOfEach {
1: bool im_true,
2: bool im_false,
- 3: byte a_bite,
- 4: i16 integer16,
+ 3: byte a_bite = 200,
+ 4: i16 integer16 = 33000,
5: i32 integer32,
- 6: i64 integer64,
+ 6: i64 integer64 = 10000000000,
7: double double_precision,
8: string some_characters,
9: string zomg_unicode,
10: bool what_who,
11: binary base64,
+ 12: list<byte> byte_list = [1, 2, 3],
+ 13: list<i16> i16_list = [1,2,3],
+ 14: list<i64> i64_list = [1,2,3]
}
struct Bonk {