Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1b7c3308ba68
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8e631ca1d2f3
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Mar 27, 2018

  1. Revert "Eliminate unnecessary abstract class."

    This reverts commit d344f52.
    headius committed Mar 27, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    d237355 View commit details
  2. Copy the full SHA
    8e631ca View commit details
148 changes: 72 additions & 76 deletions core/src/main/java/org/jruby/RubyException.java
Original file line number Diff line number Diff line change
@@ -37,16 +37,13 @@

package org.jruby;

import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.Exception;
import org.jruby.exceptions.JumpException.FlowControlException;
import org.jruby.exceptions.RaiseException;
import org.jruby.java.proxies.ConcreteJavaProxy;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ObjectMarshal;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.*;
import org.jruby.runtime.backtrace.BacktraceData;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.backtrace.TraceType;
@@ -63,13 +60,74 @@
import static org.jruby.runtime.Visibility.PRIVATE;

/**
* An abstract representation of a Ruby exception. All Ruby exception types descend from here.
*
* Logic here handles aggregating a cause, connecting up a Java throwable, managing the backtrace, and other Ruby
* behavior surrounding exceptions.
* @author jpetersen
*/
@JRubyClass(name="Exception")
public class RubyException extends RubyObject {

public static final int TRACE_HEAD = 8;
public static final int TRACE_TAIL = 4;
public static final int TRACE_MAX = RubyException.TRACE_HEAD + RubyException.TRACE_TAIL + 6;
protected BacktraceData backtraceData;
IRubyObject message;
IRubyObject cause;
private IRubyObject backtrace;
private RaiseException throwable;

protected RubyException(Ruby runtime, RubyClass rubyClass) {
super(runtime, rubyClass);
}

public RubyException(Ruby runtime, RubyClass rubyClass, String message) {
super(runtime, rubyClass);

this.setMessage(message == null ? runtime.getNil() : runtime.newString(message));
this.cause = RubyBasicObject.UNDEF;
}

@JRubyMethod(name = "exception", optional = 1, rest = true, meta = true)
public static IRubyObject exception(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
return ((RubyClass) recv).newInstance(context, args, block);
}

@JRubyMethod(name = "===", meta = true)
public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) {
Ruby runtime = context.runtime;
// special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception
if (other instanceof ConcreteJavaProxy &&
(recv == runtime.getException() || recv == runtime.getStandardError())) {

Object object = ((ConcreteJavaProxy)other).getObject();
if (object instanceof Throwable && !(object instanceof FlowControlException)) {
if (recv == runtime.getException() || object instanceof java.lang.Exception) {
return context.runtime.getTrue();
}
}
}
// fall back on default logic
return ((RubyClass)recv).op_eqq(context, other);
}

protected RaiseException constructThrowable(String message) {
return new Exception(message, this);
}

public static RubyClass createExceptionClass(Ruby runtime) {
RubyClass exceptionClass = runtime.defineClass("Exception", runtime.getObject(), EXCEPTION_ALLOCATOR);
runtime.setException(exceptionClass);

exceptionClass.setClassIndex(ClassIndex.EXCEPTION);
exceptionClass.setReifiedClass(RubyException.class);

exceptionClass.setMarshal(EXCEPTION_MARSHAL);
exceptionClass.defineAnnotatedMethods(RubyException.class);

return exceptionClass;
}

public static ObjectAllocator EXCEPTION_ALLOCATOR = (runtime, klass) -> new RubyException(runtime, klass);

private static final ObjectMarshal EXCEPTION_MARSHAL = new ObjectMarshal() {
@Override
public void marshalTo(Ruby runtime, Object obj, RubyClass type,
@@ -99,31 +157,6 @@ public Object unmarshalFrom(Ruby runtime, RubyClass type,
return exc;
}
};
public static ObjectAllocator EXCEPTION_ALLOCATOR = (runtime, klass) -> new RubyException(runtime, klass);

protected RubyException(Ruby runtime, RubyClass rubyClass) {
this(runtime, rubyClass, null);
}

public RubyException(Ruby runtime, RubyClass rubyClass, String message) {
super(runtime, rubyClass);

this.setMessage(message == null ? runtime.getNil() : runtime.newString(message));
this.cause = RubyBasicObject.UNDEF;
}

public static RubyClass createExceptionClass(Ruby runtime) {
RubyClass exceptionClass = runtime.defineClass("Exception", runtime.getObject(), EXCEPTION_ALLOCATOR);
runtime.setException(exceptionClass);

exceptionClass.setClassIndex(ClassIndex.EXCEPTION);
exceptionClass.setReifiedClass(RubyException.class);

exceptionClass.setMarshal(EXCEPTION_MARSHAL);
exceptionClass.defineAnnotatedMethods(RubyException.class);

return exceptionClass;
}

public static RubyException newException(Ruby runtime, RubyClass excptnClass, String msg) {
if (msg == null) {
@@ -133,11 +166,16 @@ public static RubyException newException(Ruby runtime, RubyClass excptnClass, St
return (RubyException) excptnClass.newInstance(runtime.getCurrentContext(), RubyString.newString(runtime, msg), Block.NULL_BLOCK);
}


@Deprecated
public static IRubyObject newException(ThreadContext context, RubyClass exceptionClass, IRubyObject message) {
return exceptionClass.callMethod(context, "new", message.convertToString());
}

public IRubyObject full_message(ThreadContext context) {
return RubyString.newString(context.runtime, TraceType.Format.MRI.printBacktrace(this, false));
}

@JRubyMethod(optional = 2, visibility = PRIVATE)
public IRubyObject initialize(IRubyObject[] args, Block block) {
if ( args.length == 1 ) setMessage(args[0]);
@@ -176,11 +214,6 @@ public IRubyObject backtrace_locations(ThreadContext context) {
return RubyThread.Location.newLocationArray(runtime, elements);
}

@JRubyMethod(name = "exception", optional = 1, rest = true, meta = true)
public static IRubyObject exception(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
return ((RubyClass) recv).newInstance(context, args, block);
}

@JRubyMethod(optional = 1)
public RubyException exception(IRubyObject[] args) {
switch (args.length) {
@@ -211,11 +244,6 @@ public IRubyObject message(ThreadContext context) {
return callMethod(context, "to_s");
}

@JRubyMethod(name = "full_message")
public IRubyObject full_message(ThreadContext context) {
return RubyString.newString(context.runtime, TraceType.Format.MRI.printBacktrace(this, false));
}

/** inspects an object and return a kind of debug information
*
*@return A RubyString containing the debug information.
@@ -246,24 +274,6 @@ public RubyBoolean op_equal(ThreadContext context, IRubyObject other) {
return context.runtime.newBoolean(equal);
}

@JRubyMethod(name = "===", meta = true)
public static IRubyObject op_eqq(ThreadContext context, IRubyObject recv, IRubyObject other) {
Ruby runtime = context.runtime;
// special case non-FlowControlException Java exceptions so they'll be caught by rescue Exception
if (other instanceof ConcreteJavaProxy &&
(recv == runtime.getException() || recv == runtime.getStandardError())) {

Object object = ((ConcreteJavaProxy)other).getObject();
if (object instanceof Throwable && !(object instanceof FlowControlException)) {
if (recv == runtime.getException() || object instanceof Exception) {
return context.runtime.getTrue();
}
}
}
// fall back on default logic
return ((RubyClass)recv).op_eqq(context, other);
}

@JRubyMethod(name = "cause")
public IRubyObject cause(ThreadContext context) {
assert cause != null;
@@ -288,17 +298,13 @@ public Object toJava(Class target) {
return super.toJava(target);
}

protected RaiseException constructThrowable(String message) {
return new Exception(message, this);
}

/**
* Get a throwable suitable for throwing in Java.
*
* The throwable here will be constructed lazily by calling constructThrowable and then cached for future calls.
*
* All throwables returned by Ruby exception objects will descend from RaiseException and follow the Throwable
* hierarchy below {@link org.jruby.exceptions.Exception}
* hierarchy below {@link Exception}
* @return
*/
public RaiseException toThrowable() {
@@ -446,14 +452,4 @@ public String getMessageAsJavaString() {
final IRubyObject msg = getMessage();
return msg.isNil() ? null : msg.toString();
}

protected BacktraceData backtraceData;
private IRubyObject backtrace;
IRubyObject message;
IRubyObject cause;
private RaiseException throwable;

public static final int TRACE_HEAD = 8;
public static final int TRACE_TAIL = 4;
public static final int TRACE_MAX = TRACE_HEAD + TRACE_TAIL + 6;
}
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@
package org.jruby.exceptions;

import java.lang.reflect.Member;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyException;
1 change: 0 additions & 1 deletion core/src/main/java/org/jruby/ext/zlib/RubyZlib.java
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@
import java.util.zip.CRC32;
import java.util.zip.Adler32;

import org.jruby.RubyException;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBasicObject;