Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2001 Alan Moore <alan_moore@gmx.net> Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2002-2006 Thomas E Enebo <enebo@acm.org> Copyright (C) 2004 Joey Gibson <joey@joeygibson.com> Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Copyright (C) 2005 David Corbin <dcorbin@users.sf.net> Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby;
 
 import java.util.List;
 
 import static org.jruby.runtime.Visibility.*;

Author(s):
jpetersen
 
 @JRubyClass(name="Exception")
 public class RubyException extends RubyObject {
     protected RubyException(Ruby runtimeRubyClass rubyClass) {
         this(runtimerubyClassnull);
     }
 
     public RubyException(Ruby runtimeRubyClass rubyClassString message) {
         super(runtimerubyClass);
         
         this. = message == null ? runtime.getNil() : runtime.newString(message);
     }
 
     @JRubyMethod(optional = 2, visibility = )
     public IRubyObject initialize(IRubyObject[] argsBlock block) {
         if (args.length == 1)  = args[0];
         return this;
     }
 
     @JRubyMethod
     public IRubyObject backtrace() {
         IRubyObject bt = getBacktrace();
         return bt;
     }
 
     @JRubyMethod(required = 1)
     public IRubyObject set_backtrace(IRubyObject obj) {
         if (obj.isNil()) {
              = null;
         } else if (!isArrayOfStrings(obj)) {
             throw getRuntime().newTypeError("backtrace must be Array of String");
         } else {
              = obj;
         }
         return backtrace();
    }
    
    @JRubyMethod(compat = ., omit = true)
    public IRubyObject backtrace_locations(ThreadContext context) {
        Ruby runtime = context.runtime;
        RubyStackTraceElement[] elements = .getBacktrace(runtime);
        
        return RubyThread.Location.newLocationArray(runtimeelements);
    }
    @JRubyMethod(name = "exception", optional = 1, rest = true, meta = true)
    public static IRubyObject exception(ThreadContext contextIRubyObject recvIRubyObject[] argsBlock block) {
        return ((RubyClassrecv).newInstance(contextargsblock);
    }
    @JRubyMethod(optional = 1)
    public RubyException exception(IRubyObject[] args) {
        switch (args.length) {
            case 0 :
                return this;
            case 1 :
                if(args[0] == this) {
                    return this;
                }
                RubyException ret = (RubyException)rbClone();
                ret.initialize(args.); // This looks wrong, but it's the way MRI does it.
                return ret;
            default :
                throw getRuntime().newArgumentError("Wrong argument count");
        }
    }
    @JRubyMethod(name = "to_s", compat = .)
    public IRubyObject to_s(ThreadContext context) {
        if (.isNil()) {
            return context.runtime.newString(getMetaClass().getRealClass().getName());
        }
        .setTaint(isTaint());
        return ;
    }
    @JRubyMethod(name = "to_s", compat = .)
    public IRubyObject to_s19(ThreadContext context) {
        if (.isNil()) {
            return context.runtime.newString(getMetaClass().getRealClass().getName());
        }
        .setTaint(isTaint());
        return .asString();
    }
    @JRubyMethod(name = "to_str", compat = .)
    public IRubyObject to_str(ThreadContext context) {
        return callMethod(context"to_s");
    }
    @JRubyMethod(name = "message")
    public IRubyObject message(ThreadContext context) {
        return callMethod(context"to_s");
    }

    
inspects an object and return a kind of debug information

Returns:
A RubyString containing the debug information.
    @JRubyMethod(name = "inspect")
    public IRubyObject inspect(ThreadContext context) {
        // rb_class_name skips intermediate classes (JRUBY-6786)
        RubyModule rubyClass = getMetaClass().getRealClass();
        RubyString exception = RubyString.objAsString(contextthis);
        if (exception.size() == 0) return getRuntime().newString(rubyClass.getName());
        StringBuilder sb = new StringBuilder("#<");
        sb.append(rubyClass.getName()).append(": ").append(exception.getByteList()).append(">");
        return getRuntime().newString(sb.toString());
    }
    @JRubyMethod(name = "==", compat = .)
    @Override
    public IRubyObject op_equal(ThreadContext contextIRubyObject other) {
        if (this == otherreturn context.runtime.getTrue();
        boolean equal = context.runtime.getException().isInstance(other) &&
                        callMethod(context"message").equals(other.callMethod(context"message")) &&
                        callMethod(context"backtrace").equals(other.callMethod(context"backtrace"));
        if (context.runtime.is2_0()) {
            equal = equal && getMetaClass().getRealClass() == other.getMetaClass().getRealClass();
        }
        return context.runtime.newBoolean(equal);
    }
    @JRubyMethod(name = "===", meta = true)
    public static IRubyObject op_eqq(ThreadContext contextIRubyObject recvIRubyObject 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)) {
                return context.runtime.getTrue();
            }
        }
        // fall back on default logic
        return ((RubyClass)recv).op_eqq(contextother);
    }
    public void setBacktraceData(BacktraceData backtraceData) {
        this. = backtraceData;
    }
    public BacktraceData getBacktraceData() {
        return ;
    }
        if ( == null) {
            return .;
        }
        return .getBacktrace(getRuntime());
    }
    public void prepareBacktrace(ThreadContext contextboolean nativeException) {
        // if it's null, build a backtrace
        if ( == null) {
             = context.runtime.getInstanceConfig().getTraceType().getBacktrace(contextnativeException);
        }
    }

    
Prepare an "integrated" backtrace that includes the normal Ruby trace plus non-filtered Java frames. Used by Java integration to show the Java frames for a JI-called method.

Parameters:
context
javaTrace
    public void prepareIntegratedBacktrace(ThreadContext contextStackTraceElement[] javaTrace) {
        // if it's null, build a backtrace
        if ( == null) {
             = context.runtime.getInstanceConfig().getTraceType().getIntegratedBacktrace(contextjavaTrace);
        }
    }
    public void forceBacktrace(IRubyObject backtrace) {
         = .;
        set_backtrace(backtrace);
    }
    
    public IRubyObject getBacktrace() {
        if ( == null) {
            initBacktrace();
        }
        return ;
    }
    
    public void initBacktrace() {
        Ruby runtime = getRuntime();
        if ( == null) {
             = runtime.getNil();
        } else {
             = TraceType.generateMRIBacktrace(runtime.getBacktrace(runtime));
        }
    }
    @Override
    public void copySpecialInstanceVariables(IRubyObject clone) {
        RubyException exception = (RubyException)clone;
        exception.backtraceData = ;
        exception.backtrace = ;
        exception.message = ;
    }

    
Print the Ruby exception's backtrace to the given PrintStream.

Parameters:
errorStream the PrintStream to which backtrace should be printed
    public void printBacktrace(PrintStream errorStream) {
        printBacktrace(errorStream, 0);
    }

    
Print the Ruby exception's backtrace to the given PrintStream. This version accepts a number of lines to skip and is primarily used internally for exception printing where the first line is treated specially.

Parameters:
errorStream the PrintStream to which backtrace should be printed
    public void printBacktrace(PrintStream errorStreamint skip) {
        IRubyObject backtrace = callMethod(getRuntime().getCurrentContext(), "backtrace");
        if (!backtrace.isNil() && backtrace instanceof RubyArray) {
            IRubyObject[] elements = backtrace.convertToArray().toJavaArray();
            for (int i = skipi < elements.lengthi++) {
                IRubyObject stackTraceLine = elements[i];
                if (stackTraceLine instanceof RubyString) {
                    printStackTraceLine(errorStreamstackTraceLine);
                }
            }
        }
    }
    private void printStackTraceLine(PrintStream errorStreamIRubyObject stackTraceLine) {
            errorStream.print("\tfrom " + stackTraceLine + '\n');
    }
    private boolean isArrayOfStrings(IRubyObject backtrace) {
        if (!(backtrace instanceof RubyArray)) return false
            
        IRubyObject[] elements = ((RubyArraybacktrace).toJavaArray();
        
        for (int i = 0 ; i < elements.length ; i++) {
            if (!(elements[iinstanceof RubyString)) return false;
        }
            
        return true;
    }
    public static ObjectAllocator EXCEPTION_ALLOCATOR = new ObjectAllocator() {
        public IRubyObject allocate(Ruby runtimeRubyClass klass) {
            RubyException instance = new RubyException(runtimeklass);
            // for future compatibility as constructors move toward not accepting metaclass?
            instance.setMetaClass(klass);
            return instance;
        }
    };
    private static final ObjectMarshal EXCEPTION_MARSHAL = new ObjectMarshal() {
        public void marshalTo(Ruby runtimeObject objRubyClass type,
                              MarshalStream marshalStreamthrows IOException {
            RubyException exc = (RubyException)obj;
            marshalStream.registerLinkTarget(exc);
            List<Variable<Object>> attrs = exc.getVariableList();
            attrs.add(new VariableEntry<Object>(
                    "mesg"exc.message == null ? runtime.getNil() : exc.message));
            attrs.add(new VariableEntry<Object>("bt"exc.getBacktrace()));
            marshalStream.dumpVariables(attrs);
        }
        public Object unmarshalFrom(Ruby runtimeRubyClass type,
                                    UnmarshalStream unmarshalStreamthrows IOException {
            RubyException exc = (RubyException)type.allocate();
            unmarshalStream.registerLinkTarget(exc);
            // FIXME: Can't just pull these off the wire directly? Or maybe we should
            // just use real vars all the time for these?
            unmarshalStream.defaultVariablesUnmarshal(exc);
            exc.message = (IRubyObject)exc.removeInternalVariable("mesg");
            exc.set_backtrace((IRubyObject)exc.removeInternalVariable("bt"));
            return exc;
        }
    };
    public static RubyClass createExceptionClass(Ruby runtime) {
        RubyClass exceptionClass = runtime.defineClass("Exception"runtime.getObject(), );
        runtime.setException(exceptionClass);
        exceptionClass.index = .;
        exceptionClass.setReifiedClass(RubyException.class);
        exceptionClass.setMarshal();
        exceptionClass.defineAnnotatedMethods(RubyException.class);
        return exceptionClass;
    }
    public static RubyException newException(Ruby runtimeRubyClass excptnClassString msg) {
        return new RubyException(runtimeexcptnClassmsg);
    }
    // rb_exc_new3
    public static IRubyObject newException(ThreadContext contextRubyClass exceptionClassIRubyObject message) {
        return exceptionClass.callMethod(context"new"message.convertToString());
    }
    private BacktraceData backtraceData;
    private IRubyObject backtrace;
    public IRubyObject message;
    public static final int TRACE_HEAD = 8;
    public static final int TRACE_TAIL = 4;
    public static final int TRACE_MAX =  +  + 6;
New to GrepCode? Check out our FAQ X