Skip to content

Commit 9858905

Browse files
authored
Merge pull request #7557 from kares/ji-thread-to-java++
[ji] revert Thread#to_java behavior to stay compatible, for now
2 parents 637fe72 + a4eee01 commit 9858905

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

core/src/main/java/org/jruby/RubyThread.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,13 @@ private String identityString() {
24712471
*/
24722472
@Override
24732473
public <T> T toJava(final Class<T> target) {
2474-
// since 9.4 the default toJava(java.lang.Object.class) is formalized to return a Java thread
2474+
// since 9.4 due compatibility with JRuby <= 9.3, hopefully to be removed in 9.5
2475+
if (target == Object.class) {
2476+
// NOTE: a deprecation wasn't introduced in 9.4 simply due the need to cleanup libraries first and their
2477+
// Thread.current.to_java.getNativeThread/getContext usage, in 9.5 a deprecation should be added here
2478+
return super.toJava(target); // simply returns the (internal) org.jruby.RubyThread
2479+
}
2480+
24752481
if (target.isAssignableFrom(Thread.class)) { // Thread | Runnable | Object
24762482
return target.cast(getNativeThread());
24772483
}

spec/java_integration/extensions/thread_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
context 'RubyThread' do
66

7-
it 'is (default) convertible to a java thread' do
8-
expect( Thread.current.to_java ).to be_a java.lang.Thread
9-
expect( Thread.current.to_java ).to be java.lang.Thread.currentThread
7+
it 'maintains compatibility with <= 9.3 when doing a default to_java conversion (till 9.6)' do
8+
if JRUBY_VERSION < '9.6'
9+
expect( Thread.current.to_java ).to be_a org.jruby.RubyThread
10+
else
11+
# NOTE: a naive attempt to get this looked into in 9.6 and channge the Thread#to_java default
12+
expect( Thread.current.to_java ).to be_a java.lang.Thread
13+
expect( Thread.current.to_java(java.lang.Object) ).to be java.lang.Thread.currentThread
14+
end
1015
end
1116

1217
it 'is explicitly convertible to a java thread' do
1318
thread = Thread.start { sleep 1.0 }
1419
expect( thread.to_java(java.lang.Thread) ).to be_a java.lang.Thread
15-
expect( thread.to_java('java.lang.Object') ).to be_a java.lang.Thread
20+
expect( thread.to_java('java.lang.Runnable') ).to be_a java.lang.Thread
1621
end
1722

1823
it 'can be converted to internal JRuby class' do

test/jruby/test_thread.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ def test_inspect_and_to_s
365365
end
366366

367367
def test_thread_name
368-
pend 'java.lang.Thread.native_thread is not implemented'
369368
Thread.new do
370369
assert_match(/\#\<Thread\:0x\h+( ([A-Z]:)?[\w\/\.\-_]+\:\d+)?\srun\>/, Thread.current.inspect)
371370
end.join
@@ -384,15 +383,16 @@ def test_thread_name
384383

385384
Thread.new do
386385
my_name = 'user-set-native-thread-name'
387-
Thread.current.to_java.native_thread.name = my_name
386+
java_thread = java.lang.Thread.currentThread
387+
java_thread.setName(my_name)
388388
Thread.current.name = 'foo'
389389

390390
assert Thread.current.inspect.index('@foo')
391-
assert_equal my_name, Thread.current.to_java.native_thread.name
391+
assert_equal my_name, java_thread.getName
392392

393393
Thread.current.name = nil
394394
assert ! Thread.current.inspect.index('@foo')
395-
assert_equal my_name, Thread.current.to_java.native_thread.name
395+
assert_equal my_name, java.lang.Thread.currentThread.getName
396396
end.join
397397
end
398398

0 commit comments

Comments
 (0)