class GSONClassGenerator::JavaClass

Attributes

class_name[R]
fields[R]

Public Class Methods

new(class_name, json, depth = 0) click to toggle source
# File lib/gson-class-generator.rb, line 86
def initialize(class_name, json, depth = 0)
        @class_name = class_name
        @nested_classes = Array.new
        @fields = Array.new
        @depth = depth
        
        if (json.is_a?(Array))
                
                # Pull out first object to examine what it looks like
                first_json = json.first
                fields = self.class.new(nil, first_json).fields
                
                @fields += fields
        
        else
        
                json.each do |k, v|
                        field = JavaField.new(k, v)
                        
                        if (field.custom_class?)
                                nested_name = self.class.get_nested_class_name(field)
                                nested_class = self.class.new(nested_name, v, depth + 1)
                                @nested_classes << nested_class
                        end
                        
                        @fields << field
                end
                
        end
end

Private Class Methods

get_nested_class_name(field) click to toggle source
# File lib/gson-class-generator.rb, line 132
def self.get_nested_class_name(field)
        class_name = field.java_class
        if (field.is_array?)
                # Remove 's' if array
                class_name.chomp!("s")
        end
        class_name
end
indentation(depth) click to toggle source
# File lib/gson-class-generator.rb, line 141
def self.indentation(depth)
        Array.new(depth, "\t").join
end

Public Instance Methods

write(stream, opts) click to toggle source
# File lib/gson-class-generator.rb, line 117
def write(stream, opts)
        tabs = self.class.indentation(@depth)
        field_tabs = self.class.indentation(@depth + 1)
        
        stream << "#{tabs}#{class_definition}\n"
        stream << "#{tabs}{\n"
        write_field_declarations(stream, field_tabs, opts)
        write_constructor(stream, field_tabs, opts)
        write_field_accessors(stream, field_tabs, opts)
        write_nested_classes(stream, opts)
        stream << "#{tabs}}\n"
end

Private Instance Methods

class_definition() click to toggle source
# File lib/gson-class-generator.rb, line 145
def class_definition
        prefix = "public"
        if (@depth > 0)
                prefix += " static"
        end
        "#{prefix} final class #{@class_name}"
end
write_constructor(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 160
def write_constructor(stream, tabs, opts)
                
        constructor_tabs = tabs + "\t"
                
        if (opts.field_constructor?)
                parameters = @fields.map { |f| "final #{f.type_definition(opts)} #{f.name}" }
                stream << "\n"
                stream << tabs
                stream << "public #{class_name}(#{parameters.join(', ')})\n"
                stream << tabs
                stream << "{\n"
                @fields.each do |f|
                        f.write_constructor_assignment(stream, constructor_tabs, opts)
                end
                stream << tabs
                stream << "}\n"
        end
        if (opts.final_fields?)
        
                stream << "\n"
                stream << tabs
                stream << "public #{class_name}()\n"
                stream << tabs
                stream << "{\n"
                if (opts.field_constructor?)
                        arguments = @fields.map { |f| f.default_initalized_object(opts) }
                        # Call public constructor instead
                        stream << constructor_tabs
                        stream << "this(#{arguments.join(', ')});\n"
                else
                        @fields.each do |f|
                                f.write_default_constructor_assignment(stream, constructor_tabs, opts)
                        end
                end
                stream << tabs
                stream << "}\n"
        end
end
write_field_accessors(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 199
def write_field_accessors(stream, tabs, opts)
        @fields.each do |f|
                f.write_accessors(stream, tabs, opts)
        end
end
write_field_declarations(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 153
def write_field_declarations(stream, tabs, opts)
        @fields.each do |f|
                stream << "\n"
                f.write_declaration(stream, tabs, opts)
        end
end
write_nested_classes(stream, opts) click to toggle source
# File lib/gson-class-generator.rb, line 205
def write_nested_classes(stream, opts)
        @nested_classes.each do |nested_class|
                stream << "\n"
                nested_class.write(stream, opts)
        end
end