pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/dee977db2a9aed0fa9d3080ce87cd3a459a0d5f5

/> a start of native<->Ruby error handling. · jruby/jruby@dee977d · GitHub
Skip to content

Commit dee977d

Browse files
author
Jan Arne Petersen
committed
a start of native<->Ruby error handling.
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@498 961051c9-f516-0410-bf72-c9f7e237a7b7
1 parent 7e6b67c commit dee977d

5 files changed

Lines changed: 66 additions & 21 deletions

File tree

org/jruby/RubyJava.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public static RubyModule createJavaModule(Ruby ruby) {
88

99
javaModule.defineModuleFunction("import", CallbackFactory.getSingletonMethod(RubyJava.class, "rbImport", RubyString.class));
1010
javaModule.defineModuleFunction("name", CallbackFactory.getSingletonMethod(RubyJava.class, "name", RubyString.class, RubyString.class));
11+
javaModule.defineModuleFunction("define_exception_handler", CallbackFactory.getOptSingletonMethod(RubyJava.class, "define_exception_handler"));
1112

1213
return javaModule;
1314
}
@@ -22,4 +23,17 @@ public static RubyObject name(Ruby ruby, RubyObject recv, RubyString javaName, R
2223
ruby.getJavaSupport().rename(rubyName.getValue(), javaName.getValue());
2324
return recv;
2425
}
26+
27+
public static RubyObject define_exception_handler(Ruby ruby, RubyObject recv, RubyObject[] args) {
28+
String name = args[0].toString();
29+
RubyProc handler = null;
30+
if (args.length > 1) {
31+
handler = (RubyProc)args[1];
32+
} else {
33+
handler = RubyProc.newProc(ruby, ruby.getClasses().getProcClass());
34+
}
35+
ruby.getJavaSupport().defineExceptionHandler(name, handler);
36+
37+
return recv;
38+
}
2539
}

org/jruby/javasupport/JavaMethod.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jruby.*;
3838
import org.jruby.exceptions.*;
3939
import org.jruby.runtime.*;
40+
import org.jruby.util.Asserts;
4041

4142
/**
4243
*
@@ -83,27 +84,23 @@ public RubyObject execute(RubyObject recv, RubyObject[] args, Ruby ruby) {
8384
Object receiver = !singleton ? ((RubyJavaObject)recv).getValue() : null;
8485

8586
return JavaUtil.convertJavaToRuby(ruby, method.invoke(receiver, newArgs));
86-
} catch (Exception e) {
87-
throw convertException(ruby, e);
87+
} catch (InvocationTargetException itExcptn) {
88+
convertException(ruby, (Exception)itExcptn.getTargetException());
89+
90+
return ruby.getNil();
91+
} catch (Exception excptn) {
92+
Asserts.assertNotReached();
93+
return null;
8894
}
8995
}
9096

91-
private static RuntimeException convertException(Ruby ruby, Exception e) {
97+
private static void convertException(Ruby ruby, Exception e) {
9298
if (e instanceof RaiseException) {
93-
return (RaiseException) e;
99+
throw (RaiseException) e;
94100
} else if (e instanceof IOException) {
95-
return IOError.fromException(ruby, (IOException) e);
101+
throw IOError.fromException(ruby, (IOException) e);
96102
} else {
97-
StringWriter stackTrace = new StringWriter();
98-
e.printStackTrace(new PrintWriter(stackTrace));
99-
100-
StringBuffer sb = new StringBuffer();
101-
sb.append("Native Exception: '");
102-
sb.append(e.getClass()).append("\'; Message: ");
103-
sb.append(e.getMessage());
104-
sb.append("; StackTrace: ");
105-
sb.append(stackTrace.getBuffer().toString());
106-
throw new RaiseException(ruby, "RuntimeError", sb.toString());
103+
ruby.getJavaSupport().handleNativeException(e);
107104
}
108105
}
109106

org/jruby/javasupport/JavaSupport.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.jruby.javasupport;
22

3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
35
import java.lang.reflect.*;
46
import java.util.*;
57

68
import org.jruby.*;
79
import org.jruby.exceptions.NameError;
10+
import org.jruby.exceptions.RaiseException;
811

912
public class JavaSupport {
1013
private Ruby ruby;
1114

1215
private Map loadedJavaClasses = new HashMap();
1316
private List importedPackages = new ArrayList();
1417
private Map renamedJavaClasses = new HashMap();
18+
private Map exceptionHandlers = new HashMap();
1519

1620
private ClassLoader javaClassLoader = new ClassLoader() {};
1721

@@ -282,4 +286,31 @@ public Class getJavaClass(RubyClass type) {
282286
}
283287
return null;
284288
}
289+
290+
public void defineExceptionHandler(String exceptionClass, RubyProc handler) {
291+
exceptionHandlers.put(exceptionClass, handler);
292+
}
293+
294+
public void handleNativeException(Exception excptn) {
295+
Class excptnClass = excptn.getClass();
296+
RubyProc handler = (RubyProc)exceptionHandlers.get(excptnClass.getName());
297+
while (handler == null &&
298+
excptnClass != Exception.class) {
299+
excptnClass = excptnClass.getSuperclass();
300+
}
301+
if (handler != null) {
302+
handler.call(new RubyObject[]{JavaUtil.convertJavaToRuby(ruby, excptn)});
303+
} else {
304+
StringWriter stackTrace = new StringWriter();
305+
excptn.printStackTrace(new PrintWriter(stackTrace));
306+
307+
StringBuffer sb = new StringBuffer();
308+
sb.append("Native Exception: '");
309+
sb.append(excptn.getClass()).append("\'; Message: ");
310+
sb.append(excptn.getMessage());
311+
sb.append("; StackTrace: ");
312+
sb.append(stackTrace.getBuffer().toString());
313+
throw new RaiseException(ruby, "RuntimeError", sb.toString());
314+
}
315+
}
285316
}

org/jruby/runtime/ReflectionCallbackMethod.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,10 @@ protected final RubyObject invokeMethod(
128128
} catch (InvocationTargetException itExcptn) {
129129
if (itExcptn.getTargetException() instanceof RaiseException) {
130130
throw (RaiseException) itExcptn.getTargetException();
131-
} else if (
132-
itExcptn.getTargetException() instanceof RuntimeException) {
133-
throw (RuntimeException) itExcptn.getTargetException();
131+
} else if (itExcptn.getTargetException() instanceof JumpException) {
132+
throw (JumpException) itExcptn.getTargetException();
134133
} else {
135-
System.err.println(
136-
"[ERROR] Calling method: " + klass + "#" + method);
137-
itExcptn.getTargetException().printStackTrace();
134+
ruby.getJavaSupport().handleNativeException((Exception)itExcptn.getTargetException());
138135
return ruby.getNil();
139136
}
140137
} catch (final IllegalAccessException iaExcptn) {

samples/error.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Java::define_exception_handler "java.lang.NumberFormatException" do |e|
2+
puts e.type
3+
puts e.message
4+
end
5+
6+
Java::Lang::Integer.parseInt "23aa"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy