Skip to content

Commit ff9400b

Browse files
committed
[ji] pass around context + handle JavaClass being a proxy
not relying on getRuntime() will allow unplugging these classes
1 parent 76e4df6 commit ff9400b

File tree

4 files changed

+165
-156
lines changed

4 files changed

+165
-156
lines changed

core/src/main/java/org/jruby/javasupport/JavaAccessibleObject.java

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.jruby.RubyObject;
4040
import org.jruby.RubyString;
4141
import org.jruby.anno.JRubyMethod;
42+
import org.jruby.runtime.ThreadContext;
4243
import org.jruby.runtime.builtin.IRubyObject;
4344

4445
public abstract class JavaAccessibleObject extends RubyObject {
@@ -69,25 +70,25 @@ public int hashCode() {
6970
}
7071

7172
@JRubyMethod
72-
public RubyFixnum hash() {
73-
return getRuntime().newFixnum(hashCode());
73+
public RubyFixnum hash(ThreadContext context) {
74+
return context.runtime.newFixnum(hashCode());
7475
}
7576

7677
@JRubyMethod(name = {"==", "eql?"})
77-
public RubyBoolean op_equal(final IRubyObject other) {
78-
return RubyBoolean.newBoolean(getRuntime(), equals(other));
78+
public RubyBoolean op_equal(ThreadContext context, final IRubyObject other) {
79+
return RubyBoolean.newBoolean(context.runtime, equals(other));
7980
}
8081

8182
@JRubyMethod(name = "equal?")
82-
public RubyBoolean same(final IRubyObject other) {
83+
public RubyBoolean same(ThreadContext context, final IRubyObject other) {
8384
final boolean same = other instanceof JavaAccessibleObject && same((JavaAccessibleObject) other);
84-
return same ? getRuntime().getTrue() : getRuntime().getFalse();
85+
return same ? context.runtime.getTrue() : context.runtime.getFalse();
8586
}
8687

8788
@JRubyMethod(name = "accessible?")
8889
@Deprecated
89-
public RubyBoolean isAccessible() {
90-
return RubyBoolean.newBoolean(getRuntime(), accessibleObject().isAccessible());
90+
public RubyBoolean isAccessible(ThreadContext context) {
91+
return RubyBoolean.newBoolean(context.runtime, accessibleObject().isAccessible());
9192
}
9293

9394
@JRubyMethod(name = "accessible=")
@@ -98,72 +99,80 @@ public IRubyObject setAccessible(IRubyObject object) {
9899

99100
@SuppressWarnings("unchecked")
100101
@JRubyMethod
101-
public IRubyObject annotation(final IRubyObject annoClass) {
102-
if ( ! ( annoClass instanceof JavaClass ) ) {
103-
throw getRuntime().newTypeError(annoClass, getRuntime().getJavaSupport().getJavaClassClass());
102+
public IRubyObject annotation(ThreadContext context, final IRubyObject annoClass) {
103+
final Class annotation;
104+
if (annoClass instanceof RubyClass && ((RubyClass) annoClass).getJavaProxy()) {
105+
annotation = ((RubyClass) annoClass).getJavaClass();
106+
} else if (annoClass instanceof JavaClass) {
107+
annotation = ((JavaClass) annoClass).javaClass();
108+
} else {
109+
throw context.runtime.newTypeError("expected a Java (proxy) class, got: " + annoClass);
104110
}
105-
final Class annotation = ((JavaClass) annoClass).javaClass();
106-
return Java.getInstance(getRuntime(), accessibleObject().getAnnotation(annotation));
111+
return Java.getInstance(context.runtime, accessibleObject().getAnnotation(annotation));
107112
}
108113

109114
@JRubyMethod
110-
public IRubyObject annotations() {
111-
return Java.getInstance(getRuntime(), accessibleObject().getAnnotations());
115+
public IRubyObject annotations(ThreadContext context) {
116+
return Java.getInstance(context.runtime, accessibleObject().getAnnotations());
112117
}
113118

114119
@JRubyMethod(name = "annotations?")
115-
public RubyBoolean annotations_p() {
116-
return getRuntime().newBoolean(accessibleObject().getAnnotations().length > 0);
120+
public RubyBoolean annotations_p(ThreadContext context) {
121+
return context.runtime.newBoolean(accessibleObject().getAnnotations().length > 0);
117122
}
118123

119124
@JRubyMethod
120-
public IRubyObject declared_annotations() {
121-
return Java.getInstance(getRuntime(), accessibleObject().getDeclaredAnnotations());
125+
public IRubyObject declared_annotations(ThreadContext context) {
126+
return Java.getInstance(context.runtime, accessibleObject().getDeclaredAnnotations());
122127
}
123128

124129
@JRubyMethod(name = "declared_annotations?")
125-
public RubyBoolean declared_annotations_p() {
126-
return getRuntime().newBoolean(accessibleObject().getDeclaredAnnotations().length > 0);
130+
public RubyBoolean declared_annotations_p(ThreadContext context) {
131+
return context.runtime.newBoolean(accessibleObject().getDeclaredAnnotations().length > 0);
127132
}
128133

129134
@SuppressWarnings("unchecked")
130135
@JRubyMethod(name = "annotation_present?")
131-
public IRubyObject annotation_present_p(final IRubyObject annoClass) {
132-
if ( ! ( annoClass instanceof JavaClass ) ) {
133-
throw getRuntime().newTypeError(annoClass, getRuntime().getJavaSupport().getJavaClassClass());
136+
public IRubyObject annotation_present_p(ThreadContext context, final IRubyObject annoClass) {
137+
final Class annotation;
138+
if (annoClass instanceof RubyClass && ((RubyClass) annoClass).getJavaProxy()) {
139+
annotation = ((RubyClass) annoClass).getJavaClass();
140+
} else if (annoClass instanceof JavaClass) {
141+
annotation = ((JavaClass) annoClass).javaClass();
142+
} else {
143+
throw context.runtime.newTypeError("expected a Java (proxy) class, got: " + annoClass);
134144
}
135-
final Class annotation = ((JavaClass) annoClass).javaClass();
136-
return getRuntime().newBoolean( accessibleObject().isAnnotationPresent(annotation) );
145+
return context.runtime.newBoolean( accessibleObject().isAnnotationPresent(annotation) );
137146
}
138147

139148
// for our purposes, Accessibles are also Members, and vice-versa,
140149
// so we'll include Member methods here.
141150
@JRubyMethod
142151
@SuppressWarnings("deprecation")
143-
public IRubyObject declaring_class() {
152+
public IRubyObject declaring_class(ThreadContext context) {
144153
Class<?> clazz = ((Member) accessibleObject()).getDeclaringClass();
145-
if ( clazz != null ) return JavaClass.get(getRuntime(), clazz);
146-
return getRuntime().getNil();
154+
if ( clazz != null ) return Java.getProxyClass(context.runtime, clazz);
155+
return context.runtime.getNil();
147156
}
148157

149158
@JRubyMethod
150-
public IRubyObject modifiers() {
151-
return getRuntime().newFixnum(((Member) accessibleObject()).getModifiers());
159+
public IRubyObject modifiers(ThreadContext context) {
160+
return context.runtime.newFixnum(((Member) accessibleObject()).getModifiers());
152161
}
153162

154163
@JRubyMethod
155-
public IRubyObject name() {
156-
return getRuntime().newString(((Member) accessibleObject()).getName());
164+
public IRubyObject name(ThreadContext context) {
165+
return context.runtime.newString(((Member) accessibleObject()).getName());
157166
}
158167

159168
@JRubyMethod(name = "synthetic?")
160-
public IRubyObject synthetic_p() {
161-
return getRuntime().newBoolean(((Member) accessibleObject()).isSynthetic());
169+
public IRubyObject synthetic_p(ThreadContext context) {
170+
return context.runtime.newBoolean(((Member) accessibleObject()).isSynthetic());
162171
}
163172

164173
@JRubyMethod(name = {"to_s", "to_string"})
165-
public RubyString to_string() {
166-
return getRuntime().newString( toString() );
174+
public RubyString to_string(ThreadContext context) {
175+
return context.runtime.newString( toString() );
167176
}
168177

169178
@Override

core/src/main/java/org/jruby/javasupport/JavaCallable.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,55 +89,55 @@ public static void registerRubyMethods(Ruby runtime, RubyClass result) {
8989
protected abstract String nameOnInspection();
9090

9191
@JRubyMethod
92-
public final RubyFixnum arity() {
93-
return getRuntime().newFixnum(getArity());
92+
public final RubyFixnum arity(ThreadContext context) {
93+
return context.runtime.newFixnum(getArity());
9494
}
9595

9696
@JRubyMethod(name = { "argument_types", "parameter_types" })
9797
@SuppressWarnings("deprecation")
98-
public final RubyArray parameter_types() {
99-
return toRubyArray(getRuntime(), getParameterTypes());
98+
public final RubyArray parameter_types(ThreadContext context) {
99+
return toRubyArray(context.runtime, getParameterTypes());
100100
}
101101

102102
@JRubyMethod
103103
@SuppressWarnings("deprecation")
104-
public RubyArray exception_types() {
105-
return toRubyArray(getRuntime(), getExceptionTypes());
104+
public RubyArray exception_types(ThreadContext context) {
105+
return toRubyArray(context.runtime, getExceptionTypes());
106106
}
107107

108108
@JRubyMethod
109-
public IRubyObject generic_parameter_types() {
110-
return Java.getInstance(getRuntime(), getGenericParameterTypes());
109+
public IRubyObject generic_parameter_types(ThreadContext context) {
110+
return Java.getInstance(context.runtime, getGenericParameterTypes());
111111
}
112112

113113
@JRubyMethod
114-
public IRubyObject generic_exception_types() {
115-
return Java.getInstance(getRuntime(), getGenericExceptionTypes());
114+
public IRubyObject generic_exception_types(ThreadContext context) {
115+
return Java.getInstance(context.runtime, getGenericExceptionTypes());
116116
}
117117

118118
@JRubyMethod
119-
public IRubyObject parameter_annotations() {
120-
return Java.getInstance(getRuntime(), getParameterAnnotations());
119+
public IRubyObject parameter_annotations(ThreadContext context) {
120+
return Java.getInstance(context.runtime, getParameterAnnotations());
121121
}
122122

123123
@JRubyMethod(name = "varargs?")
124-
public RubyBoolean varargs_p() {
125-
return getRuntime().newBoolean(isVarArgs());
124+
public RubyBoolean varargs_p(ThreadContext context) {
125+
return context.runtime.newBoolean(isVarArgs());
126126
}
127127

128128
@JRubyMethod
129-
public RubyString to_generic_string() {
130-
return getRuntime().newString(toGenericString());
129+
public RubyString to_generic_string(ThreadContext context) {
130+
return context.runtime.newString(toGenericString());
131131
}
132132

133133
@JRubyMethod(name = "public?")
134-
public RubyBoolean public_p() {
135-
return RubyBoolean.newBoolean(getRuntime(), Modifier.isPublic(getModifiers()));
134+
public RubyBoolean public_p(ThreadContext context) {
135+
return RubyBoolean.newBoolean(context.runtime, Modifier.isPublic(getModifiers()));
136136
}
137137

138-
protected final void checkArity(final int length) {
138+
protected final void checkArity(ThreadContext context, final int length) {
139139
if ( length != getArity() ) {
140-
throw getRuntime().newArgumentError(length, getArity());
140+
throw context.runtime.newArgumentError(length, getArity());
141141
}
142142
}
143143

@@ -165,40 +165,40 @@ protected final IRubyObject handleThrowable(ThreadContext context, final Throwab
165165
}
166166

167167
protected final IRubyObject handleInvocationTargetEx(ThreadContext context, InvocationTargetException ex) {
168-
return handleThrowable(context, ex.getTargetException());
168+
return handleThrowable(context, ex.getTargetException()); // NOTE: we no longer unwrap
169169
}
170170

171-
final IRubyObject handleIllegalAccessEx(final IllegalAccessException ex, Member target) throws RaiseException {
172-
throw getRuntime().newTypeError("illegal access on '" + target.getName() + "': " + ex.getMessage());
171+
final IRubyObject handleIllegalAccessEx(ThreadContext context, final IllegalAccessException ex, Member target) throws RaiseException {
172+
throw context.runtime.newTypeError("illegal access on '" + target.getName() + "': " + ex.getMessage());
173173
}
174174

175-
final IRubyObject handleIllegalAccessEx(final IllegalAccessException ex, Constructor target) throws RaiseException {
176-
throw getRuntime().newTypeError("illegal access on constructor for type '" + target.getDeclaringClass().getSimpleName() + "': " + ex.getMessage());
175+
final IRubyObject handleIllegalAccessEx(ThreadContext context, final IllegalAccessException ex, Constructor target) throws RaiseException {
176+
throw context.runtime.newTypeError("illegal access on constructor for type '" + target.getDeclaringClass().getSimpleName() + "': " + ex.getMessage());
177177
}
178178

179-
final IRubyObject handlelIllegalArgumentEx(final IllegalArgumentException ex, Method target, Object... arguments) throws RaiseException {
179+
final IRubyObject handlelIllegalArgumentEx(ThreadContext context, final IllegalArgumentException ex, Method target, Object... arguments) throws RaiseException {
180180
final StringBuilder msg = new StringBuilder(64);
181181
msg.append("for method ").append( target.getDeclaringClass().getSimpleName() )
182182
.append('.').append( target.getName() );
183183
msg.append(" expected "); dumpParameterTypes(msg);
184184
msg.append("; got: "); dumpArgTypes(arguments, msg);
185185
msg.append("; error: ").append( ex.getMessage() );
186-
throw getRuntime().newTypeError( msg.toString() );
186+
throw context.runtime.newTypeError( msg.toString() );
187187
}
188188

189-
final IRubyObject handlelIllegalArgumentEx(final IllegalArgumentException ex, Constructor target, Object... arguments) throws RaiseException {
190-
return handlelIllegalArgumentEx(ex, target, true, arguments);
189+
final IRubyObject handlelIllegalArgumentEx(ThreadContext context, final IllegalArgumentException ex, Constructor target, Object... arguments) throws RaiseException {
190+
return handlelIllegalArgumentEx(context, ex, target, true, arguments);
191191
}
192192

193-
final IRubyObject handlelIllegalArgumentEx(final IllegalArgumentException ex, Constructor target, final boolean targetInfo, Object... arguments) throws RaiseException {
193+
final IRubyObject handlelIllegalArgumentEx(ThreadContext context, final IllegalArgumentException ex, Constructor target, final boolean targetInfo, Object... arguments) throws RaiseException {
194194
final StringBuilder msg = new StringBuilder(64);
195195
if ( targetInfo ) {
196196
msg.append("for constructor of type ").append( target.getDeclaringClass().getSimpleName() );
197197
}
198198
msg.append(" expected "); dumpParameterTypes(msg);
199199
msg.append("; got: "); dumpArgTypes(arguments, msg);
200200
msg.append("; error: ").append( ex.getMessage() );
201-
throw getRuntime().newTypeError( msg.toString() );
201+
throw context.runtime.newTypeError( msg.toString() );
202202
}
203203

204204
private void dumpParameterTypes(final StringBuilder str) {

0 commit comments

Comments
 (0)