Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/jni_util.h
38825 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifndef JNI_UTIL_H26#define JNI_UTIL_H2728#include "jni.h"29#include "jlong.h"3031#ifdef __cplusplus32extern "C" {33#endif3435/*36* This file contains utility functions that can be implemented in pure JNI.37*38* Caution: Callers of functions declared in this file should be39* particularly aware of the fact that these functions are convenience40* functions, and as such are often compound operations, each one of41* which may throw an exception. Therefore, the functions this file42* will often return silently if an exception has occurred, and callers43* must check for exception themselves.44*/4546/* Throw a Java exception by name. Similar to SignalError. */47JNIEXPORT void JNICALL48JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg);4950/* Throw common exceptions */51JNIEXPORT void JNICALL52JNU_ThrowNullPointerException(JNIEnv *env, const char *msg);5354JNIEXPORT void JNICALL55JNU_ThrowArrayIndexOutOfBoundsException(JNIEnv *env, const char *msg);5657JNIEXPORT void JNICALL58JNU_ThrowOutOfMemoryError(JNIEnv *env, const char *msg);5960JNIEXPORT void JNICALL61JNU_ThrowIllegalArgumentException(JNIEnv *env, const char *msg);6263JNIEXPORT void JNICALL64JNU_ThrowIllegalAccessError(JNIEnv *env, const char *msg);6566JNIEXPORT void JNICALL67JNU_ThrowIllegalAccessException(JNIEnv *env, const char *msg);6869JNIEXPORT void JNICALL70JNU_ThrowInternalError(JNIEnv *env, const char *msg);7172JNIEXPORT void JNICALL73JNU_ThrowIOException(JNIEnv *env, const char *msg);7475JNIEXPORT void JNICALL76JNU_ThrowNoSuchFieldException(JNIEnv *env, const char *msg);7778JNIEXPORT void JNICALL79JNU_ThrowNoSuchMethodException(JNIEnv *env, const char *msg);8081JNIEXPORT void JNICALL82JNU_ThrowClassNotFoundException(JNIEnv *env, const char *msg);8384JNIEXPORT void JNICALL85JNU_ThrowNumberFormatException(JNIEnv *env, const char *msg);8687JNIEXPORT void JNICALL88JNU_ThrowNoSuchFieldError(JNIEnv *env, const char *msg);8990JNIEXPORT void JNICALL91JNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg);9293JNIEXPORT void JNICALL94JNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg);9596JNIEXPORT void JNICALL97JNU_ThrowInstantiationException(JNIEnv *env, const char *msg);9899/* Throw an exception by name, using the string returned by100* JVM_LastErrorString for the detail string. If the last-error101* string is NULL, use the given default detail string.102*/103JNIEXPORT void JNICALL104JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,105const char *defaultMessage);106107/* Throw an exception by name, using a given message and the string108* returned by getLastErrorString to construct the detail string.109*/110JNIEXPORT void JNICALL111JNU_ThrowByNameWithMessageAndLastError112(JNIEnv *env, const char *name, const char *message);113114/* Throw an IOException, using the last-error string for the detail115* string. If the last-error string is NULL, use the given default116* detail string.117*/118JNIEXPORT void JNICALL119JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail);120121/* Convert between Java strings and i18n C strings */122JNIEXPORT jstring123NewStringPlatform(JNIEnv *env, const char *str);124125JNIEXPORT const char *126GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);127128JNIEXPORT jstring JNICALL129JNU_NewStringPlatform(JNIEnv *env, const char *str);130131JNIEXPORT const char * JNICALL132JNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);133134JNIEXPORT void JNICALL135JNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str);136137/* Class constants */138JNIEXPORT jclass JNICALL139JNU_ClassString(JNIEnv *env);140141JNIEXPORT jclass JNICALL142JNU_ClassClass(JNIEnv *env);143144JNIEXPORT jclass JNICALL145JNU_ClassObject(JNIEnv *env);146147JNIEXPORT jclass JNICALL148JNU_ClassThrowable(JNIEnv *env);149150/* Copy count number of arguments from src to dst. Array bounds151* and ArrayStoreException are checked.152*/153JNIEXPORT jint JNICALL154JNU_CopyObjectArray(JNIEnv *env, jobjectArray dst, jobjectArray src,155jint count);156157/* Invoke a object-returning static method, based on class name,158* method name, and signature string.159*160* The caller should check for exceptions by setting hasException161* argument. If the caller is not interested in whether an exception162* has occurred, pass in NULL.163*/164JNIEXPORT jvalue JNICALL165JNU_CallStaticMethodByName(JNIEnv *env,166jboolean *hasException,167const char *class_name,168const char *name,169const char *signature,170...);171172/* Invoke an instance method by name.173*/174JNIEXPORT jvalue JNICALL175JNU_CallMethodByName(JNIEnv *env,176jboolean *hasException,177jobject obj,178const char *name,179const char *signature,180...);181182JNIEXPORT jvalue JNICALL183JNU_CallMethodByNameV(JNIEnv *env,184jboolean *hasException,185jobject obj,186const char *name,187const char *signature,188va_list args);189190/* Construct a new object of class, specifying the class by name,191* and specififying which constructor to run and what arguments to192* pass to it.193*194* The method will return an initialized instance if successful.195* It will return NULL if an error has occurred (for example if196* it ran out of memory) and the appropriate Java exception will197* have been thrown.198*/199JNIEXPORT jobject JNICALL200JNU_NewObjectByName(JNIEnv *env, const char *class_name,201const char *constructor_sig, ...);202203/* returns:204* 0: object is not an instance of the class named by classname.205* 1: object is an instance of the class named by classname.206* -1: the class named by classname cannot be found. An exception207* has been thrown.208*/209JNIEXPORT jint JNICALL210JNU_IsInstanceOfByName(JNIEnv *env, jobject object, char *classname);211212213/* Get or set class and instance fields.214* Note that set functions take a variable number of arguments,215* but only one argument of the appropriate type can be passed.216* For example, to set an integer field i to 100:217*218* JNU_SetFieldByName(env, &exc, obj, "i", "I", 100);219*220* To set a float field f to 12.3:221*222* JNU_SetFieldByName(env, &exc, obj, "f", "F", 12.3);223*224* The caller should check for exceptions by setting hasException225* argument. If the caller is not interested in whether an exception226* has occurred, pass in NULL.227*/228JNIEXPORT jvalue JNICALL229JNU_GetFieldByName(JNIEnv *env,230jboolean *hasException,231jobject obj,232const char *name,233const char *sig);234JNIEXPORT void JNICALL235JNU_SetFieldByName(JNIEnv *env,236jboolean *hasException,237jobject obj,238const char *name,239const char *sig,240...);241242JNIEXPORT jvalue JNICALL243JNU_GetStaticFieldByName(JNIEnv *env,244jboolean *hasException,245const char *classname,246const char *name,247const char *sig);248JNIEXPORT void JNICALL249JNU_SetStaticFieldByName(JNIEnv *env,250jboolean *hasException,251const char *classname,252const char *name,253const char *sig,254...);255256257/*258* Calls the .equals method.259*/260JNIEXPORT jboolean JNICALL261JNU_Equals(JNIEnv *env, jobject object1, jobject object2);262263264/************************************************************************265* Thread calls266*267* Convenience thread-related calls on the java.lang.Object class.268*/269270JNIEXPORT void JNICALL271JNU_MonitorWait(JNIEnv *env, jobject object, jlong timeout);272273JNIEXPORT void JNICALL274JNU_Notify(JNIEnv *env, jobject object);275276JNIEXPORT void JNICALL277JNU_NotifyAll(JNIEnv *env, jobject object);278279280/************************************************************************281* Miscellaneous utilities used by the class libraries282*/283284#define IS_NULL(obj) ((obj) == NULL)285#define JNU_IsNull(env,obj) ((obj) == NULL)286287/************************************************************************288* Miscellaneous utilities used by the class libraries to return from289* a function if a value is NULL or an exception is pending.290*/291292#define CHECK_NULL(x) \293do { \294if ((x) == NULL) { \295return; \296} \297} while (0) \298299#define CHECK_NULL_THROW_NPE(env, x, msg) \300do { \301if ((x) == NULL) { \302JNU_ThrowNullPointerException((env), (msg));\303return; \304} \305} while(0) \306307#define CHECK_NULL_THROW_NPE_RETURN(env, x, msg, z)\308do { \309if ((x) == NULL) { \310JNU_ThrowNullPointerException((env), (msg));\311return (z); \312} \313} while(0) \314315#define CHECK_NULL_RETURN(x, y) \316do { \317if ((x) == NULL) { \318return (y); \319} \320} while (0) \321322#ifdef __cplusplus323#define JNU_CHECK_EXCEPTION(env) \324do { \325if ((env)->ExceptionCheck()) { \326return; \327} \328} while (0) \329330#define JNU_CHECK_EXCEPTION_RETURN(env, y) \331do { \332if ((env)->ExceptionCheck()) { \333return (y); \334} \335} while (0)336#else337#define JNU_CHECK_EXCEPTION(env) \338do { \339if ((*env)->ExceptionCheck(env)) { \340return; \341} \342} while (0) \343344#define JNU_CHECK_EXCEPTION_RETURN(env, y) \345do { \346if ((*env)->ExceptionCheck(env)) { \347return (y); \348} \349} while (0)350#endif /* __cplusplus */351/************************************************************************352* Debugging utilities353*/354355JNIEXPORT void JNICALL356JNU_PrintString(JNIEnv *env, char *hdr, jstring string);357358JNIEXPORT void JNICALL359JNU_PrintClass(JNIEnv *env, char *hdr, jobject object);360361JNIEXPORT jstring JNICALL362JNU_ToString(JNIEnv *env, jobject object);363364/*365* Package shorthand for use by native libraries366*/367#define JNU_JAVAPKG "java/lang/"368#define JNU_JAVAIOPKG "java/io/"369#define JNU_JAVANETPKG "java/net/"370371/*372* Check if the current thread is attached to the VM, and returns373* the JNIEnv of the specified version if the thread is attached.374*375* If the current thread is not attached, this function returns 0.376*377* If the current thread is attached, this function returns the378* JNI environment, or returns (void *)JNI_ERR if the specified379* version is not supported.380*/381JNIEXPORT void * JNICALL382JNU_GetEnv(JavaVM *vm, jint version);383384/*385* Warning free access to pointers stored in Java long fields.386*/387#define JNU_GetLongFieldAsPtr(env,obj,id) \388(jlong_to_ptr((*(env))->GetLongField((env),(obj),(id))))389#define JNU_SetLongFieldFromPtr(env,obj,id,val) \390(*(env))->SetLongField((env),(obj),(id),ptr_to_jlong(val))391392/*393* Internal use only.394*/395enum {396NO_ENCODING_YET = 0, /* "sun.jnu.encoding" not yet set */397NO_FAST_ENCODING, /* Platform encoding is not fast */398FAST_8859_1, /* ISO-8859-1 */399FAST_CP1252, /* MS-DOS Cp1252 */400FAST_646_US /* US-ASCII : ISO646-US */401};402403jstring nativeNewStringPlatform(JNIEnv *env, const char *str);404405char* nativeGetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);406407int getFastEncoding();408409void initializeEncoding();410411void* getProcessHandle();412413void buildJniFunctionName(const char *sym, const char *cname,414char *jniEntryName);415416extern int getErrorString(int err, char *buf, size_t len);417#ifdef __cplusplus418} /* extern "C" */419#endif /* __cplusplus */420421#endif /* JNI_UTIL_H */422423424