Repository navigation
Expand file tree
/
Copy pathKernelJavaAddons.java
More file actions
130 lines (112 loc) · 5 KB
/
Copy pathKernelJavaAddons.java
File metadata and controls
130 lines (112 loc) · 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
package org.jruby.java.addons;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.javasupport.*;
import org.jruby.RubyKernel;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.anno.JRubyMethod;
import org.jruby.java.proxies.ConcreteJavaProxy;
import org.jruby.java.proxies.JavaProxy;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.unsafe.UnsafeFactory;
public class KernelJavaAddons {
@JRubyMethod(name = "raise", optional = 3, frame = true, module = true, visibility = Visibility.PRIVATE, omit = true)
public static IRubyObject rbRaise(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
// Check for a Java exception
ConcreteJavaProxy exception = null;
if (args.length == 0 && runtime.getGlobalVariables().get("$!") instanceof ConcreteJavaProxy) {
exception = (ConcreteJavaProxy)runtime.getGlobalVariables().get("$!");
} else if (args.length == 1 && args[0] instanceof ConcreteJavaProxy) {
exception = (ConcreteJavaProxy)args[0];
}
if (exception != null) {
// looks like someone's trying to raise a Java exception. Let them.
Object maybeThrowable = exception.getObject();
if (maybeThrowable instanceof Throwable) {
// yes, we're cheating here.
UnsafeFactory.getUnsafe().throwException((Throwable)maybeThrowable);
return recv; // not reached
} else {
throw runtime.newTypeError("can't raise a non-Throwable Java object");
}
} else {
return RubyKernel.raise(context, recv, args, block);
}
}
@JRubyMethod
public static IRubyObject to_java(ThreadContext context, IRubyObject fromObject) {
if (fromObject instanceof RubyArray) {
return context.runtime.getJavaSupport().getObjectJavaClass().javaArrayFromRubyArray(context, fromObject);
} else {
return Java.getInstance(context.runtime, fromObject.toJava(Object.class));
}
}
@JRubyMethod
public static IRubyObject to_java(ThreadContext context, IRubyObject fromObject, IRubyObject type) {
if (type.isNil()) {
return to_java(context, fromObject);
}
Ruby runtime = context.runtime;
JavaClass targetType = getTargetType(context, runtime, type);
if (fromObject instanceof RubyArray) {
return targetType.javaArrayFromRubyArray(context, fromObject);
} else {
return Java.getInstance(runtime, fromObject.toJava(targetType.javaClass()));
}
}
@JRubyMethod(rest = true)
public static IRubyObject java_signature(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
@JRubyMethod(rest = true)
public static IRubyObject java_name(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
@JRubyMethod(rest = true)
public static IRubyObject java_implements(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
@JRubyMethod(rest = true)
public static IRubyObject java_annotation(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
@JRubyMethod(rest = true)
public static IRubyObject java_require(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
@JRubyMethod(rest = true)
public static IRubyObject java_package(IRubyObject recv, IRubyObject[] args) {
// empty stub for now
return recv.getRuntime().getNil();
}
private static JavaClass getTargetType(ThreadContext context, Ruby runtime, IRubyObject type) {
JavaClass targetType;
if (type instanceof RubyString || type instanceof RubySymbol) {
targetType = runtime.getJavaSupport().getNameClassMap().get(type.asJavaString());
if (targetType == null) targetType = JavaClass.forNameVerbose(runtime, type.asJavaString());
} else if (type instanceof RubyModule && type.respondsTo("java_class")) {
targetType = (JavaClass)RuntimeHelpers.invoke(context, type, "java_class");
} else if (type instanceof JavaProxy) {
if (((JavaProxy)type).getObject() instanceof Class) {
targetType = JavaClass.get(runtime, (Class)((JavaProxy)type).getObject());
} else {
throw runtime.newTypeError("not a valid target type: " + type);
}
} else {
throw runtime.newTypeError("unable to convert to type: " + type);
}
return targetType;
}
}