NativeException.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Copyright (C) 2005 David Corbin <dcorbin@users.sourceforge.net>
  15.  *
  16.  * Alternatively, the contents of this file may be used under the terms of
  17.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  18.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  19.  * in which case the provisions of the GPL or the LGPL are applicable instead
  20.  * of those above. If you wish to allow use of your version of this file only
  21.  * under the terms of either the GPL or the LGPL, and not to allow others to
  22.  * use your version of this file under the terms of the EPL, indicate your
  23.  * decision by deleting the provisions above and replace them with the notice
  24.  * and other provisions required by the GPL or the LGPL. If you do not delete
  25.  * the provisions above, a recipient may use your version of this file under
  26.  * the terms of any one of the EPL, the GPL or the LGPL.
  27.  ***** END LICENSE BLOCK *****/
  28. package org.jruby;

  29. import java.io.PrintStream;

  30. import java.lang.reflect.Member;
  31. import org.jruby.anno.JRubyClass;
  32. import org.jruby.anno.JRubyMethod;
  33. import org.jruby.javasupport.Java;
  34. import org.jruby.runtime.Block;
  35. import org.jruby.runtime.ObjectAllocator;
  36. import org.jruby.runtime.builtin.IRubyObject;

  37. @JRubyClass(name = "NativeException", parent = "RuntimeError")
  38. public class NativeException extends RubyException {

  39.     private final Throwable cause;
  40.     public static final String CLASS_NAME = "NativeException";
  41.     private final Ruby runtime;

  42.     public NativeException(Ruby runtime, RubyClass rubyClass, Throwable cause) {
  43.         super(runtime, rubyClass);
  44.         this.runtime = runtime;
  45.         this.cause = cause;
  46.         this.message = runtime.newString(cause.getClass().getName() + ": " + searchStackMessage(cause));
  47.     }
  48.    
  49.     private NativeException(Ruby runtime, RubyClass rubyClass) {
  50.         super(runtime, rubyClass);
  51.         this.runtime = runtime;
  52.         this.cause   = new Throwable();
  53.         this.message = runtime.newString();
  54.     }
  55.    
  56.     private static ObjectAllocator NATIVE_EXCEPTION_ALLOCATOR = new ObjectAllocator() {
  57.         public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
  58.             NativeException instance = new NativeException(runtime, klazz);
  59.             instance.setMetaClass(klazz);
  60.             return instance;
  61.         }
  62.     };

  63.     public static RubyClass createClass(Ruby runtime, RubyClass baseClass) {
  64.         RubyClass exceptionClass = runtime.defineClass(CLASS_NAME, baseClass, NATIVE_EXCEPTION_ALLOCATOR);

  65.         exceptionClass.defineAnnotatedMethods(NativeException.class);

  66.         return exceptionClass;
  67.     }

  68.     @JRubyMethod
  69.     public IRubyObject cause(Block unusedBlock) {
  70.         return Java.getInstance(getRuntime(), cause);
  71.     }

  72.     public IRubyObject backtrace() {
  73.         IRubyObject rubyTrace = super.backtrace();
  74.         if (rubyTrace.isNil()) {
  75.             return rubyTrace;
  76.         }
  77.         RubyArray array = (RubyArray) rubyTrace.dup();
  78.         StackTraceElement[] stackTrace = cause.getStackTrace();
  79.         for (int i = stackTrace.length - 1; i >= 0; i--) {
  80.             StackTraceElement element = stackTrace[i];
  81.             String className = element.getClassName();
  82.             String line = null;
  83.             if (element.getFileName() == null) {
  84.                 line = className + ":" + element.getLineNumber() + ":in `" + element.getMethodName() + "'";
  85.             } else {
  86.                 int index = className.lastIndexOf(".");
  87.                 String packageName = null;
  88.                 if (index == -1) {
  89.                     packageName = "";
  90.                 } else {
  91.                     packageName = className.substring(0, index) + "/";
  92.                 }
  93.                 line = packageName.replace(".", "/") + element.getFileName() + ":" + element.getLineNumber() + ":in `" + element.getMethodName() + "'";
  94.             }
  95.             RubyString string = runtime.newString(line);
  96.             array.unshift(string);
  97.         }
  98.         return array;
  99.     }

  100.     public void trimStackTrace(Member target) {
  101.         Throwable t = new Throwable();
  102.         StackTraceElement[] origStackTrace = cause.getStackTrace();
  103.         StackTraceElement[] currentStackTrace = t.getStackTrace();
  104.         int skip = 0;
  105.         for (int i = 1;
  106.                 i <= origStackTrace.length && i <= currentStackTrace.length;
  107.                 ++i) {
  108.             StackTraceElement a = origStackTrace[origStackTrace.length - i];
  109.             StackTraceElement b = currentStackTrace[currentStackTrace.length - i];
  110.             if (a.equals(b)) {
  111.                 skip += 1;
  112.             } else {
  113.                 break;
  114.             }
  115.         }
  116.         // If we know what method was being called, strip everything
  117.         // before the call. This hides the JRuby and reflection internals.
  118.         if (target != null) {
  119.             String className = target.getDeclaringClass().getName();
  120.             String methodName = target.getName();
  121.             for (int i = origStackTrace.length - skip - 1; i >= 0; --i) {
  122.                 StackTraceElement frame = origStackTrace[i];
  123.                 if (frame.getClassName().equals(className) &&
  124.                     frame.getMethodName().equals(methodName)) {
  125.                     skip = origStackTrace.length - i - 1;
  126.                     break;
  127.                 }
  128.             }
  129.         }
  130.         if (skip > 0) {
  131.             StackTraceElement[] newStackTrace =
  132.                     new StackTraceElement[origStackTrace.length - skip];
  133.             for (int i = 0; i < newStackTrace.length; ++i) {
  134.                 newStackTrace[i] = origStackTrace[i];
  135.             }
  136.             cause.setStackTrace(newStackTrace);
  137.         }
  138.     }

  139.     public void printBacktrace(PrintStream errorStream) {
  140.         super.printBacktrace(errorStream);
  141.         if (getRuntime().getDebug().isTrue()) {
  142.             errorStream.println("Complete Java stackTrace");
  143.             cause.printStackTrace(errorStream);
  144.         }
  145.     }

  146.     public Throwable getCause() {
  147.         return cause;
  148.     }

  149.     private String searchStackMessage(Throwable cause) {
  150.         String message = null;

  151.         do {
  152.             message = cause.getMessage();
  153.             cause = cause.getCause();
  154.         } while (message == null && cause != null);

  155.         return message;
  156.     }
  157. }