FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java4cpp/examples/simple/simple0.cpp at java4cpp-1.0 · wshackle/java4cpp · GitHub
wshackle
/
java4cpp
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
9
Code
Issues
1
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
java4cpp
/
examples
/
simple
/
simple0.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
461 lines (414 loc) · 15.8 KB
Breadcrumbs
java4cpp
/
examples
/
simple
/
simple0.cpp
Copy path
File metadata and controls
461 lines (414 loc) · 15.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#
include
<
jni.h
>
#
include
<
cstdlib
>
#
include
<
iostream
>
#
include
<
string
>
#
include
"
simple.h
"
namespace
simple
{
JNIEnv *
getEnv
();
static
bool
debug_j4cpp =
false
;
namespace
java
{
namespace
lang
{
Object::Object
() {
jthis =
NULL
;
};
Object::Object
(
const
Object &objref) {
jobject _jthis = objref.
jthis
;
if
(_jthis !=
NULL
) {
jthis =
getEnv
()->
NewGlobalRef
(_jthis);
}
};
Object::Object
(jobject _jthis,
bool
copy) {
//
std::cout << "_jthis = " << _jthis << std::endl;
jthis =
NULL
;
if
(_jthis !=
NULL
) {
JNIEnv *env =
getEnv
();
jobjectRefType ref0 = env->
GetObjectRefType
(jthis);
if
(copy || ref0 != JNIGlobalRefType) {
jthis = env->
NewGlobalRef
(_jthis);
}
}
};
Object & Object::
operator
=(
const
Object &otherobject) {
jobject oldjthis = jthis;
jobject _newjthis = otherobject.
jthis
;
jobject newjthis=
NULL
;
jthis=
NULL
;
JNIEnv *env =
getEnv
();
if
(_newjthis !=
NULL
) {
if
(debug_j4cpp)
DebugPrintJObject
(__FILE__,__LINE__,
"
Object::operator= called for _newjthis=
"
,_newjthis);
newjthis = env->
NewGlobalRef
(_newjthis);
}
if
(
NULL
!= oldjthis) {
if
(debug_j4cpp)
DebugPrintJObject
(__FILE__,__LINE__,
"
Object::operator= called for oldjthis=
"
,oldjthis);
env->
DeleteGlobalRef
(oldjthis);
oldjthis =
NULL
;
}
jthis=newjthis;
releaseEnv
(env);
return
*
this
;
}
Object::~Object
() {
if
(
NULL
!= jthis) {
if
(debug_j4cpp)
DebugPrintJObject
(__FILE__,__LINE__,
"
Destructor called for
"
,jthis);
getEnv
()->
DeleteGlobalRef
(jthis);
jthis =
NULL
;
}
};
jstring
Object::toString
() {
if
(jthis ==
NULL
) {
std::cerr <<
"
Call of method toString of java.lang.Object with jthis == NULL.
"
<< std::endl;
return
NULL
;
}
JNIEnv *env =
getEnv
();
jobjectRefType ref0 = env->
GetObjectRefType
(jthis);
if
(debug_j4cpp)
DebugPrintJObject
(__FILE__,__LINE__,
"
Object::toString() jthis=
"
,jthis);
jclass cls = env->
GetObjectClass
(jthis);
jstring retVal =
NULL
;
if
(cls !=
NULL
) {
jmethodID mid = env->
GetMethodID
(cls,
"
toString
"
,
"
()Ljava/lang/String;
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class ava.lang.Object has no method named toString with signature ()Ljava/lang/String;.
"
<< std::endl;
return
NULL
;
}
else
{
retVal = (jstring) env->
CallObjectMethod
(jthis, mid);
}
}
return
retVal;
}
}
}
//
get JNI handle for class Simple
static
inline
jclass
getSimpleClass
();
Simple::Simple
(jobject _jthis,
bool
copy): java::lang::Object(_jthis,copy) {
//
Place holder for future extensibility
}
Simple::Simple
(
const
Simple &objref): java::lang::Object((jobject)
NULL
,
false
) {
jobject _jthis = objref.
jthis
;
if
(
GetDebugJ4Cpp
())
DebugPrintJObject
(__FILE__,__LINE__,
"
Copy Constructor for class Simple _jthis=
"
,_jthis);
if
(_jthis !=
NULL
) {
jthis =
getEnv
()->
NewGlobalRef
(_jthis);
if
(
GetDebugJ4Cpp
())
DebugPrintJObject
(__FILE__,__LINE__,
"
Copy Constructor for class Simple jthis=
"
,jthis);
}
}
Simple
Simple::cast
(
const
java::lang::Object &objref) {
JNIEnv *env =
getEnv
();
static
jclass cls =
getSimpleClass
();
jclass objcls = env->
GetObjectClass
(objref.
jthis
);
if
(!env->
IsAssignableFrom
(objcls,cls)) {
throw
objcls;
}
Simple
retVal
(objref.
jthis
,
true
);
return
retVal;
}
Simple::Simple
() : java::lang::Object((jobject)
NULL
,
false
) {
JNIEnv *env =
getEnv
();
static
jclass cls =
getSimpleClass
();
if
(cls !=
NULL
) {
static
jmethodID mid = env->
GetMethodID
(cls,
"
<init>
"
,
"
()V
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class Simple has no method constructor signature ()V
"
<< std::endl;
}
else
{
jthis = env->
NewObject
(cls, mid );
jthrowable t = env->
ExceptionOccurred
();
if
(t !=
NULL
) {
if
(
GetDebugJ4Cpp
()) {
DebugPrintJObject
(__FILE__,__LINE__,
"
Simple::%METHOD_NAME% jthis=
"
,t);
env->
ExceptionDescribe
();
}
throw
t;
}
if
(jthis ==
NULL
) {
std::cerr <<
"
Call to create new Simple with signature ()V returned null.
"
<< std::endl;
releaseEnv
(env);
return
;
}
jobjectRefType ref = env->
GetObjectRefType
(jthis);
if
(
GetDebugJ4Cpp
())
DebugPrintJObject
(__FILE__,__LINE__,
"
new Simple jthis=
"
,jthis);
if
(ref != JNIGlobalRefType) {
jthis = env->
NewGlobalRef
(jthis);
}
}
}
releaseEnv
(env);
}
Simple::Simple
(jint int_0) : java::lang::Object((jobject)
NULL
,
false
) {
JNIEnv *env =
getEnv
();
static
jclass cls =
getSimpleClass
();
if
(cls !=
NULL
) {
static
jmethodID mid = env->
GetMethodID
(cls,
"
<init>
"
,
"
(I)V
"
);
if
(
NULL
== mid) {
std::cerr <<
"
Class Simple has no method constructor signature (I)V
"
<< std::endl;
}
else
{
jthis = env->
NewObject
(cls, mid ,int_0);
jthrowable t = env->
ExceptionOccurred
();
if
(t !=
NULL
) {
if
(
GetDebugJ4Cpp
()) {
DebugPrintJObject
(__FILE__,__LINE__,
"
Simple::%METHOD_NAME% jthis=
"
,t);
env->
ExceptionDescribe
();
}
throw
t;
}
if
(jthis ==
NULL
) {
std::cerr <<
"
Call to create new Simple with signature (I)V returned null.
"
<< std::endl;
releaseEnv
(env);
return
;
}
jobjectRefType ref = env->
GetObjectRefType
(jthis);
if
(
GetDebugJ4Cpp
())
DebugPrintJObject
(__FILE__,__LINE__,
"
new Simple jthis=
"
,jthis);
if
(ref != JNIGlobalRefType) {
jthis = env->
NewGlobalRef
(jthis);
}
}
}
releaseEnv
(env);
}
//
Destructor for Simple
Simple::~Simple
() {
//
Place-holder for later extensibility.
}
jint
Simple::getValue
() {
if
(jthis ==
NULL
) {
std::cerr << __FILE__ <<
"
:
"
<< __LINE__ <<
"
Call of method getValue of Simple with jthis == NULL.
"
<< std::endl;
return
(jint) -
1
;
}
JNIEnv *env =
getEnv
();
jclass cls = env->
GetObjectClass
(jthis);
if
(
GetDebugJ4Cpp
())
DebugPrintJObject
(__FILE__,__LINE__,
"
Simple::getValue jthis=
"
,jthis);
jint retVal= (jint) -
1
;
if
(cls !=
NULL
) {
static
jmethodID mid = env->
GetMethodID
(cls,
"
getValue
"
,
"
()I
"
);
if
(
NULL
== mid) {
DebugPrintJObject
(__FILE__,__LINE__,
"
Simple::getValue jthis=
"
,jthis);
std::cerr << __FILE__ <<
"
:
"
<< __LINE__ <<
"
Class Simple has no method named getValue with signature ()I.
"
<< std::endl;
return
(jint) -
1
;
}
else
{
retVal= (jint) env->
CallIntMethod
(jthis, mid );
jthrowable t = env->
ExceptionOccurred
();
if
(t !=
NULL
) {
if
(
GetDebugJ4Cpp
()) {
DebugPrintJObject
(__FILE__,__LINE__,
"
Simple::getValue jthrowable t=
"
,t);
env->
ExceptionDescribe
();
}
//
env->ExceptionClear();
throw
t;
}
}
}
releaseEnv
(env);
return
retVal;
}
static
jclass
getNewSimpleClass
() {
jclass clss =
getEnv
()->
FindClass
(
"
Simple
"
);
if
(
NULL
== clss) {
std::cerr <<
"
Can't find class Simple
"
<< std::endl;
}
return
clss;
}
static
jclass SimpleClass =
NULL
;
static
inline
jclass
getSimpleClass
() {
if
(SimpleClass !=
NULL
) {
return
SimpleClass;
}
SimpleClass =
getNewSimpleClass
();
return
SimpleClass;
}
static
JNIEnv *
getNewEnv
() {
JavaVM *jvm;
/*
denotes a Java VM
*/
JNIEnv *env;
/*
pointer to native method interface
*/
JavaVM * jvmBuf[
1
];
jsize nVMs;
char
*debugJ4CppEnv =
NULL
;
char
*classPathEnv =
NULL
;
char
*jvmOptionsEnv =
NULL
;
#
if
defined(_WIN32) || defined(_WIN64)
errno_t
errno_val;
size_t
requiredSize;
errno_val =
getenv_s
( &requiredSize,
NULL
,
0
,
"
DEBUG_J4CPP
"
);
if
(requiredSize >
0
) {
debugJ4CppEnv = (
char
*)
malloc
(requiredSize);
errno_val =
getenv_s
( &requiredSize, debugJ4CppEnv, requiredSize,
"
DEBUG_J4CPP
"
);
}
errno_val =
getenv_s
( &requiredSize,
NULL
,
0
,
"
CLASSPATH
"
);
if
(requiredSize >
0
) {
classPathEnv = (
char
*)
malloc
(requiredSize);
errno_val =
getenv_s
( &requiredSize, classPathEnv, requiredSize,
"
CLASSPATH
"
);
}
errno_val =
getenv_s
( &requiredSize,
NULL
,
0
,
"
JVM_OPTIONS
"
);
if
(requiredSize >
0
) {
jvmOptionsEnv = (
char
*)
malloc
(requiredSize);
errno_val =
getenv_s
( &requiredSize, jvmOptionsEnv, requiredSize,
"
JVM_OPTIONS
"
);
}
#
else
debugJ4CppEnv =
getenv
(
"
DEBUG_J4CPP
"
);
classPathEnv =
getenv
(
"
CLASSPATH
"
);
jvmOptionsEnv =
getenv
(
"
JVM_OPTIONS
"
);
#
endif
debug_j4cpp = debug_j4cpp || (debugJ4CppEnv !=
NULL
&& debugJ4CppEnv[
0
] !=
0
);
std::string str;
str +=
"
/home/shackle/java4cpp/examples/simple
"
;
if
(classPathEnv !=
NULL
) {
std::string
classPathEnvStr
(classPathEnv);
if
(debug_j4cpp) std::cout <<
"
classPathEnv=
"
<< classPathEnvStr << std::endl;
str +=
"
:
"
;
str += classPathEnvStr;
}
if
(debug_j4cpp) std::cout <<
"
str=
"
<< str << std::endl;
#
if
defined(_WIN32) || defined(_WIN64)
_putenv_s
(
"
CLASSPATH
"
, str.
c_str
());
#
else
setenv
(
"
CLASSPATH
"
, str.
c_str
(),
1
);
#
endif
std::string optsString;
optsString +=
"
-Djava.class.path=
"
;
optsString += str;
if
(jvmOptionsEnv !=
NULL
) {
std::string
jvmOptionsEnvStr
(jvmOptionsEnv);
if
(debug_j4cpp) std::cout <<
"
jvmOptionsEnvStr=
"
<< jvmOptionsEnvStr << std::endl;
optsString +=
"
"
;
optsString += jvmOptionsEnvStr;
}
if
(debug_j4cpp) std::cout <<
"
optsString=
"
<< optsString << std::endl;
jint v =
JNI_GetCreatedJavaVMs
(jvmBuf,
1
, &nVMs);
if
(nVMs >
0
) {
jvmBuf[
0
]->
GetEnv
((
void
**) &env,
JNI_VERSION_1_6
);
return
env;
}
JavaVMInitArgs vm_args;
/*
JDK/JRE 6 VM initialization arguments
*/
JavaVMOption* options =
new
JavaVMOption[
1
];
options[
0
].
optionString
= (
char
*) optsString.
c_str
();
vm_args.
version
=
JNI_VERSION_1_6
;
vm_args.
nOptions
=
NULL
!= options[
0
].
optionString
?
1
:
0
;
vm_args.
options
= options;
vm_args.
ignoreUnrecognized
=
false
;
/*
load and initialize a Java VM, return a JNI interface
* pointer in env
*/
JNI_CreateJavaVM
(&jvm,
((
void
**) (&env)),
((
void
*) (&vm_args)));
delete
options;
#
if
defined(_WIN32) || defined(_WIN64)
if
(debugJ4CppEnv !=
NULL
) {
free
(debugJ4CppEnv);
debugJ4CppEnv =
NULL
;
}
if
(classPathEnv !=
NULL
) {
free
(classPathEnv);
classPathEnv =
NULL
;
}
if
(jvmOptionsEnv !=
NULL
) {
free
(jvmOptionsEnv);
jvmOptionsEnv =
NULL
;
}
#
endif
return
env;
}
static
JNIEnv *env =
NULL
;
JNIEnv *
getEnv
() {
if
(env !=
NULL
) {
return
env;
}
env =
getNewEnv
();
return
env;
}
void
releaseEnv
(JNIEnv *env) {
//
Maybe implement this later,
//
This will probably cause a memory leak.
//
Don't like leaks? Why the hell are using C++?
}
static
jclass
getNewStringClass
() {
jclass clss =
getEnv
()->
FindClass
(
"
java/lang/String
"
);
if
(
NULL
== clss) {
std::cerr <<
"
Can't find class java/lang/String
"
<< std::endl;
}
return
clss;
}
static
jclass StringClass =
NULL
;
jclass
getStringClass
() {
if
(StringClass !=
NULL
) {
return
StringClass;
}
StringClass =
getNewStringClass
();
return
StringClass;
}
static
jclass
getNewClassClass
() {
jclass clss =
getEnv
()->
FindClass
(
"
java/lang/Class
"
);
if
(
NULL
== clss) {
std::cerr <<
"
Can't find class java/lang/Class
"
<< std::endl;
}
return
clss;
}
static
jclass ClassClass =
NULL
;
jclass
getClassClass
() {
if
(ClassClass !=
NULL
) {
return
ClassClass;
}
ClassClass =
getNewStringClass
();
return
ClassClass;
}
static
jstring
getNewEmptyString
() {
return
getEnv
()->
NewStringUTF
(
"
"
);
}
static
jstring EmptyString =
NULL
;
jstring
getEmptyString
() {
if
(EmptyString !=
NULL
) {
return
EmptyString;
}
EmptyString =
getNewEmptyString
();
return
EmptyString;
}
void
PrintJObject
(
const
char
*prefix, jobject jobj) {
if
(
NULL
== jobj) {
std::cout << prefix <<
"
NULL
"
<< std::endl;
return
;
}
JNIEnv *env =
getEnv
();
jclass clss = env->
GetObjectClass
(jobj);
jmethodID midToString = env->
GetMethodID
(clss,
"
toString
"
,
"
()Ljava/lang/String;
"
);
jstring jobjstr = (jstring) env->
CallObjectMethod
(jobj, midToString);
jboolean iscopy =
JNI_FALSE
;
const
char
*cstr = env->
GetStringUTFChars
(jobjstr, &iscopy);
std::cout << prefix << cstr << std::endl;
env->
ReleaseStringUTFChars
(jobjstr,cstr);
}
void
PrintJThrowable
(
const
char
*prefix, jthrowable jobj) {
JNIEnv *env =
getEnv
();
jclass clss = env->
GetObjectClass
(jobj);
jmethodID midToString = env->
GetMethodID
(clss,
"
toString
"
,
"
()Ljava/lang/String;
"
);
jstring jobjstr = (jstring) env->
CallObjectMethod
(jobj, midToString);
jboolean iscopy =
JNI_FALSE
;
const
char
*cstr = env->
GetStringUTFChars
(jobjstr, &iscopy);
std::cerr << prefix << cstr << std::endl;
env->
ReleaseStringUTFChars
(jobjstr,cstr);
env->
ExceptionDescribe
();
env->
ExceptionClear
();
}
void
DebugPrintJObject
(
const
char
*file,
int
lineno,
const
char
*prefix, jobject jobj) {
if
(
NULL
== jobj) {
std::cout << file <<
"
:
"
<< lineno <<
"
"
<< prefix <<
"
NULL
"
<< std::endl;
return
;
}
JNIEnv *env =
getEnv
();
jclass clss = env->
GetObjectClass
(jobj);
bool
isnull = (jobj ==
NULL
) || (env->
IsSameObject
(jobj,
NULL
) ==
JNI_TRUE
);
if
(isnull) {
std::cout << file <<
"
:
"
<< lineno <<
"
jobj=
"
<< jobj <<
"
"
<< prefix <<
"
NULL
"
<< std::endl;
return
;
}
jmethodID midToString = env->
GetMethodID
(clss,
"
toString
"
,
"
()Ljava/lang/String;
"
);
jstring jobjstr = (jstring) env->
CallObjectMethod
(jobj, midToString);
jboolean iscopy =
JNI_FALSE
;
const
char
*cstr = env->
GetStringUTFChars
(jobjstr, &iscopy);
std::cout << file <<
"
:
"
<< lineno <<
"
jobj=
"
<< jobj <<
"
"
<< prefix << cstr << std::endl;
env->
ReleaseStringUTFChars
(jobjstr,cstr);
}
void
PrintObject
(
const
char
*prefix,
const
::simple::java::lang::Object &objref) {
PrintJObject
(prefix,objref.
jthis
);
}
extern
void
SetDebugJ4Cpp
(
bool
debug) {
debug_j4cpp = debug;
}
extern
bool
GetDebugJ4Cpp
() {
return
debug_j4cpp;
}
//
end namespace simple
}
Back
|
FazBrowse Home
|
New Git URL