Class: CoderCompanion::Java::JavaTreeWalker

Inherits:
TreeWalker
  • Object
show all
Defined in:
lib/codercompanion/java/java_tree_walker.rb

Instance Method Summary collapse

Instance Method Details

#create_class_obj(param, name_of_class, obj, modified) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/codercompanion/java/java_tree_walker.rb', line 47

def create_class_obj(param, name_of_class, obj, modified)
    class_object = {}
    if param[:inherits_from] != ""
        class_object[:inheritsfrom] = [param[:inherits_from]]
    end
    if param[:implements] && param[:implements].length > 0
        class_object[:implements] = param[:implements]
    end
    method_list = []
    variable_list = []
    constant_list = []
    hasa = []
    ownsa = []
    associatedto = []
    enum_constants = []
    class_type = nil
    if param[:type]
        class_type = param[:type].gsub(/_definition/, '')     
    end 
    param[:class_elements].each do |class_el|
        case class_el[:type]
        when "enum_constant"
            variable_list << class_el[:value]
        when "method_definition"
            method_list << class_el[:value] + ":" + class_el[:return_type]
            if class_el[:params].count > 0
                class_el[:params].each do |p|
                    param_type = is_array_or_list(p[:type])
                    if !hasa.index(param_type) && !associatedto.index(param_type)
                        associatedto << param_type
                    end
                end
            end
        when "variable_definition"
            return_type = is_array_or_list(class_el[:return_type])
            variable_type = class_el[:variable_type]
            variable_string = class_el[:value] + ":" + class_el[:return_type]
            if variable_type.match(/constant/)
                constant_list << variable_string 
            else  
                variable_list <<  variable_string
            end
            generic_less = remove_generics(return_type)
            hasa << generic_less if !hasa.index(generic_less)
            associatedto - [generic_less] if associatedto.index(generic_less)

        when "class_definition", "enum_definition", "interface_definition"
            name = name_of_class + "::" + class_el[:value]
            ownsa << name
            associatedto - [name] if associatedto.index(name)
            obj[name] = create_class_obj(class_el, name, obj, modified) 
            obj[name][:modified] = modified
        end
    end
    class_object[:classname] = name_of_class
    class_object[:variables] = variable_list
    class_object[:methods] = method_list
    class_object[:constants] = constant_list
    class_object[:hasa] = hasa if hasa.count > 0
    class_object[:ownsa] = ownsa if ownsa.count > 0
    class_object[:isassociatedto] = associatedto if associatedto.count > 0
    class_object[:classtype] = class_type unless class_type.nil?
    return {:classobject => class_object}
end

#create_uploadable(results, cache_handler) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/codercompanion/java/java_tree_walker.rb', line 30

def create_uploadable(results, cache_handler)
    obj = {}
    results.each do |source|
        modified = cache_handler.is_file_modified?(source[:file_path])
        cache_handler.update_cache_path(source[:file_path])
        source[:body].each do |element|
            if (element[:type] == "class_definition" || element[:type] == "enum_definition" || element[:type] == "interface_definition")
                obj[element[:value]] = create_class_obj(element, element[:value], obj, modified)
                obj[element[:value]][:modified] = modified
            end
        end
    end
    return obj
end

#is_array_or_list(string) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/codercompanion/java/java_tree_walker.rb', line 4

def is_array_or_list(string)
    ret = ""
    if string.match(/(?<=<)([\w]+)(?=>)/) 
        ret = string.match(/(?<=<)([\w]+)(?=>)/)[0]
    elsif string.match(/([\w]+)(?=\[\]\[\])/)
        ret = string.match(/([\w]+)(?=\[\]\[\])/)[0]
    elsif string.match(/([\w]+)(?=\[\])/ ) 
        ret = string.match(/([\w]+)(?=\[\])/ )[0]
    else
        ret = remove_generics(string)
    end
    ret
end

#remove_generics(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/codercompanion/java/java_tree_walker.rb', line 18

def remove_generics(value)
    loop do 
        has_generics = value.match(/[<>]/)
        if has_generics
            value = value.gsub(/(<[^<>]*>)/, '')
        else
            break
        end
    end
    return value
end