Repository navigation
Expand file tree
/
Copy pathArrayJavaProxy.java
More file actions
267 lines (222 loc) · 10.5 KB
/
Copy pathArrayJavaProxy.java
File metadata and controls
267 lines (222 loc) · 10.5 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package org.jruby.java.proxies;
import java.lang.reflect.Array;
import java.util.Arrays;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyInteger;
import org.jruby.RubyModule;
import org.jruby.RubyRange;
import org.jruby.anno.JRubyMethod;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.java.util.ArrayUtils;
import org.jruby.javasupport.JavaArray;
import org.jruby.javasupport.JavaClass;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
public class ArrayJavaProxy extends JavaProxy {
private final JavaUtil.JavaConverter converter;
public ArrayJavaProxy(Ruby runtime, RubyClass klazz, Object ary) {
this(runtime, klazz, ary, JavaUtil.getJavaConverter(ary.getClass().getComponentType()));
}
public ArrayJavaProxy(Ruby runtime, RubyClass klazz, Object ary, JavaUtil.JavaConverter converter) {
super(runtime, klazz, ary);
this.converter = converter;
}
public static RubyClass createArrayJavaProxy(ThreadContext context) {
Ruby runtime = context.runtime;
RubyClass arrayJavaProxy = runtime.defineClass("ArrayJavaProxy",
runtime.getJavaSupport().getJavaProxyClass(),
ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
RubyClass singleton = arrayJavaProxy.getSingletonClass();
final DynamicMethod oldNew = singleton.searchMethod("new");
singleton.addMethod("new", new ArrayNewMethod(singleton, Visibility.PUBLIC, oldNew));
arrayJavaProxy.defineAnnotatedMethods(ArrayJavaProxy.class);
arrayJavaProxy.includeModule(runtime.getEnumerable());
return arrayJavaProxy;
}
public JavaArray getJavaArray() {
JavaArray javaArray = (JavaArray)dataGetStruct();
if (javaArray == null) {
javaArray = new JavaArray(getRuntime(), getObject());
dataWrapStruct(javaArray);
}
return javaArray;
}
@JRubyMethod(name = {"length","size"})
public IRubyObject length(ThreadContext context) {
return context.runtime.newFixnum(Array.getLength(this.getObject()));
}
@JRubyMethod(name = "empty?")
public IRubyObject empty(ThreadContext context) {
return context.runtime.newBoolean(Array.getLength(this.getObject()) == 0);
}
@JRubyMethod(name = "[]")
public IRubyObject op_aref(ThreadContext context, IRubyObject arg) {
if (arg instanceof RubyInteger) {
int index = (int)((RubyInteger)arg).getLongValue();
return ArrayUtils.arefDirect(context.runtime, getObject(), converter, index);
} else {
return getRange(context, arg);
}
}
@JRubyMethod(name = "[]", required = 1, rest = true)
public IRubyObject op_aref(ThreadContext context, IRubyObject[] args) {
if (args.length == 1 && args[0] instanceof RubyInteger) {
int index = (int)((RubyInteger)args[0]).getLongValue();
return ArrayUtils.arefDirect(context.runtime, getObject(), converter, index);
} else {
return getRange(context, args);
}
}
@JRubyMethod(name = "[]=")
public IRubyObject op_aset(ThreadContext context, IRubyObject index, IRubyObject value) {
ArrayUtils.asetDirect(context.runtime, getObject(), converter, (int)((RubyInteger)index).getLongValue(), value);
return value;
}
@JRubyMethod
public IRubyObject at(ThreadContext context, IRubyObject indexObj) {
Ruby runtime = context.runtime;
Object array = getObject();
int length = Array.getLength(array);
long index = indexObj.convertToInteger().getLongValue();
if (index < 0) {
index = index + length;
}
if (index >= 0 && index < length) {
return ArrayUtils.arefDirect(runtime, array, converter, (int)index);
} else {
return context.nil;
}
}
@JRubyMethod(name = "+")
public IRubyObject op_plus(ThreadContext context, IRubyObject other) {
if (other instanceof ArrayJavaProxy) {
Object otherArray = ((ArrayJavaProxy)other).getObject();
if (getObject().getClass().getComponentType().isAssignableFrom(otherArray.getClass().getComponentType())) {
return ArrayUtils.concatArraysDirect(context, getObject(), otherArray);
}
}
return ArrayUtils.concatArraysDirect(context, getObject(), other);
}
@JRubyMethod
public IRubyObject each(ThreadContext context, Block block) {
Ruby runtime = context.runtime;
int length = Array.getLength(getObject());
for (int i = 0; i < length; i++) {
IRubyObject rubyObj = ArrayUtils.arefDirect(runtime, getObject(), converter, i);
block.yield(context, rubyObj);
}
return this;
}
@JRubyMethod(name = {"to_a","to_ary"})
public IRubyObject to_a(ThreadContext context) {
return JavaUtil.convertJavaArrayToRubyWithNesting(context, this.getObject());
}
@JRubyMethod
public IRubyObject inspect(ThreadContext context) {
StringBuffer buffer = new StringBuffer();
Class componentClass = getObject().getClass().getComponentType();
buffer.append(componentClass.getName());
if (componentClass.isPrimitive()) {
switch (componentClass.getName().charAt(0)) {
case 'b':
if (componentClass == byte.class) buffer.append(Arrays.toString((byte[])getObject()));
if (componentClass == boolean.class) buffer.append(Arrays.toString((boolean[])getObject()));
break;
case 's':
if (componentClass == short.class) buffer.append(Arrays.toString((short[])getObject()));
break;
case 'c':
if (componentClass == char.class) buffer.append(Arrays.toString((char[])getObject()));
break;
case 'i':
if (componentClass == int.class) buffer.append(Arrays.toString((int[])getObject()));
break;
case 'l':
if (componentClass == long.class) buffer.append(Arrays.toString((long[])getObject()));
break;
case 'f':
if (componentClass == float.class) buffer.append(Arrays.toString((float[])getObject()));
break;
case 'd':
if (componentClass == double.class) buffer.append(Arrays.toString((double[])getObject()));
break;
}
} else {
buffer.append(Arrays.toString((Object[])getObject()));
}
buffer.append('@')
.append(Integer.toHexString(inspectHashCode()));
return context.runtime.newString(buffer.toString());
}
public IRubyObject getRange(ThreadContext context, IRubyObject[] args) {
if (args.length == 1) {
return getRange(context, args[0]);
} else if (args.length == 2) {
return getRange(context, args[0], args[1]);
} else {
throw context.runtime.newArgumentError(args.length, 1);
}
}
public IRubyObject getRange(ThreadContext context, IRubyObject arg0) {
int length = Array.getLength(getObject());
if (arg0 instanceof RubyRange) {
RubyRange range = (RubyRange)arg0;
if (range.first() instanceof RubyFixnum && range.last() instanceof RubyFixnum) {
int first = (int)((RubyFixnum)range.first()).getLongValue();
int last = (int)((RubyFixnum)range.last()).getLongValue();
first = first >= 0 ? first : length + first;
last = last >= 0 ? last : length + last;
int newLength = last - first;
if (range.exclude_end_p().isFalse()) newLength += 1;
if (newLength <= 0) {
return ArrayUtils.emptyJavaArrayDirect(context, getObject().getClass().getComponentType());
}
return ArrayUtils.javaArraySubarrayDirect(context, getObject(), first, newLength);
} else {
throw context.runtime.newTypeError("only Fixnum ranges supported");
}
} else {
throw context.runtime.newTypeError(arg0, context.runtime.getRange());
}
}
public IRubyObject getRange(ThreadContext context, IRubyObject firstObj, IRubyObject lengthObj) {
if (firstObj instanceof RubyFixnum && lengthObj instanceof RubyFixnum) {
int first = (int)((RubyFixnum)firstObj).getLongValue();
int length = (int)((RubyFixnum)lengthObj).getLongValue();
if (length > Array.getLength(getObject())) {
throw context.runtime.newIndexError("length specifed is longer than array");
}
first = first >= 0 ? first : Array.getLength(getObject()) + first;
if (length <= 0) {
return ArrayUtils.emptyJavaArrayDirect(context, getObject().getClass().getComponentType());
}
return ArrayUtils.javaArraySubarrayDirect(context, getObject(), first, length);
} else {
throw context.runtime.newTypeError("only Fixnum ranges supported");
}
}
public static class ArrayNewMethod extends org.jruby.internal.runtime.methods.JavaMethod.JavaMethodOne {
private DynamicMethod oldNew;
public ArrayNewMethod(RubyModule implClass, Visibility visibility, DynamicMethod oldNew) {
super(implClass, visibility);
this.oldNew = oldNew;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) {
Ruby runtime = context.runtime;
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy");
if (arg0 instanceof JavaArray) {
proxy.dataWrapStruct(arg0);
return proxy;
} else {
throw runtime.newTypeError(arg0, runtime.getJavaSupport().getJavaArrayClass());
}
}
}
}