mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 19:01:10 +00:00
Nullable java (#4455)
* add _Nullable Support for C++ interface * generate @Nullable for Java generated code
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
a1b5f565d9
commit
01bac38c84
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,6 +5,7 @@
|
|||||||
*.o.d
|
*.o.d
|
||||||
*.class
|
*.class
|
||||||
*.a
|
*.a
|
||||||
|
*.swp
|
||||||
*~
|
*~
|
||||||
*.vcxproj
|
*.vcxproj
|
||||||
*.vcxproj.filters
|
*.vcxproj.filters
|
||||||
@@ -43,6 +44,7 @@ flatsampletext.exe
|
|||||||
grpctest
|
grpctest
|
||||||
grpctest.exe
|
grpctest.exe
|
||||||
snapshot.sh
|
snapshot.sh
|
||||||
|
tags
|
||||||
tests/go_gen
|
tests/go_gen
|
||||||
tests/monsterdata_java_wire.mon
|
tests/monsterdata_java_wire.mon
|
||||||
tests/monsterdata_go_wire.mon
|
tests/monsterdata_go_wire.mon
|
||||||
|
|||||||
@@ -372,7 +372,7 @@ struct IDLOptions {
|
|||||||
bool generate_object_based_api;
|
bool generate_object_based_api;
|
||||||
std::string cpp_object_api_pointer_type;
|
std::string cpp_object_api_pointer_type;
|
||||||
std::string cpp_object_api_string_type;
|
std::string cpp_object_api_string_type;
|
||||||
bool clang_nullable;
|
bool gen_nullable;
|
||||||
std::string object_prefix;
|
std::string object_prefix;
|
||||||
std::string object_suffix;
|
std::string object_suffix;
|
||||||
bool union_value_namespacing;
|
bool union_value_namespacing;
|
||||||
@@ -428,7 +428,7 @@ struct IDLOptions {
|
|||||||
generate_name_strings(false),
|
generate_name_strings(false),
|
||||||
generate_object_based_api(false),
|
generate_object_based_api(false),
|
||||||
cpp_object_api_pointer_type("std::unique_ptr"),
|
cpp_object_api_pointer_type("std::unique_ptr"),
|
||||||
clang_nullable(false),
|
gen_nullable(false),
|
||||||
object_suffix("T"),
|
object_suffix("T"),
|
||||||
union_value_namespacing(true),
|
union_value_namespacing(true),
|
||||||
allow_non_utf8(false),
|
allow_non_utf8(false),
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const {
|
|||||||
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
|
" --cpp-ptr-type T Set object API pointer type (default std::unique_ptr)\n"
|
||||||
" --cpp-str-type T Set object API string type (default std::string)\n"
|
" --cpp-str-type T Set object API string type (default std::string)\n"
|
||||||
" T::c_str() and T::length() must be supported\n"
|
" T::c_str() and T::length() must be supported\n"
|
||||||
" --clang-nullable Add Clang _Nullable for C++ pointers.\n"
|
" --gen-nullable Add Clang _Nullable for C++ pointer. or @Nullable for Java\n"
|
||||||
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
" --object-prefix Customise class prefix for C++ object-based API.\n"
|
||||||
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
" --object-suffix Customise class suffix for C++ object-based API.\n"
|
||||||
" Default value is \"T\"\n"
|
" Default value is \"T\"\n"
|
||||||
@@ -211,8 +211,8 @@ int FlatCompiler::Compile(int argc, const char** argv) {
|
|||||||
} else if (arg == "--cpp-str-type") {
|
} else if (arg == "--cpp-str-type") {
|
||||||
if (++argi >= argc) Error("missing type following" + arg, true);
|
if (++argi >= argc) Error("missing type following" + arg, true);
|
||||||
opts.cpp_object_api_string_type = argv[argi];
|
opts.cpp_object_api_string_type = argv[argi];
|
||||||
} else if (arg == "--clang-nullable") {
|
} else if (arg == "--gen-nullable") {
|
||||||
opts.clang_nullable = true;
|
opts.gen_nullable = true;
|
||||||
} else if (arg == "--object-prefix") {
|
} else if (arg == "--object-prefix") {
|
||||||
if (++argi >= argc) Error("missing prefix following" + arg, true);
|
if (++argi >= argc) Error("missing prefix following" + arg, true);
|
||||||
opts.object_prefix = argv[argi];
|
opts.object_prefix = argv[argi];
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ class CppGenerator : public BaseGenerator {
|
|||||||
code_ += "#define " + include_guard;
|
code_ += "#define " + include_guard;
|
||||||
code_ += "";
|
code_ += "";
|
||||||
|
|
||||||
if (parser_.opts.clang_nullable) {
|
if (parser_.opts.gen_nullable) {
|
||||||
code_ += "#pragma clang system_header\n\n";
|
code_ += "#pragma clang system_header\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -433,7 +433,7 @@ class CppGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string NullableExtension() {
|
std::string NullableExtension() {
|
||||||
return parser_.opts.clang_nullable ? " _Nullable " : "";
|
return parser_.opts.gen_nullable ? " _Nullable " : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string NativeName(const std::string &name, const StructDef *sd, const IDLOptions & opts) {
|
static std::string NativeName(const std::string &name, const StructDef *sd, const IDLOptions & opts) {
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ struct LanguageParameters {
|
|||||||
std::string accessor_prefix_static;
|
std::string accessor_prefix_static;
|
||||||
std::string optional_suffix;
|
std::string optional_suffix;
|
||||||
std::string includes;
|
std::string includes;
|
||||||
|
std::string class_annotation;
|
||||||
CommentConfig comment_config;
|
CommentConfig comment_config;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -99,8 +100,8 @@ const LanguageParameters& GetLangParams(IDLOptions::Language lang) {
|
|||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
"import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n"
|
"import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\nimport com.google.flatbuffers.*;\n",
|
||||||
"import com.google.flatbuffers.*;\n\n@SuppressWarnings(\"unused\")\n",
|
"\n@SuppressWarnings(\"unused\")\n",
|
||||||
{
|
{
|
||||||
"/**",
|
"/**",
|
||||||
" *",
|
" *",
|
||||||
@@ -132,6 +133,7 @@ const LanguageParameters& GetLangParams(IDLOptions::Language lang) {
|
|||||||
"Table.",
|
"Table.",
|
||||||
"?",
|
"?",
|
||||||
"using global::System;\nusing global::FlatBuffers;\n\n",
|
"using global::System;\nusing global::FlatBuffers;\n\n",
|
||||||
|
"",
|
||||||
{
|
{
|
||||||
nullptr,
|
nullptr,
|
||||||
"///",
|
"///",
|
||||||
@@ -220,7 +222,13 @@ class GeneralGenerator : public BaseGenerator {
|
|||||||
code += lang_.namespace_ident + namespace_name + lang_.namespace_begin;
|
code += lang_.namespace_ident + namespace_name + lang_.namespace_begin;
|
||||||
code += "\n\n";
|
code += "\n\n";
|
||||||
}
|
}
|
||||||
if (needs_includes) code += lang_.includes;
|
if (needs_includes) {
|
||||||
|
code += lang_.includes;
|
||||||
|
if (parser_.opts.gen_nullable) {
|
||||||
|
code += "\nimport javax.annotation.Nullable;\n";
|
||||||
|
}
|
||||||
|
code += lang_.class_annotation;
|
||||||
|
}
|
||||||
code += classcode;
|
code += classcode;
|
||||||
if (!namespace_name.empty()) code += lang_.namespace_end;
|
if (!namespace_name.empty()) code += lang_.namespace_end;
|
||||||
auto filename = NamespaceDir(ns) + defname + lang_.file_extension;
|
auto filename = NamespaceDir(ns) + defname + lang_.file_extension;
|
||||||
@@ -235,6 +243,12 @@ class GeneralGenerator : public BaseGenerator {
|
|||||||
: upper);
|
: upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string GenNullableAnnotation(const Type& t) {
|
||||||
|
return lang_.language == IDLOptions::kJava
|
||||||
|
&& parser_.opts.gen_nullable
|
||||||
|
&& !IsScalar(DestinationType(t, true).base_type) ? " @Nullable ": "";
|
||||||
|
}
|
||||||
|
|
||||||
static bool IsEnum(const Type& type) {
|
static bool IsEnum(const Type& type) {
|
||||||
return type.enum_def != nullptr && IsInteger(type.base_type);
|
return type.enum_def != nullptr && IsInteger(type.base_type);
|
||||||
}
|
}
|
||||||
@@ -869,7 +883,8 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) {
|
|||||||
std::string dest_mask = DestinationMask(field.value.type, true);
|
std::string dest_mask = DestinationMask(field.value.type, true);
|
||||||
std::string dest_cast = DestinationCast(field.value.type);
|
std::string dest_cast = DestinationCast(field.value.type);
|
||||||
std::string src_cast = SourceCast(field.value.type);
|
std::string src_cast = SourceCast(field.value.type);
|
||||||
std::string method_start = " public " + type_name_dest + optional + " " +
|
std::string method_start = " public " + GenNullableAnnotation(field.value.type) +
|
||||||
|
type_name_dest + optional + " " +
|
||||||
MakeCamel(field.name, lang_.first_camel_upper);
|
MakeCamel(field.name, lang_.first_camel_upper);
|
||||||
std::string obj = lang_.language == IDLOptions::kCSharp
|
std::string obj = lang_.language == IDLOptions::kCSharp
|
||||||
? "(new " + type_name + "())"
|
? "(new " + type_name + "())"
|
||||||
|
|||||||
Reference in New Issue
Block a user