Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/sample/vm/clr-jvm/jinvoker.cpp
38829 views
1
/*
2
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
*
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
*
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* - Neither the name of Oracle nor the names of its
16
* contributors may be used to endorse or promote products derived
17
* from this software without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/*
33
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
#include <memory.h>
42
#include <stdlib.h>
43
#include "jinvokerExp.h"
44
45
static int g_nExitCode = 0;
46
47
void system_exit(jint nCode){
48
g_nExitCode = nCode;
49
}
50
51
/*
52
Allocating and providing the JVM init argumets.
53
By MakeJavaVMInitArgs() it is provided two options: providing CLASSPATH
54
environment variable value and function java.lang.System.exit()
55
redefinition in order to get the exit code.
56
See the description of the JNI API in
57
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#wp9502
58
*/
59
60
int MakeJavaVMInitArgs( void** ppArgs ){
61
62
int nOptSize = 2;
63
JavaVMInitArgs* pArgs = new JavaVMInitArgs();
64
JavaVMOption* pOptions = new JavaVMOption[nOptSize];
65
66
//provide CLASSPATH value to java.class.path
67
68
char* szClassPath = getenv("CLASSPATH");
69
if( szClassPath == NULL )
70
szClassPath = ".";
71
72
pOptions[0].optionString = new char[strlen("-Djava.class.path=")+
73
strlen(szClassPath)+1];
74
sprintf( pOptions[0].optionString, "-Djava.class.path=%s", szClassPath );
75
76
//redefine java.lang.System.exit()
77
78
pOptions[1].optionString = "exit";
79
pOptions[1].extraInfo = system_exit;
80
81
//Fill the arguments
82
83
memset(pArgs, 0, sizeof(JavaVMInitArgs));
84
pArgs->version = 0x00010002;
85
pArgs->options = pOptions;
86
pArgs->nOptions = nOptSize;
87
pArgs->ignoreUnrecognized = JNI_TRUE;
88
89
*ppArgs = pArgs;
90
91
return 0;
92
}
93
94
/*
95
Free the allocated JVM init argumets
96
*/
97
98
void FreeJavaVMInitArgs( void* pArgs ){
99
delete ((JavaVMInitArgs*)pArgs)->options[0].optionString;
100
delete ((JavaVMInitArgs*)pArgs)->options;
101
delete pArgs;
102
}
103
104
/*
105
Static wrapper on FindClass() JNI function.
106
See the description in
107
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp16027
108
*/
109
110
int FindClass( JNIEnv* pEnv,
111
const char* szClass,
112
jclass* pClass ){
113
114
*pClass = pEnv->FindClass( szClass );
115
116
if(pEnv->ExceptionCheck() == JNI_TRUE){
117
pEnv->ExceptionDescribe();
118
return -1;
119
}
120
if(*pClass != NULL)
121
return 0;
122
else
123
return -2;
124
125
}
126
127
/*
128
Static wrapper on GetStaticMethodID() JNI function.
129
See the description in
130
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp20949
131
*/
132
133
int GetStaticMethodID(JNIEnv* pEnv,
134
jclass pClass,
135
const char* szName,
136
const char* szArgs,
137
jmethodID* pMid){
138
139
*pMid = pEnv->GetStaticMethodID( pClass, szName, szArgs);
140
141
if(pEnv->ExceptionCheck() == JNI_TRUE){
142
pEnv->ExceptionDescribe();
143
return -1;
144
}
145
146
if( *pMid != NULL )
147
return 0;
148
else
149
return -2;
150
}
151
152
/*
153
Static wrapper on NewObjectArray() JNI function.
154
See the description in
155
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp21619
156
*/
157
158
int NewObjectArray( JNIEnv* pEnv,
159
int nDimension,
160
const char* szType,
161
jobjectArray* pArray ){
162
163
*pArray = pEnv->NewObjectArray( nDimension, pEnv->FindClass( szType ), NULL);
164
165
if(pEnv->ExceptionCheck() == JNI_TRUE){
166
pEnv->ExceptionDescribe();
167
return -1;
168
}
169
170
if( pArray != NULL )
171
return 0;
172
else
173
return -2;
174
175
}
176
177
/*
178
Static wrapper on CallStaticVoidMethod() JNI function.
179
See the description in
180
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/functions.html#wp4796
181
*/
182
183
int CallStaticVoidMethod( JNIEnv* pEnv,
184
jclass pClass,
185
jmethodID pMid,
186
void* pArgs){
187
188
g_nExitCode = 0;
189
pEnv->CallStaticVoidMethod( pClass, pMid, pArgs);
190
if( pEnv->ExceptionCheck() == JNI_TRUE ){
191
pEnv->ExceptionDescribe();
192
return -1;
193
}else
194
return g_nExitCode;
195
}
196
197
/*
198
Static wrapper on DestroyJavaVM() JNI function.
199
See the description in
200
http://jre.sfbay/java/re/jdk/6/promoted/latest/docs/technotes/guides/jni/spec/invocation.html#destroy_java_vm
201
*/
202
203
int DestroyJavaVM( JavaVM* pJVM ){
204
pJVM->DestroyJavaVM();
205
return 0;
206
}
207
208