Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -874,21 +874,20 @@ final <T> T defaultToJava(Class<T> target) {
// for callers that unconditionally pass null retval type (JRUBY-4737)
if (target == void.class) return null;

final Object innerWrapper = dataGetStruct();
if (innerWrapper instanceof JavaObject) {
// for interface impls

final Object value = ((JavaObject) innerWrapper).getValue();
final Object value = unwrap_java_object();
if (value != null) {
// ensure the object is associated with the wrapper we found it in,
// so that if it comes back we don't re-wrap it
if (target.isAssignableFrom(value.getClass())) {
getRuntime().getJavaSupport().getObjectProxyCache().put(value, this);

return (T) value;
}
}
else if (JavaUtil.isDuckTypeConvertable(getClass(), target)) {
if (!respondsTo("java_object")) {
synchronized (this) {
if (unwrap_java_object() != null) { // double check under lock
return defaultToJava(target); // concurrent proxy interface impl initialization
}
return JavaUtil.convertProcToInterface(getRuntime().getCurrentContext(), this, target);
}
}
Expand All @@ -899,6 +898,14 @@ else if (target.isAssignableFrom(getClass())) {
throw getRuntime().newTypeError("cannot convert instance of " + getClass() + " to " + target);
}

private Object unwrap_java_object() {
final Object innerWrapper = dataGetStruct(); // java_object
if (innerWrapper instanceof JavaObject) { // for interface impls
return ((JavaObject) innerWrapper).getValue(); // never null
}
return null;
}

@Override
public IRubyObject dup() {
if (isSpecialObject()) {
Expand Down
16 changes: 16 additions & 0 deletions test/jruby/test_higher_javasupport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,22 @@ def test_java_constructor_with_prefered_match
assert_equal 255, color.getRed # assert we called (float,float,float)
end

class Runner
def run; end
end

def test_concurrent_interface_proxy_generation
100.times do |_|
runner = Runner.new

assert_nothing_raised do
3.times.map do
Thread.start { assert runner.to_java(java.lang.Runnable) }
end.each(&:join)
end
end
end

# original report: https://jira.codehaus.org/browse/JRUBY-5582
# NOTE: we're not testing this "early" on still a good JI exercise
def test_set_security_manager
Expand Down