rtype 0.6.8-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,274 @@
1
+ # true or false
2
+ module Boolean; end
3
+ class TrueClass; include Boolean; end
4
+ class FalseClass; include Boolean; end
5
+
6
+ Any = BasicObject
7
+
8
+ class Object
9
+ include ::Rtype::MethodAnnotator
10
+ end
11
+
12
+ module Kernel
13
+ private
14
+ def _rtype_proxy
15
+ unless @_rtype_proxy
16
+ @_rtype_proxy = ::Rtype::RtypeProxy.new
17
+ prepend @_rtype_proxy
18
+ end
19
+ @_rtype_proxy
20
+ end
21
+
22
+ # Makes the method typed
23
+ #
24
+ # With 'annotation mode', this method works for both instance method and singleton method (class method).
25
+ # Without it, this method only works for instance method.
26
+ #
27
+ # @param [#to_sym, nil] method_name The name of method. If nil, annotation mode works
28
+ # @param [Hash] type_sig_info A type signature. e.g. [Integer] => Any
29
+ # @return [void]
30
+ #
31
+ # @note Annotation mode doesn't work in the outside of module
32
+ # @raise [ArgumentError] If method_name is nil in the outside of module
33
+ # @raise [TypeSignatureError] If type_sig_info is invalid
34
+ def rtype(method_name=nil, type_sig_info)
35
+ if is_a?(Module)
36
+ if method_name.nil?
37
+ ::Rtype::assert_valid_type_sig(type_sig_info)
38
+ _rtype_proxy.annotation_mode = true
39
+ _rtype_proxy.annotation_type_sig = type_sig_info
40
+ nil
41
+ else
42
+ ::Rtype::define_typed_method(self, method_name, type_sig_info)
43
+ end
44
+ else
45
+ if method_name.nil?
46
+ raise ArgumentError, "Annotation mode doesn't work in the outside of module"
47
+ else
48
+ rtype_self(method_name, type_sig_info)
49
+ end
50
+ end
51
+ end
52
+
53
+ # Makes the singleton method (class method) typed
54
+ #
55
+ # @param [#to_sym] method_name
56
+ # @param [Hash] type_sig_info A type signature. e.g. [Integer] => Any
57
+ # @return [void]
58
+ #
59
+ # @raise [ArgumentError] If method_name is nil
60
+ # @raise [TypeSignatureError] If type_sig_info is invalid
61
+ def rtype_self(method_name, type_sig_info)
62
+ ::Rtype.define_typed_method(singleton_class, method_name, type_sig_info)
63
+ end
64
+
65
+ # Calls `attr_accessor` if the accessor method(getter/setter) is not defined.
66
+ # and makes it typed.
67
+ #
68
+ # @param [Array<#to_sym>] names
69
+ # @param type_behavior A type behavior
70
+ # @return [void]
71
+ #
72
+ # @raise [ArgumentError] If names contains nil
73
+ # @raise [TypeSignatureError] If type_behavior is invalid
74
+ # @see #rtype
75
+ def rtype_accessor(*names, type_behavior)
76
+ rtype_reader(*names, type_behavior)
77
+ rtype_writer(*names, type_behavior)
78
+ end
79
+
80
+ # Calls `attr_accessor` if the accessor method(getter/setter) is not defined.
81
+ # and makes it typed.
82
+ #
83
+ # @param [Array<#to_sym>] names
84
+ # @param type_behavior A type behavior
85
+ # @return [void]
86
+ #
87
+ # @raise [ArgumentError] If names contains nil
88
+ # @raise [TypeSignatureError] If type_behavior is invalid
89
+ # @see #rtype_self
90
+ def rtype_accessor_self(*names, type_behavior)
91
+ rtype_reader_self(*names, type_behavior)
92
+ rtype_writer_self(*names, type_behavior)
93
+ end
94
+
95
+ # Calls `attr_reader` if the getter method is not defined.
96
+ # and makes it typed.
97
+ #
98
+ # @param [Array<#to_sym>] names
99
+ # @param type_behavior A type behavior
100
+ # @return [void]
101
+ #
102
+ # @raise [ArgumentError] If names contains nil
103
+ # @raise [TypeSignatureError] If type_behavior is invalid
104
+ # @see #rtype
105
+ def rtype_reader(*names, type_behavior)
106
+ names.each do |name|
107
+ raise ArgumentError, "names contains nil" if name.nil?
108
+
109
+ name = name.to_sym
110
+ if !respond_to?(name)
111
+ attr_reader name
112
+ end
113
+
114
+ if is_a?(Module)
115
+ ::Rtype::define_typed_reader(self, name, type_behavior)
116
+ else
117
+ rtype_reader_self(name, type_behavior)
118
+ end
119
+ end
120
+ nil
121
+ end
122
+
123
+ # Calls `attr_reader` if the getter method is not defined.
124
+ # and makes it typed.
125
+ #
126
+ # @param [Array<#to_sym>] names
127
+ # @param type_behavior A type behavior
128
+ # @return [void]
129
+ #
130
+ # @raise [ArgumentError] If names contains nil
131
+ # @raise [TypeSignatureError] If type_behavior is invalid
132
+ # @see #rtype_self
133
+ def rtype_reader_self(*names, type_behavior)
134
+ names.each do |name|
135
+ raise ArgumentError, "names contains nil" if name.nil?
136
+
137
+ name = name.to_sym
138
+ if !respond_to?(name)
139
+ singleton_class.send(:attr_reader, name)
140
+ end
141
+ ::Rtype::define_typed_reader(singleton_class, name, type_behavior)
142
+ end
143
+ nil
144
+ end
145
+
146
+ # Calls `attr_writer` if the setter method is not defined.
147
+ # and makes it typed.
148
+ #
149
+ # @param [Array<#to_sym>] names
150
+ # @param type_behavior A type behavior
151
+ # @return [void]
152
+ #
153
+ # @raise [ArgumentError] If names contains nil
154
+ # @raise [TypeSignatureError] If type_behavior is invalid
155
+ # @see #rtype
156
+ def rtype_writer(*names, type_behavior)
157
+ names.each do |name|
158
+ raise ArgumentError, "names contains nil" if name.nil?
159
+
160
+ name = name.to_sym
161
+ if !respond_to?(:"#{name}=")
162
+ attr_writer name
163
+ end
164
+
165
+ if is_a?(Module)
166
+ ::Rtype::define_typed_writer(self, name, type_behavior)
167
+ else
168
+ rtype_reader_self(name, type_behavior)
169
+ end
170
+ end
171
+ nil
172
+ end
173
+
174
+ # Calls `attr_writer` if the setter method is not defined.
175
+ # and makes it typed.
176
+ #
177
+ # @param [Array<#to_sym>] names
178
+ # @param type_behavior A type behavior
179
+ # @return [void]
180
+ #
181
+ # @raise [ArgumentError] If names contains nil
182
+ # @raise [TypeSignatureError] If type_behavior is invalid
183
+ # @see #rtype_self
184
+ def rtype_writer_self(*names, type_behavior)
185
+ names.each do |name|
186
+ raise ArgumentError, "names contains nil" if name.nil?
187
+
188
+ name = name.to_sym
189
+ if !respond_to?(:"#{name}=")
190
+ singleton_class.send(:attr_writer, name)
191
+ end
192
+ ::Rtype::define_typed_writer(singleton_class, name, type_behavior)
193
+ end
194
+ nil
195
+ end
196
+
197
+ # Creates getter, setter methods.
198
+ # The getter method is typed with Float
199
+ # and the setter method is typed with Numeric.
200
+ #
201
+ # If the setter is called with a numeric given,
202
+ # the setter convert the numeric to a float, and store it.
203
+ #
204
+ # As a result, setter can accept a Numeric(Integer/Float), and getter always returns a Float
205
+ #
206
+ # @param [Array<#to_sym>] names
207
+ # @return [void]
208
+ #
209
+ # @raise [ArgumentError] If names contains nil
210
+ def float_accessor(*names)
211
+ names.each do |name|
212
+ raise ArgumentError, "names contains nil" if name.nil?
213
+
214
+ name = name.to_sym
215
+ rtype_reader name, Float
216
+ define_method(:"#{name}=") do |val|
217
+ instance_variable_set(:"@#{name}", val.to_f)
218
+ end
219
+ ::Rtype::define_typed_writer(self, name, Numeric)
220
+ end
221
+ nil
222
+ end
223
+
224
+ # Creates getter, setter methods. And makes it typed with Boolean.
225
+ # The name of the getter ends with `?`.
226
+ #
227
+ # e.g. `bool_accessor :closed` will create `closed=` and `closed?` methods
228
+ #
229
+ # @param [Array<#to_sym>] names
230
+ # @return [void]
231
+ #
232
+ # @raise [ArgumentError] If names contains nil
233
+ def bool_accessor(*names)
234
+ names.each do |name|
235
+ raise ArgumentError, "names contains nil" if name.nil?
236
+
237
+ name = name.to_sym
238
+ rtype_writer name, Boolean
239
+ define_method(:"#{name}?") do
240
+ instance_variable_get(:"@#{name}")
241
+ end
242
+ ::Rtype::define_typed_reader(self, :"#{name}?", Boolean)
243
+ end
244
+ nil
245
+ end
246
+ end
247
+
248
+ class Method
249
+ # @return [Boolean] Whether the method is typed with rtype
250
+ def typed?
251
+ !!::Rtype.type_signatures[owner][name]
252
+ end
253
+
254
+ # @return [TypeSignature]
255
+ def type_signature
256
+ ::Rtype.type_signatures[owner][name]
257
+ end
258
+
259
+ # @return [Hash]
260
+ # @see TypeSignature#info
261
+ def type_info
262
+ ::Rtype.type_signatures[owner][name].info
263
+ end
264
+
265
+ # @return [Array, Hash]
266
+ def argument_type
267
+ ::Rtype.type_signatures[owner][name].argument_type
268
+ end
269
+
270
+ # @return A type behavior
271
+ def return_type
272
+ ::Rtype.type_signatures[owner][name].return_type
273
+ end
274
+ end
@@ -0,0 +1,31 @@
1
+ module Rtype
2
+ module MethodAnnotator
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def method_added(name)
9
+ if @_rtype_proxy
10
+ proxy = _rtype_proxy
11
+ if proxy.annotation_mode
12
+ ::Rtype::define_typed_method(self, name, proxy.annotation_type_sig)
13
+ proxy.annotation_mode = false
14
+ proxy.annotation_type_sig = nil
15
+ end
16
+ end
17
+ end
18
+
19
+ def singleton_method_added(name)
20
+ if @_rtype_proxy
21
+ proxy = _rtype_proxy
22
+ if proxy.annotation_mode
23
+ ::Rtype::define_typed_method(singleton_class, name, proxy.annotation_type_sig)
24
+ proxy.annotation_mode = false
25
+ proxy.annotation_type_sig = nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module Rtype
2
+ class ReturnTypeError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module Rtype
2
+ class RtypeProxy < Module
3
+ attr_accessor :annotation_mode
4
+ attr_accessor :annotation_type_sig
5
+ def initialize
6
+ @annotation_mode = false
7
+ @annotation_type_sig = nil
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Rtype
2
+ class TypeSignature
3
+ attr_accessor :argument_type, :return_type
4
+
5
+ # @return [Hash] A type signature
6
+ def info
7
+ {argument_type => return_type}
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Rtype
2
+ class TypeSignatureError < ArgumentError
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module Rtype
2
+ VERSION = "0.6.8".freeze
3
+ # rtype java extension version. nil If the extension is not used
4
+ JAVA_EXT_VERSION = nil unless const_defined?(:JAVA_EXT_VERSION, false)
5
+ # rtype c extension version. nil If the extension is not used
6
+ NATIVE_EXT_VERSION = nil unless const_defined?(:NATIVE_EXT_VERSION, false)
7
+ end