From: Alexis Laferrière Date: Mon, 27 Jul 2015 16:23:05 +0000 (-0400) Subject: contrib/jwrapper: generate top-level getter & setter for static attributes X-Git-Tag: v0.7.7~2^2~23 X-Git-Url: http://nitlanguage.org contrib/jwrapper: generate top-level getter & setter for static attributes Signed-off-by: Alexis Laferrière --- diff --git a/contrib/jwrapper/src/code_generator.nit b/contrib/jwrapper/src/code_generator.nit index 2ed3020..1431458 100644 --- a/contrib/jwrapper/src/code_generator.nit +++ b/contrib/jwrapper/src/code_generator.nit @@ -86,8 +86,8 @@ class CodeGenerator end # Attributes - for id, java_type in jclass.attributes do - generate_getter_setter(jclass, id, java_type) + for id, attribute in jclass.attributes do if not attribute.is_static then + generate_getter_setter(jclass, id, attribute) end # Close the class @@ -102,6 +102,11 @@ class CodeGenerator file_out.write "\n" end end + + # Static attributes as top-level getters and setters + for id, attribute in jclass.attributes do if attribute.is_static then + generate_getter_setter(jclass, id, attribute) + end end if stub_for_unknown_types then @@ -213,26 +218,41 @@ class CodeGenerator end # Generate getter and setter to access an attribute, of field - private fun generate_getter_setter(java_class: JavaClass, java_id: String, java_type: JavaType) + private fun generate_getter_setter(java_class: JavaClass, java_id: String, + attribute: JavaAttribute) do + var java_type = attribute.java_type var nit_type = model.java_to_nit_type(java_type) - var nit_id = java_id.to_nit_method_name + + var nit_id = java_id + if attribute.is_static then nit_id = java_class.class_type.extern_name.to_snake_case + "_" + nit_id + nit_id = nit_id.to_nit_method_name nit_id = java_class.nit_name_for(nit_id, [java_type], false) var c = "" if not nit_type.is_known and comment_unknown_types then c = "#" if java_type.is_primitive_array then c = "#" + var recv + if attribute.is_static == true then + recv = java_class.class_type.to_package_name + else recv = "self" + + # Tabulation + var t = "\t" + if attribute.is_static then t = "" + var ct = c+t + file_out.write """ - # Java getter: {{{java_class}}}.{{{java_id}}} -{{{c}}} fun {{{nit_id}}}: {{{nit_type}}} in "Java" `{ -{{{c}}} return self.{{{java_id}}}; -{{{c}}} `} +{{{t}}}# Java getter: {{{java_class}}}.{{{java_id}}} +{{{ct}}}fun {{{nit_id}}}: {{{nit_type}}} in "Java" `{ +{{{ct}}} return {{{recv}}}.{{{java_id}}}; +{{{ct}}}`} - # Java setter: {{{java_class}}}.{{{java_id}}} -{{{c}}} fun {{{nit_id}}}=(value: {{{nit_type}}}) in "Java" `{ -{{{c}}} self.{{{java_id}}} = value; -{{{c}}} `} +{{{t}}}# Java setter: {{{java_class}}}.{{{java_id}}} +{{{ct}}}fun {{{nit_id}}}=(value: {{{nit_type}}}) in "Java" `{ +{{{ct}}} {{{recv}}}.{{{java_id}}} = value; +{{{ct}}}`} """ end diff --git a/contrib/jwrapper/src/javap_visitor.nit b/contrib/jwrapper/src/javap_visitor.nit index e4a8cfb..269f125 100644 --- a/contrib/jwrapper/src/javap_visitor.nit +++ b/contrib/jwrapper/src/javap_visitor.nit @@ -126,7 +126,11 @@ redef class Nproperty_declaration_attribute var brackets = n_brackets if brackets != null then jtype.array_dimension += brackets.children.length - v.java_class.attributes[id] = jtype + var is_static = false + var modifiers = n_modifier + if modifiers != null then is_static = modifiers.has_static + + v.java_class.attributes[id] = new JavaAttribute(is_static, jtype) end end diff --git a/contrib/jwrapper/src/model.nit b/contrib/jwrapper/src/model.nit index 964e5cc..3e89d2e 100644 --- a/contrib/jwrapper/src/model.nit +++ b/contrib/jwrapper/src/model.nit @@ -148,7 +148,7 @@ class JavaClass var class_type: JavaType # Attributes of this class - var attributes = new HashMap[String, JavaType] + var attributes = new HashMap[String, JavaAttribute] # Methods of this class organized by their name var methods = new MultiHashMap[String, JavaMethod] @@ -244,6 +244,14 @@ class JavaMethod var params: Array[JavaType] end +# An attribute in a Java class +class JavaAttribute + super JavaProperty + + # Type of the attribute + var java_type: JavaType +end + # A Java method, with its signature class JavaConstructor # Type of the parameters of this constructor