class GSONClassGenerator::JavaField

Constants

JAVA_DEFAULT_INITIALIZED_PRIMATIVES
JAVA_KEYWORDS
JAVA_TYPES
JAVA_TYPES_BOXED

Attributes

java_class[R]
name[R]

Public Class Methods

new(key, value) click to toggle source
# File lib/gson-class-generator.rb, line 244
def initialize(key, value)
        @array_dimens = self.class.array_dimens(value)
        @json_key = key
        @name = self.class.javaify_key(key, self.is_array?)
        @java_class = self.class.class_for_value(key, value)
        @custom_class = !JAVA_TYPES.value?(@java_class)
end

Private Class Methods

array_dimens(value) click to toggle source

STATIC METHODS

# File lib/gson-class-generator.rb, line 348
def self.array_dimens(value)
        
        dimens = 0
        
        array = value
        while (array.is_a?(Array))
                dimens += 1
                array = array.first
        end
        
        dimens
end
class_for_value(key, value) click to toggle source
# File lib/gson-class-generator.rb, line 361
def self.class_for_value(key, value)

        java_class = nil
        
        if (value.is_a?(Hash))
                # Use key as class name
                java_class = key
                                        
                # Clean up first character
                java_class = java_class.gsub(/^[^\p{Pc}\p{Alpha}\p{Sc}]/, "")

                # Clean up allowed characters
                java_class = java_class.gsub(/[^\p{Pc}\p{Alnum}\p{Sc}]/, "")
                
                # Upper case it
                java_class = java_class.gsub(/^([\p{Pc}\p{Sc}]*)(\p{Alpha})/) { "#{$1}#{$2.upcase}" }
                
        elsif (value.is_a?(Array))
        
                # First check that the array is a native one
                first_object = value.first
        
                java_class = self.class_for_value(key, first_object)
        else
        
                JAVA_TYPES.each do |ruby_cls, java_cls|
                        if (value.is_a?(ruby_cls))
                                java_class = java_cls
                                break
                        end
                end
        end
        
        abort "No class is able to be determined for JSON key #{key} (ruby class: #{value.class})" if !java_class
        
        java_class
end
javaify_key(json_key, is_array) click to toggle source
# File lib/gson-class-generator.rb, line 399
def self.javaify_key(json_key, is_array)
        field_name = json_key.dup
        
        if (is_array)
                if (!field_name.end_with?("s"))
                        # Append 's' if it doesn't have it
                        field_name += "s"
                end
        end
        
        # Clean up first character
        field_name.gsub!(/^[^\p{Pc}\p{Alnum}\p{Sc}]+/, "")
        
        # Clean up allowed characters
        field_name.gsub!(/[^\p{Pc}\p{Alnum}\p{Sc}]/, "_")
        
        # Camel case underscores
        field_name.gsub!(/(?!^_)_+(.)/) { $1.upcase }
        
        # Clean up field name in case its a Java keyword
        if (!(JAVA_KEYWORDS.index(field_name) === nil))
                field_name += "_"
        end
        
        # Lower case first letter, but allow if 2 or more upper case characters are present
        field_name.gsub!(/^(\p{Upper})(?!\p{Upper}+)/) { $1.downcase }
        
        field_name
end

Public Instance Methods

custom_class?() click to toggle source
# File lib/gson-class-generator.rb, line 256
def custom_class?
        @custom_class
end
default_initalized_object(opts) click to toggle source
# File lib/gson-class-generator.rb, line 295
def default_initalized_object(opts)
        if (opts.boxed_primatives?)
                "null"
        else
                JAVA_DEFAULT_INITIALIZED_PRIMATIVES.fetch(@java_class, "null")
        end
end
is_array?() click to toggle source
# File lib/gson-class-generator.rb, line 252
def is_array?
        @array_dimens > 0
end
type_definition(opts) click to toggle source
# File lib/gson-class-generator.rb, line 286
def type_definition(opts)
        class_name = @java_class
        if (opts.boxed_primatives?)
                class_name = JAVA_TYPES_BOXED.fetch(class_name, @java_class)
        end
        array_def = Array.new(@array_dimens, "[]").join
        "#{class_name}#{array_def}"
end
write_accessors(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 277
def write_accessors(stream, tabs, opts)
        if (opts.getters?)
                write_getter_definition(stream, tabs)
        end
        if (opts.setters?)
                write_setter_definition(stream, tabs)
        end
end
write_constructor_assignment(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 272
def write_constructor_assignment(stream, tabs, opts)
        stream << tabs
        stream << "this.#{@name} = #{@name};\n"
end
write_declaration(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 260
def write_declaration(stream, tabs, opts)
        stream << tabs
        stream << serialized_name_definition
        stream << tabs
        stream << field_definition(opts)
end
write_default_constructor_assignment(stream, tabs, opts) click to toggle source
# File lib/gson-class-generator.rb, line 267
def write_default_constructor_assignment(stream, tabs, opts)
        stream << tabs
        stream << "#{@name} = #{default_initalized_object(opts)};\n"
end

Private Instance Methods

accessor_name() click to toggle source
# File lib/gson-class-generator.rb, line 318
def accessor_name
        @name.gsub(/^(.)/) { $1.upcase }
end
field_definition(opts) click to toggle source
# File lib/gson-class-generator.rb, line 309
def field_definition(opts)
        definition = "private"
        if (opts.final_fields?)
                definition += " final"
        end
        type_definition = type_definition(opts)
        "#{definition} #{type_definition} #{@name};\n"
end
serialized_name_definition() click to toggle source
# File lib/gson-class-generator.rb, line 305
def serialized_name_definition
        "@SerializedName(\"#{@json_key}\")\n"
end
write_getter_definition(stream, tabs) click to toggle source
# File lib/gson-class-generator.rb, line 322
def write_getter_definition(stream, tabs)
        stream << "\n"
        stream << tabs
        stream << "public final #{@type_definition} get#{accessor_name}()\n"
        stream << tabs
        stream << "{\n"
        stream << tabs
        stream << "\treturn #{@name};\n"
        stream << tabs
        stream << "}\n"
end
write_setter_definition(stream, tabs) click to toggle source
# File lib/gson-class-generator.rb, line 334
def write_setter_definition(stream, tabs)
        stream << "\n"
        stream << tabs
        stream << "public final void set#{accessor_name}(final #{@type_definition} #{@name})\n"
        stream << tabs
        stream << "{\n"
        stream << tabs
        stream << "\tthis.#{@name} = #{@name};\n"
        stream << tabs
        stream << "}\n"
end