Repository navigation
Expand file tree
/
Copy pathSingletonMethodInvoker.java
More file actions
164 lines (140 loc) · 7.12 KB
/
Copy pathSingletonMethodInvoker.java
File metadata and controls
164 lines (140 loc) · 7.12 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
package org.jruby.java.invokers;
import java.lang.reflect.Method;
import java.util.List;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyProc;
import org.jruby.javasupport.JavaMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class SingletonMethodInvoker extends MethodInvoker {
private Object singleton;
public SingletonMethodInvoker(Object singleton, RubyClass host, List<Method> methods) {
super(host, methods);
this.singleton = singleton;
}
public SingletonMethodInvoker(Object singleton, RubyClass host, Method method) {
super(host, method);
this.singleton = singleton;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
int len = args.length;
Object[] convertedArgs = new Object[len];
JavaMethod method = (JavaMethod)findCallable(self, name, args, len);
if (method.isVarArgs()) {
len = method.getParameterTypes().length - 1;
convertedArgs = new Object[len + 1];
for (int i = 0; i < len && i < args.length; i++) {
convertedArgs[i] = convertArg(args[i], method, i);
}
convertedArgs[len] = convertVarargs(args, method);
} else {
convertedArgs = new Object[len];
for (int i = 0; i < len && i < args.length; i++) {
convertedArgs[i] = convertArg(args[i], method, i);
}
}
return method.invokeDirect(singleton, convertedArgs);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
if (javaVarargsCallables != null) return call(context, self, clazz, name, IRubyObject.NULL_ARRAY);
JavaMethod method = (JavaMethod)findCallableArityZero(self, name);
return method.invokeDirect(singleton);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) {
if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0});
JavaMethod method = (JavaMethod)findCallableArityOne(self, name, arg0);
if (method.isVarArgs()) return call(context, self, clazz, name, new IRubyObject[] {arg0});
Object cArg0 = convertArg(arg0, method, 0);
return method.invokeDirect(singleton, cArg0);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1) {
if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1});
JavaMethod method = (JavaMethod)findCallableArityTwo(self, name, arg0, arg1);
Object cArg0 = convertArg(arg0, method, 0);
Object cArg1 = convertArg(arg1, method, 1);
return method.invokeDirect(singleton, cArg0, cArg1);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
if (javaVarargsCallables != null) return call(context, self, clazz, name, new IRubyObject[] {arg0, arg1, arg2});
JavaMethod method = (JavaMethod)findCallableArityThree(self, name, arg0, arg1, arg2);
Object cArg0 = convertArg(arg0, method, 0);
Object cArg1 = convertArg(arg1, method, 1);
Object cArg2 = convertArg(arg2, method, 2);
return method.invokeDirect(singleton, cArg0, cArg1, cArg2);
}
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
if (block.isGiven()) {
int len = args.length;
// too much array creation!
Object[] convertedArgs = new Object[len + 1];
IRubyObject[] intermediate = new IRubyObject[len + 1];
System.arraycopy(args, 0, intermediate, 0, len);
intermediate[len] = RubyProc.newProc(context.runtime, block, block.type);
JavaMethod method = (JavaMethod)findCallable(self, name, intermediate, len + 1);
for (int i = 0; i < len + 1; i++) {
convertedArgs[i] = convertArg(intermediate[i], method, i);
}
return method.invokeDirect(singleton, convertedArgs);
} else {
return call(context, self, clazz, name, args);
}
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) {
if (block.isGiven()) {
RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
JavaMethod method = (JavaMethod)findCallableArityOne(self, name, proc);
Object cArg0 = convertArg(proc, method, 0);
return method.invokeDirect(singleton, cArg0);
} else {
return call(context, self, clazz, name);
}
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, Block block) {
if (block.isGiven()) {
RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
JavaMethod method = (JavaMethod)findCallableArityTwo(self, name, arg0, proc);
Object cArg0 = convertArg(arg0, method, 0);
Object cArg1 = convertArg(proc, method, 1);
return method.invokeDirect(singleton, cArg0, cArg1);
} else {
return call(context, self, clazz, name, arg0);
}
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
if (block.isGiven()) {
RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
JavaMethod method = (JavaMethod)findCallableArityThree(self, name, arg0, arg1, proc);
Object cArg0 = convertArg(arg0, method, 0);
Object cArg1 = convertArg(arg1, method, 1);
Object cArg2 = convertArg(proc, method, 2);
return method.invokeDirect(singleton, cArg0, cArg1, cArg2);
} else {
return call(context, self, clazz, name, arg0, arg1);
}
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
if (block.isGiven()) {
RubyProc proc = RubyProc.newProc(context.runtime, block, block.type);
JavaMethod method = (JavaMethod)findCallableArityFour(self, name, arg0, arg1, arg2, proc);
Object cArg0 = convertArg(arg0, method, 0);
Object cArg1 = convertArg(arg1, method, 1);
Object cArg2 = convertArg(arg2, method, 2);
Object cArg3 = convertArg(proc, method, 3);
return method.invokeDirect(singleton, cArg0, cArg1, cArg2, cArg3);
} else {
return call(context, self, clazz, name, arg0, arg1, arg2);
}
}
}