Top Description Inners Fields Constructors Methods
org.graalvm.jniutils

public final Class JNIExceptionWrapper

extends RuntimeException
Class Inheritance
Annotations
@SuppressWarnings:serial
Static Imports
org.graalvm.jniutils.JNIUtil.ExceptionCheck, .JNIUtil.ExceptionClear, .JNIUtil.ExceptionDescribe, .JNIUtil.ExceptionOccurred, .JNIUtil.GetObjectClass, .JNIUtil.GetStaticMethodID, .JNIUtil.IsSameObject, .JNIUtil.NewGlobalRef, .JNIUtil.Throw, .JNIUtil.createHSString, .JNIUtil.createString, .JNIUtil.encodeMethodSignature, .JNIUtil.getBinaryName, .JNIUtil.findClass, .JNIUtil.getJVMCIClassLoader, org.graalvm.nativeimage.c.type.CTypeConversion.toCString

Wraps an exception thrown by a JNI call into HotSpot. If the exception propagates up to an native image entry point, the exception is re-thrown in HotSpot.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
public static class
private static class

Field Summary

Modifier and TypeField and Description
private static final JNIExceptionWrapper.JNIMethodResolver
private static volatile JNI.JClass
private static final JNIExceptionWrapper.JNIMethodResolver
private static final JNIExceptionWrapper.JNIMethodResolver
private static final JNIExceptionWrapper.JNIMethodResolver
private static final String
private static final String
private static final Set<String>
JNI_TRANSITION_METHODS

Names of the methods in the JNICalls class annotated by the JNICall.

private static final long
private final JNI.JThrowable
private final boolean
private static final JNIExceptionWrapper.JNIMethodResolver

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static JNI.JThrowable
pack-priv static <T extends JNI.JObject> T
private static <T extends JNI.JObject> T
private static <T extends JNI.JObject> T
private static <T extends JNI.JObject> T
private static boolean
containsJNIHostCall(StackTraceElement[] stackTrace)

Determines if stackTrace contains a frame denoting a call into HotSpot.

public static JNI.JThrowable
createHSException(JNI.JNIEnv
the JNIEnv
env
,
Throwable
an exception to be created in HotSpot
original
)

Crates an exception in HotSpot representing the given original exception.

private boolean

Returns:

true if the stack trace needed a merge
createMergedStackTrace
(JNI.JNIEnv env)

Creates a merged native image and HotSpot stack trace and updates this JNIExceptionWrapper stack trace to it.

private static String
formatExceptionMessage(String className, String message)

private static String
getClassName(JNI.JNIEnv env, JNI.JThrowable throwableHandle)

private static JNI.JClass
private static int

Returns:

0 if no caller found
getIndexOfPropagateJNIExceptionFrame
(StackTraceElement[] stackTrace)

Gets the index of the first frame denoting the caller of wrapAndThrowPendingJNIException(JNI.

private static int

Returns:

0 if no caller found
getIndexOfTransitionToNativeFrame
(StackTraceElement[] stackTrace)

Gets the index of the first frame denoting the native method call.

private static StackTraceElement[]

Returns:

the stack trace
getJNIExceptionStackTrace
(JNI.JNIEnv
the JNIEnv
env
,
JNI.JObject
the JNI exception to get the stack trace from.
throwableHandle
)

Gets the stack trace from a JNI exception.

private static String
getMessage(JNI.JNIEnv env, JNI.JThrowable throwableHandle)

private static boolean
isJNIHostCall(StackTraceElement frame)

Determines if frame is for a method denoting a call into HotSpot.

private static boolean
isStackFrame(StackTraceElement stackTraceElement, Class<?> clazz, String methodName)

public static StackTraceElement[]
mergeStackTraces(StackTraceElement[] hotSpotStackTrace, StackTraceElement[] nativeStackTrace, boolean originatedInHotSpot)

private static StackTraceElement[]
mergeStackTraces(StackTraceElement[] hotSpotStackTrace, StackTraceElement[] nativeStackTrace, int hotSpotStackStartIndex, int nativeStackStartIndex, boolean originatedInHotSpot)

Merges hotSpotStackTrace with nativeStackTrace.

private void
throwInHotSpot(JNI.JNIEnv env)

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.

public static void
throwInHotSpot(JNI.JNIEnv
the JNIEnv
env
,
Throwable
an exception to be thrown in HotSpot
original
)

Throws an exception into HotSpot.

private static JNI.JThrowable
updateStackTrace(JNI.JNIEnv env, JNI.JThrowable throwableHandle, StackTraceElement[] mergedStackTrace)

public static void
wrapAndThrowPendingJNIException(JNI.JNIEnv env)

If there is a pending JNI exception, this method wraps it in a JNIExceptionWrapper, clears the pending exception and throws the JNIExceptionWrapper wrapper.

public static void
wrapAndThrowPendingJNIException(JNI.JNIEnv env, JNIExceptionWrapper.ExceptionHandler exceptionHandler)

If there is a pending JNI exception, this method wraps it in a JNIExceptionWrapper, clears the pending exception and throws the JNIExceptionWrapper wrapper.

Field Detail

CreateExceptionback to summary
private static final JNIExceptionWrapper.JNIMethodResolver CreateException
entryPointsClassback to summary
private static volatile JNI.JClass entryPointsClass
GetClassNameback to summary
private static final JNIExceptionWrapper.JNIMethodResolver GetClassName
GetStackTraceback to summary
private static final JNIExceptionWrapper.JNIMethodResolver GetStackTrace
GetThrowableMessageback to summary
private static final JNIExceptionWrapper.JNIMethodResolver GetThrowableMessage
HS_ENTRYPOINTS_CLASSback to summary
private static final String HS_ENTRYPOINTS_CLASS
JNI_TRANSITION_CLASSback to summary
private static final String JNI_TRANSITION_CLASS
JNI_TRANSITION_METHODSback to summary
private static final Set<String> JNI_TRANSITION_METHODS

Names of the methods in the JNICalls class annotated by the JNICall.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.RuntimeException.serialVersionUID.

throwableHandleback to summary
private final JNI.JThrowable throwableHandle
throwableRequiresStackTraceUpdateback to summary
private final boolean throwableRequiresStackTraceUpdate
UpdateStackTraceback to summary
private static final JNIExceptionWrapper.JNIMethodResolver UpdateStackTrace

Constructor Detail

JNIExceptionWrapperback to summary
private JNIExceptionWrapper(JNI.JNIEnv env, JNI.JThrowable throwableHandle)

Method Detail

callCreateExceptionback to summary
private static JNI.JThrowable callCreateException(JNI.JNIEnv env, JNI.JObject p0)
callGetClassNameback to summary
pack-priv static <T extends JNI.JObject> T callGetClassName(JNI.JNIEnv env, JNI.JObject p0)
Annotations
@SuppressWarnings:unchecked
callGetStackTraceback to summary
private static <T extends JNI.JObject> T callGetStackTrace(JNI.JNIEnv env, JNI.JObject p0)
Annotations
@SuppressWarnings:unchecked
callGetThrowableMessageback to summary
private static <T extends JNI.JObject> T callGetThrowableMessage(JNI.JNIEnv env, JNI.JObject p0)
Annotations
@SuppressWarnings:unchecked
callUpdateStackTraceback to summary
private static <T extends JNI.JObject> T callUpdateStackTrace(JNI.JNIEnv env, JNI.JObject p0, JNI.JByteArray p1)
containsJNIHostCallback to summary
private static boolean containsJNIHostCall(StackTraceElement[] stackTrace)

Determines if stackTrace contains a frame denoting a call into HotSpot.

createHSExceptionback to summary
public static JNI.JThrowable createHSException(JNI.JNIEnv env, Throwable original)

Crates an exception in HotSpot representing the given original exception.

Parameters
env:JNI.JNIEnv

the JNIEnv

original:Throwable

an exception to be created in HotSpot

createMergedStackTraceback to summary
private boolean createMergedStackTrace(JNI.JNIEnv env)

Creates a merged native image and HotSpot stack trace and updates this JNIExceptionWrapper stack trace to it.

Returns:boolean

true if the stack trace needed a merge

formatExceptionMessageback to summary
private static String formatExceptionMessage(String className, String message)
getClassNameback to summary
private static String getClassName(JNI.JNIEnv env, JNI.JThrowable throwableHandle)
getEntryPointsback to summary
private static JNI.JClass getEntryPoints(JNI.JNIEnv env)
getIndexOfPropagateJNIExceptionFrameback to summary
private static int getIndexOfPropagateJNIExceptionFrame(StackTraceElement[] stackTrace)

Gets the index of the first frame denoting the caller of wrapAndThrowPendingJNIException(JNI.JNIEnv) or wrapAndThrowPendingJNIException(JNI.JNIEnv, ExceptionHandler) in stackTrace.

Returns:int

0 if no caller found

getIndexOfTransitionToNativeFrameback to summary
private static int getIndexOfTransitionToNativeFrame(StackTraceElement[] stackTrace)

Gets the index of the first frame denoting the native method call.

Returns:int

0 if no caller found

getJNIExceptionStackTraceback to summary
private static StackTraceElement[] getJNIExceptionStackTrace(JNI.JNIEnv env, JNI.JObject throwableHandle)

Gets the stack trace from a JNI exception.

Parameters
env:JNI.JNIEnv

the JNIEnv

throwableHandle:JNI.JObject

the JNI exception to get the stack trace from.

Returns:StackTraceElement[]

the stack trace

getMessageback to summary
private static String getMessage(JNI.JNIEnv env, JNI.JThrowable throwableHandle)
isJNIHostCallback to summary
private static boolean isJNIHostCall(StackTraceElement frame)

Determines if frame is for a method denoting a call into HotSpot.

isStackFrameback to summary
private static boolean isStackFrame(StackTraceElement stackTraceElement, Class<?> clazz, String methodName)
mergeStackTracesback to summary
public static StackTraceElement[] mergeStackTraces(StackTraceElement[] hotSpotStackTrace, StackTraceElement[] nativeStackTrace, boolean originatedInHotSpot)
mergeStackTracesback to summary
private static StackTraceElement[] mergeStackTraces(StackTraceElement[] hotSpotStackTrace, StackTraceElement[] nativeStackTrace, int hotSpotStackStartIndex, int nativeStackStartIndex, boolean originatedInHotSpot)

Merges hotSpotStackTrace with nativeStackTrace.

throwInHotSpotback to summary
private void throwInHotSpot(JNI.JNIEnv env)

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.

throwInHotSpotback to summary
public static void throwInHotSpot(JNI.JNIEnv env, Throwable original)

Throws an exception into HotSpot. If original is a JNIExceptionWrapper the wrapped JNI exception is thrown. Otherwise a new RuntimeException is thrown. The RuntimeException message is composed of original.getClass().getName() and original.getMessage(). The stack trace is result of merging the original.getStackTrace() with the current execution stack in HotSpot.

Parameters
env:JNI.JNIEnv

the JNIEnv

original:Throwable

an exception to be thrown in HotSpot

updateStackTraceback to summary
private static JNI.JThrowable updateStackTrace(JNI.JNIEnv env, JNI.JThrowable throwableHandle, StackTraceElement[] mergedStackTrace)
wrapAndThrowPendingJNIExceptionback to summary
public static void wrapAndThrowPendingJNIException(JNI.JNIEnv env)

If there is a pending JNI exception, this method wraps it in a JNIExceptionWrapper, clears the pending exception and throws the JNIExceptionWrapper wrapper. The JNIExceptionWrapper message is composed of the JNI exception class name and the JNI exception message. For exception filtering or custom handling of JNI exceptions see wrapAndThrowPendingJNIException(JNIEnv, ExceptionHandler).

wrapAndThrowPendingJNIExceptionback to summary
public static void wrapAndThrowPendingJNIException(JNI.JNIEnv env, JNIExceptionWrapper.ExceptionHandler exceptionHandler)

If there is a pending JNI exception, this method wraps it in a JNIExceptionWrapper, clears the pending exception and throws the JNIExceptionWrapper wrapper. The JNIExceptionWrapper message is composed of the JNI exception class name and the JNI exception message.

See Also
ExceptionHandler
org.graalvm.jniutils back to summary

public Interface JNIExceptionWrapper.ExceptionHandler


Field Summary

Modifier and TypeField and Description
public static final JNIExceptionWrapper.ExceptionHandler
DEFAULT

Default handler throwing JNIExceptionWrapper for the pending JNI exception.

Method Summary

Modifier and TypeMethod and Description
public static JNIExceptionWrapper.ExceptionHandler
allowExceptions(Class<? extends Throwable>... allowedExceptions)

Creates an exception handler suppressing allowedExceptions.

public void
handleException(JNIExceptionWrapper.ExceptionHandlerContext context)

Handles the JNI pending exception.

Field Detail

DEFAULTback to summary
public static final JNIExceptionWrapper.ExceptionHandler DEFAULT

Default handler throwing JNIExceptionWrapper for the pending JNI exception.

Method Detail

allowExceptionsback to summary
public static JNIExceptionWrapper.ExceptionHandler allowExceptions(Class<? extends Throwable>... allowedExceptions)

Creates an exception handler suppressing allowedExceptions. Other JNI exceptions are rethrown as JNIExceptionWrapper.

Annotations
@SafeVarargs
handleExceptionback to summary
public void handleException(JNIExceptionWrapper.ExceptionHandlerContext context)

Handles the JNI pending exception.

org.graalvm.jniutils back to summary

public final Class JNIExceptionWrapper.ExceptionHandlerContext

extends Object
Class Inheritance
  • java.lang.Object
  • org.graalvm.jniutils.JNIExceptionWrapper.ExceptionHandlerContext

Context for ExceptionHandler.

Field Summary

Modifier and TypeField and Description
private final JNI.JNIEnv
private final JNI.JThrowable

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public JNI.JNIEnv
getEnv()

Returns current thread JNIEnv.

public JNI.JThrowable
getThrowable()

Returns pending JNI exception.

public String
getThrowableClassName()

Returns pending JNI exception class name.

public void
throwJNIExceptionWrapper()

Throws JNIExceptionWrapper for the pending JNI exception.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

envback to summary
private final JNI.JNIEnv env
throwableback to summary
private final JNI.JThrowable throwable

Constructor Detail

ExceptionHandlerContextback to summary
pack-priv ExceptionHandlerContext(JNI.JNIEnv env, JNI.JThrowable throwable)

Method Detail

getEnvback to summary
public JNI.JNIEnv getEnv()

Returns current thread JNIEnv.

getThrowableback to summary
public JNI.JThrowable getThrowable()

Returns pending JNI exception.

getThrowableClassNameback to summary
public String getThrowableClassName()

Returns pending JNI exception class name.

throwJNIExceptionWrapperback to summary
public void throwJNIExceptionWrapper()

Throws JNIExceptionWrapper for the pending JNI exception.

org.graalvm.jniutils back to summary

private final Class JNIExceptionWrapper.JNIMethodResolver

extends Object
implements JNIMethod
Class Inheritance
All Implemented Interfaces
org.graalvm.jniutils.JNICalls.JNIMethod

Field Summary

Modifier and TypeField and Description
private volatile JNI.JMethodID
private final String
private final String

Constructor Summary

AccessConstructor and Description
private
JNIMethodResolver(String methodName, String methodSignature)

Method Summary

Modifier and TypeMethod and Description
pack-priv static JNIExceptionWrapper.JNIMethodResolver
create(String methodName, Class<?> returnType, Class<?>... parameterTypes)

public String
getDisplayName()

Implements org.graalvm.jniutils.JNICalls.JNIMethod.getDisplayName.

Returns a method display name used for logging.

public JNI.JMethodID
pack-priv JNIExceptionWrapper.JNIMethodResolver
resolve(JNI.JNIEnv jniEnv)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

methodIdback to summary
private volatile JNI.JMethodID methodId
methodNameback to summary
private final String methodName
methodSignatureback to summary
private final String methodSignature

Constructor Detail

JNIMethodResolverback to summary
private JNIMethodResolver(String methodName, String methodSignature)

Method Detail

createback to summary
pack-priv static JNIExceptionWrapper.JNIMethodResolver create(String methodName, Class<?> returnType, Class<?>... parameterTypes)
getDisplayNameback to summary
public String getDisplayName()

Implements org.graalvm.jniutils.JNICalls.JNIMethod.getDisplayName.

Doc from org.graalvm.jniutils.JNICalls.JNIMethod.getDisplayName.

Returns a method display name used for logging.

Annotations
@Override
getJMethodIDback to summary
public JNI.JMethodID getJMethodID()

Implements org.graalvm.jniutils.JNICalls.JNIMethod.getJMethodID.

Doc from org.graalvm.jniutils.JNICalls.JNIMethod.getJMethodID.

Returns a method JNI JMethodID.

Annotations
@Override
resolveback to summary
pack-priv JNIExceptionWrapper.JNIMethodResolver resolve(JNI.JNIEnv jniEnv)