my_jni.h 4.11 KB
Newer Older
Yann Diorcet's avatar
Yann Diorcet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
my_jni.cc
Copyright (C) 2013  Belledonne Communications, Grenoble, France

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Yann Diorcet's avatar
Yann Diorcet committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 */

#ifndef __MY_JNI__H
#define __MY_JNI__H
#include <jni.h>
extern "C" {
#include "linphonecore_utils.h"
}

#define defCallMethod(Type)												\
template <typename ReturnType>												\
static ReturnType call##Type##Method(JNIEnv *env, jobject obj, const char *className, const char *methodName,		\
					const char *methodSignature, ...) {						\
	jclass my_class = env->GetObjectClass(obj);									\
	if(my_class == 0) {												\
		ms_error("Can't get %s JNI class", className);								\
		return NULL;												\
	}														\
	jmethodID my_method = env->GetMethodID(my_class, methodName, methodSignature);					\
	if(my_method == 0) {												\
38
		ms_error("Can't get %s %s %s method", className, methodName, methodSignature);					\
Yann Diorcet's avatar
Yann Diorcet committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		return NULL;												\
	}														\
	va_list vl;													\
	va_start(vl, methodSignature);											\
	ReturnType ret = env->Call##Type##MethodV(obj, my_method, vl);							\
	va_end(vl);													\
	return ret;													\
}															\

#define defGetterTypeField(Type, JavaType, JavaStringType)								\
template <typename ValueType> 												\
static ValueType get##Type##Field(JNIEnv *env, jobject obj, const char *className, const char *fieldName) {		\
	jclass my_class = env->GetObjectClass(obj);									\
	if(my_class == 0) {												\
		ms_error("Can't get %s JNI class", className);								\
		return NULL;												\
	}														\
	jfieldID my_field = env->GetFieldID(my_class, fieldName, JavaStringType);					\
	if(my_field == 0) {												\
		ms_error("Can't get %s %s field", className, fieldName);						\
		return NULL;												\
	}														\
	return (ValueType) env->Get##Type##Field(obj, my_field);							\
}															\

#define defSetterTypeField(Type, JavaType, JavaStringType) 								\
template <typename ValueType>												\
static bool set##Type##Field(JNIEnv *env, jobject obj, const char *className, const char *fieldName, ValueType val) {	\
	jclass my_class = env->GetObjectClass(obj);									\
	if(my_class == 0) {												\
		ms_error("Can't get %s JNI class", className);								\
		return false;												\
	}														\
	jfieldID my_field = env->GetFieldID(my_class, fieldName, JavaStringType);					\
	if(my_field == 0) {												\
		ms_error("Can't get %s %s field", className, fieldName);						\
		return false;												\
	}														\
	env->Set##Type##Field(obj, my_field, (JavaType) val);								\
	return true;													\
}															\

#define defGetterAndSetterTypeField(Type, JavaType, JavaStringType) 							\
	defGetterTypeField(Type, JavaType, JavaStringType) 								\
	defSetterTypeField(Type, JavaType, JavaStringType) 								\

namespace my_jni {
	template <typename ReturnType>
	static void callVoidMethod(JNIEnv *env, jobject obj, const char *className, const char *methodName,
						const char *methodSignature, ...) {
		jclass my_class = env->GetObjectClass(obj);
		if(my_class == 0) {
			ms_error("Can't get %s JNI class", className);
			return;
		}
		jmethodID my_method = env->GetMethodID(my_class, methodName, methodSignature);
		if(my_method == 0) {
			ms_error("Can't get %s %s %s method", className, methodName, methodSignature);
			return;
		}
		va_list vl;
		va_start(vl, methodSignature);
		env->CallVoidMethodV(obj, my_method, vl);
		va_end(vl);
	}
	defCallMethod(Object)
	defGetterAndSetterTypeField(Long, jlong, "J")
}

#endif //__MY_JNI__H