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
18 changes: 16 additions & 2 deletions core/src/main/java/org/jruby/javasupport/ext/JavaLang.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jruby.runtime.JavaInternalBlockBody;
import org.jruby.runtime.Signature;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.RubyStringBuilder;
Expand Down Expand Up @@ -229,9 +230,8 @@ static RubyModule define(final Ruby runtime, final RubyClass proxy) {
public static IRubyObject backtrace(final ThreadContext context, final IRubyObject self) {
final Ruby runtime = context.runtime;
java.lang.Throwable throwable = unwrapIfJavaObject(self);
// TODO instead this should get aligned with NativeException !?!
StackTraceElement[] stackTrace = throwable.getStackTrace();
if ( stackTrace == null ) return context.nil; // never actually happens
if ( stackTrace == null ) return context.nil;
final int len = stackTrace.length;
if ( len == 0 ) return RubyArray.newEmptyArray(runtime);
IRubyObject[] backtrace = new IRubyObject[len];
Expand All @@ -241,6 +241,20 @@ public static IRubyObject backtrace(final ThreadContext context, final IRubyObje
return RubyArray.newArrayMayCopy(runtime, backtrace);
}

@JRubyMethod
public static IRubyObject backtrace_locations(final ThreadContext context, final IRubyObject self) {
java.lang.Throwable throwable = unwrapIfJavaObject(self);
StackTraceElement[] stackTrace = throwable.getStackTrace();
if ( stackTrace == null ) return context.nil;
final int len = stackTrace.length;
if ( len == 0 ) return RubyArray.newEmptyArray(context.runtime);
RubyStackTraceElement[] rubyStackTrace = new RubyStackTraceElement[len];
for ( int i = 0; i < len; i++ ) {
rubyStackTrace[i] = new RubyStackTraceElement(stackTrace[i]);
}
return RubyThread.Location.newLocationArray(context.runtime, rubyStackTrace);
}

@JRubyMethod // can not set backtrace for a java.lang.Throwable
public static IRubyObject set_backtrace(final IRubyObject self, final IRubyObject backtrace) {
return self.getRuntime().getNil();
Expand Down
16 changes: 16 additions & 0 deletions test/jruby/test_backtraces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def test_java_backtrace
assert_equal 1, ruby_trace.length # only once!
end

def test_java_backtrace_locations
org.jruby.test.TestHelper.throwTestHelperException
raise 'did no raise exception'
rescue java.lang.Exception => e
locations = e.backtrace_locations
assert_kind_of Array, locations
assert_kind_of Thread::Backtrace::Location, locations[0]

location = locations[0]
assert_equal 'org/jruby/test/TestHelper.java', location.path

assert_operator location.lineno, :>, 1
assert_equal 'throwTestHelperException', location.label
assert_match /TestHelper\.java:\d+:in .throwTestHelperException/, location.to_s
end

def test_simple_exception_recursive
@offset = __LINE__
def meth(n)
Expand Down
Loading