Author: mturk
Date: Sat Mar 26 11:30:39 2011
New Revision: 1085699
URL: http://svn.apache.org/viewvc?rev=1085699&view=rev
Log:
Catch exception classes on their first use
Modified:
commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
commons/sandbox/runtime/trunk/src/main/native/shared/error.c
commons/sandbox/runtime/trunk/src/main/native/shared/string.c
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h?rev=1085699&r1=1085698&r2=1085699&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h Sat Mar
26 11:30:39 2011
@@ -1601,11 +1601,11 @@ ACR_INLINE(DWORD) AcrNetOsError()
#endif /* !defined(_WINDOWS) */
#if defined(DEBUG) || defined(_DEBUG)
-#define ACR_THROW_EX(CL, ER) AcrDebugThrowException(_E, __FILE_FUNC_LINE__,
(CL), (ER))
-#define ACR_THROW_ERROR(CL) AcrDebugThrowException(_E, __FILE_FUNC_LINE__,
(CL), ACR_GET_OS_ERROR())
+#define ACR_THROW(CL, ER) AcrDebugThrowException(_E, __FILE_FUNC_LINE__,
(CL), (ER))
+#define ACR_THROW_OS_ERROR(CL) AcrDebugThrowException(_E, __FILE_FUNC_LINE__,
(CL), ACR_GET_OS_ERROR())
#else
-#define ACR_THROW_EX(CL, ER) AcrThrowException(_E, (CL), (ER))
-#define ACR_THROW_ERROR(CL) AcrThrowException(_E, (CL), ACR_GET_OS_ERROR())
+#define ACR_THROW(CL, ER) AcrThrowException(_E, (CL), (ER))
+#define ACR_THROW_OS_ERROR(CL) AcrThrowException(_E, (CL), ACR_GET_OS_ERROR())
#endif
#ifdef __cplusplus
@@ -1618,7 +1618,10 @@ AcrFatalError(JNI_STDENV,
int error, const char *msg, ...);
void
-AcrThrow(JNI_STDENV, const char *clazz, const char *msg);
+AcrThrow(JNI_STDENV, int cls, const char *msg);
+
+void
+AcrThrowClass(JNI_STDENV, const char *clazz, const char *msg);
void
AcrThrowException(JNI_STDENV, int clazz, int error);
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1085699&r1=1085698&r2=1085699&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Sat Mar 26
11:30:39 2011
@@ -20,30 +20,33 @@
#include "acr/string.h"
#include "acr/port.h"
-static const char *_throw_classes[] = {
- "java/lang/Exception",
- "java/lang/RuntimeException",
- "java/lang/OutOfMemoryError",
- "java/lang/NullPointerException",
- "java/lang/IllegalArgumentException",
- "java/lang/IllegalStateException",
- "java/lang/IndexOutOfBoundsException",
- "java/lang/ClassCastException",
- "java/lang/InstantiationException",
- "java/lang/InternalError",
- "java/lang/SecurityException",
- "java/lang/UnsatisfiedLinkError",
- "java/lang/UnsupportedOperationException",
- "java/io/IOException",
- "java/io/InterruptedIOException",
- "java/io/FileNotFoundException",
- "java/io/SyncFailedException",
- "java/net/SocketException",
- ACR_EXCEPTION_CP "NoSuchObjectException",
- ACR_EXCEPTION_CP "OperatingSystemException",
- ACR_EXCEPTION_CP "TimeoutException",
- ACR_EXCEPTION_CP "UnsupportedOperatingSystemException",
- 0
+static struct {
+ jclass clazz;
+ const char *name;
+} _throw_classes[] = {
+ { 0, "java/lang/Exception" },
+ { 0, "java/lang/RuntimeException" },
+ { 0, "java/lang/OutOfMemoryError" },
+ { 0, "java/lang/NullPointerException" },
+ { 0, "java/lang/IllegalArgumentException" },
+ { 0, "java/lang/IllegalStateException" },
+ { 0, "java/lang/IndexOutOfBoundsException" },
+ { 0, "java/lang/ClassCastException" },
+ { 0, "java/lang/InstantiationException" },
+ { 0, "java/lang/InternalError" },
+ { 0, "java/lang/SecurityException" },
+ { 0, "java/lang/UnsatisfiedLinkError" },
+ { 0, "java/lang/UnsupportedOperationException" },
+ { 0, "java/io/IOException" },
+ { 0, "java/io/InterruptedIOException" },
+ { 0, "java/io/FileNotFoundException" },
+ { 0, "java/io/SyncFailedException" },
+ { 0, "java/net/SocketException" },
+ { 0, ACR_EXCEPTION_CP "NoSuchObjectException" },
+ { 0, ACR_EXCEPTION_CP "OperatingSystemException" },
+ { 0, ACR_EXCEPTION_CP "TimeoutException" },
+ { 0, ACR_EXCEPTION_CP "UnsupportedOperatingSystemException" },
+ { 0, 0 }
};
static const char *const _canon_errors[] = {
@@ -543,7 +546,7 @@ AcrFatalError(JNI_STDENV,
}
void
-AcrThrow(JNI_STDENV, const char *clazz, const char *msg)
+AcrThrowClass(JNI_STDENV, const char *clazz, const char *msg)
{
jclass ec;
@@ -582,25 +585,68 @@ AcrThrow(JNI_STDENV, const char *clazz,
}
void
+AcrThrow(JNI_STDENV, int cls, const char *msg)
+{
+ jclass ec;
+
+ if (_E == 0)
+ _E = AcrGetJNIEnv();
+ if (cls < 0 && cls >= ACR_EX_LEN)
+ cls = 0;
+ if (IS_INVALID_HANDLE(_E)) {
+ ACR_DEBUG_TRACE("JNI Environment is invalid or unavailable (%d).",
ACR_GET_OS_ERROR());
+ ACR_DEBUG_TRACE("Throwing class '%s'", _throw_classes[cls].name);
+ if (msg) {
+ ACR_DEBUG_TRACE("%s", msg);
+ }
+ return;
+ }
+ if ((*_E)->ExceptionCheck(_E)) {
+ /* We already have a pending exception. */
+ ACR_DEBUG_TRACE("Exception is already in the queue. Throwing '%s'",
+ _throw_classes[cls].name);
+ if (msg) {
+ ACR_DEBUG_TRACE("%s", msg);
+ }
+ return;
+ }
+ if (_throw_classes[cls].clazz == 0) {
+ ec = (*_E)->FindClass(_E, _throw_classes[cls].name);
+ if (ec == 0) {
+ /* If the class cannot be found,
+ * the Exception has already been thrown.
+ * See JNI Find Class for the Exceptions thrown.
+ */
+ ACR_DEBUG_TRACE("Cannot find class '%s'",
_throw_classes[cls].name);
+ if (msg) {
+ ACR_DEBUG_TRACE("%s", msg);
+ }
+ return;
+ }
+ _throw_classes[cls].clazz = (jclass)(*_E)->NewGlobalRef(_E, ec);
+ if (_throw_classes[cls].clazz == 0)
+ return;
+ (*_E)->DeleteLocalRef(_E, ec);
+ }
+ if (_throw_classes[cls].clazz != 0)
+ (*_E)->ThrowNew(_E, _throw_classes[cls].clazz, msg);
+}
+
+void
AcrDebugThrowException(JNI_STDENV,
const char *file, const char *func, int line,
int cls, int err)
{
- const char *clazz;
const char *msg = 0;
char *ptr;
char buffer[ACR_MBUFF_SIZ * 2] = "";
- if (cls > 0 && cls < ACR_EX_LEN)
- clazz = _throw_classes[cls];
- else
- clazz = _throw_classes[0];
- ptr = buffer;
if (err == 0 || err == ACR_EGENERAL) {
msg = 0;
}
else {
msg = buffer;
+ ptr = buffer;
if (file != 0) {
time_t t;
struct tm *l;
@@ -618,17 +664,21 @@ AcrDebugThrowException(JNI_STDENV,
}
_cr_strerror_r(err, ptr, ACR_MBUFF_SIZ);
}
- AcrThrow(_E, clazz, msg);
+ AcrThrow(_E, cls, msg);
}
void
AcrThrowException(JNI_STDENV, int cls, int err)
{
- const char *clazz;
+ const char *msg = 0;
+ char buffer[ACR_MBUFF_SIZ] = "";
- if (cls > 0 && cls < ACR_EX_LEN)
- clazz = _throw_classes[cls];
- else
- clazz = _throw_classes[0];
- AcrThrow(_E, clazz, 0);
+ if (err == 0 || err == ACR_EGENERAL) {
+ msg = 0;
+ }
+ else {
+ msg = buffer;
+ _cr_strerror_r(err, buffer, ACR_MBUFF_SIZ);
+ }
+ AcrThrow(_E, cls, msg);
}
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=1085699&r1=1085698&r2=1085699&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Sat Mar 26
11:30:39 2011
@@ -792,7 +792,7 @@ static jstring new_string_utf_8(JNIEnv *
if ((ex = conv_utf8_to_ucs2(s, sl, cc, &wl)) == ACR_SUCCESS)
rs = (*_E)->NewString(_E, cc, sl);
else
- ACR_THROW_EX(ACR_EX_EINVAL, ex);
+ ACR_THROW(ACR_EX_EINVAL, ex);
}
else {
jchar *cc;
@@ -801,7 +801,7 @@ static jstring new_string_utf_8(JNIEnv *
if ((ex = conv_utf8_to_ucs2(s, sl, cc, &wl)) == ACR_SUCCESS)
rs = (*_E)->NewString(_E, cc, sl);
else
- ACR_THROW_EX(ACR_EX_EINVAL, ex);
+ ACR_THROW(ACR_EX_EINVAL, ex);
AcrFree(cc);
}
}