jni_util.h revision 0
3020N/A/*
3020N/A * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
3020N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3020N/A *
3020N/A * This code is free software; you can redistribute it and/or modify it
3020N/A * under the terms of the GNU General Public License version 2 only, as
3020N/A * published by the Free Software Foundation. Sun designates this
3020N/A * particular file as subject to the "Classpath" exception as provided
3020N/A * by Sun in the LICENSE file that accompanied this code.
3020N/A *
3020N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3020N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3020N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3020N/A * version 2 for more details (a copy is included in the LICENSE file that
3020N/A * accompanied this code).
3020N/A *
3020N/A * You should have received a copy of the GNU General Public License version
3020N/A * 2 along with this work; if not, write to the Free Software Foundation,
3020N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3020N/A *
3020N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3020N/A * CA 95054 USA or visit www.sun.com if you need additional information or
3020N/A * have any questions.
3020N/A */
3020N/A
3020N/A#ifndef JNI_UTIL_H
3020N/A#define JNI_UTIL_H
3020N/A
3020N/A#include "jni.h"
3020N/A#include "jlong.h"
3020N/A
3020N/A#ifdef __cplusplus
3020N/Aextern "C" {
3020N/A#endif
3020N/A
3020N/A/*
3020N/A * This file contains utility functions that can be implemented in pure JNI.
3020N/A *
3020N/A * Caution: Callers of functions declared in this file should be
3020N/A * particularly aware of the fact that these functions are convenience
3020N/A * functions, and as such are often compound operations, each one of
3020N/A * which may throw an exception. Therefore, the functions this file
3020N/A * will often return silently if an exception has occured, and callers
3020N/A * must check for exception themselves.
3020N/A */
3020N/A
3020N/A/* Throw a Java exception by name. Similar to SignalError. */
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowByName(JNIEnv *env, const char *name, const char *msg);
3020N/A
3020N/A/* Throw common exceptions */
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNullPointerException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowArrayIndexOutOfBoundsException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowOutOfMemoryError(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowIllegalArgumentException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowIllegalAccessError(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowIllegalAccessException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowInternalError(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowIOException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNoSuchFieldException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNoSuchMethodException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowClassNotFoundException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNumberFormatException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNoSuchFieldError(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowInstantiationException(JNIEnv *env, const char *msg);
3020N/A
3020N/A/* Throw an exception by name, using the string returned by
3020N/A * JVM_LastErrorString for the detail string. If the last-error
3020N/A * string is NULL, use the given default detail string.
3020N/A */
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
3020N/A const char *defaultMessage);
3020N/A
3020N/A/* Throw an IOException, using the last-error string for the detail
3020N/A * string. If the last-error string is NULL, use the given default
3020N/A * detail string.
3020N/A */
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail);
3020N/A
3020N/A/* Convert between Java strings and i18n C strings */
3020N/AJNIEXPORT jstring
3020N/ANewStringPlatform(JNIEnv *env, const char *str);
3020N/A
3020N/AJNIEXPORT const char *
3020N/AGetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);
3020N/A
3020N/AJNIEXPORT jstring JNICALL
3020N/AJNU_NewStringPlatform(JNIEnv *env, const char *str);
3020N/A
3020N/AJNIEXPORT const char * JNICALL
3020N/AJNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);
3020N/A
3020N/AJNIEXPORT void JNICALL
3020N/AJNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str);
3020N/A
3020N/A/* Class constants */
3020N/AJNIEXPORT jclass JNICALL
3020N/AJNU_ClassString(JNIEnv *env);
3020N/A
3020N/AJNIEXPORT jclass JNICALL
3020N/AJNU_ClassClass(JNIEnv *env);
3020N/A
3020N/AJNIEXPORT jclass JNICALL
3020N/AJNU_ClassObject(JNIEnv *env);
3020N/A
3020N/AJNIEXPORT jclass JNICALL
3020N/AJNU_ClassThrowable(JNIEnv *env);
3020N/A
3020N/A/* Copy count number of arguments from src to dst. Array bounds
3020N/A * and ArrayStoreException are checked.
3020N/A */
3020N/AJNIEXPORT jint JNICALL
3020N/AJNU_CopyObjectArray(JNIEnv *env, jobjectArray dst, jobjectArray src,
3020N/A jint count);
3020N/A
3020N/A/* Invoke a object-returning static method, based on class name,
3020N/A * method name, and signature string.
3020N/A *
3020N/A * The caller should check for exceptions by setting hasException
3020N/A * argument. If the caller is not interested in whether an exception
3020N/A * has occurred, pass in NULL.
3020N/A */
3020N/AJNIEXPORT jvalue JNICALL
3020N/AJNU_CallStaticMethodByName(JNIEnv *env,
3020N/A jboolean *hasException,
3020N/A const char *class_name,
3020N/A const char *name,
3020N/A const char *signature,
3020N/A ...);
3020N/A
3020N/A/* Invoke an instance method by name.
3020N/A */
3020N/AJNIEXPORT jvalue JNICALL
3020N/AJNU_CallMethodByName(JNIEnv *env,
3020N/A jboolean *hasException,
3020N/A jobject obj,
3020N/A const char *name,
3020N/A const char *signature,
3020N/A ...);
3020N/A
3020N/AJNIEXPORT jvalue JNICALL
3020N/AJNU_CallMethodByNameV(JNIEnv *env,
jboolean *hasException,
jobject obj,
const char *name,
const char *signature,
va_list args);
/* Construct a new object of class, specifying the class by name,
* and specififying which constructor to run and what arguments to
* pass to it.
*
* The method will return an initialized instance if successful.
* It will return NULL if an error has occured (for example if
* it ran out of memory) and the appropriate Java exception will
* have been thrown.
*/
JNIEXPORT jobject JNICALL
JNU_NewObjectByName(JNIEnv *env, const char *class_name,
const char *constructor_sig, ...);
/* returns:
* 0: object is not an instance of the class named by classname.
* 1: object is an instance of the class named by classname.
* -1: the class named by classname cannot be found. An exception
* has been thrown.
*/
JNIEXPORT jint JNICALL
JNU_IsInstanceOfByName(JNIEnv *env, jobject object, char *classname);
/* Get or set class and instance fields.
* Note that set functions take a variable number of arguments,
* but only one argument of the appropriate type can be passed.
* For example, to set an integer field i to 100:
*
* JNU_SetFieldByName(env, &exc, obj, "i", "I", 100);
*
* To set a float field f to 12.3:
*
* JNU_SetFieldByName(env, &exc, obj, "f", "F", 12.3);
*
* The caller should check for exceptions by setting hasException
* argument. If the caller is not interested in whether an exception
* has occurred, pass in NULL.
*/
JNIEXPORT jvalue JNICALL
JNU_GetFieldByName(JNIEnv *env,
jboolean *hasException,
jobject obj,
const char *name,
const char *sig);
JNIEXPORT void JNICALL
JNU_SetFieldByName(JNIEnv *env,
jboolean *hasException,
jobject obj,
const char *name,
const char *sig,
...);
JNIEXPORT jvalue JNICALL
JNU_GetStaticFieldByName(JNIEnv *env,
jboolean *hasException,
const char *classname,
const char *name,
const char *sig);
JNIEXPORT void JNICALL
JNU_SetStaticFieldByName(JNIEnv *env,
jboolean *hasException,
const char *classname,
const char *name,
const char *sig,
...);
/*
* Calls the .equals method.
*/
JNIEXPORT jboolean JNICALL
JNU_Equals(JNIEnv *env, jobject object1, jobject object2);
/************************************************************************
* Thread calls
*
* Convenience thread-related calls on the java.lang.Object class.
*/
JNIEXPORT void JNICALL
JNU_MonitorWait(JNIEnv *env, jobject object, jlong timeout);
JNIEXPORT void JNICALL
JNU_Notify(JNIEnv *env, jobject object);
JNIEXPORT void JNICALL
JNU_NotifyAll(JNIEnv *env, jobject object);
/************************************************************************
* Miscellaneous utilities used by the class libraries
*/
#define IS_NULL(obj) ((obj) == NULL)
#define JNU_IsNull(env,obj) ((obj) == NULL)
/************************************************************************
* Debugging utilities
*/
JNIEXPORT void JNICALL
JNU_PrintString(JNIEnv *env, char *hdr, jstring string);
JNIEXPORT void JNICALL
JNU_PrintClass(JNIEnv *env, char *hdr, jobject object);
JNIEXPORT jstring JNICALL
JNU_ToString(JNIEnv *env, jobject object);
/*
* Package shorthand for use by native libraries
*/
#define JNU_JAVAPKG "java/lang/"
#define JNU_JAVAIOPKG "java/io/"
#define JNU_JAVANETPKG "java/net/"
/*
* Check if the current thread is attached to the VM, and returns
* the JNIEnv of the specified version if the thread is attached.
*
* If the current thread is not attached, this function returns 0.
*
* If the current thread is attached, this function returns the
* JNI environment, or returns (void *)JNI_ERR if the specified
* version is not supported.
*/
JNIEXPORT void * JNICALL
JNU_GetEnv(JavaVM *vm, jint version);
/*
* Warning free access to pointers stored in Java long fields.
*/
#define JNU_GetLongFieldAsPtr(env,obj,id) \
(jlong_to_ptr((*(env))->GetLongField((env),(obj),(id))))
#define JNU_SetLongFieldFromPtr(env,obj,id,val) \
(*(env))->SetLongField((env),(obj),(id),ptr_to_jlong(val))
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif /* JNI_UTIL_H */