add some reusable List functions

This commit is contained in:
Doug MacEachern 2008-03-26 16:44:48 +00:00
parent 42459486f7
commit 3b5c3a85f0
2 changed files with 48 additions and 0 deletions

View File

@ -218,6 +218,35 @@ static void sigar_set_pointer(JNIEnv *env, jobject obj, const void *ptr) {
#endif
}
int jsigar_init_list(JNIEnv *env, jsigar_list_t *obj)
{
jclass listclass =
JENV->FindClass(env, "java/util/ArrayList");
jmethodID listid =
JENV->GetMethodID(env, listclass, "<init>", "()V");
jmethodID addid =
JENV->GetMethodID(env, listclass, "add",
"(Ljava/lang/Object;)"
"Z");
obj->env = env;
obj->obj = JENV->NewObject(env, listclass, listid);
obj->id = addid;
return SIGAR_OK;
}
int jsigar_list_add(void *data, char *value, int len)
{
jsigar_list_t *obj = (jsigar_list_t *)data;
JNIEnv *env = obj->env;
JENV->CallBooleanMethod(env, obj->obj, obj->id,
JENV->NewStringUTF(env, value));
return SIGAR_OK;
}
JNIEXPORT jstring SIGAR_JNIx(formatSize)
(JNIEnv *env, jclass cls, jlong size)
{

View File

@ -16,6 +16,8 @@
* USA.
*/
#include <jni.h>
#define JENV (*env)
#define SIGAR_PACKAGE "org/hyperic/sigar/"
@ -30,3 +32,20 @@
#define SIGAR_CLASS_SIG(name) \
"L" SIGAR_PACKAGE name ";"
typedef struct {
JNIEnv *env;
jobject obj;
jmethodID id;
} jsigar_list_t;
#ifdef __cplusplus
extern "C" {
#endif
int jsigar_init_list(JNIEnv *env, jsigar_list_t *obj);
int jsigar_list_add(void *data, char *value, int len);
#ifdef __cplusplus
}
#endif