Class: Mirah::JVM::Types::Type

Inherits:
Object
  • Object
show all
Includes:
MethodLookup, Logging::Logged, ResolvedType
Defined in:
lib/mirah/jvm/types/type.rb,
lib/mirah/jvm/types/enumerable.rb,
lib/mirah/jvm/types/extensions.rb,
lib/mirah/jvm/types/intrinsics.rb,
lib/mirah/jvm/types/methods.rb

Defined Under Namespace

Classes: SpecialType

Constant Summary

Constants included from Logging::Logged

Logging::Logged::VLEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

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(type_system, mirror_or_name) ⇒ Type

Returns a new instance of Type.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mirah/jvm/types/type.rb', line 36

def initialize(type_system, mirror_or_name)
  @type_system = type_system
  raise ArgumentError if type_system.nil?
  if mirror_or_name.kind_of?(BiteScript::ASM::ClassMirror)
    @type = mirror_or_name
    @name = mirror_or_name.type.class_name
  else
    @name = mirror_or_name.to_s
  end
  raise ArgumentError, "Bad type #{mirror_or_name}" if name =~ /Java::/
end

Instance Attribute Details

#inner_class=(value) ⇒ Object (writeonly)

Sets the attribute inner_class

Parameters:

  • value

    the value to set the attribute inner_class to.



34
35
36
# File 'lib/mirah/jvm/types/type.rb', line 34

def inner_class=(value)
  @inner_class = value
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/mirah/jvm/types/type.rb', line 33

def name
  @name
end

#type_systemObject (readonly)

Returns the value of attribute type_system.



33
34
35
# File 'lib/mirah/jvm/types/type.rb', line 33

def type_system
  @type_system
end

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mirah/jvm/types/type.rb', line 132

def abstract?
  (@type || BiteScript::ASM::ClassMirror.for_name(@name)).abstract? rescue nil
end

#add_compiled_macro(klass) ⇒ Object



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
# File 'lib/mirah/jvm/types/intrinsics.rb', line 59

def add_compiled_macro(klass)
  name, arg_types, is_static = read_macrodef_annotation(klass)
  if arg_types.nil?
    return
  elsif is_static && !self.meta?
    self.meta.add_compiled_macro(klass)
    return
  end

  log "Adding macro #{self.name}.#{name}(#{arg_types.map{|t| t.full_name}.join(', ')})"
  # TODO separate static and instance macros
  macro = Macro.new(self, name, arg_types) do |call, typer|
    #TODO scope
    # We probably need to set the scope on all the parameters, plus the
    # arguments and body of any block params. Also make sure scope is copied
    # when cloned.
    scope = typer.scoper.get_scope(call)
    # call.parameters.each do |arg|
    #   arg.scope = scope
    # end

    begin
      expander = klass.constructors[0].newInstance(typer.macro_compiler, call)
      ast = expander.expand
    # rescue
    #   puts "Unable to expand macro #{name.inspect} from #{klass} with #{call}"
    end
    if ast
      body = Mirah::AST::NodeList.new(ast.position.to_java(Mirah::AST::Position))
      # TODO scope
      # static_scope = typer.scoper.add_scope(body)
      # static_scope.parent = typer.scoper.get_scope(call)
      body.add(ast)
      # if call.target
      #   static_scope.self_type = call.target.inferred_type!
      #   static_scope.self_node = call.target
      # else
      #   static_scope.self_type = scope.self_type
      # end

      body
    else
      Mirah::AST::Noop.new
    end
  end
  macros[name][arg_types] = macro
end

#add_enumerable_macrosObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/mirah/jvm/types/enumerable.rb', line 24

def add_enumerable_macros
  all_proc = proc do |transformer, call|
    if !call.block
      # We need to create a block that just returns the item passed to it
      # First get a new temp for the block argument
      var = transformer.tmp
      # Parse a fake function call to create a block. Then pull of the
      # block and attach it to the real call.
      call.block = transformer.eval("foo {|#{var}| #{var}}").block
    end

    # Now that we've got a block we can transform it into a Loop.
    forloop = expand_each(transformer, call)

    # Start adding stuff to the loop.
    # At the beginning of the loop we create a temp initialized to true
    all = transformer.tmp
    forloop.init << transformer.eval("#{all} = true")

    # Now we want to wrap the body of the loop. Start off by using
    # foo as a placeholder.
    body = transformer.eval(
        "unless foo;#{all} = false;break;end", '', forloop)
    # Then replace foo with the real body.
    body.condition.predicate = call.block.body
    # And finally patch the new body back into the forloop.
    forloop.body = call.block.body.parent = body

    # Loops don't have a return value, so we need somewhere to
    # put the result.
    result = Mirah::AST::Body.new(call.parent, call.position)
    result << forloop << transformer.eval("#{all}", '', nil, all)
  end
  add_macro('all?', &all_proc)
  add_macro('all?', Mirah::AST.block_type, &all_proc)

  any_proc = proc do |transformer, call|
    if !call.block
      var = transformer.tmp
      call.block = transformer.eval("foo {|#{var}| #{var}}").block
    end
    forloop = expand_each(transformer, call)
    any = transformer.tmp
    forloop.init << transformer.eval("#{any} = false")
    body = transformer.eval(
        "if foo;#{any} = true;break;end", '', forloop)
    body.condition.predicate = call.block.body
    forloop.body = call.block.body.parent = body

    result = Mirah::AST::Body.new(call.parent, call.position)
    result << forloop << transformer.eval("#{any}", '', nil, any)
  end
  add_macro('any?', &any_proc)
  add_macro('any?', Mirah::AST.block_type, &any_proc)
end

#add_intrinsicsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/mirah/jvm/types/intrinsics.rb', line 192

def add_intrinsics
  boolean = @type_system.type(nil, 'boolean')
  object_type = @type_system.type(nil, 'java.lang.Object')
  class_type = @type_system.type(nil, 'java.lang.Class')

  add_method('nil?', [], boolean, 'IS_NULL') do |compiler, call, expression|
    if expression
      compiler.visit(call.target, true)
      compiler.method.op_to_bool do |target|
        compiler.method.ifnull(target)
      end
    end
  end

  add_method('==', [object_type], boolean, 'COMPARISON_OP') do |compiler, call, expression|
    # Should this call Object.equals for consistency with Ruby?
    if expression
      compiler.visit(call.target, true)
      compiler.visit(call.parameters(0), true)
      compiler.method.op_to_bool do |target|
        compiler.method.if_acmpeq(target)
      end
    end
  end

  add_method('!=', [object_type], boolean, 'COMPARISON_OP') do |compiler, call, expression|
    # Should this call Object.equals for consistency with Ruby?
    if expression
      compiler.visit(call.target, true)
      compiler.visit(call.parameters(0), true)
      compiler.method.op_to_bool do |target|
        compiler.method.if_acmpne(target)
      end
    end
  end

  add_method('kind_of?', [object_type.meta], boolean, "INSTANCEOF") do |compiler, call, expression|
    compiler.visit(call.target, expression)
    if expression
      klass = compiler.inferred_type(call.parameters(0))
      compiler.method.instanceof(klass.unmeta)
    end
  end

  # add_macro('kind_of?', class_type) do |transformer, call|
  #   klass, object = call.parameters(0), call.target
  #   Mirah::AST::Call.new(call.parent, call.position, 'isInstance') do |call2|
  #     klass.parent = object.parent = call2
  #     [
  #       klass,
  #       [object]
  #     ]
  #   end
  # end
  #
end

#add_method(name, args, method_or_type = nil, kind = nil, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/mirah/jvm/types/intrinsics.rb', line 51

def add_method(name, args, method_or_type=nil, kind=nil, &block)
  if block_given?
    method_or_type = Intrinsic.new(self, name, args,
                                   method_or_type, kind, &block)
  end
  intrinsics[name][args] = method_or_type
end

#add_method_listener(name, listener = nil, &block) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/mirah/jvm/types/methods.rb', line 544

def add_method_listener(name, listener=nil, &block)
  listeners = method_listeners[name] ||= {}
  if listener
    unless listener.respond_to?(:method_updated) || listener.kind_of?(Proc)
      raise "Invalid listener"
    end
    listeners[listener] = listener
  else
    listeners[block] = block
  end
  if !self.meta? && jvm_type && super_class_valid?
    superclass.add_method_listener(name, self)
  end
  interfaces.each {|i| i.add_method_listener(name, self) unless i.isError}
end

#aload(builder) ⇒ Object



301
302
303
304
305
306
307
# File 'lib/mirah/jvm/types/type.rb', line 301

def aload(builder)
  if primitive?
    builder.send "#{name[0,1]}aload"
  else
    builder.aaload
  end
end

#ancestors_and_interfacesObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/mirah/jvm/types/type.rb', line 275

def ancestors_and_interfaces
  if self.primitive?
    []
  else
    ancestors = []
    get_ancestors = lambda {|c| [c.superclass] + c.interfaces(false)}
    new_ancestors = get_ancestors.call(self)
    until new_ancestors.empty?
      klass = new_ancestors.shift
      next if klass.nil? || klass.name == 'java.lang.Object' || klass.isError
      ancestors << klass
      new_ancestors.concat(get_ancestors.call(klass))
    end
    ancestors << @type_system.type(nil, 'java.lang.Object')
    ancestors
  end
end

#array?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/mirah/jvm/types/type.rb', line 112

def array?
  false
end

#array_typeObject



225
226
227
# File 'lib/mirah/jvm/types/type.rb', line 225

def array_type
  @array_type ||= Mirah::JVM::Types::ArrayType.new(self)
end

#assignable_from?(other) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/mirah/jvm/types/type.rb', line 159

def assignable_from?(other)
  return false unless other
  return true if !primitive? && other.kind_of?(NullType)
  return true if other == self
  return true if other.matchesAnything
  return true if other.kind_of?(InlineCode)

  return interface? || abstract? if other.isBlock

  return true if jvm_type && (jvm_type == other.jvm_type)

  return assignable_from?(other.ungeneric) if other.generic?

  return other.convertible_to?(self) if other.primitive?

  assignable_from?(other.superclass) ||
      other.interfaces.any? {|i| assignable_from?(i)}
end

#assignableFrom(other) ⇒ Object



178
179
180
# File 'lib/mirah/jvm/types/type.rb', line 178

def assignableFrom(other)
  assignable_from?(other)
end

#astore(builder) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/mirah/jvm/types/type.rb', line 293

def astore(builder)
  if primitive?
    builder.send "#{name[0,1]}astore"
  else
    builder.aastore
  end
end

#basic_typeObject



221
222
223
# File 'lib/mirah/jvm/types/type.rb', line 221

def basic_type
  self
end

#class_idObject



56
57
58
# File 'lib/mirah/jvm/types/type.rb', line 56

def class_id
  BiteScript::Signature.class_id(self)
end

#compatible?(other) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/mirah/jvm/types/type.rb', line 148

def compatible?(other)
  assignable_from?(other)
end

#component_typeObject



196
197
198
# File 'lib/mirah/jvm/types/type.rb', line 196

def component_type
  @type_system.type(nil, 'java.lang.Object') if iterable?
end

#constructor(*types) ⇒ Object

Raises:

  • (NameError)


608
609
610
611
612
613
614
615
616
# File 'lib/mirah/jvm/types/methods.rb', line 608

def constructor(*types)
  begin
    constructor = jvm_type.getConstructor(*bitescript_signatures(types))
    return JavaConstructor.new(@type_system, constructor) if constructor
  rescue => ex
    log("#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  raise NameError, "No constructor #{name}(#{types.join ', '})"
end

#declared_class_methods(name = nil) ⇒ Object



651
652
653
654
# File 'lib/mirah/jvm/types/methods.rb', line 651

def declared_class_methods(name=nil)
  all_declared_jvm_methods(name).select{ |method| method.static? && !method.synthetic? } +
  meta.declared_intrinsics(name)
end

#declared_constructorsObject



656
657
658
659
660
# File 'lib/mirah/jvm/types/methods.rb', line 656

def declared_constructors
  jvm_type.getConstructors.map do |method|
    JavaConstructor.new(@type_system, method)
  end
end

#declared_instance_methods(name = nil) ⇒ Object



646
647
648
649
# File 'lib/mirah/jvm/types/methods.rb', line 646

def declared_instance_methods(name=nil)
  all_declared_jvm_methods(name).reject{ |method| method.static? && !method.synthetic? } +
  (meta? ? unmeta : self).declared_intrinsics(name)
end

#declared_intrinsics(name = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mirah/jvm/types/intrinsics.rb', line 134

def declared_intrinsics(name=nil)
  methods = []
  all_intrinsics = if name.nil?
    intrinsics
  else
    [[name, intrinsics[name]]]
  end
  all_intrinsics.each do |name, group|
    group.each do |args, method|
      methods << method
    end
  end
  interfaces.each do |interface|
    methods.concat(interface.declared_intrinsics(name)) unless interface.isError
  end
  methods
end

#declared_macros(name = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mirah/jvm/types/intrinsics.rb', line 107

def declared_macros(name=nil)
  result = []
  each_name = lambda do |name, hash|
    hash.each do |args, macro|
      result << macro
    end
  end
  if name
    each_name.call(name, self.macros[name])
  else
    self.macros.each &each_name
  end
  result
end

#dynamic?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/mirah/jvm/types/type.rb', line 136

def dynamic?
  false
end

#error?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/mirah/jvm/types/type.rb', line 152

def error?
  false
end

#expand_each(transformer, call) ⇒ Object



18
19
20
21
22
# File 'lib/mirah/jvm/types/enumerable.rb', line 18

def expand_each(transformer, call)
  arg_types = [Mirah::AST.block_type]
  code = intrinsics['each'][arg_types].return_type
  code.inline(transformer, call)
end

#field_getter(name) ⇒ Object



662
663
664
665
666
667
# File 'lib/mirah/jvm/types/methods.rb', line 662

def field_getter(name)
  field = jvm_field(name)
  return nil unless field

  JavaFieldGetter.new(@type_system, field)
end

#field_setter(name) ⇒ Object



669
670
671
672
673
674
# File 'lib/mirah/jvm/types/methods.rb', line 669

def field_setter(name)
  field = jvm_field(name)
  return nil unless field

  JavaFieldSetter.new(@type_system, field)
end

#find_callable_macros(name) ⇒ Object

TODO take a scope and check visibility



561
562
563
564
565
# File 'lib/mirah/jvm/types/methods.rb', line 561

def find_callable_macros(name)
  macros = find_callable_macros2 name
  macros.concat collect_up_interface_tree {|interface| interface.declared_macros(name) }
  macros
end

#find_callable_macros2(name) ⇒ Object



567
568
569
# File 'lib/mirah/jvm/types/methods.rb', line 567

def find_callable_macros2(name)
  collect_up_inheritance_tree {|type| type.declared_macros name }
end

#find_callable_methods(name, include_interfaces = false, &proc) ⇒ Object

TODO take a scope and check visibility



576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/mirah/jvm/types/methods.rb', line 576

def find_callable_methods(name, include_interfaces=false, &proc)
  if block_given?
    add_method_listener(name) {proc.call(find_callable_methods(name))}
    proc.call(find_callable_methods(name))
    return
  end

  methods = find_callable_methods2 name

  if self.interface? || include_interfaces # TODO || self.abstract?
    methods.concat collect_up_interface_tree { |interface| interface.declared_instance_methods(name) }
  end
  methods
end

#find_callable_methods2(name) ⇒ Object



591
592
593
# File 'lib/mirah/jvm/types/methods.rb', line 591

def find_callable_methods2(name)
  collect_up_inheritance_tree { |type| type.declared_instance_methods(name) }
end

#find_callable_static_methods(name) ⇒ Object



571
572
573
# File 'lib/mirah/jvm/types/methods.rb', line 571

def find_callable_static_methods(name)
  collect_up_inheritance_tree { |type| type.declared_class_methods(name) }
end

#flagsObject



68
69
70
71
72
73
74
75
# File 'lib/mirah/jvm/types/type.rb', line 68

def flags
  flags = BiteScript::ASM::Opcodes::ACC_PUBLIC
  flags |= BiteScript::ASM::Opcodes::ACC_ANNOTATION if isAnnotation
  flags |= BiteScript::ASM::Opcodes::ACC_ENUM if isEnum
  flags |= BiteScript::ASM::Opcodes::ACC_INTERFACE if self.interface?
  flags |= BiteScript::ASM::Opcodes::ACC_ABSTRACT if self.abstract?
  flags
end

#full_nameObject



48
49
50
51
# File 'lib/mirah/jvm/types/type.rb', line 48

def full_name
  desc = BiteScript::Signature.class_id(self)
  BiteScript::ASM::Type.get_type(desc).class_name
end

#genericObject



208
209
210
# File 'lib/mirah/jvm/types/type.rb', line 208

def generic
  @generic ||= GenericType.new(self)
end

#generic?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/mirah/jvm/types/type.rb', line 101

def generic?
  false
end

#get_method(name, args) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/mirah/jvm/types/methods.rb', line 595

def get_method(name, args)
  method = find_method(self, name, args, nil, meta?)
  unless method
    # Allow constant narrowing for assignment methods
    if name =~ /=$/ && args[-1].respond_to?(:narrow!)
      if args[-1].narrow!
        method = find_method(self, name, args, meta?)
      end
    end
  end
  method
end

#getAsmTypeObject



59
60
61
# File 'lib/mirah/jvm/types/type.rb', line 59

def getAsmType
  BiteScript::ASM::Type.get_type(class_id)
end

#getDeclaredField(name) ⇒ Object



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

def getDeclaredField(name)
  @member.getDeclaredField(name)
end

#getDeclaredFieldsObject



688
689
690
# File 'lib/mirah/jvm/types/methods.rb', line 688

def getDeclaredFields
  @member.getDeclaredFields.to_java(JVMMethod)
end

#hasStaticField(name) ⇒ Object



696
697
698
699
# File 'lib/mirah/jvm/types/methods.rb', line 696

def hasStaticField(name)
  f = getDeclaredField(name)
  f && f.static?
end

#include(type) ⇒ Object



106
107
108
# File 'lib/mirah/jvm/types/extensions.rb', line 106

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

#init_value(builder) ⇒ Object



35
36
37
# File 'lib/mirah/jvm/types/intrinsics.rb', line 35

def init_value(builder)
  builder.aconst_null
end

#inner_class?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mirah/jvm/types/type.rb', line 140

def inner_class?
  @inner_class
end

#inner_class_getter(name) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
# File 'lib/mirah/jvm/types/methods.rb', line 676

def inner_class_getter(name)
  full_name = "#{self.name}$#{name}"
  inner_class = @type_system.type(nil, full_name) rescue nil
  return unless inner_class

  inner_class.inner_class = true
  macro = Macro.new(self, name, []) do |call, typer|
    Mirah::AST::Constant.new(call.position, Mirah::AST::SimpleString.new(call.position, full_name))
  end
  intrinsics[name][[]] = macro
end

#inspect(indent = 0) ⇒ Object



238
239
240
# File 'lib/mirah/jvm/types/type.rb', line 238

def inspect(indent=0)
  "#{' ' * indent}#<#{self.class.name.split(/::/)[-1]} #{name}>"
end

#interface?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/mirah/jvm/types/type.rb', line 126

def interface?
  # FIXME: Don't do rescue nil. Figure out a cleaner way to handle
  # mirrors for all incoming types without blowing up on e.g. 'boolean' or 'int'
  (@type || BiteScript::ASM::ClassMirror.for_name(@name)).interface? rescue nil
end

#interfaces(include_parent = true) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/mirah/jvm/types/type.rb', line 263

def interfaces(include_parent=true)
  raise "Incomplete type #{self} (#{self.class})" unless jvm_type
  @interfaces ||= begin
    interfaces = jvm_type.interfaces.map {|i| @type_system.type(nil, i)}.to_set
    if superclass && include_parent
      interfaces |= superclass.interfaces
    end
    interfaces.to_a
  end
  @interfaces
end

#internal_nameObject



53
54
55
# File 'lib/mirah/jvm/types/type.rb', line 53

def internal_name
  full_name.tr('.', '/')
end

#intrinsicsObject



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

def intrinsics
  @intrinsics ||= begin
    @intrinsics = Hash.new {|h, k| h[k] = {}}
    add_intrinsics
    @intrinsics
  end
end

#is_parent(other) ⇒ Object



144
145
146
# File 'lib/mirah/jvm/types/type.rb', line 144

def is_parent(other)
  assignable_from?(other)
end

#isAnnotationObject



62
63
64
# File 'lib/mirah/jvm/types/type.rb', line 62

def isAnnotation
  jvm_type ? jvm_type.annotation? : false
end

#isArrayObject



115
116
117
# File 'lib/mirah/jvm/types/type.rb', line 115

def isArray
  self.array?
end

#isBlockObject



108
109
110
# File 'lib/mirah/jvm/types/type.rb', line 108

def isBlock
  false
end

#isEnumObject



65
66
67
# File 'lib/mirah/jvm/types/type.rb', line 65

def isEnum
  jvm_type ? jvm_type.enum? : false
end

#isErrorObject



155
156
157
# File 'lib/mirah/jvm/types/type.rb', line 155

def isError
  false
end

#isGenericObject



104
105
106
# File 'lib/mirah/jvm/types/type.rb', line 104

def isGeneric
  self.generic?
end

#isMetaObject



96
97
98
# File 'lib/mirah/jvm/types/type.rb', line 96

def isMeta
  self.meta?
end

#isPrimitiveObject



122
123
124
# File 'lib/mirah/jvm/types/type.rb', line 122

def isPrimitive
  self.primitive?
end

#iterable?Boolean

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/mirah/jvm/types/type.rb', line 190

def iterable?
  ['java.lang.Iterable',
   'java.util.Iterator',
   'java.util.Enumeration'].any? {|n| @type_system.type(nil, n).assignable_from(self)}
end

#java_method(name, *types) ⇒ Object

Raises:

  • (NameError)


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/mirah/jvm/types/methods.rb', line 618

def java_method(name, *types)
  intrinsic = intrinsics[name][types]
  return intrinsic if intrinsic

  begin
    method = jvm_type.getDeclaredMethod(name, *bitescript_signatures(types)) if jvm_type

    if method.nil? && superclass
      method = superclass.java_method(name, *types) rescue nil
    end

    if method.nil? && jvm_type && jvm_type.abstract?
      interfaces.each do |interface|
        method = interface.java_method(name, *types) rescue nil
        break if method
      end
    end

    return method if method.kind_of?(JavaCallable)
    if method && method.static? == meta?
      return wrap_jvm_method method
    end
  rescue   => ex
    log("#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
end

#jvm_typeObject



85
86
87
# File 'lib/mirah/jvm/types/type.rb', line 85

def jvm_type
  @type
end

#load(builder, index) ⇒ Object



23
24
25
# File 'lib/mirah/jvm/types/intrinsics.rb', line 23

def load(builder, index)
  builder.send "#{prefix}load", index
end

#load_extensions(klass = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/mirah/jvm/types/intrinsics.rb', line 152

def load_extensions(klass=nil)
  mirror = nil
  if klass
    mirror = @type_system.mirror_class(klass)
  elsif jvm_type
    mirror = jvm_type
  end
  if mirror
    extensions = mirror.getDeclaredAnnotation('org.mirah.macros.anno.Extensions')
    return self if extensions.nil?
    macros = extensions['macros']
    return self if macros.nil?
    macros.each do |macro_class|
      klass = begin
                JRuby.runtime.jruby_class_loader.loadClass(macro_class)
              rescue java.lang.NoClassDefFoundError => ex
                raise ex
              end
      add_compiled_macro(klass)
    end
  end
  self
end

#macro(name, types) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mirah/jvm/types/intrinsics.rb', line 122

def macro(name, types)
  macro = macros[name][types]
  return macro if macro
  macro = superclass.macro(name, types) if (superclass && !superclass.isError)
  return macro if macro
  interfaces.each do |interface|
    macro = interface.macro(name, types) unless interface.isError
    return macro if macro
  end
  nil
end

#macrosObject



47
48
49
# File 'lib/mirah/jvm/types/intrinsics.rb', line 47

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

#matchesAnythingObject



188
# File 'lib/mirah/jvm/types/type.rb', line 188

def matchesAnything; false; end

#metaObject



200
201
202
# File 'lib/mirah/jvm/types/type.rb', line 200

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

#meta?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mirah/jvm/types/type.rb', line 93

def meta?
  false
end

#method_listenersObject



525
526
527
528
529
530
531
# File 'lib/mirah/jvm/types/methods.rb', line 525

def method_listeners
  if meta?
    unmeta.method_listeners
  else
    @method_listeners ||= {}
  end
end

#method_updated(name) ⇒ Object



533
534
535
536
537
538
539
540
541
542
# File 'lib/mirah/jvm/types/methods.rb', line 533

def method_updated(name)
  listeners = method_listeners[name]
  listeners.values.each do |l|
    if l.kind_of?(Proc)
      l.call(name)
    else
      l.method_updated(name)
    end
  end if listeners
end

#newarray(method) ⇒ Object



246
247
248
# File 'lib/mirah/jvm/types/type.rb', line 246

def newarray(method)
  method.anewarray(self)
end

#pop(method) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/mirah/jvm/types/type.rb', line 250

def pop(method)
  if wide?
    method.pop2
  else
    method.pop
  end
end

#prefixObject



229
230
231
# File 'lib/mirah/jvm/types/type.rb', line 229

def prefix
  'a'
end

#primitive?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/mirah/jvm/types/type.rb', line 119

def primitive?
  false
end

#read_macrodef_annotation(klass) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mirah/jvm/types/intrinsics.rb', line 176

def read_macrodef_annotation(klass)
  macro = klass.getAnnotation(MacroDef.java_class)
  if macro.nil?
    error("Unable to load macro #{klass.name}: no MacroDef annotation")
    return
  end
  macro_name = macro.name
  # TODO optional, rest args
  args = macro.arguments.required.map do |typename|
    type = @type_system.get_type(typename)
    raise "Unable to find class #{typename}" unless type
    type
  end
  [macro_name, args, macro.is_static]
end

#retentionObject



76
77
78
79
80
81
82
83
# File 'lib/mirah/jvm/types/type.rb', line 76

def retention
  if jvm_type.respond_to?(:getDeclaredAnnotation)
    retention = jvm_type.getDeclaredAnnotation('java.lang.annotation.Retention')
    retention.value.name
  else
    nil
  end
end

#return(builder) ⇒ Object



31
32
33
# File 'lib/mirah/jvm/types/intrinsics.rb', line 31

def return(builder)
  builder.send "#{prefix}return"
end

#store(builder, index) ⇒ Object



27
28
29
# File 'lib/mirah/jvm/types/intrinsics.rb', line 27

def store(builder, index)
  builder.send "#{prefix}store", index
end

#superclassObject



258
259
260
261
# File 'lib/mirah/jvm/types/type.rb', line 258

def superclass
  raise "Incomplete type #{self}" unless jvm_type
  @type_system.type(nil, jvm_type.superclass) if jvm_type.superclass
end

#to_sObject



242
243
244
# File 'lib/mirah/jvm/types/type.rb', line 242

def to_s
  inspect
end

#type_parametersObject



216
217
218
219
# File 'lib/mirah/jvm/types/type.rb', line 216

def type_parameters
  return nil unless jvm_type
  jvm_type.type_parameters
end

#ungenericObject



212
213
214
# File 'lib/mirah/jvm/types/type.rb', line 212

def ungeneric
  self
end

#unmetaObject



204
205
206
# File 'lib/mirah/jvm/types/type.rb', line 204

def unmeta
  self
end

#void?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mirah/jvm/types/type.rb', line 89

def void?
  false
end

#wide?Boolean

is this a 64 bit type?

Returns:

  • (Boolean)


234
235
236
# File 'lib/mirah/jvm/types/type.rb', line 234

def wide?
  false
end

#widen(other) ⇒ Object



182
183
184
185
186
# File 'lib/mirah/jvm/types/type.rb', line 182

def widen(other)
  return self if assignable_from?(other)
  common_parent = (ancestors_and_interfaces & ([other] + other.ancestors_and_interfaces))[0]
  common_parent || ErrorType.new([["Incompatible types #{self} and #{other}."]])
end