Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/vm/clr-jvm/jinvoker.cpp
38829 views
/*1* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940#include <memory.h>41#include <stdlib.h>42#include "jinvokerExp.h"4344static int g_nExitCode = 0;4546void system_exit(jint nCode){47g_nExitCode = nCode;48}4950/*51Allocating and providing the JVM init argumets.52By MakeJavaVMInitArgs() it is provided two options: providing CLASSPATH53environment variable value and function java.lang.System.exit()54redefinition in order to get the exit code.55See the description of the JNI API in56http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#wp950257*/5859int MakeJavaVMInitArgs( void** ppArgs ){6061int nOptSize = 2;62JavaVMInitArgs* pArgs = new JavaVMInitArgs();63JavaVMOption* pOptions = new JavaVMOption[nOptSize];6465//provide CLASSPATH value to java.class.path6667char* szClassPath = getenv("CLASSPATH");68if( szClassPath == NULL )69szClassPath = ".";7071pOptions[0].optionString = new char[strlen("-Djava.class.path=")+72strlen(szClassPath)+1];73sprintf( pOptions[0].optionString, "-Djava.class.path=%s", szClassPath );7475//redefine java.lang.System.exit()7677pOptions[1].optionString = "exit";78pOptions[1].extraInfo = system_exit;7980//Fill the arguments8182memset(pArgs, 0, sizeof(JavaVMInitArgs));83pArgs->version = 0x00010002;84pArgs->options = pOptions;85pArgs->nOptions = nOptSize;86pArgs->ignoreUnrecognized = JNI_TRUE;8788*ppArgs = pArgs;8990return 0;91}9293/*94Free the allocated JVM init argumets95*/9697void FreeJavaVMInitArgs( void* pArgs ){98delete ((JavaVMInitArgs*)pArgs)->options[0].optionString;99delete ((JavaVMInitArgs*)pArgs)->options;100delete pArgs;101}102103/*104Static wrapper on FindClass() JNI function.105See the description in106http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp16027107*/108109int FindClass( JNIEnv* pEnv,110const char* szClass,111jclass* pClass ){112113*pClass = pEnv->FindClass( szClass );114115if(pEnv->ExceptionCheck() == JNI_TRUE){116pEnv->ExceptionDescribe();117return -1;118}119if(*pClass != NULL)120return 0;121else122return -2;123124}125126/*127Static wrapper on GetStaticMethodID() JNI function.128See the description in129http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp20949130*/131132int GetStaticMethodID(JNIEnv* pEnv,133jclass pClass,134const char* szName,135const char* szArgs,136jmethodID* pMid){137138*pMid = pEnv->GetStaticMethodID( pClass, szName, szArgs);139140if(pEnv->ExceptionCheck() == JNI_TRUE){141pEnv->ExceptionDescribe();142return -1;143}144145if( *pMid != NULL )146return 0;147else148return -2;149}150151/*152Static wrapper on NewObjectArray() JNI function.153See the description in154http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp21619155*/156157int NewObjectArray( JNIEnv* pEnv,158int nDimension,159const char* szType,160jobjectArray* pArray ){161162*pArray = pEnv->NewObjectArray( nDimension, pEnv->FindClass( szType ), NULL);163164if(pEnv->ExceptionCheck() == JNI_TRUE){165pEnv->ExceptionDescribe();166return -1;167}168169if( pArray != NULL )170return 0;171else172return -2;173174}175176/*177Static wrapper on CallStaticVoidMethod() JNI function.178See the description in179http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp4796180*/181182int CallStaticVoidMethod( JNIEnv* pEnv,183jclass pClass,184jmethodID pMid,185void* pArgs){186187g_nExitCode = 0;188pEnv->CallStaticVoidMethod( pClass, pMid, pArgs);189if( pEnv->ExceptionCheck() == JNI_TRUE ){190pEnv->ExceptionDescribe();191return -1;192}else193return g_nExitCode;194}195196/*197Static wrapper on DestroyJavaVM() JNI function.198See the description in199http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#destroy_java_vm200*/201202int DestroyJavaVM( JavaVM* pJVM ){203pJVM->DestroyJavaVM();204return 0;205}206207208