Sha256: dbff38d397aca74fcab45e417e4b6a9a7eed9abb564a3e3d5e308cd813cfd04b

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'delegate'

module Duby::JVM::Types
  class ExtendedType < DelegateClass(Type)
    def initialize(*args)
      super
      @static_includes = []
    end

    def include(type)
      @static_includes << type
      self
    end

    def meta
      if meta?
        self
      else
        __getobj__.meta
      end
    end

    def unmeta
      if meta?
        __getobj__.unmeta
      else
        self
      end
    end

    def get_method(name, args)
      method = __getobj__.get_method(name, args)
      return method if method
      @static_includes.each do |type|
        method = type.meta.get_method(name, args)
        return method if method
      end
      nil
    end

    def java_method(name, *types)
      __getobj__.java_method(name, *types) || __included_method(name, types)
    end

    def java_static_method(name, *types)
      __getobj__.java_static_method(name, *types) || __included_method(name, types)
    end

    def declared_instance_methods(name=nil)
      __combine_methods(__getobj__.declared_instance_methods)
    end

    def declared_class_methods(name=nil)
      __combine_methods(__getobj__.declared_class_methods)
    end

    def __combine_methods(basic_methods)
      methods = {}
      basic_methods.each do |method|
        key = [method.name, method.parameter_types, method.return_type]
        methods[key] = method
      end
      @static_includes.each do |type|
        type.declared_class_methods.each do |method|
          key = [method.name, method.parameter_types, method.return_type]
          methods[key] ||= method
        end
      end
      methods.values
    end

    def __included_method(name, types)
      @static_includes.each do |type|
        method = type.meta.java_method(name, *types)
        return method if method
      end
      nil
    end
  end

  class Type
    def include(type)
      ExtendedType.new(self).include(type)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mirah-0.0.4-java lib/mirah/jvm/types/extensions.rb