1
/* -----------------------------------------------------------------------------
6
* Copyright (C) 1999-2000. The University of Chicago
7
* See the file LICENSE for information on usage and redistribution.
8
* ----------------------------------------------------------------------------- */
17
static char *usage = (char*)"\
19
-jnic - use c syntax for JNI calls\n\
20
-jnicpp - use c++ syntax for JNI calls\n\
21
-package <name> - set name of the package\n\
22
-shadow - generate shadow classes\n\
23
-nofinalize - do not generate finalize methods in shadow classes\n\
24
-rn - generate register natives code\n\n";
26
static char *module = 0; // Name of the module
27
static char *java_path = (char*)"java";
28
static char *package = 0; // Name of the package
29
static char *c_pkgstr; // Name of the package
30
static char *jni_pkgstr; // Name of the package
31
static char *shadow_classname;
32
static FILE *f_java = 0;
33
static FILE *f_shadow = 0;
35
static File *f_runtime = 0;
36
static File *f_header = 0;
37
static File *f_wrappers = 0;
38
static File *f_init = 0;
40
static int shadow = 0;
41
static Hash *shadow_classes;
42
static String *shadow_classdef;
43
static String *shadow_code;
44
static char *shadow_variable_name = 0; //Name of a c struct variable or c++ public member variable (may or may not be const)
45
static int classdef_emitted = 0;
46
static int have_default_constructor = 0;
47
static int native_func = 0; // Set to 1 when wrapping a native function
48
static int enum_flag = 0; // Set to 1 when wrapping an enum
49
static int static_flag = 0; // Set to 1 when wrapping a static functions or member variables
50
static int variable_wrapper_flag = 0; // Set to 1 when wrapping a nonstatic member variable
51
static int wrapping_member = 0; // Set to 1 when wrapping a member variable/enum/const
52
static int abstract_class_flag = 0;
53
static int jnic = -1; // 1: use c syntax jni; 0: use c++ syntax jni
54
static int nofinalize = 0; // for generating finalize methods
55
static int useRegisterNatives = 0; // Set to 1 when doing stuff with register natives
56
static String *registerNativesList = 0;
57
static String *shadow_constants_code = 0;
58
static String *module_constants_code = 0;
59
static String *module_extra_code = 0; // Extra code for the module class from %pragma
60
static String *all_shadow_extra_code = 0; // Extra code for all shadow classes from %pragma
61
static String *this_shadow_extra_code = 0; // Extra code for current single shadow class from %pragma
62
static String *module_import = 0; //module import from %pragma
63
static String *all_shadow_import = 0; //import for all shadow classes from %pragma
64
static String *this_shadow_import = 0; //import for current shadow classes from %pragma
65
static String *module_baseclass = 0; //inheritance for module class from %pragma
66
static String *all_shadow_baseclass = 0; //inheritance for all shadow classes from %pragma
67
static String *this_shadow_baseclass = 0; //inheritance for shadow class from %pragma and cpp_inherit
68
static String *module_interfaces = 0; //interfaces for module class from %pragma
69
static String *all_shadow_interfaces = 0; //interfaces for all shadow classes from %pragma
70
static String *this_shadow_interfaces = 0; //interfaces for shadow class from %pragma
71
static String *module_class_modifiers = 0; //class modifiers for module class overriden by %pragma
72
static String *all_shadow_class_modifiers = 0; //class modifiers for all shadow classes overriden by %pragma
73
static String *this_shadow_class_modifiers = 0; //class modifiers for shadow class overriden by %pragma
74
static String *module_method_modifiers = 0; //native method modifiers overridden by %pragma
76
/* Test to see if a type corresponds to something wrapped with a shadow class */
77
/* Return NULL if not otherwise the shadow name */
78
static String *is_shadow(SwigType *t) {
79
return Getattr(shadow_classes,SwigType_base(t));
82
// Return the type of the c array
83
static SwigType *get_array_type(SwigType *t) {
85
if (SwigType_type(t) == T_ARRAY) {
88
aop = SwigType_pop(ta);
93
/* JavaMethodSignature still needs updating for changes from SWIG1.3a3 to SWIG1.3a5 and for current version */
94
char *JAVA::JavaMethodSignature(SwigType *t, int ret, int inShadow) {
95
if(SwigType_ispointer(t) == 1) {
96
switch(SwigType_type(t)) {
97
case T_CHAR: return (char*)"Ljava/lang/String;";
98
case T_SCHAR: return (char*)"[B";
99
case T_UCHAR: return (char*)"[S";
100
case T_SHORT: return (char*)"[S";
101
case T_USHORT: return (char*)"[I";
102
case T_INT: return (char*)"[I";
103
case T_UINT: return (char*)"[J";
104
case T_LONG: return (char*)"[I";
105
case T_ULONG: return (char*)"[J";
106
case T_FLOAT: return (char*)"[F";
107
case T_DOUBLE: return (char*)"[D";
108
case T_BOOL: return (char*)"[Z";
109
case T_STRING: return (char*)"Ljava/lang/String;";
110
case T_POINTER: return (char*)"[J";
111
case T_REFERENCE: return (char*)"[J";
112
case T_ARRAY: return (char*)"???";
114
case T_USER: if(inShadow && is_shadow(t))
115
return Char(is_shadow(t));
116
else return (char*)"J";
118
} else if(SwigType_ispointer(t) > 1) {
119
if(ret) return (char*)"J";
120
else return (char*)"[J";
122
switch(SwigType_type(t)) {
123
case T_CHAR: return (char*)"B";
124
case T_SCHAR: return (char*)"B";
125
case T_UCHAR: return (char*)"S";
126
case T_SHORT: return (char*)"S";
127
case T_USHORT: return (char*)"I";
128
case T_INT: return (char*)"I";
129
case T_UINT: return (char*)"J";
130
case T_LONG: return (char*)"I";
131
case T_ULONG: return (char*)"J";
132
case T_FLOAT: return (char*)"F";
133
case T_DOUBLE: return (char*)"D";
134
case T_BOOL: return (char*)"Z";
135
case T_STRING: return (char*)"Ljava/lang/String;";
136
case T_POINTER: return (char*)"J";
137
case T_REFERENCE: return (char*)"J";
138
case T_ARRAY: return (char*)"???";
139
case T_VOID: return (char*)"V";
140
case T_USER: return (char*)"J";
143
Printf(stderr, "JavaMethodSignature: unhandled SWIG type [%d] %s\n", SwigType_type(t), SwigType_str(t,0));
147
char *JAVA::makeValidJniName(const char *name) {
148
const char *c = name;
159
return Swig_copy_string(bigbuf);
162
// !! this approach fails for functions without arguments
163
char *JAVA::JNICALL(String_or_char *func) {
165
sprintf(bigbuf, "(*jenv)->%s(jenv, ", Char(func));
167
sprintf(bigbuf, "jenv->%s(", Char(func));
169
return Swig_copy_string(bigbuf);
172
void JAVA::writeRegisterNatives()
174
if(Len(registerNativesList) == 0)
177
Printf(f_wrappers,"\n");
178
Printf(f_wrappers,"JNINativeMethod nativeMethods[] = {\n");
179
Printv(f_wrappers, registerNativesList, 0);
180
Printf(f_wrappers, "};\n");
182
Printf(f_wrappers,"\nint numberOfNativeMethods=sizeof(nativeMethods)/sizeof(JNINativeMethod);\n\n");
184
// The registerNatives function
187
"jint registerNatives(JNIEnv *jenv) {", "\n",
188
tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
189
"\"", jni_pkgstr, module, "\");","\n",
193
tab4, "if (nativeClass == 0)", "\n", tab8, "return -1;", "\n",
194
tab4, "return ", JNICALL((char*)"RegisterNatives"), "nativeClass, nativeMethods, ", "numberOfNativeMethods);", "\n",
198
// The unregisterNatives function
201
"jint unregisterNatives(JNIEnv *jenv) {", "\n",
202
tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
203
"\"", jni_pkgstr, module, "\");","\n",
207
tab4, "if (nativeClass == 0)", "\n", tab8, "return -1;", "\n",
208
tab4, "// Sun documentation suggests that this method should not be invoked in ",
209
"\"normal native code\".", "\n",
210
tab4, "// return ", JNICALL((char*)"UnregisterNatives"), "nativeClass);", "\n",
211
tab4, "return 0;", "\n",
216
// ---------------------------------------------------------------------
219
// Parse my command line options and initialize by variables.
220
// ---------------------------------------------------------------------
222
void JAVA::main(int argc, char *argv[]) {
224
SWIG_library_directory("java");
226
// Look for certain command line options
227
for (int i = 1; i < argc; i++) {
229
if (strcmp(argv[i],"-package") == 0) {
231
package = new char[strlen(argv[i+1])+1];
232
strcpy(package, argv[i+1]);
239
} else if (strcmp(argv[i],"-shadow") == 0) {
242
} else if (strcmp(argv[i],"-jnic") == 0) {
245
} else if (strcmp(argv[i],"-nofinalize") == 0) {
248
} else if (strcmp(argv[i],"-rn") == 0) {
250
useRegisterNatives = 1;
251
} else if (strcmp(argv[i],"-jnicpp") == 0) {
254
} else if (strcmp(argv[i],"-help") == 0) {
255
Printf(stderr,"%s\n", usage);
266
// Add a symbol to the parser for conditional compilation
267
Preprocessor_define((void *) "SWIGJAVA 1",0);
269
// Add typemap definitions
270
SWIG_typemap_lang("java");
271
SWIG_config_file("java.swg");
274
// ---------------------------------------------------------------------
276
// ---------------------------------------------------------------------
278
int JAVA::top(Node *n) {
280
/* Initialize all of the output files */
281
String *outfile = Getattr(n,"outfile");
283
f_runtime = NewFile(outfile,"w");
285
Printf(stderr,"*** Can't open '%s'\n", outfile);
286
SWIG_exit(EXIT_FAILURE);
288
f_init = NewString("");
289
f_header = NewString("");
290
f_wrappers = NewString("");
292
/* Register file targets with the SWIG file handler */
293
Swig_register_filebyname("header",f_header);
294
Swig_register_filebyname("wrapper",f_wrappers);
295
Swig_register_filebyname("runtime",f_runtime);
296
Swig_register_filebyname("init",f_init);
298
shadow_classes = NewHash();
299
shadow_classdef = NewString("");
300
shadow_code = NewString("");
301
registerNativesList = NewString("");
302
module_constants_code = NewString("");
303
module_extra_code = NewString("");
304
module_baseclass = NewString("");
305
module_interfaces = NewString("");
306
module_class_modifiers = NewString("");
307
all_shadow_extra_code = NewString("");
308
all_shadow_import = NewString("");
309
all_shadow_baseclass = NewString("");
310
all_shadow_interfaces = NewString("");
311
all_shadow_class_modifiers = NewString("");
312
module_import = NewString("");
313
module_method_modifiers = NewString("public final static");
315
Swig_banner(f_runtime); // Print the SWIG banner message
316
Printf(f_runtime,"/* Implementation : Java */\n\n");
318
set_module(Char(Getattr(n,"name")));
320
String* wrapper_name = NewString("");
323
String *s = NewString(package);
324
char *jniname = makeValidJniName(Char(s));
326
Printv(s, jniname, 0);
328
Replace(s,".","_", DOH_REPLACE_ANY);
330
c_pkgstr = Swig_copy_string(Char(s));
333
String *s2 = NewString(package);
334
Replace(s2,".","/", DOH_REPLACE_ANY);
336
jni_pkgstr = Swig_copy_string(Char(s2));
339
package = c_pkgstr = jni_pkgstr = (char*)"";
342
char *jniname = makeValidJniName(module);
343
Printf(wrapper_name, "Java_%s%s_%%f", c_pkgstr, jniname);
346
Swig_name_register((char*)"wrapper", Char(wrapper_name));
347
Swig_name_register((char*)"set", (char*)"set_%v");
348
Swig_name_register((char*)"get", (char*)"get_%v");
349
Swig_name_register((char*)"member", (char*)"%c_%m");
351
Delete(wrapper_name);
353
// Generate the java class
354
sprintf(bigbuf, "%s.java", module);
355
if((f_java = fopen(bigbuf, "w")) == 0) {
356
Printf(stderr,"Unable to open %s\n", bigbuf);
360
Printf(f_header, "#define J_CLASSNAME %s\n", module);
361
if(package && *package) {
362
Printf(f_java, "package %s;\n\n", package);
363
Printf(f_header, "#define J_PACKAGE %s\n", package);
365
Printf(f_header, "#define J_PACKAGE\n");
371
if(!classdef_emitted) emit_classdef();
373
// Write out all the enums constants
374
if (strlen(Char(module_constants_code)) != 0 )
375
Printv(f_java, " // enums and constants\n", module_constants_code, 0);
377
// Finish off the java class
378
Printf(f_java, "}\n");
381
if(useRegisterNatives)
382
writeRegisterNatives();
384
Delete(shadow_classes); shadow_classes = NULL;
385
Delete(shadow_classdef); shadow_classdef = NULL;
386
Delete(shadow_code); shadow_code = NULL;
387
Delete(registerNativesList); registerNativesList = NULL;
388
Delete(module_constants_code); module_constants_code = NULL;
389
Delete(module_extra_code); module_extra_code = NULL;
390
Delete(module_baseclass); module_baseclass = NULL;
391
Delete(module_interfaces); module_interfaces = NULL;
392
Delete(module_class_modifiers); module_class_modifiers = NULL;
393
Delete(all_shadow_extra_code); all_shadow_extra_code = NULL;
394
Delete(all_shadow_import); all_shadow_import = NULL;
395
Delete(all_shadow_baseclass); all_shadow_baseclass = NULL;
396
Delete(all_shadow_interfaces); all_shadow_interfaces = NULL;
397
Delete(all_shadow_class_modifiers); all_shadow_class_modifiers = NULL;
398
Delete(module_import); module_import = NULL;
399
Delete(module_method_modifiers); module_method_modifiers = NULL;
401
/* Close all of the files */
402
Dump(f_header,f_runtime);
403
Dump(f_wrappers,f_runtime);
404
Wrapper_pretty_print(f_init,f_runtime);
413
// ---------------------------------------------------------------------
414
// JAVA::set_module(char *mod_name)
416
// Sets the module name. Does nothing if it's already set (so it can
417
// be overriddent as a command line option).
419
//----------------------------------------------------------------------
421
void JAVA::set_module(char *mod_name) {
423
module = new char[strlen(mod_name)+1];
424
strcpy(module,mod_name);
428
static void emit_banner(FILE *f) {
429
Printf(f, "/* ----------------------------------------------------------------------------\n");
430
Printf(f, " * This file was automatically generated by SWIG (http://www.swig.org).\n");
431
Printf(f, " * Version: %s\n", SWIG_VERSION);
433
Printf(f, " * Do not make changes to this file unless you know what you are doing--modify\n");
434
Printf(f, " * the SWIG interface file instead.\n");
435
Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
438
void JAVA::emit_classdef() {
439
if(!classdef_emitted) {
442
Printf(f_java, "%s\n", module_import);
444
if (module_class_modifiers && *Char(module_class_modifiers))
445
Printf(f_java, "%s", module_class_modifiers);
447
Printf(f_java, "public");
448
Printf(f_java, " class %s ", module);
450
if (module_baseclass && *Char(module_baseclass))
451
Printf(f_java, "extends %s ", module_baseclass);
452
if (module_interfaces)
453
Printv(f_java, module_interfaces, " ", 0);
454
Printf(f_java, "{\n", module);
455
if (module_extra_code)
456
Printv(f_java, module_extra_code, 0);
458
classdef_emitted = 1;
461
int JAVA::nativeWrapper(Node *n) {
463
Swig_save(&n,"name",0);
464
Setattr(n,"name", Getattr(n,"wrap:name"));
472
// ----------------------------------------------------------------------
473
// JAVA::functionWrapper()
475
// Create a function declaration and register it with the interpreter.
476
// ----------------------------------------------------------------------
478
int JAVA::functionWrapper(Node *n) {
479
char *name = GetChar(n,"name");
480
char *iname = GetChar(n,"sym:name");
481
SwigType *t = Getattr(n,"type");
482
ParmList *l = Getattr(n,"parms");
489
char *javaReturnSignature = 0;
490
String *jnirettype = NewString("");
491
String *javarettype = NewString("");
492
String *cleanup = NewString("");
493
String *outarg = NewString("");
494
String *body = NewString("");
495
String *javaParameterSignature = NewString("");
496
int num_arguments = 0;
497
int num_required = 0;
500
Generate the java class wrapper function ie the java shadow class. Only done for public
501
member variables. That is this generates the getters/setters for member variables.
503
if(shadow && wrapping_member && !enum_flag) {
504
String *member_function_name = NewString("");
505
String *java_function_name = NewString(iname);
506
if(strcmp(iname, Char(Swig_name_set(Swig_name_member(shadow_classname, shadow_variable_name)))) == 0)
507
Printf(member_function_name,"set");
508
else Printf(member_function_name,"get");
509
Putc(toupper((int) *shadow_variable_name), member_function_name);
510
Printf(member_function_name, "%s", shadow_variable_name+1);
512
Setattr(n,"java:shadfuncname", member_function_name);
513
Setattr(n,"java:funcname", iname);
514
javashadowfunctionHandler(n, NOT_VIRTUAL);
516
Delete(java_function_name);
517
Delete(member_function_name);
521
The rest of this function deals with generating the java wrapper function (that wraps
522
a c/c++ function) and generating the JNI c code. Each java wrapper function has a
523
matching JNI c function call.
526
// A new wrapper function object
527
Wrapper *f = NewWrapper();
529
if(!classdef_emitted) emit_classdef();
531
// Make a wrapper name for this function
532
char *jniname = makeValidJniName(iname);
533
String *wname = Swig_name_wrapper(jniname);
536
/* Get the jni and java types of the return.
537
* The non-standard typemaps must first be attached to the parameter list. */
538
Swig_typemap_attach_parms("jni", l, f);
539
Swig_typemap_attach_parms("jtype", l, f);
541
if ((tm = Swig_typemap_lookup_new("jni",n,"",0))) {
542
Printf(jnirettype,"%s", tm);
544
Printf(stderr, "No jni typemap defined for %s\n", SwigType_str(t,0));
547
if ((tm = Swig_typemap_lookup_new("jtype",n,"",0))) {
548
Printf(javarettype,"%s", tm);
550
Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(t,0));
553
// If dumping the registerNative outputs, store the method return type
555
if (useRegisterNatives) {
556
javaReturnSignature = JavaMethodSignature(t, 1, 0);
559
if (SwigType_type(t) != T_VOID) {
560
Wrapper_add_localv(f,"jresult", jnirettype, "jresult = 0",0);
563
Printf(f_java, " %s ", module_method_modifiers);
564
Printf(f_java, "native %s %s(", javarettype, iname);
567
Printv(f->def, "extern \"C\"{\n", 0);
568
Printv(f->def, "JNIEXPORT ", jnirettype, " JNICALL ", wname, "(JNIEnv *jenv, jclass jcls", 0);
570
// Emit all of the local variables for holding arguments.
573
/* Attach the standard typemaps */
574
emit_attach_parmmaps(l,f);
576
/* Get number of required and total arguments */
577
num_arguments = emit_num_arguments(l);
578
num_required = emit_num_required(l);
582
// Now walk the function parameter list and generate code to get arguments
583
for (i = 0, p=l, jnip=l, jtypep=l; i < num_arguments; i++) {
585
while (Getattr(p,"tmap:ignore")) {
586
p = Getattr(p,"tmap:ignore:next");
589
SwigType *pt = Getattr(p,"type");
590
String *ln = Getattr(p,"lname");
591
String *javaparamtype = NewString("");
592
String *jni_param_type = NewString("");
594
sprintf(source,"j%s", Char(ln));
596
if (useRegisterNatives) {
597
Printv(javaParameterSignature, JavaMethodSignature(pt, 0, 0), 0);
600
/* Get the jni types of the parameter */
601
if ((tm = Getattr(jnip,"tmap:jni"))) {
602
jnip = Getattr(jnip,"tmap:jni:next");
603
Printv(jni_param_type, tm, 0);
605
jnip = nextSibling(jnip);
606
Printf(stderr, "No jni typemap defined for %s\n", SwigType_str(pt,0));
609
/* Get the java types of the parameter */
610
if ((tm = Getattr(jtypep,"tmap:jtype"))) {
611
jtypep = Getattr(jtypep,"tmap:jtype:next");
612
Printv(javaparamtype, tm, 0);
614
jtypep = nextSibling(jtypep);
615
Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(pt,0));
618
/* Add to java function header */
619
if(gencomma) Printf(f_java, ", ");
620
Printf(f_java, "%s %s", javaparamtype, source);
624
// Add to Jni function header
625
Printv(f->def, ", ", jni_param_type, " ", source, 0);
627
// Get typemap for this argument
628
if ((tm = Getattr(p,"tmap:in"))) {
629
Replaceall(tm,"$source",source); /* deprecated */
630
Replaceall(tm,"$target",ln); /* deprecated */
631
Replaceall(tm,"$arg",source); /* deprecated? */
632
Replaceall(tm,"$input", source);
633
Setattr(p,"emit:input", source);
634
Printf(f->code,"%s\n", tm);
635
p = Getattr(p,"tmap:in:next");
637
Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0));
640
Delete(javaparamtype);
641
Delete(jni_param_type);
644
/* Insert constraint checking code */
646
if ((tm = Getattr(p,"tmap:check"))) {
647
Replaceall(tm,"$target",Getattr(p,"lname")); /* deprecated */
648
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
649
Replaceall(tm,"$input",Getattr(p,"emit:input"));
650
Printv(f->code,tm,"\n",0);
651
p = Getattr(p,"tmap:check:next");
657
/* Insert cleanup code */
659
while (Getattr(p,"tmap:ignore")) {
660
p = Getattr(p,"tmap:ignore:next");
662
if ((tm = Getattr(p,"tmap:freearg"))) {
663
Replaceall(tm,"$source",Getattr(p,"emit:input")); /* deprecated */
664
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
665
Replaceall(tm,"$input",Getattr(p,"emit:input"));
666
Printv(cleanup,tm,"\n",0);
667
p = Getattr(p,"tmap:freearg:next");
673
/* Insert argument output code */
675
while (Getattr(p,"tmap:ignore")) {
676
p = Getattr(p,"tmap:ignore:next");
678
if ((tm = Getattr(p,"tmap:argout"))) {
679
Replaceall(tm,"$source",Getattr(p,"emit:input")); /* deprecated */
680
Replaceall(tm,"$target",Getattr(p,"lname")); /* deprecated */
681
Replaceall(tm,"$arg",Getattr(p,"emit:input")); /* deprecated? */
682
Replaceall(tm,"$result","jresult");
683
Replaceall(tm,"$input",Getattr(p,"emit:input"));
684
Printv(outarg,tm,"\n",0);
685
p = Getattr(p,"tmap:argout:next");
691
Printf(f_java, ");\n");
692
Printf(f->def,") {");
694
// Now write code to make the function call
698
/* Return value if necessary */
699
if((SwigType_type(t) != T_VOID) && !native_func) {
700
if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) {
701
Replaceall(tm,"$source", "result"); /* deprecated */
702
Replaceall(tm,"$target", "jresult"); /* deprecated */
703
Replaceall(tm,"$result","jresult");
704
Printf(f->code,"%s\n", tm);
706
Printf(stderr,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(t,0), name);
710
/* Output argument output code */
711
Printv(f->code,outarg,0);
713
/* Output cleanup code */
714
Printv(f->code,cleanup,0);
716
/* Look to see if there is any newfree cleanup code */
717
if (NewObject || (Getattr(n,"feature:new"))) {
718
if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
719
Replaceall(tm,"$source","result"); /* deprecated */
720
Printf(f->code,"%s\n",tm);
724
/* See if there is any return cleanup code */
725
if((SwigType_type(t) != T_VOID) && !native_func) {
726
if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) {
727
Replaceall(tm,"$source","result"); /* deprecated */
728
Printf(f->code,"%s\n",tm);
732
if(SwigType_type(t) != T_VOID)
733
Printv(f->code, tab4, "return jresult;\n", 0);
735
Printf(f->code, "}");
736
Printf(f->code, "}\n");
738
/* Substitute the cleanup code */
739
Replaceall(f->code,"$cleanup",cleanup);
741
if(SwigType_type(t) != T_VOID)
742
Replaceall(f->code,"$null","0");
744
Replaceall(f->code,"$null","");
746
/* Dump the function out */
748
Wrapper_print(f,f_wrappers);
750
// If registerNatives is active, store the table entry for this method
751
if (useRegisterNatives) {
752
Printv(registerNativesList,
754
"\"", name, "\", \"(", javaParameterSignature, ")", javaReturnSignature, "\", ", wname,
765
Delete(javaParameterSignature);
770
// -----------------------------------------------------------------------
771
// JAVA::variableWrapper()
773
// Create a JAVA link to a C variable.
774
// -----------------------------------------------------------------------
776
int JAVA::variableWrapper(Node *n)
778
Language::variableWrapper(n); /* Default to functions */
783
// -----------------------------------------------------------------------
784
// JAVA::enumDeclaration()
785
// ------------------------------------------------------------------------
787
int JAVA::enumDeclaration(Node *n) {
788
String *name = Getattr(n,"sym:name");
789
String* s1 = NewStringf("enum %s", name);
790
String* s2 = NewStringf("%s", name);
791
String *swigtype = NewString("enum SWIGTYPE");
793
/* Apply typemaps for handling arrays of enums and arrays of enum pointers for all known enums*/
794
TypemapApply(swigtype, NULL, s1, none, 1); //%apply enum SWIGTYPE[ANY] {enum name[ANY]};
795
TypemapApply(swigtype, NULL, s2, none, 1); //%apply enum SWIGTYPE[ANY] {name[ANY]};
796
TypemapApply(swigtype, NULL, s1, pointer, 1); //%apply enum SWIGTYPE*[ANY] {enum name*[ANY]};
797
TypemapApply(swigtype, NULL, s2, pointer, 1); //%apply enum SWIGTYPE*[ANY] {name*[ANY]};
798
return Language::enumDeclaration(n);
804
// -----------------------------------------------------------------------
805
// JAVA::constantWrapper()
806
// ------------------------------------------------------------------------
808
int JAVA::constantWrapper(Node *n) {
809
char *name = GetChar(n,"name");
810
char *iname = GetChar(n,"sym:name");
811
SwigType *type = Getattr(n,"type");
812
char *value = GetChar(n,"value");
813
ParmList *l = Getattr(n,"parms");
815
int OldReadOnly = ReadOnly;
819
String *constants_code;
821
String *java_type = NewString("");
823
if(!classdef_emitted) emit_classdef();
825
if(shadow && wrapping_member) {
827
jname = shadow_variable_name;
828
constants_code = shadow_constants_code;
832
constants_code = module_constants_code;
835
if ((tm = Swig_typemap_lookup_new("const",n,"",0))) {
837
Replace(str,"$value",value, DOH_REPLACE_ANY);
838
Printf(jout," %s\n\n", str);
841
if (wrapping_member) {
842
Swig_typemap_attach_parms("jstype", l, 0);
843
if ((tm = Swig_typemap_lookup_new("jstype",n,"",0))) {
844
String *javaclassname = is_shadow(SwigType_base(Getattr(n,"type")));
845
Replaceall(javaclassname, "enum ", "");
846
Replaceall(tm,"$javaclassname", javaclassname);
847
Printf(java_type,"%s", tm);
849
Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(type,0));
852
Swig_typemap_attach_parms("jtype", l, 0);
853
if ((tm = Swig_typemap_lookup_new("jtype",n,"",0))) {
854
Printf(java_type,"%s", tm);
856
Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(type,0));
859
if(strcmp(jname, value) == 0 || strstr(value,"::") != NULL) {
861
We have found an enum. The enum implementation is done using a public final static int in Java.
863
if(shadow && wrapping_member) {
864
Printf(shadow_constants_code, " public final static %s %s = %s.%s;\n", java_type, jname, module, iname);
865
Printf(module_constants_code, " public final static %s %s = %s();\n", java_type, iname, Swig_name_get(iname));
868
Printf(module_constants_code, " public final static %s %s = %s();\n", java_type, jname, Swig_name_get(iname));
874
if(SwigType_type(type) == T_STRING)
875
Printf(constants_code, " public final static %s %s = \"%s\";\n", java_type, jname, value);
876
else if(SwigType_type(type) == T_CHAR)
877
Printf(constants_code, " public final static String %s = \"%s\";\n", jname, value);
879
Printf(constants_code, " public final static %s %s = %s;\n", java_type, jname, value);
883
ReadOnly = OldReadOnly;
888
These pragmas start with 'allshadow' or 'module'
889
modulebase - base (extends) for the java module class
890
allshadowbase - base (extends) for all java shadow classes
891
modulecode - text (java code) is copied verbatim to the java module class
892
allshadowcode - text (java code) is copied verbatim to all java shadow classes
893
moduleclassmodifiers - class modifiers for the module class
894
allshadowclassmodifiers - class modifiers for all shadow classes
895
moduleimport - import statement generation for the java module class
896
allshadowimport - import statement generation for all java shadow classes
897
moduleinterface - interface (implements) for the module class
898
allshadowinterface - interface (implements) for all shadow classes
899
modulemethodmodifiers - replaces the generated native calls' default modifiers
903
C++ pragmas: pragmas declared within a class or c struct for the shadow class.
904
These pragmas start with 'shadow'
906
shadowbase - base (extends) for all java shadow classes
907
shadowcode - text (java code) is copied verbatim to the shadow class
908
shadowclassmodifiers - class modifiers for the shadow class
909
shadowimport - import statement generation for the shadow class
910
shadowinterface - interfaces (extends) for the shadow class
913
void JAVA::pragma(char *lang, char *code, char *value) {
914
if(strcmp(lang, "java") != 0) return;
916
String *strvalue = NewString(value);
917
Replace(strvalue,"\\\"", "\"", DOH_REPLACE_ANY);
919
if(strcmp(code, "moduleimport") == 0) {
920
Printf(module_import, "import %s;\n", strvalue);
922
else if(strcmp(code, "allshadowimport") == 0) {
923
if(shadow && all_shadow_import)
924
Printf(all_shadow_import, "import %s;\n", strvalue);
926
else if(strcmp(code, "import") == 0) {
927
Printf(stderr,"%s : Line %d. Ignored: Deprecated pragma. Please replace with moduleimport, shadowimport and/or allshadowimport pragmas.\n", input_file, line_number);
929
else if(strcmp(code, "modulecode") == 0 || strcmp(code, "module") == 0) {
930
if(strcmp(code, "module") == 0)
931
Printf(stderr,"%s : Line %d. Soon to be deprecated pragma. Please replace with modulecode pragma.\n", input_file, line_number);
932
Printf(module_extra_code, "%s\n", strvalue);
934
else if(strcmp(code, "allshadowcode") == 0 || strcmp(code, "shadow") == 0) {
935
if(shadow && all_shadow_extra_code) {
936
if(strcmp(code, "shadow") == 0)
937
Printf(stderr,"%s : Line %d. Soon to be deprecated pragma. Please replace with allshadowcode pragma.\n", input_file, line_number);
938
Printf(all_shadow_extra_code, "%s\n", strvalue);
941
else if(strcmp(code, "modulebase") == 0) {
942
if(shadow && module_baseclass)
943
Printf(module_baseclass, "%s", strvalue);
945
else if(strcmp(code, "allshadowbase") == 0) {
946
if(shadow && all_shadow_baseclass)
947
Printf(all_shadow_baseclass, "%s", strvalue);
949
else if(strcmp(code, "moduleinterface") == 0) {
950
if(shadow && module_interfaces)
951
if (!*Char(module_interfaces))
952
Printf(module_interfaces, "implements %s", strvalue);
954
Printf(module_interfaces, ", %s", strvalue);
956
else if(strcmp(code, "allshadowinterface") == 0) {
957
if(shadow && all_shadow_interfaces) {
958
if (!*Char(all_shadow_interfaces))
959
Printf(all_shadow_interfaces, "implements %s", strvalue);
961
Printf(all_shadow_interfaces, ", %s", strvalue);
964
else if(strcmp(code, "allshadowclassmodifiers") == 0) {
965
if(shadow && all_shadow_class_modifiers)
966
Printv(all_shadow_class_modifiers, strvalue, 0);
968
else if(strcmp(code, "moduleclassmodifiers") == 0) {
969
if(shadow && module_class_modifiers)
970
Printv(module_class_modifiers, strvalue, 0);
972
else if(strcmp(code, "modulemethodmodifiers") == 0 || strcmp(code, "modifiers") == 0) {
973
if(strcmp(code, "modifiers") == 0)
974
Printf(stderr,"%s : Line %d. Soon to be deprecated pragma. Please replace with modulemethodmodifiers pragma.\n", input_file, line_number);
975
Clear(module_method_modifiers);
976
Printv(module_method_modifiers, strvalue, 0);
978
if (strcmp(code,"shadowcode") == 0) {
979
if (f_shadow && shadow_code)
980
Printf(shadow_code, "%s\n", strvalue);
982
Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
984
else if (strcmp(code,"shadowimport") == 0) {
985
if (this_shadow_import)
986
Printf(this_shadow_import, "import %s;\n", strvalue);
988
Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
990
else if (strcmp(code,"shadowbase") == 0) {
991
if (this_shadow_baseclass)
992
Printf(this_shadow_baseclass, "%s", strvalue);
994
Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
996
else if (strcmp(code,"shadowinterface") == 0) {
997
if (this_shadow_interfaces) {
998
if (!*Char(this_shadow_interfaces))
999
Printf(this_shadow_interfaces, "implements %s", strvalue);
1001
Printf(this_shadow_interfaces, ", %s", strvalue);
1004
Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
1006
else if (strcmp(code,"shadowclassmodifiers") == 0) {
1007
if (this_shadow_class_modifiers)
1008
Printv(this_shadow_class_modifiers, strvalue, 0);
1010
Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
1012
Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
1015
Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
1020
void JAVA::emit_shadow_classdef() {
1021
String* baseclass = NULL;
1023
// Import statements
1024
if(all_shadow_import)
1025
Printf(shadow_classdef, "%s", all_shadow_import);
1026
if(this_shadow_import)
1027
Printf(shadow_classdef, "%s", this_shadow_import);
1028
Printf(shadow_classdef, "\n");
1031
if (this_shadow_class_modifiers && *Char(this_shadow_class_modifiers))
1032
Printv(shadow_classdef, this_shadow_class_modifiers, 0);
1033
else if (all_shadow_class_modifiers && *Char(all_shadow_class_modifiers))
1034
Printv(shadow_classdef, all_shadow_class_modifiers, 0);
1036
Printv(shadow_classdef, "public", 0);
1037
if (abstract_class_flag)
1038
Printv(shadow_classdef, " abstract", 0);
1039
Printf(shadow_classdef, " class $class ");
1041
// Inherited classes
1042
if(this_shadow_baseclass && *Char(this_shadow_baseclass)) {
1043
Printf(shadow_classdef, "extends %s ", this_shadow_baseclass);
1044
baseclass = this_shadow_baseclass;
1046
if(all_shadow_baseclass && *Char(all_shadow_baseclass)) {
1047
Printf(shadow_classdef, "extends %s ", all_shadow_baseclass);
1048
baseclass = all_shadow_baseclass;
1051
// Implemented interfaces
1052
if(this_shadow_interfaces && *Char(this_shadow_interfaces))
1053
Printv(shadow_classdef, this_shadow_interfaces, " ", 0);
1054
Printf(shadow_classdef, "{\n");
1056
//Display warning on attempt to use multiple inheritance
1057
char* search_str = Char(shadow_classdef);
1059
while((search_str = strstr(search_str, "extends"))) {
1060
search_str += strlen("extends");
1064
Printf(stderr, "Warning for shadow class %s: Multiple inheritance is not supported in Java.\n", shadow_classname);
1066
// Different code depending on whether or not the base class is a SWIG shadow class
1067
if (baseclass && is_shadow(baseclass)) {
1068
Printv(shadow_classdef,
1069
" public $class(long cPointer, boolean cMemoryOwn) {\n",
1070
" super(cPointer, cMemoryOwn);\n",
1074
if(!have_default_constructor) {
1075
Printv(shadow_classdef,
1076
" protected $class() {\n",
1080
// Control which super constructor is called - we don't want 2 malloc/new c/c++ calls
1081
Replace(shadow_code, "$superconstructorcall", " super(0, false);\n", DOH_REPLACE_ANY);
1084
Printv(shadow_classdef,
1085
" protected long _cPtr;\n",
1086
" protected boolean _cMemOwn;\n",
1088
" public $class(long cPointer, boolean cMemoryOwn) {\n",
1089
" _cPtr = cPointer;\n",
1090
" _cMemOwn = cMemoryOwn;\n",
1093
" public long getCPtr() {\n",
1098
if(!have_default_constructor) {
1099
Printv(shadow_classdef,
1100
" protected $class() {\n",
1102
" _cMemOwn = false;\n",
1106
// No explicit super constructor call as this class does not have a SWIG base class.
1107
Replace(shadow_code, "$superconstructorcall", "", DOH_REPLACE_ANY);
1110
Replace(shadow_classdef, "$class", shadow_classname, DOH_REPLACE_ANY);
1112
if (all_shadow_extra_code)
1113
Printv(shadow_classdef, all_shadow_extra_code, 0);
1115
if (this_shadow_extra_code)
1116
Printv(shadow_classdef, this_shadow_extra_code, 0);
1119
int JAVA::classHandler(Node *n) {
1122
char *classname = GetChar(n,"name");
1123
char *rename = GetChar(n,"sym:name");
1124
char *ctype = GetChar(n,"kind");
1126
shadow_classname = Swig_copy_string(rename);
1128
if (strcmp(shadow_classname, module) == 0) {
1129
Printf(stderr, "class name cannot be equal to module name: %s\n", shadow_classname);
1133
Setattr(shadow_classes,classname, shadow_classname);
1134
if(ctype && strcmp(ctype, "struct") == 0) {
1135
sprintf(bigbuf, "struct %s", classname);
1136
Setattr(shadow_classes, bigbuf, shadow_classname);
1139
sprintf(bigbuf, "%s.java", shadow_classname);
1140
if(!(f_shadow = fopen(bigbuf, "w"))) {
1141
Printf(stderr, "Unable to create shadow class file: %s\n", bigbuf);
1144
emit_banner(f_shadow);
1147
Printf(f_shadow, "package %s;\n\n", package);
1149
Printf(f_shadow, "import %s;\n", module);
1151
Clear(shadow_classdef);
1154
abstract_class_flag = 0;
1155
have_default_constructor = 0;
1156
shadow_constants_code = NewString("");
1157
this_shadow_baseclass = NewString("");
1158
this_shadow_extra_code = NewString("");
1159
this_shadow_interfaces = NewString("");
1160
this_shadow_import = NewString("");
1161
this_shadow_class_modifiers = NewString("");
1162
if(all_shadow_interfaces)
1163
Printv(this_shadow_interfaces, all_shadow_interfaces, 0);
1165
Language::classHandler(n);
1169
/* Deal with inheritance */
1170
List *baselist = Getattr(n,"bases");
1172
Node *base = Firstitem(baselist);
1173
String *basename = is_shadow(Getattr(base,"name"));
1175
Printv(this_shadow_baseclass, basename, 0);
1176
base = Nextitem(baselist);
1178
Printf(stderr, "Warning: %s inherits from multiple base classes. Multiple inheritance is not supported.\n", shadow_classname);
1182
emit_shadow_classdef();
1184
Printv(f_shadow, shadow_classdef, shadow_code, 0);
1186
// Write out all the enums and constants
1187
if (strlen(Char(shadow_constants_code)) != 0 )
1188
Printv(f_shadow, " // enums and constants\n", shadow_constants_code, 0);
1190
Printf(f_shadow, "}\n");
1194
free(shadow_classname);
1195
shadow_classname = NULL;
1197
Delete(shadow_constants_code); shadow_constants_code = NULL;
1198
Delete(this_shadow_baseclass); this_shadow_baseclass = NULL;
1199
Delete(this_shadow_extra_code); this_shadow_extra_code = NULL;
1200
Delete(this_shadow_interfaces); this_shadow_interfaces = NULL;
1201
Delete(this_shadow_import); this_shadow_import = NULL;
1202
Delete(this_shadow_class_modifiers); this_shadow_class_modifiers = NULL;
1208
int JAVA::memberfunctionHandler(Node *n) {
1209
Language::memberfunctionHandler(n);
1212
String* java_function_name = Swig_name_member(shadow_classname, Getattr(n, "sym:name"));
1213
Setattr(n,"java:shadfuncname", Getattr(n, "sym:name"));
1214
Setattr(n,"java:funcname", java_function_name);
1215
javashadowfunctionHandler(n, IsVirtual);
1220
int JAVA::staticmemberfunctionHandler(Node *n) {
1222
Language::staticmemberfunctionHandler(n);
1225
String* java_function_name = Swig_name_member(shadow_classname, Getattr(n,"sym:name"));
1226
Setattr(n,"java:shadfuncname", Getattr(n,"sym:name"));
1227
Setattr(n,"java:funcname", java_function_name);
1229
javashadowfunctionHandler(n, NOT_VIRTUAL);
1236
Function called for creating a java class wrapper function around a c++ function in the
1237
java wrapper class. Used for both static and non static functions.
1238
C++ static functions map to java static functions.
1239
Two extra attributes in the Node must be available. These are "java:shadfuncname" - the name of the java class shadow
1240
function, which in turn will call "java:funcname" - the java native function name which wraps the c++ function.
1242
void JAVA::javashadowfunctionHandler(Node* n, int is_virtual) {
1243
SwigType *t = Getattr(n,"type");
1244
ParmList *l = Getattr(n,"parms");
1245
String* java_function_name = Getattr(n,"java:funcname");
1246
String* java_shadow_function_name = Getattr(n,"java:shadfuncname");
1252
String *nativecall = NewString("");
1253
String *shadowrettype = NewString("");
1254
String *user_arrays = NewString("");
1255
int num_arguments = 0;
1256
int num_required = 0;
1261
if (SwigType_type(Getattr(l,"type")) == T_VOID) {
1266
/* Get java shadow types of the return.
1267
* The non-standard typemaps must first be attached to the parameter list. */
1268
Swig_typemap_attach_parms("jstype", l, 0);
1270
if ((tm = Swig_typemap_lookup_new("jstype",n,"",0))) {
1271
String *javaclassname = is_shadow(SwigType_base(Getattr(n,"type")));
1272
Replaceall(tm,"$javaclassname", javaclassname);
1273
Printf(shadowrettype,"%s", tm);
1275
Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(t,0));
1278
Printf(shadow_code, " public ");
1280
Printf(shadow_code, "static ");
1281
if (is_virtual == PURE_VIRTUAL)
1282
Printf(shadow_code, "abstract ");
1283
Printf(shadow_code, "%s %s(", shadowrettype, java_shadow_function_name);
1285
if(SwigType_type(t) == T_ARRAY && is_shadow(get_array_type(t))) {
1286
Printf(nativecall, "long[] cArray = ");
1289
if(SwigType_type(t) != T_VOID) {
1290
Printf(nativecall,"return ");
1293
Printv(nativecall, "new ", shadowrettype, "(", 0);
1297
Printv(nativecall, module, ".", java_function_name, "(", 0);
1299
Printv(nativecall, "_cPtr", 0);
1301
/* Get number of required and total arguments */
1302
num_arguments = emit_num_arguments(l);
1303
num_required = emit_num_required(l);
1305
int gencomma = !static_flag;
1307
/* Workaround to overcome Getignore(p) not working - p does not always have the Getignore
1308
attribute set. Noticeable when javashadowfunctionHandlerk is called from memberfunctionHandler() */
1309
Wrapper* f = NewWrapper();
1310
emit_args(NULL, l, f);
1312
/* Workaround end */
1314
/* Output each parameter */
1315
for (i = 0, p=l, jstypep=l; i < num_arguments; i++) {
1316
if(Getattr(p,"ignore")) continue;
1318
/* Ignore the 'this' argument for variable wrappers */
1319
if (!(variable_wrapper_flag && i==0))
1321
SwigType *pt = Getattr(p,"type");
1322
String *pn = Getattr(p,"name");
1323
String *javaparamtype = NewString("");
1325
/* Get the java type of the parameter */
1326
if ((tm = Getattr(jstypep,"tmap:jstype"))) {
1327
String *javaclassname = is_shadow(SwigType_base(pt));
1328
Replaceall(tm,"$javaclassname", javaclassname);
1329
jstypep = Getattr(jstypep,"tmap:jstype:next");
1330
Printv(javaparamtype, tm, 0);
1332
jstypep = nextSibling(jstypep);
1333
Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(pt,0));
1336
/* Create a name for the parameter */
1337
if(pn && *(Char(pn)))
1338
strcpy(arg,Char(pn));
1340
sprintf(arg,"arg%d",i);
1344
Printf(nativecall, ", ");
1346
if(SwigType_type(pt) == T_ARRAY && is_shadow(get_array_type(pt))) {
1347
Printv(user_arrays, tab4, "long[] $arg_cArray = new long[$arg.length];\n", 0);
1348
Printv(user_arrays, tab4, "for (int i=0; i<$arg.length; i++)\n", 0);
1349
Printv(user_arrays, tab4, " $arg_cArray[i] = $arg[i].getCPtr();\n", 0);
1350
Replace(user_arrays, "$arg", pn, DOH_REPLACE_ANY);
1352
Printv(nativecall, arg, "_cArray", 0);
1353
} else if (is_shadow(pt)) {
1354
Printv(nativecall, arg, ".getCPtr()", 0);
1355
} else Printv(nativecall, arg, 0);
1356
/* Add to java shadow function header */
1358
Printf(shadow_code, ", ");
1360
Printf(shadow_code, "%s %s", javaparamtype, arg);
1362
Delete(javaparamtype);
1365
jstypep = nextSibling(jstypep);
1371
if(SwigType_type(t) == T_ARRAY && is_shadow(get_array_type(t))) {
1372
String* array_ret = NewString("");
1373
String* array_type = NewString("");
1374
Printf(array_ret,");\n");
1375
Printv(array_ret, tab4, "$type[] arrayWrapper = new $type[cArray.length];\n", 0);
1376
Printv(array_ret, tab4, "for (int i=0; i<cArray.length; i++)\n", 0);
1377
Printv(array_ret, tab4, " arrayWrapper[i] = new $type(cArray[i], false);\n", 0);
1378
Printv(array_ret, tab4, "return arrayWrapper;\n", 0);
1380
// Hack the jstype typemap apart
1381
Printv(array_type, shadowrettype, 0);
1382
Replaceall(array_type,"[]", "");
1384
Replace(array_ret, "$type", array_type, DOH_REPLACE_ANY);
1385
Printv(nativecall, array_ret, 0);
1391
switch(SwigType_type(t)) {
1393
Printf(nativecall, "), true");
1397
if (NewObject) // %new indicating Java must take responsibility for memory ownership
1398
Printf(nativecall, "), true");
1400
Printf(nativecall, "), false");
1403
Printf(stderr, "Internal Error: unknown shadow type: %s\n", SwigType_str(t,0));
1407
Printf(nativecall,");\n");
1410
if (is_virtual == PURE_VIRTUAL) {
1411
Printf(shadow_code, ");\n\n");
1412
abstract_class_flag = 1;
1415
Printf(shadow_code, ") {\n");
1416
Printv(shadow_code, user_arrays, 0);
1417
Printf(shadow_code, " %s", nativecall);
1418
Printf(shadow_code, " }\n\n");
1421
Delete(shadowrettype);
1423
Delete(user_arrays);
1426
int JAVA::constructorHandler(Node *n) {
1428
char *iname = GetChar(n,"sym:name");
1429
ParmList *l = Getattr(n,"parms");
1434
int num_arguments = 0;
1435
int num_required = 0;
1437
Language::constructorHandler(n);
1440
String *nativecall = NewString("");
1443
Printf(shadow_code, " public %s(", shadow_classname);
1445
Printv(nativecall, "$superconstructorcall", 0); // Super call for filling in later.
1447
Printv(nativecall, tab4, "_cPtr = ", module, ".", Swig_name_construct(iname), "(", 0);
1449
Printv(nativecall, tab4, "_cPtr = ", module, ".", Swig_name_construct(shadow_classname), "(", 0);
1451
int pcount = ParmList_len(l);
1452
if(pcount == 0) // We must have a default constructor
1453
have_default_constructor = 1;
1455
Swig_typemap_attach_parms("jstype", l, 0);
1457
/* Get number of required and total arguments */
1458
num_arguments = emit_num_arguments(l);
1459
num_required = emit_num_required(l);
1461
for (i = 0, p=l, jstypep=l; i < num_arguments; i++) {
1462
SwigType *pt = Getattr(p,"type");
1463
String *pn = Getattr(p,"name");
1464
String *javaparamtype = NewString("");
1466
/* Create a name for the parameter */
1467
if(pn && *(Char(pn)))
1468
strcpy(arg,Char(pn));
1470
sprintf(arg,"arg%d",i);
1474
Printv(nativecall, arg, ".getCPtr()", 0);
1475
} else Printv(nativecall, arg, 0);
1477
/* Get the java type of the parameter */
1478
if ((tm = Getattr(jstypep,"tmap:jstype"))) {
1479
String *javaclassname = is_shadow(SwigType_base(pt));
1480
Replaceall(tm,"$javaclassname", javaclassname);
1481
jstypep = Getattr(jstypep,"tmap:jstype:next");
1482
Printv(javaparamtype, tm, 0);
1484
jstypep = nextSibling(jstypep);
1485
Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(pt,0));
1488
/* Add to java shadow function header */
1489
Printf(shadow_code, "%s %s", javaparamtype, arg);
1492
Printf(nativecall, ", ");
1493
Printf(shadow_code, ", ");
1496
Delete(javaparamtype);
1499
Printf(shadow_code, ") {\n");
1502
tab4, "_cMemOwn = true;\n",
1505
Printf(shadow_code, "%s", nativecall);
1506
Printf(shadow_code, " }\n\n");
1512
int JAVA::destructorHandler(Node *n) {
1513
Language::destructorHandler(n);
1517
Printf(shadow_code, " protected void finalize() {\n");
1518
Printf(shadow_code, " _delete();\n");
1519
Printf(shadow_code, " }\n\n");
1522
Printf(shadow_code, " public void _delete() {\n");
1523
Printf(shadow_code, " if(_cPtr!=0 && _cMemOwn) {\n");
1524
Printf(shadow_code, " %s.%s(_cPtr);\n", module, Swig_name_destroy(shadow_classname));
1525
Printf(shadow_code, " _cPtr = 0;\n");
1526
Printf(shadow_code, " }\n");
1527
Printf(shadow_code, " }\n\n");
1532
/* This function does the equivalent of
1533
* %apply type *tmap { name * }; when additions=pointer or
1534
* %apply type &tmap { name & }; when additions=reference
1535
* %apply type tmap[ANY] { name [ANY] }; when array_flag set etc... */
1536
void JAVA::TypemapApply(String *type, String *tmap, String *name, type_additions additions, int array_flag)
1538
String* nametemp = Copy(name);
1539
String* swigtypetemp = Copy(type);
1541
if (additions == pointer) {
1542
SwigType_add_pointer(swigtypetemp);
1543
SwigType_add_pointer(nametemp);
1545
if (additions == reference) {
1546
SwigType_add_reference(swigtypetemp);
1547
SwigType_add_reference(nametemp);
1550
SwigType_add_array(swigtypetemp, (char *)"ANY");
1551
SwigType_add_array(nametemp, (char *)"ANY");
1554
Parm *srcpat = NewParm(swigtypetemp,tmap);
1555
Parm *destpat = NewParm(nametemp,0);
1556
Swig_typemap_apply(srcpat,destpat);
1558
Delete(swigtypetemp);
1561
int JAVA::classforwardDeclaration(Node *n) {
1562
String *name = Getattr(n,"name");
1563
String *kind = Getattr(n,"kind");
1564
String *class_tmap = NewString("CLASS");
1565
String *array_tmap = NewString("ARRAYSOFCLASSPOINTERS");
1566
String *swigtype = NewString("SWIGTYPE");
1568
/* Add to the hash table of shadow classes */
1570
String *shadowclassname = Getattr(n,"sym:name");
1571
Setattr(shadow_classes,name,shadowclassname);
1572
if (kind && (Len(kind) > 0)) {
1573
String *stype = NewStringf("%s %s",kind,name);
1574
Setattr(shadow_classes,stype,shadowclassname);
1579
/* Apply typemaps for handling pointers and references for all known classes/structs/unions. Also for
1580
* arrays of these. This is a workaround because SWIG does not have a default SWIGTYPE * or SWIGTYPE &. */
1581
TypemapApply(swigtype, class_tmap, name, pointer, 0); //%apply SWIGTYPE *CLASS {name*};
1582
TypemapApply(swigtype, class_tmap, name, reference, 0); //%apply SWIGTYPE &CLASS {name&};
1583
TypemapApply(swigtype, array_tmap, name, pointer, 1); //%apply SWIGTYPE *ARRAYSOFCLASSPOINTERS[ANY] {name*[ANY]};
1585
/* More typemap applying for types declared with the kind eg struct, union or class.
1586
For example when type is declared as 'struct name'. */
1587
if (Cmp(Getattr(n,"storage"), "typedef") != 0) {
1588
String *namewithkind = NewString("");
1589
Printf(namewithkind, "%s %s", Getattr(n,"kind"), name);
1590
TypemapApply(swigtype, class_tmap, namewithkind, pointer, 0); //%apply SWIGTYPE *CLASS {kind name*};
1591
TypemapApply(swigtype, class_tmap, namewithkind, reference, 0); //%apply SWIGTYPE &CLASS {kind name&};
1592
TypemapApply(swigtype, array_tmap, namewithkind, pointer, 1); //%apply SWIGTYPE *ARRAYSOFCLASSPOINTERS[ANY] {kind name*[ANY]};
1593
Delete(namewithkind);
1601
int JAVA::membervariableHandler(Node *n) {
1602
shadow_variable_name = GetChar(n,"sym:name");
1603
wrapping_member = 1;
1604
variable_wrapper_flag = 1;
1605
Language::membervariableHandler(n);
1606
wrapping_member = 0;
1607
variable_wrapper_flag = 0;
1611
int JAVA::staticmembervariableHandler(Node *n) {
1612
shadow_variable_name = GetChar(n,"sym:name");
1613
wrapping_member = 1;
1615
Language::staticmembervariableHandler(n);
1616
wrapping_member = 0;
1621
int JAVA::memberconstantHandler(Node *n) {
1622
shadow_variable_name = GetChar(n,"sym:name");
1623
wrapping_member = 1;
1624
Language::memberconstantHandler(n);
1625
wrapping_member = 0;