Repository navigation
Expand file tree
/
Copy pathConcreteJavaProxy.java
More file actions
215 lines (190 loc) · 10.4 KB
/
Copy pathConcreteJavaProxy.java
File metadata and controls
215 lines (190 loc) · 10.4 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
package org.jruby.java.proxies;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.javasupport.Java;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallSite;
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
public class ConcreteJavaProxy extends JavaProxy {
public ConcreteJavaProxy(Ruby runtime, RubyClass klazz) {
super(runtime, klazz);
}
public ConcreteJavaProxy(Ruby runtime, RubyClass klazz, Object object) {
super(runtime, klazz, object);
}
public static RubyClass createConcreteJavaProxy(ThreadContext context) {
Ruby runtime = context.runtime;
RubyClass concreteJavaProxy = runtime.defineClass("ConcreteJavaProxy",
runtime.getJavaSupport().getJavaProxyClass(),
new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new ConcreteJavaProxy(runtime, klazz);
}
});
initialize(concreteJavaProxy);
return concreteJavaProxy;
}
protected static void initialize(RubyClass concreteJavaProxy) {
concreteJavaProxy.addMethod("initialize", new org.jruby.internal.runtime.methods.JavaMethod(concreteJavaProxy, Visibility.PUBLIC) {
private final CallSite jcreateSite = MethodIndex.getFunctionalCallSite("__jcreate!");
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return jcreateSite.call(context, self, self, args, block);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) {
return jcreateSite.call(context, self, self, block);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, Block block) {
return jcreateSite.call(context, self, self, arg0, block);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
return jcreateSite.call(context, self, self, arg0, arg1, block);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
return jcreateSite.call(context, self, self, arg0, arg1, arg2, block);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
return jcreateSite.call(context, self, self, args);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
return jcreateSite.call(context, self, self);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) {
return jcreateSite.call(context, self, self, arg0);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1) {
return jcreateSite.call(context, self, self, arg0, arg1);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
return jcreateSite.call(context, self, self, arg0, arg1, arg2);
}
});
// We define a custom "new" method to ensure that __jcreate! is getting called,
// so that if the user doesn't call super in their subclasses, the object will
// still get set up properly. See JRUBY-4704.
RubyClass singleton = concreteJavaProxy.getSingletonClass();
final DynamicMethod oldNew = singleton.searchMethod("new");
singleton.addMethod("new", new org.jruby.internal.runtime.methods.JavaMethod(singleton, Visibility.PUBLIC) {
private final CallSite jcreateSite = MethodIndex.getFunctionalCallSite("__jcreate!");
private boolean needsCreate(IRubyObject recv) {
return ((JavaProxy)recv).object == null;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", args, block);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, args, block);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, Block block) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", block);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, block);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, Block block) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0, block);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0, block);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0, arg1, block);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0, arg1, block);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0, arg1, arg2, block);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0, arg1, arg2, block);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", args);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, args);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy");
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0, arg1);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0, arg1);
return proxy;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
IRubyObject proxy = oldNew.call(context, self, clazz, "new_proxy", arg0, arg1, arg2);
if (needsCreate(proxy)) jcreateSite.call(context, proxy, proxy, arg0, arg1, arg2);
return proxy;
}
});
}
// This alternate ivar logic is disabled because it can cause self-referencing
// chains to keep the original object alive. See JRUBY-4832.
// @Override
// public Object getVariable(int index) {
// return getRuntime().getJavaSupport().getJavaObjectVariable(this, index);
// }
//
// @Override
// public void setVariable(int index, Object value) {
// getRuntime().getJavaSupport().setJavaObjectVariable(this, index, value);
// }
/**
* Because we can't physically associate an ID with a Java object, we can
* only use the identity hashcode here.
*
* @return The identity hashcode for the Java object.
*/
public IRubyObject id() {
return getRuntime().newFixnum(System.identityHashCode(getObject()));
}
@Override
public Object toJava(Class type) {
Object obj = getObject();
Class cls = obj.getClass();
if (type.isPrimitive()) {
if (type == Void.TYPE) return null;
if (obj instanceof Number && type != Boolean.TYPE ||
obj instanceof Character && type == Character.TYPE ||
obj instanceof Boolean && type == Boolean.TYPE) {
// FIXME in more permissive call paths, like invokedynamic, this can allow
// precision-loading downcasts to happen silently
return obj;
}
} else if (type.isAssignableFrom(cls)) {
if (Java.OBJECT_PROXY_CACHE || metaClass.getCacheProxy()) {
getRuntime().getJavaSupport().getObjectProxyCache().put(obj, this);
}
return obj;
}
throw getRuntime().newTypeError("failed to coerce " + cls.getName() + " to " + type.getName());
}
}