Skip to content

Commit

Permalink
avoid the deprecated unwrapJavaValue - check return null for raisin…
Browse files Browse the repository at this point in the history
…g a TypeError
  • Loading branch information
kares committed Mar 25, 2015
1 parent b390857 commit 7bf94dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
27 changes: 22 additions & 5 deletions core/src/main/java/org/jruby/javasupport/JavaField.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -112,12 +113,16 @@ public IRubyObject field_type() {

@JRubyMethod
public IRubyObject value(ThreadContext context, IRubyObject object) {
Ruby runtime = context.runtime;
final Ruby runtime = context.runtime;

Object javaObject = null;
final Object javaObject;
if ( ! Modifier.isStatic( field.getModifiers() ) ) {
javaObject = JavaUtil.unwrapJavaValue(runtime, object, "not a java object");
javaObject = unwrapJavaObject(object);
}
else {
javaObject = null;
}

try {
return convertToRuby(runtime, field.get(javaObject));
}
Expand All @@ -128,10 +133,14 @@ public IRubyObject value(ThreadContext context, IRubyObject object) {

@JRubyMethod
public IRubyObject set_value(IRubyObject object, IRubyObject value) {
Object javaObject = null;
final Object javaObject;
if ( ! Modifier.isStatic( field.getModifiers() ) ) {
javaObject = JavaUtil.unwrapJavaValue(getRuntime(), object, "not a java object: " + object);
javaObject = unwrapJavaObject(object);
}
else {
javaObject = null;
}

final Object javaValue = convertValueToJava(value);
try {
field.set(javaObject, javaValue);
Expand Down Expand Up @@ -191,6 +200,14 @@ public AccessibleObject accessibleObject() {
return field;
}

private Object unwrapJavaObject(final IRubyObject object) throws RaiseException {
Object javaObject = JavaUtil.unwrapJavaValue(object);
if ( javaObject == null ) {
throw getRuntime().newTypeError("not a java object: " + object);
}
return javaObject;
}

private Object convertValueToJava(IRubyObject value) {
Object val = value.dataGetStruct();
if ( val instanceof JavaObject ) value = (IRubyObject) val;
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/org/jruby/javasupport/JavaMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,12 @@ public IRubyObject invoke(IRubyObject[] args) {
return invokeWithExceptionHandling(method, null, arguments);
}

Object javaInvokee = null;

final Object javaInvokee;
if (!isStatic()) {
javaInvokee = JavaUtil.unwrapJavaValue(getRuntime(), invokee, "invokee not a java object");
javaInvokee = JavaUtil.unwrapJavaValue(invokee);
if ( javaInvokee == null ) {
throw getRuntime().newTypeError("invokee not a java object");
}

if ( ! method.getDeclaringClass().isInstance(javaInvokee) ) {
throw getRuntime().newTypeError(
Expand All @@ -246,6 +248,10 @@ public IRubyObject invoke(IRubyObject[] args) {
}
}
}
else {
javaInvokee = null;
}

return invokeWithExceptionHandling(method, javaInvokee, arguments);
}

Expand Down

0 comments on commit 7bf94dd

Please sign in to comment.