# Services to generate extern class `in "Java"`
module code_generator
+import gen_nit
+
intrude import model
class CodeGenerator
# Serialize `model` to a file next to `file_name`
fun write_model_to_file
do
+ if not sys.opt_save_model.value then return
+
# Write the model to file next to the Nit module
var model_path = file_name.strip_extension + ".jwrapper.bin"
var model_stream = model_path.to_path.open_wo
end
if effective_supers == 0 then
- if java_class.class_type.package_name == "java.lang.Object" or
+ if java_class.class_type.java_full_name == "java.lang.Object" or
not model.knows_the_object_class then
supers.add "super JavaObject"
else supers.add "super Java_lang_Object"
end
file_out.write """
-# Java class: {{{java_type}}}
+# Java class: {{{java_type.extern_equivalent}}}
extern class {{{nit_type}}} in "Java" `{ {{{java_type.extern_equivalent}}} `}
{{{supers.join("\n\t")}}}
# Build the call in Java
var java_call
if is_static == true then
- java_call = java_class.class_type.package_name
+ java_call = java_class.class_type.java_full_name
else java_call = "self"
java_call += ".{java_method_id}({java_args.join(", ")})"
# Write
file_out.write """
-{{{t}}}# Java implementation: {{{java_return_type}}} {{{java_class}}}.{{{java_method_id}}}({{{java_params.join(", ")}}})
+{{{t}}}# Java implementation: {{{java_return_type}}} {{{java_class.class_type.extern_equivalent}}}.{{{java_method_id}}}({{{java_params.join(", ")}}})
{{{ct}}}{{{nit_signature.join}}} in "Java" `{
{{{ct}}} {{{java_call}}};
{{{ct}}}`}
var recv
if attribute.is_static then
- recv = java_class.class_type.package_name
+ recv = java_class.class_type.java_full_name
else recv = "self"
# Tabulation
var ct = c+t
file_out.write """
-{{{t}}}# Java getter: {{{java_class}}}.{{{java_id}}}
+{{{t}}}# Java getter: {{{java_class.class_type.extern_equivalent}}}.{{{java_id}}}
{{{ct}}}fun {{{nit_id}}}: {{{nit_type}}} in "Java" `{
{{{ct}}} return {{{recv}}}.{{{java_id}}};
{{{ct}}}`}
-{{{t}}}# Java setter: {{{java_class}}}.{{{java_id}}}
+{{{t}}}# Java setter: {{{java_class.class_type.extern_equivalent}}}.{{{java_id}}}
{{{ct}}}fun {{{nit_id}}}=(value: {{{nit_type}}}) in "Java" `{
{{{ct}}} {{{recv}}}.{{{java_id}}} = value;
{{{ct}}}`}
end
file_out.write """
- # Java constructor: {{{java_class}}}
+ # Java constructor: {{{java_class.class_type.extern_equivalent}}}
{{{c}}} new {{{name}}}{{{nit_params_s}}} in "Java" `{
-{{{c}}} return new {{{java_class.class_type.package_name}}}({{{java_params_s}}});
+{{{c}}} return new {{{java_class.class_type.java_full_name}}}({{{java_params_s}}});
{{{c}}} `}
"""
var nit_type = model.java_to_nit_type(java_type)
file_out.write """
-# Java primitive array: {{{java_type}}}
+# Java primitive array: {{{java_type.extern_equivalent}}}
extern class {{{nit_type}}} in "Java" `{ {{{java_type.extern_equivalent}}} `}
super AbstractJavaArray[{{{base_nit_type}}}]
# List of Nit keywords
#
# These may also be keywords in Java, but there they would be used capitalized.
- private var nit_keywords = new HashSet[String].from(["abort", "abstract", "and", "assert",
- "break", "class", "continue", "do", "else", "end", "enum", "extern", "false", "implies",
- "import", "init", "interface", "intrude", "if", "in", "is", "isa", "isset", "for", "label",
- "loop", "module", "new", "not", "null", "nullable", "or", "package", "private",
- "protected", "public", "return", "self", "super", "then", "true", "type", "var", "while",
-
- # Top-level methods
- "class_name", "get_time", "hash", "inspect", "inspect_head", "is_same_type",
- "is_same_instance", "object_id", "output", "output_class_name", "sys", "to_s",
-
- # Pointer or JavaObject methods
- "free"])
+ private var nit_keywords: Set[String] is lazy do
+ var set = new HashSet[String]
+ set.add_all keywords
+ set.add_all methods_in_pointer
+ return set
+ end
# Name of methods used at the top-level
#
# Option to _not_ generate properties (static or from classes)
var opt_no_properties = new OptionBool("Do not wrap properties, only classes and basic services", "-n", "--no-properties")
+
+ # Should the model be serialized to a file?
+ var opt_save_model = new OptionBool("Save the model next to the generated Nit module", "-s", "--save-model")
end
redef class String