contrib/jwrapper: main logic use `JavaModel` and support many files at once
[nit.git] / contrib / jwrapper / src / javap_visitor.nit
index 9c1440d..d4a0ea2 100644 (file)
@@ -1,6 +1,7 @@
 # This file is part of NIT (http://www.nitlanguage.org).
 #
 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
+# Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -23,18 +24,23 @@ import code_generator
 import jtype_converter
 intrude import model
 
+# Visitor of the AST generated from javap output
 class JavaVisitor
        super Visitor
 
        var converter: JavaTypeConverter
 
-       var java_class = new JavaClass
+       # Model of all the analyzed classes
+       var model: JavaModel
+
+       var java_class: JavaClass is noinit
+
        var declaration_type: nullable String =  null
        var declaration_element: nullable String = null
-       var class_type: JavaType
+       var class_type: JavaType is noinit
 
        var variable_id = ""
-       var variable_type: JavaType
+       var variable_type = new JavaType(self.converter) is lazy
 
        var is_generic_param = false
        var is_generic_id = false
@@ -47,21 +53,12 @@ class JavaVisitor
        var is_primitive_array = false
 
        var method_id = ""
-       var method_return_type: JavaType
+       var method_return_type = new JavaType(self.converter) is lazy
        var method_params = new Array[JavaType]
        var param_index = 0
 
        redef fun visit(n) do n.accept_visitor(self)
 
-       init(converter: JavaTypeConverter)
-       do
-               self.converter = converter
-               self.class_type = new JavaType(self.converter)
-               self.method_return_type = new JavaType(self.converter)
-               self.variable_type = new JavaType(self.converter)
-               super
-       end
-
        # Add the identifier from `token` to the current context
        fun add_identifier(token: NToken)
        do
@@ -109,7 +106,7 @@ redef class Nidentifier
                        if v.declaration_element == "id" then
                                v.method_id = self.text
                        else if v.declaration_element == "return_type" then
-                               if self.text == "void" then 
+                               if self.text == "void" then
                                        v.method_return_type.is_void = true
                                else if v.is_generic_param then
                                        v.method_return_type.generic_params[v.gen_params_index].identifier.add(self.text)
@@ -143,7 +140,7 @@ redef class Nidentifier
 end
 
 # Primitive array node
-redef class N_39d_91d_93d_39d
+redef class Nbrackets
        redef fun accept_visitor(v)
        do
                if v.declaration_type == "variable" then
@@ -216,6 +213,10 @@ end
 redef class Nclass_declaration
        redef fun accept_visitor(v)
        do
+               v.java_class = new JavaClass
+               v.model.classes.add v.java_class
+               v.class_type = new JavaType(v.converter)
+
                v.declaration_type = "class_header"
                v.declaration_element = "id"
                super
@@ -253,15 +254,18 @@ end
 #                                            #
 
 # Method declaration
-redef class Nmethod_declaration
+redef class Nproperty_declaration_method
        redef fun accept_visitor(v)
        do
                v.declaration_type = "method"
+               v.declaration_element = null
                super
                v.declaration_type = null
 
                if v.method_return_type.has_unresolved_types then v.method_return_type.resolve_types(v.generic_map)
-               v.java_class.add_method(v.method_id, v.method_return_type, v.method_params)
+
+               var method = new JavaMethod(v.method_return_type, v.method_params.clone)
+               v.java_class.methods[v.method_id].add method
 
                v.method_params.clear
                v.method_id = ""
@@ -270,7 +274,7 @@ redef class Nmethod_declaration
 end
 
 # Constructor declaration
-redef class Nconstructor_declaration
+redef class Nproperty_declaration_constructor
        redef fun accept_visitor(v)
        do
                v.declaration_type = "constructor"
@@ -280,7 +284,7 @@ redef class Nconstructor_declaration
 end
 
 # Variable property declaration
-redef class Nvariable_declaration
+redef class Nproperty_declaration_attribute
        redef fun accept_visitor(v)
        do
                v.declaration_type = "variable"
@@ -295,7 +299,7 @@ redef class Nvariable_declaration
 end
 
 # Static property declaration
-redef class Nstatic_declaration
+redef class Nproperty_declaration_static
        redef fun accept_visitor(v)
        do
                v.declaration_type = "static"
@@ -305,7 +309,7 @@ redef class Nstatic_declaration
 end
 
 # Identifier of a variable
-redef class Nvariable_id
+redef class Nattribute_id
        redef fun accept_visitor(v)
        do
                v.declaration_element = "id"