/* * Copyright (c) 1999-2001 Ivan Francolin Martinez * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the company nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef __EASY_JAVA_H__ #define __EASY_JAVA_H__ #include "jni.h" /** Java Virtual Machine **/ extern JavaVM *ej_jvm; /** Java Environment **/ extern JNIEnv *ej_env; /** Initialize JVM for use **/ int ej_init_jvm(void); /** finish JVM **/ void ej_finish_jvm(void); /** Adjust classname : replace '.' with '/' **/ char *ej_adjust_classname(char *classname); /** Check if exception has ocurred **/ jthrowable ej_CheckException(void); /** Create a new object of specified class **/ jobject ej_CreateObject(char *classname); /** http://java.sun.com/j2se/1.3/docs/guide/jni/spec/types.doc.html **/ #define EJ_CONSTRUCTOR_METHOD "" #define EJ_NO_PARAMETERS_NO_RETURN "()V" #define EJ_NO_PARAMETERS_RET_STRING "()Ljava/lang/String;" #define EJ_NO_PARAMETERS_RET_BOOL "()Z" #define EJ_STRING_NO_RETURN "(Ljava/lang/String;)V" #define EJ_STRING_RET_STRING "(Ljava/lang/String;)Ljava/lang/String;" #define EJ_INT_RET_STRING "(I)Ljava/lang/String;" /** Return a Java Class (Remember to use '/' instead of '.') **/ #define ej_FindClass(classname) \ (*ej_env)->FindClass(ej_env, classname) /** Return the class of object **/ #define ej_GetObjectClass(obj) \ (*ej_env)->GetObjectClass(ej_env, obj) /** Return the method of the class with specified parameters **/ #define ej_GetMethodId(class, method, id) \ (*ej_env)->GetMethodID(ej_env, class, method, id) /** Return the static method of the class with specified parameters **/ #define ej_GetStaticMethodId(class, method, id) \ (*ej_env)->GetStaticMethodID(ej_env, class, method, id) /** Return the method of the object with specified parameters **/ #define ej_GetMethodIdObj(obj, method, id) \ ej_GetMethodId(ej_GetObjectClass(obj), method, id) /** Return the constructor with specified parameters **/ #define ej_GetConstructor(class, id) \ ej_GetMethodId(class, EJ_CONSTRUCTOR_METHOD, id) /** Return the default constructor of class **/ #define ej_GetDefaultConstructor(class) \ ej_GetConstructor(class, EJ_NO_PARAMETERS_NO_RETURN) /** Create a new object using specified constructor **/ #define ej_NewObjectC(class, constructor) \ (*ej_env)->NewObject(ej_env, class, constructor) /** Create a new object using standard constructor **/ #define ej_NewObject(class) \ ej_NewObjectC(class, ej_GetDefaultConstructor(class)) /** Allocate a new object without calling the constructors **/ #define ej_AllocObject(class) \ (*ej_env)->AllocObject(ej_env, class) /** Call Method wich returns a boolean **/ #define ej_CallBooleanMethod(object, method, ...) \ (*ej_env)->CallBooleanMethod(ej_env, object, method, __VA_ARGS__) #define ej_CallBooleanMethod_(object, method) \ (*ej_env)->CallBooleanMethod(ej_env, object, method) /** Call Method wich returns an object **/ #define ej_CallObjectMethod(object, method, ...) \ (*ej_env)->CallObjectMethod(ej_env, object, method, __VA_ARGS__) #define ej_CallObjectMethod_(object, method) \ (*ej_env)->CallObjectMethod(ej_env, object, method) /** Call static Method wich returns an object **/ #define ej_CallStaticObjectMethod(object, method, ...) \ (*ej_env)->CallStaticObjectMethod(ej_env, object, method, __VA_ARGS__) #define ej_CallStaticObjectMethod_(object, method) \ (*ej_env)->CallStaticObjectMethod(ej_env, object, method) /** Call void Method **/ #define ej_CallVoidMethod(object, method, ...) \ (*ej_env)->CallVoidMethod(ej_env, object, method, __VA_ARGS__) #define ej_CallVoidMethod_(object, method) \ (*ej_env)->CallVoidMethod(ej_env, object, method) /** Create an java String **/ #define ej_NewString(str) \ (*ej_env)->NewString(ej_env, str) #define ej_NewStringUTF(str) \ (*ej_env)->NewStringUTF(ej_env, str) /** Return a pointer to chars of Java String **/ #define ej_GetStringUTFChars(str, isCopy) \ (*ej_env)->GetStringUTFChars(ej_env, str, isCopy) /** Release memory allocated by chars of Java String **/ #define ej_ReleaseStringUTFChars(str, chars) \ (*ej_env)->ReleaseStringUTFChars(ej_env, str, chars) /** Check if JavaException has Occurred **/ #define ej_ExceptionOccurred() \ (*ej_env)->ExceptionOccurred(ej_env) /** Clear last JavaException **/ #define ej_ExceptionClear() \ (*ej_env)->ExceptionClear(ej_env) /** Print the StackTrace related to lastException */ #define ej_ExceptionDescribe() \ (*ej_env)->ExceptionDescribe(ej_env) /** Register object in global context **/ #define ej_NewGlobalRef(obj) \ (*ej_env)->NewGlobalRef(ej_env, obj) /** Delete object from global context **/ #define ej_DeleteGlobalRef(obj) \ (*ej_env)->DeleteGlobalRef(ej_env, obj) /** Call the toString() method of the object **/ #define ej_toString(obj) \ ej_CallObjectMethod_(obj, ej_GetMethodIdObj(obj, "toString", EJ_NO_PARAMETERS_RET_STRING)) /**********************************************************************************/ /** JDBC Definitions **/ /**********************************************************************************/ #define Connection_prepareStatement(conn, query) \ ej_CallObjectMethod(conn, \ ej_GetMethodIdObj(conn, "prepareStatement", "(Ljava/lang/String;)Ljava/sql/PreparedStatement;"),\ query) #define Statement_executeQuery(statement) \ ej_CallObjectMethod_(statement, \ ej_GetMethodIdObj(statement, "executeQuery","()Ljava/sql/ResultSet;")); #define Statement_close(statement) \ ej_CallVoidMethod_(statement, \ ej_GetMethodIdObj(statement, "close", EJ_NO_PARAMETERS_NO_RET)) #define ResultSet_next(resultset) \ ej_CallBooleanMethod_(resultset, \ ej_GetMethodIdObj(resultset, "next", EJ_NO_PARAMETERS_RET_BOOL)) #define ResultSet_wasNull(resultset) \ ej_CallBooleanMethod_(resultset, \ ej_GetMethodIdObj(resultset, "wasNull", EJ_NO_PARAMETERS_RET_BOOL)) #define ResultSet_getStringByName(resultset, colname) \ ej_CallObjectMethod(resultset, \ ej_GetMethodIdObj(resultset, "getString", EJ_STRING_RET_STRING), \ colname) #define ResultSet_getString(resultset, colnum) \ ej_CallObjectMethod(resultset, \ ej_GetMethodIdObj(resultset, "getString", EJ_INT_RET_STRING), \ colnum) #define ResultSet_close(resultset) \ ej_CallVoidMethod_(resultset, \ ej_GetMethodIdObj(resultset, "close", EJ_NO_PARAMETERS_NO_RET)) #endif