You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/21 16:54:12 UTC

svn commit: r767165 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_clazz.h include/acr_private.h shared/clazz.c shared/descriptor.c shared/pointer.c shared/string.c test/testcase.c

Author: mturk
Date: Tue Apr 21 14:54:12 2009
New Revision: 767165

URL: http://svn.apache.org/viewvc?rev=767165&view=rev
Log:
Move string function to string.c

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
    commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
    commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c
    commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_clazz.h Tue Apr 21 14:54:12 2009
@@ -80,12 +80,20 @@
 ACR_DECLARE(void) ACR_UnloadClass(JNIEnv *env, JAVA_C_ID *clazz);
 
 /**
- * Crete new java.io.File object.
+ * Load Apache Commons Runtime classes into JVM.
  * @param env Current JNI environment.
- * @param pathname Pathname used to construct File object.
+ * @remark This function MUST be called before any classes using
+ *         native methods are loaded by classloader.
  */
-ACR_DECLARE(jobject) ACR_NewCoreFileObjectU(JNIEnv *_E,
-                                            const char *pathname);
+ACR_DECLARE(int) ACR_LoadRuntimeClasses(JNIEnv *env);
+
+/**
+ * Unload Apache Commons Runtime classes referenced from JVM.
+ * @param env Current JNI environment.
+ * @remark After this function is called classes using native
+ *         methods must not be used.
+ */
+ACR_DECLARE(void) ACR_UnloadRuntimeClasses(JNIEnv *env);
 
 #ifdef __cplusplus
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Tue Apr 21 14:54:12 2009
@@ -266,6 +266,8 @@
 #define J_DECLARE_F_ID(I) static JAVA_F_ID _f##I##n
 #define J_DECLARE_M_ID(I) static JAVA_M_ID _m##I##n
 
+#define J4MID(I)          _m##I##n.i
+
 #define J_LOAD_METHOD(I)    \
     if (_m##I##n.i == NULL) {                                               \
         _m##I##n.i = (*_E)->GetMethodID(_E, _clazzn.i, _m##I##n.n,          \

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Tue Apr 21 14:54:12 2009
@@ -63,31 +63,8 @@
     return ACR_SUCCESS;
 }
 
-static jmethodID    fo_cc_init;
-static jmethodID    so_cc_init;
-static jmethodID    so_gb_func;
-
-static ACR_INLINE jmethodID init_cfuncbyname(JNIEnv *_E, jclass c,
-                                             const char *name,
-                                             const char *sig)
-{
-    jmethodID m = (*_E)->GetMethodID(_E, c, name, sig);
-
-    if (m == NULL || (*_E)->ExceptionCheck(_E))
-        return NULL;
-    else
-        return m;
-}
-
-static ACR_INLINE jmethodID init_constructor(JNIEnv *_E, jclass c, const char *sig)
-{
-    jmethodID m = (*_E)->GetMethodID(_E, c, "<init>", sig);
-
-    if (m == NULL || (*_E)->ExceptionCheck(_E))
-        return NULL;
-    else
-        return m;
-}
+/* Core classes forward declarations */
+ACR_CLASS_LDEF(String);
 
 int ACR_InitCoreClasses(JNIEnv *_E)
 {
@@ -120,12 +97,8 @@
         i++;
     }
     /* Init some core Objects and it's methods */
-    fo_cc_init = init_constructor(_E, core_classes[ACR_CC_IOFILE].clazz,
-                                  "(Ljava/lang/String;)V");
-    so_cc_init = init_constructor(_E, core_classes[ACR_CC_STRING].clazz,
-                                  "([B)V");
-    so_gb_func = init_cfuncbyname(_E, core_classes[ACR_CC_STRING].clazz,
-                                  "getBytes", "()[B");
+    ACR_CLASS_LRUN(String);
+
     return ACR_SUCCESS;
 }
 
@@ -206,74 +179,6 @@
         return NULL;
 }
 
-ACR_DECLARE(jobject) ACR_NewCoreFileObjectU(JNIEnv *_E,
-                                            const char *pathname)
-{
-    jstring p = (*_E)->NewStringUTF(_E, pathname);
-    return (*_E)->NewObject(_E, core_classes[ACR_CC_IOFILE].clazz,
-                            fo_cc_init, p);
-}
-
-/* This function is here because of reference to
- * static catched method id's
- */
-ACR_DECLARE(char *) ACR_GetJavaStringA(JNIEnv *_E, jstring str)
-{
-    jbyteArray sb = NULL;
-    jthrowable ee;
-    char *rs = NULL;
-
-    if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
-        /* JNI out of memory error */
-        return NULL;
-    }
-    sb = (*_E)->CallObjectMethod(_E, str, so_gb_func);
-    ee = (*_E)->ExceptionOccurred(_E);
-    if (!ee) {
-        jint len = (*_E)->GetArrayLength(_E, sb);
-        rs = (char *)ACR_Malloc(_E, THROW_FMARK, len + 1);
-        if (rs == NULL) {
-            (*_E)->DeleteLocalRef(_E, sb);
-            return NULL;
-        }
-        (*_E)->GetByteArrayRegion(_E, sb, 0, len, (jbyte *)rs);
-        rs[len] = '\0'; /* NUL-terminate */
-    }
-    else {
-        (*_E)->DeleteLocalRef(_E, ee);
-    }
-    (*_E)->DeleteLocalRef(_E, sb);
-
-    return rs;
-}
-
-/* This function is here because of reference to
- * static catched method id's
- */
-ACR_DECLARE(jstring) ACR_NewJavaStringA(JNIEnv *_E, const char *str)
-{
-    jstring    rs;
-    jbyteArray ba;
-    jsize      sl;
-    if (!str)
-        return NULL;
-    if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
-        /* JNI out of memory error */
-        return NULL;
-    }
-    sl = (jsize)strlen(str);
-    ba = (*_E)->NewByteArray(_E, sl);
-    if (ba != NULL) {
-        (*_E)->SetByteArrayRegion(_E, ba, 0, sl, (jbyte *)str);
-        rs = (*_E)->NewObject(_E, core_classes[ACR_CC_STRING].clazz,
-                              so_cc_init, ba);
-        (*_E)->DeleteLocalRef(_E, ba);
-        return rs;
-    }
-    return NULL;
-
-}
-
 ACR_DECLARE(int) ACR_LoadClass(JNIEnv *_E, JAVA_C_ID *clazz, int init_array)
 {
     int rv = ACR_SUCCESS;
@@ -338,6 +243,37 @@
     }
 }
 
+/* Forward class loading declarations */
+ACR_CLASS_LDEC(Descriptor);
+ACR_CLASS_LDEC(Pointer);
+ACR_CLASS_LDEC(io_File);
+
+ACR_DECLARE(int) ACR_LoadRuntimeClasses(JNIEnv *_E)
+{
+
+    ACR_CLASS_LRUN(Descriptor);
+    ACR_CLASS_LRUN(Pointer);
+    ACR_CLASS_LRUN(io_File);
+#ifdef WIN32
+
+#endif
+
+    return 0;
+}
+
+ACR_DECLARE(void) ACR_UnloadRuntimeClasses(JNIEnv *_E)
+{
+
+    ACR_CLASS_URUN(Descriptor);
+    ACR_CLASS_URUN(Pointer);
+    ACR_CLASS_URUN(io_File);
+#ifdef WIN32
+
+#endif
+
+}
+
+
 #ifdef ACR_ENABLE_TEST
 /* Just for testing the cache table */
 int acr_clazz_cache_size()

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/descriptor.c Tue Apr 21 14:54:12 2009
@@ -105,7 +105,7 @@
 
 ACR_JNI_EXPORT_DECLARE(void, Descriptor, close0)(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         acr_descriptor_callback_fn_t *callback;
         jint  i  = GET_IFIELD_I(0000, _O);
         jniptr p = GET_IFIELD_P(0001, _O);
@@ -133,7 +133,7 @@
 
 ACR_JNI_EXPORT_DECLARE(void, Descriptor, sync0)(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         acr_descriptor_callback_fn_t *callback;
         jint  i  = GET_IFIELD_I(0000, _O);
         jniptr p = GET_IFIELD_P(0001, _O);
@@ -158,8 +158,8 @@
 ACR_DECLARE(jobject) ACR_DescriptorCreate(JNIEnv *_E, int i, void *p,
                                           acr_descriptor_callback_fn_t *cb)
 {
-    if (_clazzn.i && _m0000n.i)
-        return (*_E)->NewObject(_E, _clazzn.i, _m0000n.i,
+    if (_clazzn.i && J4MID(0000))
+        return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000),
                                 (jint)i, (acr_ptr_t)p, (acr_ptr_t)cb);
     else {
         ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND);
@@ -169,7 +169,7 @@
 
 ACR_DECLARE(int) ACR_DescriptorCleanup(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         acr_descriptor_callback_fn_t *callback;
         jint   i = GET_IFIELD_I(0000, _O);
         jniptr p = GET_IFIELD_P(0001, _O);
@@ -205,7 +205,7 @@
 
 ACR_DECLARE(void *) ACR_DescriptorGetPtr(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         jniptr p = GET_IFIELD_P(0001, _O);
         return (void *)((acr_ptr_t)p);
     }
@@ -217,7 +217,7 @@
 
 ACR_DECLARE(int) ACR_DescriptorGetInt(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         return GET_IFIELD_I(0000, _O);
     }
     else {
@@ -228,7 +228,7 @@
 
 ACR_DECLARE(int) ACR_DescriptorSetPtr(ACR_JNISTDARGS, void *p)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         SET_IFIELD_P(0001, _O, (jniptr)((acr_ptr_t)p));
 #ifdef _JNI_CHECK_EXCEPTIONS
         if ((*_E)->ExceptionCheck(_E)) {
@@ -245,7 +245,7 @@
 
 ACR_DECLARE(int) ACR_DescriptorSetInt(ACR_JNISTDARGS, int i)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         SET_IFIELD_I(0000, _O, i);
 #ifdef _JNI_CHECK_EXCEPTIONS
         if ((*_E)->ExceptionCheck(_E)) {

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Tue Apr 21 14:54:12 2009
@@ -99,7 +99,7 @@
 
 ACR_JNI_EXPORT_DECLARE(void, Pointer, cleanup0)(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         acr_pointer_cleanup_fn_t *cleanup;
         jniptr h = GET_IFIELD_P(0000, _O);
         jniptr c = GET_IFIELD_P(0001, _O);
@@ -124,8 +124,8 @@
 ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *_E, void *p,
                                        acr_pointer_cleanup_fn_t *cb)
 {
-    if (_clazzn.i && _m0000n.i)
-        return (*_E)->NewObject(_E, _clazzn.i, _m0000n.i,
+    if (_clazzn.i && J4MID(0000))
+        return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000),
                                 (acr_ptr_t)p, (acr_ptr_t)cb);
     else {
         ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND);
@@ -135,7 +135,7 @@
 
 ACR_DECLARE(int) ACR_PointerCleanup(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         acr_pointer_cleanup_fn_t *cleanup;
         jniptr h = GET_IFIELD_P(0000, _O);
         jniptr c = GET_IFIELD_P(0001, _O);
@@ -166,7 +166,7 @@
 
 ACR_DECLARE(void *) ACR_PointerGet(ACR_JNISTDARGS)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         jniptr h = GET_IFIELD_P(0000, _O);
         return (void *)((acr_ptr_t)h);
     }
@@ -178,7 +178,7 @@
 
 ACR_DECLARE(int) ACR_PointerSet(ACR_JNISTDARGS, void *p)
 {
-    if (_clazzn.i && _m0000n.i) {
+    if (_clazzn.i && J4MID(0000)) {
         SET_IFIELD_P(0000, _O, (jniptr)((acr_ptr_t)p));
 #ifdef _JNI_CHECK_EXCEPTIONS
         if ((*_E)->ExceptionCheck(_E)) {

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=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Tue Apr 21 14:54:12 2009
@@ -20,6 +20,42 @@
 #include "acr_memory.h"
 #include "acr_error.h"
 #include "acr_types.h"
+#include "acr_clazz.h"
+
+J_DECLARE_CLAZZ = {
+    NULL,
+    NULL,
+    "java/lang/String"
+};
+
+J_DECLARE_M_ID(0000) = {
+    NULL,
+    "<init>",
+    "([B)V"
+};
+
+J_DECLARE_M_ID(0001) = {
+    NULL,
+    "getBytes",
+    "()[B"
+};
+
+ACR_CLASS_LDEF(String)
+{
+    int rv;
+
+    if ((rv = ACR_LoadClass(_E, &_clazzn, 0)) != ACR_SUCCESS)
+        return rv;
+    J_LOAD_METHOD(0000);
+    J_LOAD_METHOD(0001);
+
+    return ACR_SUCCESS;
+}
+
+ACR_CLASS_UDEF(String)
+{
+    ACR_UnloadClass(_E, &_clazzn);
+}
 
 /*
  * Apache's "replacement" for the strncpy() function. We roll our
@@ -254,6 +290,60 @@
     return r;
 }
 
+ACR_DECLARE(char *) ACR_GetJavaStringA(JNIEnv *_E, jstring str)
+{
+    jbyteArray sb = NULL;
+    jthrowable ee;
+    char *rs = NULL;
+
+    if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
+        /* JNI out of memory error */
+        return NULL;
+    }
+    sb = (*_E)->CallObjectMethod(_E, str, J4MID(0001));
+    ee = (*_E)->ExceptionOccurred(_E);
+    if (!ee) {
+        jint len = (*_E)->GetArrayLength(_E, sb);
+        rs = (char *)ACR_Malloc(_E, THROW_FMARK, len + 1);
+        if (rs == NULL) {
+            (*_E)->DeleteLocalRef(_E, sb);
+            return NULL;
+        }
+        (*_E)->GetByteArrayRegion(_E, sb, 0, len, (jbyte *)rs);
+        rs[len] = '\0'; /* NUL-terminate */
+    }
+    else {
+        (*_E)->DeleteLocalRef(_E, ee);
+    }
+    (*_E)->DeleteLocalRef(_E, sb);
+
+    return rs;
+}
+
+ACR_DECLARE(jstring) ACR_NewJavaStringA(JNIEnv *_E, const char *str)
+{
+    jstring    rs;
+    jbyteArray ba;
+    jsize      sl;
+    if (!str)
+        return NULL;
+    if ((*_E)->EnsureLocalCapacity(_E, 2) < 0) {
+        /* JNI out of memory error */
+        return NULL;
+    }
+    sl = (jsize)strlen(str);
+    ba = (*_E)->NewByteArray(_E, sl);
+    if (ba != NULL) {
+        (*_E)->SetByteArrayRegion(_E, ba, 0, sl, (jbyte *)str);
+        rs = (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), ba);
+        (*_E)->DeleteLocalRef(_E, ba);
+        return rs;
+    }
+    return NULL;
+
+}
+
+
 /* Match = 0, NoMatch = 1, Abort = -1
  * Based loosely on sections of wildmat.c by Rich Salz
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=767165&r1=767164&r2=767165&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Tue Apr 21 14:54:12 2009
@@ -300,17 +300,24 @@
     RETURN_JWSTR(L"Hello world");
 }
 
+ACR_CLASS_LDEC(io_File);
+
+static int ldfile(JNIEnv *_E)
+{
+    ACR_CLASS_LRUN(io_File);
+    return 0;
+}
+
 ACR_JNI_EXPORT_DECLARE(jobject, TestPrivate, test029)(ACR_JNISTDARGS, int d)
 {
-    return ACR_NewCoreFileObjectU(_E, "/tmp/foo");
+    ldfile(_E);
+    return ACR_IoFileObjectCreate(_E, "/tmp/foo", -1);
 }
 
-ACR_CLASS_LDEC(io_File);
 
 ACR_JNI_EXPORT_DECLARE(jint, TestFile, ftest00)(ACR_JNISTDARGS, jint d)
 {
-    ACR_CLASS_LRUN(io_File);
-    return 0;
+    return ldfile(_E);
 }
 
 ACR_JNI_EXPORT_DECLARE(jobject, TestFile, ftest01)(ACR_JNISTDARGS, jstring f, jint t)