Nullable java (#4455)

* add _Nullable Support for C++ interface

* generate @Nullable for Java generated code
This commit is contained in:
Yonggang Li
2017-10-16 09:24:11 -07:00
committed by Wouter van Oortmerssen
parent a1b5f565d9
commit 01bac38c84
5 changed files with 28 additions and 11 deletions

View File

@@ -70,6 +70,7 @@ struct LanguageParameters {
std::string accessor_prefix_static;
std::string optional_suffix;
std::string includes;
std::string class_annotation;
CommentConfig comment_config;
};
@@ -99,8 +100,8 @@ const LanguageParameters& GetLangParams(IDLOptions::Language lang) {
"",
"",
"",
"import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n"
"import com.google.flatbuffers.*;\n\n@SuppressWarnings(\"unused\")\n",
"import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\nimport com.google.flatbuffers.*;\n",
"\n@SuppressWarnings(\"unused\")\n",
{
"/**",
" *",
@@ -132,6 +133,7 @@ const LanguageParameters& GetLangParams(IDLOptions::Language lang) {
"Table.",
"?",
"using global::System;\nusing global::FlatBuffers;\n\n",
"",
{
nullptr,
"///",
@@ -220,7 +222,13 @@ class GeneralGenerator : public BaseGenerator {
code += lang_.namespace_ident + namespace_name + lang_.namespace_begin;
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;
if (!namespace_name.empty()) code += lang_.namespace_end;
auto filename = NamespaceDir(ns) + defname + lang_.file_extension;
@@ -235,6 +243,12 @@ class GeneralGenerator : public BaseGenerator {
: 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) {
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_cast = DestinationCast(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);
std::string obj = lang_.language == IDLOptions::kCSharp
? "(new " + type_name + "())"