Repository navigation
Expand file tree
/
Copy pathJavaInterfaceTemplate.java
More file actions
384 lines (323 loc) · 18.9 KB
/
Copy pathJavaInterfaceTemplate.java
File metadata and controls
384 lines (323 loc) · 18.9 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package org.jruby.java.proxies;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodN;
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodOne;
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodOneBlock;
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodZero;
import org.jruby.internal.runtime.methods.UndefinedMethod;
import org.jruby.javasupport.Java;
import org.jruby.javasupport.JavaClass;
import org.jruby.javasupport.JavaObject;
import org.jruby.javasupport.JavaUtilities;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.runtime.Arity;
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 JavaInterfaceTemplate {
public static RubyModule createJavaInterfaceTemplateModule(ThreadContext context) {
Ruby runtime = context.runtime;
RubyModule javaInterfaceTemplate = runtime.defineModule("JavaInterfaceTemplate");
RubyClass singleton = javaInterfaceTemplate.getSingletonClass();
singleton.addReadAttribute(context, "java_class");
singleton.defineAnnotatedMethods(JavaInterfaceTemplate.class);
return javaInterfaceTemplate;
}
// not intended to be called directly by users (private)
// OLD TODO from Ruby code:
// This should be implemented in JavaClass.java, where we can
// check for reserved Ruby names, conflicting methods, etc.
@JRubyMethod(visibility = Visibility.PRIVATE)
public static IRubyObject implement(ThreadContext context, IRubyObject self, IRubyObject clazz) {
Ruby runtime = context.runtime;
if (!(clazz instanceof RubyModule)) {
throw runtime.newTypeError(clazz, runtime.getModule());
}
RubyModule targetModule = (RubyModule)clazz;
JavaClass javaClass = (JavaClass)self.getInstanceVariables().getInstanceVariable("@java_class");
Method[] javaInstanceMethods = javaClass.javaClass().getMethods();
DynamicMethod dummyMethod = new org.jruby.internal.runtime.methods.JavaMethod(targetModule, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
// dummy bodies for default impls
return context.runtime.getNil();
}
};
for (int i = 0; i < javaInstanceMethods.length; i++) {
Method method = javaInstanceMethods[i];
String name = method.getName();
if (targetModule.searchMethod(name) != UndefinedMethod.INSTANCE) continue;
targetModule.addMethod(name, dummyMethod);
}
return runtime.getNil();
}
// framed for invokeSuper
@JRubyMethod(frame = true)
public static IRubyObject append_features(ThreadContext context, IRubyObject self, IRubyObject clazz, Block block) {
if (clazz instanceof RubyClass) {
appendFeaturesToClass(context, self, (RubyClass)clazz);
} else if (clazz instanceof RubyModule) {
appendFeaturesToModule(context, self, (RubyModule)clazz);
} else {
throw context.runtime.newTypeError("received " + clazz + ", expected Class/Module");
}
return RuntimeHelpers.invokeSuper(context, self, clazz, block);
}
private static void appendFeaturesToClass(ThreadContext context, IRubyObject self, final RubyClass clazz) {
Ruby runtime = context.runtime;
checkAlreadyReified(clazz, runtime);
IRubyObject javaClassObj = RuntimeHelpers.getInstanceVariable(self, runtime, "@java_class");
IRubyObject javaInterfaces;
if (!clazz.hasInstanceVariable("@java_interfaces")) {
javaInterfaces = RubyArray.newArray(runtime, javaClassObj);
RuntimeHelpers.setInstanceVariable(javaInterfaces, clazz, "@java_interfaces");
initInterfaceImplMethods(context, clazz);
} else {
javaInterfaces = RuntimeHelpers.getInstanceVariable(clazz, runtime, "@java_interfaces");
// we've already done the above priming logic, just add another interface
// to the list of intentions unless we're past the point of no return or
// already intend to implement the given interface
if (!(javaInterfaces.isFrozen() || ((RubyArray)javaInterfaces).includes(context, javaClassObj))) {
((RubyArray)javaInterfaces).append(javaClassObj);
}
}
}
private static void checkAlreadyReified(final RubyClass clazz, Ruby runtime) throws RaiseException {
// not allowed for original (non-generated) Java classes
// note: not allowing for any previously created class right now;
// this restriction might be loosened later for generated classes
if ((Java.NEW_STYLE_EXTENSION && clazz.getReifiedClass() != null)
||
(clazz.hasInstanceVariable("@java_class")
&& clazz.getInstanceVariable("@java_class").isTrue()
&& !clazz.getSingletonClass().isMethodBound("java_proxy_class", false))
||
(clazz.hasInstanceVariable("@java_proxy_class")
&& clazz.getInstanceVariable("@java_proxy_class").isTrue())) {
throw runtime.newArgumentError("can not add Java interface to existing Java class");
}
}
private static void initInterfaceImplMethods(ThreadContext context, RubyClass clazz) {
// setup new, etc unless this is a ConcreteJavaProxy subclass
// For JRUBY-4571, check both these, since JavaProxy extension stuff adds the former and this code adds the latter
if (!(clazz.isMethodBound("__jcreate!", false) || clazz.isMethodBound("__jcreate_meta!", false))) {
// First we make modifications to the class, to adapt it to being
// both a Ruby class and a proxy for a Java type
RubyClass singleton = clazz.getSingletonClass();
// list of interfaces we implement
singleton.addReadAttribute(context, "java_interfaces");
if (
(!Java.NEW_STYLE_EXTENSION && clazz.getSuperClass().getRealClass().hasInstanceVariable("@java_class"))
|| RubyInstanceConfig.INTERFACES_USE_PROXY) {
// superclass is a Java class...use old style impl for now
// The replacement "new" allocates and inits the Ruby object as before, but
// also instantiates our proxified Java object by calling __jcreate!
final ObjectAllocator proxyAllocator = clazz.getAllocator();
clazz.setAllocator(new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
IRubyObject newObj = proxyAllocator.allocate(runtime, klazz);
RuntimeHelpers.invoke(runtime.getCurrentContext(), newObj, "__jcreate!");
return newObj;
}
});
// jcreate instantiates the proxy object which implements all interfaces
// and which is wrapped and implemented by this object
clazz.addMethod("__jcreate!", new JavaMethodN(clazz, Visibility.PRIVATE) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
return jcreateProxy(self);
}
});
} else {
// The new "new" actually generates a real Java class to use for the Ruby class's
// backing store, instantiates that, and then calls initialize on it.
addRealImplClassNew(clazz);
}
// Next, we define a few private methods that we'll use to manipulate
// the Java object contained within this Ruby object
// Used by our duck-typification of Proc into interface types, to allow
// coercing a simple proc into an interface parameter.
clazz.addMethod("__jcreate_meta!", new JavaMethodN(clazz, Visibility.PRIVATE) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
IRubyObject result = jcreateProxy(self);
return result;
}
});
// If we hold a Java object, we need a java_class accessor
clazz.addMethod("java_class", new JavaMethodZero(clazz, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
return ((JavaObject) self.dataGetStruct()).java_class();
}
});
// Because we implement Java interfaces now, we need a new === that's
// aware of those additional "virtual" supertypes
if (!clazz.searchMethod("===").isUndefined()) {
clazz.defineAlias("old_eqq", "===");
clazz.addMethod("===", new JavaMethodOne(clazz, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
// TODO: WRONG - get interfaces from class
if (arg.respondsTo("java_object")) {
IRubyObject interfaces = self.getMetaClass().getInstanceVariables().getInstanceVariable("@java_interfaces");
assert interfaces instanceof RubyArray : "interface list was not an array";
return context.runtime.newBoolean(((RubyArray) interfaces).op_diff(
((JavaClass) ((JavaObject) arg.dataGetStruct()).java_class()).interfaces()).equals(RubyArray.newArray(context.runtime)));
} else {
return RuntimeHelpers.invoke(context, self, "old_eqq", arg);
}
}
});
}
}
// Now we add an "implement" and "implement_all" methods to the class
if (!clazz.isMethodBound("implement", false)) {
RubyClass singleton = clazz.getSingletonClass();
// implement is called to force this class to create stubs for all
// methods in the given interface, so they'll show up in the list
// of methods and be invocable without passing through method_missing
singleton.addMethod("implement", new JavaMethodOne(clazz, Visibility.PRIVATE) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
IRubyObject javaInterfaces = self.getInstanceVariables().getInstanceVariable("@java_interfaces");
if (javaInterfaces != null && ((RubyArray) javaInterfaces).includes(context, arg)) {
return RuntimeHelpers.invoke(context, arg, "implement", self);
}
return context.runtime.getNil();
}
});
// implement all forces implementation of all interfaces we intend
// for this class to implement
singleton.addMethod("implement_all", new JavaMethodOne(clazz, Visibility.PRIVATE) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg) {
RubyArray javaInterfaces = (RubyArray) self.getInstanceVariables().getInstanceVariable("@java_interfaces");
for (int i = 0; i < javaInterfaces.size(); i++) {
RuntimeHelpers.invoke(context, JavaUtilities.get_interface_module(self, javaInterfaces.eltInternal(i)), "implement", self);
}
return javaInterfaces;
}
});
}
}
public static void addRealImplClassNew(RubyClass clazz) {
clazz.setAllocator(new ObjectAllocator() {
private Constructor proxyConstructor;
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
// if we haven't been here before, reify the class
Class reifiedClass = klazz.getReifiedClass();
if (proxyConstructor == null || proxyConstructor.getDeclaringClass() != reifiedClass) {
if (reifiedClass == null) {
reifiedClass = Java.generateRealClass(klazz);
}
proxyConstructor = Java.getRealClassConstructor(runtime, reifiedClass);
}
IRubyObject newObj = Java.constructProxy(runtime, proxyConstructor, klazz);
return newObj;
}
});
}
private static IRubyObject jcreateProxy(IRubyObject self) {
RubyClass current = self.getMetaClass();
// construct the new interface impl and set it into the object
IRubyObject newObject = Java.newInterfaceImpl(self, Java.getInterfacesFromRubyClass(current));
return JavaUtilities.set_java_object(self, self, newObject);
}
private static void appendFeaturesToModule(ThreadContext context, IRubyObject self, RubyModule module) {
// assuming the user wants a collection of interfaces that can be
// included together. make it so.
Ruby runtime = context.runtime;
// not allowed for existing Java interface modules
if (module.getInstanceVariables().hasInstanceVariable("@java_class") &&
module.getInstanceVariables().getInstanceVariable("@java_class").isTrue()) {
throw runtime.newTypeError("can not add Java interface to existing Java interface");
}
// To turn a module into an "interface collection" we add a class instance
// variable to hold the list of interfaces, and modify append_features
// for this module to call append_features on each of those interfaces as
// well
synchronized (module) {
if (!module.getInstanceVariables().hasInstanceVariable("@java_interface_mods")) {
RubyArray javaInterfaceMods = RubyArray.newArray(runtime, self);
module.getInstanceVariables().setInstanceVariable("@java_interface_mods", javaInterfaceMods);
RubyClass singleton = module.getSingletonClass();
singleton.addMethod("append_features", new JavaMethodOneBlock(singleton, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg, Block block) {
if (!(arg instanceof RubyClass)) {
throw context.runtime.newTypeError("append_features called with non-class");
}
RubyClass target = (RubyClass)arg;
RubyArray javaInterfaceMods = (RubyArray)self.getInstanceVariables().getInstanceVariable("@java_interface_mods");
target.include(javaInterfaceMods.toJavaArray());
return RuntimeHelpers.invokeAs(context, clazz.getSuperClass(), self, name, arg, block);
}
});
} else {
// already set up append_features, just add the interface if we haven't already
RubyArray javaInterfaceMods =(RubyArray)module.getInstanceVariables().getInstanceVariable("@java_interface_mods");
if (!javaInterfaceMods.includes(context, self)) {
javaInterfaceMods.append(self);
}
}
}
}
@JRubyMethod
public static IRubyObject extended(ThreadContext context, IRubyObject self, IRubyObject object) {
if (!(self instanceof RubyModule)) {
throw context.runtime.newTypeError(self, context.runtime.getModule());
}
RubyClass singleton = object.getSingletonClass();
singleton.include(new IRubyObject[] {self});
return singleton;
}
@JRubyMethod(name = "[]", rest = true)
public static IRubyObject op_aref(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return JavaProxy.op_aref(context, self, args);
}
@JRubyMethod(rest = true)
public static IRubyObject impl(ThreadContext context, IRubyObject self, IRubyObject[] args, final Block implBlock) {
Ruby runtime = context.runtime;
if (!implBlock.isGiven()) throw runtime.newArgumentError("block required to call #impl on a Java interface");
final RubyArray methodNames = (args.length > 0) ? runtime.newArray(args) : null;
RubyClass implClass = RubyClass.newClass(runtime, runtime.getObject());
implClass.include(new IRubyObject[] {self});
IRubyObject implObject = implClass.callMethod(context, "new");
implClass.addMethod("method_missing",
new org.jruby.internal.runtime.methods.JavaMethod(implClass, Visibility.PUBLIC) {
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
Arity.checkArgumentCount(context.runtime, name, args.length, 1, -1);
if (methodNames == null || methodNames.include_p(context, args[0]).isTrue()) {
return implBlock.call(context, args);
} else {
return clazz.getSuperClass().callMethod(context, "method_missing", args, block);
}
}
});
return implObject;
}
@JRubyMethod(name = "new", rest = true)
public static IRubyObject rbNew(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
RubyClass implClass = (RubyClass)self.getInstanceVariables().getInstanceVariable("@__implementation");
if (implClass == null) {
implClass = RubyClass.newClass(runtime, (RubyClass)runtime.getClass("InterfaceJavaProxy"));
implClass.include(new IRubyObject[] {self});
RuntimeHelpers.setInstanceVariable(implClass, self, "@__implementation");
}
return RuntimeHelpers.invoke(context, implClass, "new", args, block);
}
}