forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.lang.rb
More file actions
207 lines (162 loc) · 3.49 KB
/
Copy pathjava.lang.rb
File metadata and controls
207 lines (162 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module java::lang::Runnable
def to_proc
proc { self.run }
end
end
module java::lang::Iterable
include Enumerable
def each
iter = iterator
yield(iter.next) while iter.hasNext
end
def each_with_index
index = 0
iter = iterator
while iter.hasNext
yield(iter.next, index)
index += 1
end
end
end
module java::lang::Comparable
include Comparable
def <=>(a)
return nil if a.nil?
compareTo(a)
end
end
class java::lang::Throwable
def backtrace
stack_trace.map(&:to_s)
end
def set_backtrace(trace)
# ignored; Java exceptions can't be set to Ruby trace
trace
end
def message
msg = getLocalizedMessage
msg ? msg : ""
end
def to_s
message
end
def to_str
to_s
end
def inspect
to_string
end
class << self
alias :old_eqq :===
def ===(rhs)
if (NativeException == rhs.class) && (java_class.assignable_from?(rhs.cause.java_class))
true
else
old_eqq(rhs)
end
end
end
end
module JavaUtilities::ModifierShortcuts
java_import java.lang.reflect.Modifier
def public?
Modifier.is_public(modifiers)
end
def protected?
Modifier.is_protected(modifiers)
end
def private?
Modifier.is_private(modifiers)
end
def final?
Modifier.is_final(modifiers)
end
def static?
Modifier.is_final(modifiers)
end
end
class java::lang::ClassLoader
alias resource_as_stream get_resource_as_stream
alias resource_as_url get_resource
def resource_as_string(name)
resource_as_stream(name).to_io.read
end
end
class java::lang::Class
include Comparable
include JavaUtilities::ModifierShortcuts
def ruby_class
JRuby.runtime.java_support.get_proxy_class_from_cache(self)
end
alias to_s name
def inspect
"class #{name}"
end
def resource_as_string(name)
resource_as_stream(name).to_io.read
end
alias annotation get_annotation
def annotations?
!annotations.empty?
end
def declared_annotations?
!declared_annotations.empty?
end
alias annotation_present? is_annotation_present
def <=>(other)
return nil unless other.class == java::lang::Class
return 0 if self == other
return 1 if self.is_assignable_from(other)
return -1 if other.is_assignable_from(self)
end
def java_instance_methods
methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
end
def declared_instance_methods
declared_methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
end
def java_class_methods
methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
end
def declared_class_methods
declared_methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
end
end
class java::lang::reflect::AccessibleObject
include JavaUtilities::ModifierShortcuts
alias inspect to_s
end
class java::lang::reflect::Constructor
def return_type
nil
end
alias argument_types parameter_types
end
class java::lang::reflect::Method
def invoke_static(*args)
invoke(nil, *args)
end
alias argument_types parameter_types
end
class java::lang::reflect::Field
alias value_type name
alias value get
alias set_value set
def static_value
get(nil)
end
def set_static_value(val)
set(nil, val)
end
end
Java::byte[].class_eval do
def ubyte_get(index)
byte = self[index]
byte += 256 if byte < 0
byte
end
def ubyte_set(index, value)
value -= 256 if value > 127
self[index] = value
end
end