Class: Mirah::JVM::Types::TypeDefinition

Inherits:
Type
  • Object
show all
Defined in:
lib/mirah/jvm/types/type_definition.rb,
lib/mirah/jvm/types/methods.rb

Direct Known Subclasses

InterfaceDefinition

Constant Summary

Constants included from Logging::Logged

Logging::Logged::VLEVELS

Instance Attribute Summary collapse

Attributes inherited from Type

#inner_class, #type_system

Instance Method Summary collapse

Methods inherited from Type

#add_compiled_macro, #add_enumerable_macros, #add_intrinsics, #add_method, #add_method_listener, #aload, #ancestors_and_interfaces, #array?, #array_type, #assignableFrom, #assignable_from?, #astore, #basic_type, #compatible?, #component_type, #declared_intrinsics, #declared_macros, #dynamic?, #error?, #expand_each, #find_callable_macros, #find_callable_macros2, #find_callable_methods, #find_callable_methods2, #full_name, #generic, #generic?, #get_method, #include, #init_value, #inner_class?, #inner_class_getter, #inspect, #intrinsics, #isArray, #isBlock, #isError, #isGeneric, #isMeta, #is_parent, #iterable?, #jvm_type, #load, #load_extensions, #macro, #macros, #matchesAnything, #meta?, #method_listeners, #method_updated, #newarray, #pop, #prefix, #primitive?, #read_macrodef_annotation, #return, #store, #to_s, #type_parameters, #ungeneric, #unmeta, #void?, #wide?, #widen

Methods included from Logging::Logged

#error, #info, #log, #logger, #logger_name, #logging?, #vlog, #warning

Methods included from MethodLookup

#each_is_exact, #each_is_exact_or_subtype_or_convertible, #field_lookup, #find_jls, #find_jls2, #find_method, #find_method2, #inner_class, #is_more_specific?, #phase1, #phase2, #phase3, #primitive_convertible?

Constructor Details

#initialize(types, scope, name, node) ⇒ TypeDefinition

Returns a new instance of TypeDefinition.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/mirah/jvm/types/type_definition.rb', line 8

def initialize(types, scope, name, node)
  raise ArgumentError, "Bad name #{name}" if name[0,1] == '.'
  raise ArgumentError, "Bad name #{name}" if name.include? ?/
  @type_system = types
  @scope = scope
  @name = name
  @node = node
  raise ArgumentError, "Bad type #{name}" if self.name =~ /Java::/
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



6
7
8
# File 'lib/mirah/jvm/types/type_definition.rb', line 6

def node
  @node
end

#scopeObject

Returns the value of attribute scope.



6
7
8
# File 'lib/mirah/jvm/types/type_definition.rb', line 6

def scope
  @scope
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mirah/jvm/types/type_definition.rb', line 46

def abstract?
  node && (node.kind_of?(InterfaceDeclaration) || node.annotated_abstract?)
end

#constructor(*types) ⇒ Object

Raises:

  • (NameError)


705
706
707
708
709
# File 'lib/mirah/jvm/types/methods.rb', line 705

def constructor(*types)
  constructor = constructors.find {|c| c.argument_types == types}
  return constructor if constructor
  raise NameError, "No constructor #{name}(#{types.join ', '})"
end

#constructorsObject



731
732
733
734
735
736
737
738
# File 'lib/mirah/jvm/types/methods.rb', line 731

def constructors
  if @constructors.nil?
    @constructors = []
    declare_method('initialize', [], self, [])
    @have_default_constructor = true
  end
  @constructors
end

#declare_method(name, arguments, type, exceptions) ⇒ Object



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/mirah/jvm/types/methods.rb', line 748

def declare_method(name, arguments, type, exceptions)
  raise "Bad args" unless arguments.all?
  if type.isError
    instance_methods.delete(name)
    method_updated(name)
    return
  end
  member = MirahMember.new(self, name, arguments, type, false, exceptions)
  if name == 'initialize'
    # The ordering is important here:
    # The first call to constructors initializes @have_default_constructor.
    if constructors.size == 1 && @have_default_constructor
      constructors.clear
      @have_default_constructor = false
    elsif constructors.size > 1 && @have_default_constructor
      raise "Invalid state: default constructor but #{constructors.size} constructors"
    end
    constructors << JavaConstructor.new(@type_system, member)
  else
    instance_methods[name] << JavaMethod.new(@type_system, member)
  end
  method_updated(name)
end

#declare_static_method(name, arguments, type, exceptions) ⇒ Object



772
773
774
775
776
777
778
779
780
# File 'lib/mirah/jvm/types/methods.rb', line 772

def declare_static_method(name, arguments, type, exceptions)
  if type.isError
    static_methods.delete(name)
  else
    member = MirahMember.new(self, name, arguments, type, true, exceptions)
    static_methods[name] << JavaStaticMethod.new(@type_system, member)
  end
  method_updated(name)
end

#declared_class_methods(name = nil) ⇒ Object



719
720
721
722
723
724
725
# File 'lib/mirah/jvm/types/methods.rb', line 719

def declared_class_methods(name=nil)
  meta.declared_intrinsics(name) + if name.nil?
    static_methods.values.flatten
  else
    static_methods[name]
  end
end

#declared_constructorsObject



727
728
729
# File 'lib/mirah/jvm/types/methods.rb', line 727

def declared_constructors
  constructors
end

#declared_instance_methods(name = nil) ⇒ Object



711
712
713
714
715
716
717
# File 'lib/mirah/jvm/types/methods.rb', line 711

def declared_instance_methods(name=nil)
  declared_intrinsics(name) + if name.nil?
    instance_methods.values.flatten
  else
    instance_methods[name]
  end
end

#define(builder) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/mirah/jvm/types/type_definition.rb', line 50

def define(builder)
  class_name = name.tr('.', '/')
  abstract = node && node.kind_of?(InterfaceDeclaration)  #node.abstract
  @type ||= builder.define_class(
      class_name,
      :visibility => :public,
      :abstract => abstract?,
      :superclass => superclass,
      :interfaces => interfaces)
end

#field_getter(name) ⇒ Object



786
787
788
# File 'lib/mirah/jvm/types/methods.rb', line 786

def field_getter(name)
  nil
end

#field_setter(name) ⇒ Object



790
791
792
# File 'lib/mirah/jvm/types/methods.rb', line 790

def field_setter(name)
  nil
end

#instance_methodsObject



740
741
742
# File 'lib/mirah/jvm/types/methods.rb', line 740

def instance_methods
  @instance_methods ||= Hash.new {|h, k| h[k] = []}
end

#interface?Boolean

Returns:

  • (Boolean)


782
783
784
# File 'lib/mirah/jvm/types/methods.rb', line 782

def interface?
  false
end

#interfaces(include_parent = true) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mirah/jvm/types/type_definition.rb', line 38

def interfaces(include_parent=true)
  if node
    node.interfaces.map {|n| @type_system.get(scope, n.typeref).resolve}
  else
    []
  end
end

#java_method(name, *types) ⇒ Object

Raises:

  • (NameError)


689
690
691
692
693
694
695
# File 'lib/mirah/jvm/types/methods.rb', line 689

def java_method(name, *types)
  method = instance_methods[name].find {|m| m.argument_types == types}
  return method if method
  intrinsic = intrinsics[name][types]
  return intrinsic if intrinsic
  raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
end

#java_static_method(name, *types) ⇒ Object

Raises:

  • (NameError)


697
698
699
700
701
702
703
# File 'lib/mirah/jvm/types/methods.rb', line 697

def java_static_method(name, *types)
  method = static_methods[name].find {|m| m.argument_types == types}
  return method if method
  intrinsic = meta.intrinsics[name][types]
  return intrinsic if intrinsic
  raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
end

#metaObject



61
62
63
# File 'lib/mirah/jvm/types/type_definition.rb', line 61

def meta
  @meta ||= TypeDefMeta.new(self)
end

#nameObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mirah/jvm/types/type_definition.rb', line 18

def name
  if @type
    @type.name.tr('/', '.')
  else
    if !@name.include?(?.) && scope.package
      "#{scope.package}.#{@name}"
    else
      @name
    end
  end
end

#static_methodsObject



744
745
746
# File 'lib/mirah/jvm/types/methods.rb', line 744

def static_methods
  @static_methods ||= Hash.new {|h, k| h[k] = []}
end

#superclassObject



30
31
32
33
34
35
36
# File 'lib/mirah/jvm/types/type_definition.rb', line 30

def superclass
  if node && node.superclass
    @type_system.get(scope, node.superclass.typeref).resolve
  else
    @type_system.type(nil, 'java.lang.Object')
  end
end