jtor-stdlib 0.1.1-java → 0.1.2-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30f6a9eaeea71b4b9ed16ebf62ea7c995ed5b6f0
4
- data.tar.gz: 510159c2e64ef1cbc76635a7fba3c204bc143361
3
+ metadata.gz: 23edc4304941a8c8b28d4ed49943f1c381c092cf
4
+ data.tar.gz: 17f06f61a48310cc6cf8944451d844b7b736d550
5
5
  SHA512:
6
- metadata.gz: 32fc80c2e1a9cd631845d8104e33d15b8360b60e36cee35ffca6f6b687116dc26f5736d242bf0120e577199465cd31361279de43ec24c2e35234634fdb970292
7
- data.tar.gz: d187b0b8837f0e94729d9f2c61fd4495c99c65a504d442c283a4b66b4de9c331e58410dcd60de054ac2e62bc7683ea0f12e75a667612c2d9b6cd456b08f3e329
6
+ metadata.gz: dabbfe23ec0faceb3a6ec3d6a9920a9bdd8fc323b0d2f04484241ca89138673376f7ac7b402480ee5dab002260cbaf152d31dbcc7819a84639f001277dc8f617
7
+ data.tar.gz: 54e182ce43ee02084b0a73f8b00eba09bf64657a46a8d6019d80d5d7aff7113590bcd8c21222bf522eac3f5b823fab52d6a58a43dc1646f77b4d563b189aaf85
@@ -2,5 +2,6 @@ warn 'Loading jtor-stdlib in a non-JRuby interpreter' unless defined? JRUBY_VERS
2
2
 
3
3
  require 'java'
4
4
 
5
+ require 'jtor-stdlib/superobject'
5
6
  require 'jtor-stdlib/base'
6
7
  require 'jtor-stdlib/jtor_import'
@@ -1,42 +1,81 @@
1
1
  module Jtor
2
2
  module StdLib
3
3
  module Base
4
- @@_lookup = {}
5
-
6
- # Method overloading is not a ruby thing, so we implement a pseudo-lookup
7
- # mechanism for method based on the type/count of their args
8
- def add_java_method(name, param_types, &block)
9
- add_method_to_lookup(name, param_types, &block)
10
- unless method_defined?(name)
11
- define_method(name) { |*args| self.class.lookup(name, *args) }
12
- end
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def sup
9
+ @_sup ||= SuperObject.new(self)
13
10
  end
14
11
 
15
- def lookup(name, *args)
16
- if @@_lookup[name]
17
- @@_lookup[name].each do |param_types, method|
18
- # NOTE: This is an oversimplification of the way Java determines
19
- # which method to invoke. I may work further into this later (see
20
- # http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12)
21
- next unless params_match(param_types, args)
22
- return method.call(*args)
12
+ module ClassMethods
13
+ def _lookup_table
14
+ @_lookup_table ||= {}
15
+ end
16
+
17
+ def _constructor_calls
18
+ @_constructor_calls ||= {}
19
+ end
20
+
21
+ # Method overloading is not a ruby thing, so we implement a pseudo-lookup
22
+ # mechanism for method based on the type/count of their args
23
+ def add_java_method(name, param_types, &block)
24
+ previously_defined = _lookup_table[name]
25
+ add_method_to_lookup(name, param_types, &block)
26
+ return if previously_defined
27
+ define_method(name) do |*args|
28
+ instance_exec(*args, &self.class.lookup(name, *args))
23
29
  end
24
30
  end
25
- method_missing(name, *args)
26
- end
27
31
 
28
- def params_match(types, values)
29
- # TODO: Handle var args (...)
30
- types.each_with_index do |type, index|
31
- value = values[index]
32
- return false unless value.is_a?(type) || value.to_java.is_a?(type)
32
+ def add_java_constructor(param_types, constructor_call = nil, &block)
33
+ add_method_to_lookup(:initialize, param_types, &block)
34
+ previously_defined = _constructor_calls.any?
35
+ _constructor_calls[param_types] = constructor_call
36
+ return if previously_defined
37
+ define_method(:initialize) do |*args|
38
+ method, constructor_call = self.class.lookup(:initialize, *args)
39
+ # I really tried to avoid using eval fellows, but JRuby is very fixed
40
+ # on `super` being called here first than anything, and it wouldn't
41
+ # accept a `proc` calling `initialize` on `sup`, so this will have
42
+ # to do for now.
43
+ eval(constructor_call) #if constructor_call
44
+ instance_exec(*args, &method)
45
+ end
46
+ end
47
+
48
+ def lookup(name, *args)
49
+ if _lookup_table[name]
50
+ _lookup_table[name].each do |param_types, method|
51
+ # NOTE: This is an oversimplification of the way Java determines
52
+ # which method to invoke. I may work further into this later (see
53
+ # http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12)
54
+ next unless params_match(param_types, args)
55
+ return (if (constructor_call = _constructor_calls[param_types])
56
+ [method, constructor_call]
57
+ else
58
+ method
59
+ end)
60
+ end
61
+ end
62
+ method_missing(name, *args)
33
63
  end
34
- true
35
- end
36
64
 
37
- def add_method_to_lookup(name, param_types, &block)
38
- @@_lookup[name] ||= {}
39
- @@_lookup[name][param_types] = block
65
+ def params_match(types, values)
66
+ # TODO: Handle var args (...)
67
+ return false if types.size != values.size
68
+ types.each_with_index do |type, index|
69
+ value = values[index]
70
+ return false unless value.is_a?(type) || value.to_java.is_a?(type)
71
+ end
72
+ true
73
+ end
74
+
75
+ def add_method_to_lookup(name, param_types, &block)
76
+ _lookup_table[name] ||= {}
77
+ _lookup_table[name][param_types] = block
78
+ end
40
79
  end
41
80
  end
42
81
  end
@@ -0,0 +1,18 @@
1
+ module Jtor
2
+ module StdLib
3
+ class SuperObject
4
+ def initialize(obj)
5
+ @obj = obj
6
+ end
7
+
8
+ def method_missing(symbol, *args, &block)
9
+ superclass = @obj.class.superclass
10
+ if superclass < Base # If it's a Jtor class
11
+ superclass.lookup(@obj, symbol, *args)
12
+ else # All other classes (from http://stackoverflow.com/a/1251199)
13
+ superclass.instance_method(symbol).bind(@obj).call(*args, &block)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jtor-stdlib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: java
6
6
  authors:
7
7
  - Alejandro Rodríguez
@@ -19,6 +19,7 @@ files:
19
19
  - lib/jtor-stdlib.rb
20
20
  - lib/jtor-stdlib/base.rb
21
21
  - lib/jtor-stdlib/jtor_import.rb
22
+ - lib/jtor-stdlib/superobject.rb
22
23
  homepage: https://gitlab.com/eReGeBe/jtor-stdlib
23
24
  licenses:
24
25
  - MIT