use of org.graalvm.jniutils.JNI.JThrowable in project graal by oracle.
the class JNIExceptionWrapper method wrapAndThrowPendingJNIException.
/**
* If there is a pending JNI exception, this method wraps it in a {@link JNIExceptionWrapper},
* clears the pending exception and throws the {@link JNIExceptionWrapper} wrapper. The
* {@link JNIExceptionWrapper} message is composed of the JNI exception class name and the JNI
* exception message.
*
* @see ExceptionHandler
*/
public static void wrapAndThrowPendingJNIException(JNIEnv env, ExceptionHandler exceptionHandler) {
Objects.requireNonNull(exceptionHandler, "ExceptionHandler must be non null.");
if (ExceptionCheck(env)) {
JThrowable exception = ExceptionOccurred(env);
if (JNIUtil.tracingAt(2) && exception.isNonNull()) {
ExceptionDescribe(env);
}
ExceptionClear(env);
exceptionHandler.handleException(new ExceptionHandlerContext(env, exception));
}
}
use of org.graalvm.jniutils.JNI.JThrowable in project graal by oracle.
the class JNIExceptionWrapper method throwInHotSpot.
/**
* Re-throws this JNI exception in HotSpot after updating the stack trace to include the native
* image frames between the native image entry point and the call back to HotSpot that threw the
* original JNI exception.
*/
private void throwInHotSpot(JNIEnv env) {
JThrowable toThrow;
if (throwableRequiresStackTraceUpdate) {
toThrow = updateStackTrace(env, throwableHandle, getStackTrace());
} else {
toThrow = throwableHandle;
}
Throw(env, toThrow);
}
use of org.graalvm.jniutils.JNI.JThrowable in project graal by oracle.
the class JNIExceptionWrapper method createHSException.
/**
* Crates an exception in HotSpot representing the given {@code original} exception.
*
* @param env the {@link JNIEnv}
* @param original an exception to be created in HotSpot
*/
public static JThrowable createHSException(JNIEnv env, Throwable original) {
JThrowable hsThrowable;
if (original instanceof JNIExceptionWrapper) {
JNIExceptionWrapper jniExceptionWrapper = (JNIExceptionWrapper) original;
hsThrowable = jniExceptionWrapper.throwableHandle;
if (jniExceptionWrapper.throwableRequiresStackTraceUpdate) {
hsThrowable = updateStackTrace(env, hsThrowable, jniExceptionWrapper.getStackTrace());
}
} else {
hsThrowable = createExceptionOfSameType(env, original);
boolean hasSameExceptionType = hsThrowable.isNonNull();
if (!hasSameExceptionType) {
String message = formatExceptionMessage(original.getClass().getName(), original.getMessage());
JString hsMessage = createHSString(env, message);
hsThrowable = callCreateException(env, hsMessage);
}
StackTraceElement[] nativeStack = original.getStackTrace();
if (nativeStack.length != 0) {
// Update stack trace only for exceptions which have stack trace.
// For exceptions which override fillInStackTrace merging stack traces only adds
// useless JNI calls.
StackTraceElement[] hsStack = getJNIExceptionStackTrace(env, hsThrowable);
StackTraceElement[] mergedStack = mergeStackTraces(hsStack, nativeStack, // exception with same exception
hasSameExceptionType ? 0 : 1, // type has no factory method
0, false);
hsThrowable = updateStackTrace(env, hsThrowable, mergedStack);
}
}
return hsThrowable;
}
Aggregations