Path: blob/master/src/java.base/share/native/libjava/jni_util.h
41149 views
/*1* Copyright (c) 1997, 2020, 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_ThrowInternalError(JNIEnv *env, const char *msg);6566JNIEXPORT void JNICALL67JNU_ThrowIOException(JNIEnv *env, const char *msg);6869JNIEXPORT void JNICALL70JNU_ThrowClassNotFoundException(JNIEnv *env, const char *msg);7172/* Throw an exception by name, using the string returned by73* getLastErrorString for the detail string. If the last-error74* string is NULL, use the given default detail string.75*/76JNIEXPORT void JNICALL77JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,78const char *defaultDetail);7980/* Throw an exception by name, using a given message and the string81* returned by getLastErrorString to construct the detail string.82*/83JNIEXPORT void JNICALL84JNU_ThrowByNameWithMessageAndLastError85(JNIEnv *env, const char *name, const char *message);8687/* Throw an IOException, using the last-error string for the detail88* string. If the last-error string is NULL, use the given default89* detail string.90*/91JNIEXPORT void JNICALL92JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail);9394/* Convert between Java strings and i18n C strings */95JNIEXPORT const char *96GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);9798JNIEXPORT jstring JNICALL99JNU_NewStringPlatform(JNIEnv *env, const char *str);100101JNIEXPORT const char * JNICALL102JNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy);103104JNIEXPORT void JNICALL105JNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str);106107/* Class constants */108JNIEXPORT jclass JNICALL109JNU_ClassString(JNIEnv *env);110111/* Copy count number of arguments from src to dst. Array bounds112* and ArrayStoreException are checked.113*/114JNIEXPORT jint JNICALL115JNU_CopyObjectArray(JNIEnv *env, jobjectArray dst, jobjectArray src,116jint count);117118/* Invoke a object-returning static method, based on class name,119* method name, and signature string.120*121* The caller should check for exceptions by setting hasException122* argument. If the caller is not interested in whether an exception123* has occurred, pass in NULL.124*/125JNIEXPORT jvalue JNICALL126JNU_CallStaticMethodByName(JNIEnv *env,127jboolean *hasException,128const char *class_name,129const char *name,130const char *signature,131...);132133/* Invoke an instance method by name.134*/135JNIEXPORT jvalue JNICALL136JNU_CallMethodByName(JNIEnv *env,137jboolean *hasException,138jobject obj,139const char *name,140const char *signature,141...);142143JNIEXPORT jvalue JNICALL144JNU_CallMethodByNameV(JNIEnv *env,145jboolean *hasException,146jobject obj,147const char *name,148const char *signature,149va_list args);150151/* Construct a new object of class, specifying the class by name,152* and specififying which constructor to run and what arguments to153* pass to it.154*155* The method will return an initialized instance if successful.156* It will return NULL if an error has occurred (for example if157* it ran out of memory) and the appropriate Java exception will158* have been thrown.159*/160JNIEXPORT jobject JNICALL161JNU_NewObjectByName(JNIEnv *env, const char *class_name,162const char *constructor_sig, ...);163164/* returns:165* 0: object is not an instance of the class named by classname.166* 1: object is an instance of the class named by classname.167* -1: the class named by classname cannot be found. An exception168* has been thrown.169*/170JNIEXPORT jint JNICALL171JNU_IsInstanceOfByName(JNIEnv *env, jobject object, const char *classname);172173174/* Get or set class and instance fields.175* Note that set functions take a variable number of arguments,176* but only one argument of the appropriate type can be passed.177* For example, to set an integer field i to 100:178*179* JNU_SetFieldByName(env, &exc, obj, "i", "I", 100);180*181* To set a float field f to 12.3:182*183* JNU_SetFieldByName(env, &exc, obj, "f", "F", 12.3);184*185* The caller should check for exceptions by setting hasException186* argument. If the caller is not interested in whether an exception187* has occurred, pass in NULL.188*/189JNIEXPORT jvalue JNICALL190JNU_GetFieldByName(JNIEnv *env,191jboolean *hasException,192jobject obj,193const char *name,194const char *sig);195JNIEXPORT void JNICALL196JNU_SetFieldByName(JNIEnv *env,197jboolean *hasException,198jobject obj,199const char *name,200const char *sig,201...);202203JNIEXPORT jvalue JNICALL204JNU_GetStaticFieldByName(JNIEnv *env,205jboolean *hasException,206const char *classname,207const char *name,208const char *sig);209210211/************************************************************************212* Miscellaneous utilities used by the class libraries213*/214215#define IS_NULL(obj) ((obj) == NULL)216#define JNU_IsNull(env,obj) ((obj) == NULL)217218/************************************************************************219* Miscellaneous utilities used by the class libraries to return from220* a function if a value is NULL or an exception is pending.221*/222223#define CHECK_NULL(x) \224do { \225if ((x) == NULL) { \226return; \227} \228} while (0) \229230#define CHECK_NULL_THROW_NPE(env, x, msg) \231do { \232if ((x) == NULL) { \233JNU_ThrowNullPointerException((env), (msg));\234return; \235} \236} while(0) \237238#define CHECK_NULL_THROW_NPE_RETURN(env, x, msg, z)\239do { \240if ((x) == NULL) { \241JNU_ThrowNullPointerException((env), (msg));\242return (z); \243} \244} while(0) \245246#define CHECK_NULL_RETURN(x, y) \247do { \248if ((x) == NULL) { \249return (y); \250} \251} while (0) \252253#ifdef __cplusplus254#define JNU_CHECK_EXCEPTION(env) \255do { \256if ((env)->ExceptionCheck()) { \257return; \258} \259} while (0) \260261#define JNU_CHECK_EXCEPTION_RETURN(env, y) \262do { \263if ((env)->ExceptionCheck()) { \264return (y); \265} \266} while (0)267#else268#define JNU_CHECK_EXCEPTION(env) \269do { \270if ((*env)->ExceptionCheck(env)) { \271return; \272} \273} while (0) \274275#define JNU_CHECK_EXCEPTION_RETURN(env, y) \276do { \277if ((*env)->ExceptionCheck(env)) { \278return (y); \279} \280} while (0)281#endif /* __cplusplus */282283/************************************************************************284* Debugging utilities285*/286287JNIEXPORT jstring JNICALL288JNU_ToString(JNIEnv *env, jobject object);289290291/*292* Package shorthand for use by native libraries293*/294#define JNU_JAVAPKG "java/lang/"295#define JNU_JAVAIOPKG "java/io/"296#define JNU_JAVANETPKG "java/net/"297298/*299* Check if the current thread is attached to the VM, and returns300* the JNIEnv of the specified version if the thread is attached.301*302* If the current thread is not attached, this function returns 0.303*304* If the current thread is attached, this function returns the305* JNI environment, or returns (void *)JNI_ERR if the specified306* version is not supported.307*/308JNIEXPORT void * JNICALL309JNU_GetEnv(JavaVM *vm, jint version);310311/*312* Warning free access to pointers stored in Java long fields.313*/314#define JNU_GetLongFieldAsPtr(env,obj,id) \315(jlong_to_ptr((*(env))->GetLongField((env),(obj),(id))))316#define JNU_SetLongFieldFromPtr(env,obj,id,val) \317(*(env))->SetLongField((env),(obj),(id),ptr_to_jlong(val))318319/*320* Internal use only.321*/322enum {323NO_ENCODING_YET = 0, /* "sun.jnu.encoding" not yet set */324NO_FAST_ENCODING, /* Platform encoding is not fast */325FAST_8859_1, /* ISO-8859-1 */326FAST_CP1252, /* MS-DOS Cp1252 */327FAST_646_US, /* US-ASCII : ISO646-US */328FAST_UTF_8329};330331JNIEXPORT void InitializeEncoding(JNIEnv *env, const char *name);332333void* getProcessHandle();334335void buildJniFunctionName(const char *sym, const char *cname,336char *jniEntryName);337338JNIEXPORT size_t JNICALL339getLastErrorString(char *buf, size_t len);340341JNIEXPORT int JNICALL342getErrorString(int err, char *buf, size_t len);343344#ifdef STATIC_BUILD345/* Macros for handling declaration of static/dynamic346* JNI library Load/Unload functions347*348* Use DEF_JNI_On{Un}Load when you want a static and non-static entry points.349* Use DEF_STATIC_JNI_On{Un}Load when you only want a static one.350*351* LIBRARY_NAME must be set to the name of the library352*/353354/* These three macros are needed to get proper concatenation of355* the LIBRARY_NAME356*357* NOTE: LIBRARY_NAME must be set for static builds.358*/359#define ADD_LIB_NAME3(name, lib) name ## lib360#define ADD_LIB_NAME2(name, lib) ADD_LIB_NAME3(name, lib)361#define ADD_LIB_NAME(entry) ADD_LIB_NAME2(entry, LIBRARY_NAME)362363#define DEF_JNI_OnLoad \364ADD_LIB_NAME(JNI_OnLoad_)(JavaVM *vm, void *reserved) \365{ \366jint JNICALL ADD_LIB_NAME(JNI_OnLoad_dynamic_)(JavaVM *vm, void *reserved); \367ADD_LIB_NAME(JNI_OnLoad_dynamic_)(vm, reserved); \368return JNI_VERSION_1_8; \369} \370jint JNICALL ADD_LIB_NAME(JNI_OnLoad_dynamic_)371372#define DEF_STATIC_JNI_OnLoad \373JNIEXPORT jint JNICALL ADD_LIB_NAME(JNI_OnLoad_)(JavaVM *vm, void *reserved) { \374return JNI_VERSION_1_8; \375}376377#define DEF_JNI_OnUnload \378ADD_LIB_NAME(JNI_OnUnload_)(JavaVM *vm, void *reserved) \379{ \380void JNICALL ADD_LIB_NAME(JNI_OnUnload_dynamic_)(JavaVM *vm, void *reserved); \381ADD_LIB_NAME(JNI_OnUnload_dynamic_)(vm, reserved); \382} \383void JNICALL ADD_LIB_NAME(JNI_OnUnload_dynamic_)384385#define DEF_STATIC_JNI_OnUnload \386ADD_LIB_NAME(JNI_OnUnload_)387388#else389390#define DEF_JNI_OnLoad JNI_OnLoad391#define DEF_STATIC_JNI_OnLoad392#define DEF_JNI_OnUnload JNI_OnUnload393#define DEF_STATIC_JNI_OnUnload394#endif395396#ifdef STATIC_BUILD397/* Macros for handling declaration of static/dynamic398* Agent library Load/Attach/Unload functions399*400* Use DEF_Agent_OnLoad, DEF_Agent_OnAttach or DEF_Agent_OnUnload401* when you want both static and non-static entry points.402* Use DEF_STATIC_Agent_OnLoad, DEF_STATIC_Agent_OnAttach or403* DEF_STATIC_Agent_OnUnload when you only want a static one.404*405* LIBRARY_NAME must be set to the name of the library for static builds.406*/407408#define DEF_Agent_OnLoad \409ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) \410{ \411jint JNICALL ADD_LIB_NAME(Agent_OnLoad_dynamic_)(JavaVM *vm, char *options, void *reserved); \412return ADD_LIB_NAME(Agent_OnLoad_dynamic_)(vm, options, reserved); \413} \414jint JNICALL ADD_LIB_NAME(Agent_OnLoad_dynamic_)415416#define DEF_STATIC_Agent_OnLoad \417JNIEXPORT jint JNICALL ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) { \418return JNI_FALSE; \419}420421#define DEF_Agent_OnAttach \422ADD_LIB_NAME(Agent_OnAttach_)(JavaVM *vm, char *options, void *reserved) \423{ \424jint JNICALL ADD_LIB_NAME(Agent_OnAttach_dynamic_)(JavaVM *vm, char *options, void *reserved); \425return ADD_LIB_NAME(Agent_OnAttach_dynamic_)(vm, options, reserved); \426} \427jint JNICALL ADD_LIB_NAME(Agent_OnAttach_dynamic_)428429#define DEF_STATIC_Agent_OnAttach \430JNIEXPORT jint JNICALL ADD_LIB_NAME(Agent_OnLoad_)(JavaVM *vm, char *options, void *reserved) { \431return JNI_FALSE; \432}433434#define DEF_Agent_OnUnload \435ADD_LIB_NAME(Agent_OnUnload_)(JavaVM *vm) \436{ \437void JNICALL ADD_LIB_NAME(Agent_OnUnload_dynamic_)(JavaVM *vm); \438ADD_LIB_NAME(Agent_OnUnload_dynamic_)(vm); \439} \440void JNICALL ADD_LIB_NAME(Agent_OnUnload_dynamic_)441442#define DEF_STATIC_Agent_OnUnload \443ADD_LIB_NAME(Agent_OnUnload_)444445#else446#define DEF_Agent_OnLoad Agent_OnLoad447#define DEF_Agent_OnAttach Agent_OnAttach448#define DEF_Agent_OnUnload Agent_OnUnload449#define DEF_STATIC_Agent_OnLoad450#define DEF_STATIC_Agent_OnAttach451#define DEF_STATIC_Agent_OnUnload452#endif453454#ifdef __cplusplus455} /* extern "C" */456#endif /* __cplusplus */457458#endif /* JNI_UTIL_H */459460461