~ubuntu-branches/ubuntu/maverick/swig1.3/maverick

« back to all changes in this revision

Viewing changes to Source/Modules1.1/java.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Landschoff
  • Date: 2002-03-29 01:56:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020329015607-c0wt03xu8oy9ioj7
Tags: upstream-1.3.11
ImportĀ upstreamĀ versionĀ 1.3.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -----------------------------------------------------------------------------
 
2
 * java.cxx
 
3
 *
 
4
 *     Java wrapper module.
 
5
 *
 
6
 * Copyright (C) 1999-2000.  The University of Chicago
 
7
 * See the file LICENSE for information on usage and redistribution.
 
8
 * ----------------------------------------------------------------------------- */
 
9
 
 
10
#include <ctype.h>
 
11
 
 
12
#include "mod11.h"
 
13
#include "java.h"
 
14
 
 
15
char bigbuf[1024];
 
16
 
 
17
static char *usage = (char*)"\
 
18
Java Options\n\
 
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";
 
25
 
 
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;
 
34
 
 
35
static  File         *f_runtime = 0;
 
36
static  File         *f_header = 0;
 
37
static  File         *f_wrappers = 0;
 
38
static  File         *f_init = 0;
 
39
 
 
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
 
75
 
 
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));
 
80
}
 
81
 
 
82
// Return the type of the c array
 
83
static SwigType *get_array_type(SwigType *t) {
 
84
  SwigType *ta = 0;
 
85
  if (SwigType_type(t) == T_ARRAY) {
 
86
    SwigType *aop;
 
87
    ta = Copy(t);
 
88
    aop = SwigType_pop(ta);
 
89
  }
 
90
  return ta;
 
91
}
 
92
 
 
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*)"???";
 
113
            case T_VOID:
 
114
        case T_USER:   if(inShadow && is_shadow(t))
 
115
                         return Char(is_shadow(t));
 
116
                       else return (char*)"J";
 
117
          }
 
118
  } else if(SwigType_ispointer(t) > 1) {
 
119
    if(ret) return (char*)"J";
 
120
    else return (char*)"[J";
 
121
  } else {
 
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";
 
141
          }
 
142
  }
 
143
  Printf(stderr, "JavaMethodSignature: unhandled SWIG type [%d] %s\n", SwigType_type(t), SwigType_str(t,0));
 
144
  return NULL;
 
145
}
 
146
 
 
147
char *JAVA::makeValidJniName(const char *name) {
 
148
  const char *c = name;
 
149
  char *b = bigbuf;
 
150
 
 
151
  while(*c) {
 
152
    *b++ = *c;
 
153
    if(*c == '_')
 
154
      *b++ = '1';
 
155
    c++;
 
156
  }
 
157
  *b = '\0';
 
158
 
 
159
  return Swig_copy_string(bigbuf);
 
160
}
 
161
 
 
162
// !! this approach fails for functions without arguments
 
163
char *JAVA::JNICALL(String_or_char *func) {
 
164
  if(jnic)
 
165
        sprintf(bigbuf, "(*jenv)->%s(jenv, ", Char(func));
 
166
  else
 
167
        sprintf(bigbuf, "jenv->%s(", Char(func));
 
168
 
 
169
  return Swig_copy_string(bigbuf);
 
170
}
 
171
 
 
172
void JAVA::writeRegisterNatives()
 
173
{
 
174
  if(Len(registerNativesList) == 0)
 
175
    return;
 
176
 
 
177
  Printf(f_wrappers,"\n");
 
178
  Printf(f_wrappers,"JNINativeMethod nativeMethods[] = {\n");
 
179
  Printv(f_wrappers, registerNativesList, 0);
 
180
  Printf(f_wrappers, "};\n");
 
181
 
 
182
  Printf(f_wrappers,"\nint numberOfNativeMethods=sizeof(nativeMethods)/sizeof(JNINativeMethod);\n\n");
 
183
 
 
184
  // The registerNatives function
 
185
 
 
186
  Printv(f_wrappers,
 
187
         "jint registerNatives(JNIEnv *jenv) {", "\n",
 
188
         tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
 
189
         "\"", jni_pkgstr, module, "\");","\n",
 
190
         0);
 
191
 
 
192
  Printv(f_wrappers,
 
193
         tab4, "if (nativeClass == 0)", "\n", tab8, "return -1;", "\n",
 
194
         tab4, "return ", JNICALL((char*)"RegisterNatives"), "nativeClass, nativeMethods, ", "numberOfNativeMethods);", "\n",
 
195
         "}", "\n", "\n",
 
196
         0);
 
197
 
 
198
  // The unregisterNatives function
 
199
 
 
200
  Printv(f_wrappers,
 
201
         "jint unregisterNatives(JNIEnv *jenv) {", "\n",
 
202
         tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
 
203
         "\"", jni_pkgstr, module, "\");","\n",
 
204
         0);
 
205
 
 
206
  Printv(f_wrappers,
 
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",
 
212
         "}", "\n",
 
213
         0);
 
214
}
 
215
 
 
216
// ---------------------------------------------------------------------
 
217
// JAVA::main()
 
218
//
 
219
// Parse my command line options and initialize by variables.
 
220
// ---------------------------------------------------------------------
 
221
 
 
222
void JAVA::main(int argc, char *argv[]) {
 
223
 
 
224
  SWIG_library_directory("java");
 
225
 
 
226
  // Look for certain command line options
 
227
  for (int i = 1; i < argc; i++) {
 
228
    if (argv[i]) {
 
229
      if (strcmp(argv[i],"-package") == 0) {
 
230
        if (argv[i+1]) {
 
231
          package = new char[strlen(argv[i+1])+1];
 
232
          strcpy(package, argv[i+1]);
 
233
          Swig_mark_arg(i);
 
234
          Swig_mark_arg(i+1);
 
235
          i++;
 
236
        } else {
 
237
          Swig_arg_error();
 
238
        }
 
239
      } else if (strcmp(argv[i],"-shadow") == 0) {
 
240
            Swig_mark_arg(i);
 
241
        shadow = 1;
 
242
      } else if (strcmp(argv[i],"-jnic") == 0) {
 
243
            Swig_mark_arg(i);
 
244
        jnic = 1;
 
245
      } else if (strcmp(argv[i],"-nofinalize") == 0) {
 
246
            Swig_mark_arg(i);
 
247
        nofinalize = 1;
 
248
      } else if (strcmp(argv[i],"-rn") == 0) {
 
249
            Swig_mark_arg(i);
 
250
        useRegisterNatives = 1;
 
251
      } else if (strcmp(argv[i],"-jnicpp") == 0) {
 
252
        Swig_mark_arg(i);
 
253
            jnic = 0;
 
254
      } else if (strcmp(argv[i],"-help") == 0) {
 
255
            Printf(stderr,"%s\n", usage);
 
256
      }
 
257
    }
 
258
  }
 
259
 
 
260
  if(jnic == -1) {
 
261
    if(CPlusPlus)
 
262
        jnic = 0;
 
263
    else jnic = 1;
 
264
  }
 
265
 
 
266
  // Add a symbol to the parser for conditional compilation
 
267
  Preprocessor_define((void *) "SWIGJAVA 1",0);
 
268
 
 
269
  // Add typemap definitions
 
270
  SWIG_typemap_lang("java");
 
271
  SWIG_config_file("java.swg");
 
272
}
 
273
 
 
274
// ---------------------------------------------------------------------
 
275
// void JAVA::top()
 
276
// ---------------------------------------------------------------------
 
277
 
 
278
int JAVA::top(Node *n) {
 
279
 
 
280
  /* Initialize all of the output files */
 
281
  String *outfile = Getattr(n,"outfile");
 
282
 
 
283
  f_runtime = NewFile(outfile,"w");
 
284
  if (!f_runtime) {
 
285
    Printf(stderr,"*** Can't open '%s'\n", outfile);
 
286
    SWIG_exit(EXIT_FAILURE);
 
287
  }
 
288
  f_init = NewString("");
 
289
  f_header = NewString("");
 
290
  f_wrappers = NewString("");
 
291
 
 
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);
 
297
 
 
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");
 
314
 
 
315
  Swig_banner(f_runtime);               // Print the SWIG banner message
 
316
  Printf(f_runtime,"/* Implementation : Java */\n\n");
 
317
 
 
318
  set_module(Char(Getattr(n,"name")));
 
319
 
 
320
  String* wrapper_name = NewString("");
 
321
 
 
322
  if(package) {
 
323
    String *s = NewString(package);
 
324
    char *jniname = makeValidJniName(Char(s));
 
325
    Clear(s);
 
326
    Printv(s, jniname, 0);
 
327
    free(jniname);
 
328
    Replace(s,".","_", DOH_REPLACE_ANY);
 
329
    Append(s, "_");
 
330
    c_pkgstr = Swig_copy_string(Char(s));
 
331
    Delete(s);
 
332
 
 
333
    String *s2 = NewString(package);
 
334
    Replace(s2,".","/", DOH_REPLACE_ANY);
 
335
    Append(s2,"/");
 
336
    jni_pkgstr = Swig_copy_string(Char(s2));
 
337
    Delete(s2);
 
338
  } else {
 
339
    package = c_pkgstr = jni_pkgstr = (char*)"";
 
340
  }
 
341
 
 
342
  char *jniname = makeValidJniName(module);
 
343
  Printf(wrapper_name, "Java_%s%s_%%f", c_pkgstr, jniname);
 
344
  free(jniname);
 
345
 
 
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");
 
350
 
 
351
  Delete(wrapper_name);
 
352
 
 
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);
 
357
    SWIG_exit(1);
 
358
  }
 
359
 
 
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);
 
364
  } else {
 
365
    Printf(f_header, "#define J_PACKAGE\n");
 
366
  }
 
367
 
 
368
  /* Emit all nodes */
 
369
  Language::top(n);
 
370
 
 
371
  if(!classdef_emitted) emit_classdef();
 
372
 
 
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);
 
376
 
 
377
  // Finish off the java class
 
378
  Printf(f_java, "}\n");
 
379
  fclose(f_java);
 
380
 
 
381
  if(useRegisterNatives)
 
382
        writeRegisterNatives();
 
383
 
 
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;
 
400
 
 
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);
 
405
  Delete(f_header);
 
406
  Delete(f_wrappers);
 
407
  Delete(f_init);
 
408
  Close(f_runtime);
 
409
  Delete(f_runtime);
 
410
  return SWIG_OK;
 
411
}
 
412
 
 
413
// ---------------------------------------------------------------------
 
414
// JAVA::set_module(char *mod_name)
 
415
//
 
416
// Sets the module name.  Does nothing if it's already set (so it can
 
417
// be overriddent as a command line option).
 
418
//
 
419
//----------------------------------------------------------------------
 
420
 
 
421
void JAVA::set_module(char *mod_name) {
 
422
  if (module) return;
 
423
  module = new char[strlen(mod_name)+1];
 
424
  strcpy(module,mod_name);
 
425
}
 
426
 
 
427
 
 
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);
 
432
  Printf(f, " *\n");
 
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");
 
436
}
 
437
 
 
438
void JAVA::emit_classdef() {
 
439
  if(!classdef_emitted) {
 
440
    emit_banner(f_java);
 
441
    if(module_import)
 
442
      Printf(f_java, "%s\n", module_import);
 
443
 
 
444
    if (module_class_modifiers && *Char(module_class_modifiers))
 
445
      Printf(f_java, "%s", module_class_modifiers);
 
446
    else
 
447
      Printf(f_java, "public");
 
448
    Printf(f_java, " class %s ", module);
 
449
 
 
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);
 
457
  }
 
458
  classdef_emitted = 1;
 
459
}
 
460
 
 
461
int JAVA::nativeWrapper(Node *n) {
 
462
 
 
463
  Swig_save(&n,"name",0);
 
464
  Setattr(n,"name", Getattr(n,"wrap:name"));
 
465
  native_func = 1;
 
466
  functionWrapper(n);
 
467
  Swig_restore(&n);
 
468
  native_func = 0;
 
469
  return SWIG_OK;
 
470
}
 
471
 
 
472
// ----------------------------------------------------------------------
 
473
// JAVA::functionWrapper()
 
474
//
 
475
// Create a function declaration and register it with the interpreter.
 
476
// ----------------------------------------------------------------------
 
477
 
 
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");
 
483
  char      source[256];
 
484
  String    *tm;
 
485
  Parm      *p;
 
486
  Parm      *jnip;
 
487
  Parm      *jtypep;
 
488
  int       i;
 
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;
 
498
 
 
499
  /* 
 
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.
 
502
  */
 
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);
 
511
 
 
512
    Setattr(n,"java:shadfuncname", member_function_name);
 
513
    Setattr(n,"java:funcname", iname);
 
514
    javashadowfunctionHandler(n, NOT_VIRTUAL);
 
515
 
 
516
    Delete(java_function_name);
 
517
    Delete(member_function_name);
 
518
  }
 
519
 
 
520
  /*
 
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.
 
524
  */
 
525
 
 
526
  // A new wrapper function object
 
527
  Wrapper *f = NewWrapper();
 
528
 
 
529
  if(!classdef_emitted) emit_classdef();
 
530
 
 
531
  // Make a wrapper name for this function
 
532
  char *jniname = makeValidJniName(iname);
 
533
  String *wname = Swig_name_wrapper(jniname);
 
534
  free(jniname);
 
535
 
 
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);
 
540
  
 
541
  if ((tm = Swig_typemap_lookup_new("jni",n,"",0))) {
 
542
    Printf(jnirettype,"%s", tm);
 
543
  } else {
 
544
    Printf(stderr, "No jni typemap defined for %s\n", SwigType_str(t,0));
 
545
  }
 
546
 
 
547
  if ((tm = Swig_typemap_lookup_new("jtype",n,"",0))) {
 
548
    Printf(javarettype,"%s", tm);
 
549
  } else {
 
550
    Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(t,0));
 
551
  }
 
552
 
 
553
  // If dumping the registerNative outputs, store the method return type
 
554
  // signature
 
555
  if (useRegisterNatives) {
 
556
      javaReturnSignature = JavaMethodSignature(t, 1, 0);
 
557
  }
 
558
 
 
559
  if (SwigType_type(t) != T_VOID) {
 
560
         Wrapper_add_localv(f,"jresult", jnirettype, "jresult = 0",0);
 
561
  }
 
562
 
 
563
  Printf(f_java, "  %s ", module_method_modifiers);
 
564
  Printf(f_java, "native %s %s(", javarettype, iname);
 
565
 
 
566
  if(!jnic) 
 
567
    Printv(f->def, "extern \"C\"{\n", 0);
 
568
  Printv(f->def, "JNIEXPORT ", jnirettype, " JNICALL ", wname, "(JNIEnv *jenv, jclass jcls", 0);
 
569
 
 
570
  // Emit all of the local variables for holding arguments.
 
571
  emit_args(t,l,f);
 
572
 
 
573
  /* Attach the standard typemaps */
 
574
  emit_attach_parmmaps(l,f);
 
575
 
 
576
  /* Get number of required and total arguments */
 
577
  num_arguments = emit_num_arguments(l);
 
578
  num_required  = emit_num_required(l);
 
579
 
 
580
  int gencomma = 0;
 
581
 
 
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++) {
 
584
    
 
585
    while (Getattr(p,"tmap:ignore")) {
 
586
      p = Getattr(p,"tmap:ignore:next");
 
587
    }
 
588
 
 
589
    SwigType *pt = Getattr(p,"type");
 
590
    String   *ln = Getattr(p,"lname");
 
591
    String   *javaparamtype = NewString("");
 
592
    String   *jni_param_type = NewString("");
 
593
 
 
594
    sprintf(source,"j%s", Char(ln));
 
595
 
 
596
    if (useRegisterNatives) {
 
597
      Printv(javaParameterSignature, JavaMethodSignature(pt, 0, 0), 0);
 
598
    }
 
599
 
 
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);
 
604
    } else {
 
605
      jnip = nextSibling(jnip);
 
606
      Printf(stderr, "No jni typemap defined for %s\n", SwigType_str(pt,0));
 
607
    }
 
608
 
 
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);
 
613
    } else {
 
614
      jtypep = nextSibling(jtypep);
 
615
      Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(pt,0));
 
616
    }
 
617
 
 
618
    /* Add to java function header */
 
619
    if(gencomma) Printf(f_java, ", ");
 
620
    Printf(f_java, "%s %s", javaparamtype, source);
 
621
 
 
622
    gencomma = 1;
 
623
 
 
624
    // Add to Jni function header
 
625
    Printv(f->def, ", ", jni_param_type, " ", source, 0);
 
626
 
 
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");
 
636
    } else {
 
637
      Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0));
 
638
      p = nextSibling(p);
 
639
    }
 
640
    Delete(javaparamtype);
 
641
    Delete(jni_param_type);
 
642
  }
 
643
 
 
644
  /* Insert constraint checking code */
 
645
  for (p = l; p;) {
 
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");
 
652
    } else {
 
653
      p = nextSibling(p);
 
654
    }
 
655
  }
 
656
  
 
657
  /* Insert cleanup code */
 
658
  for (p = l; p;) {
 
659
    while (Getattr(p,"tmap:ignore")) {
 
660
      p = Getattr(p,"tmap:ignore:next");
 
661
    }
 
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");
 
668
    } else {
 
669
      p = nextSibling(p);
 
670
    }
 
671
  }
 
672
 
 
673
  /* Insert argument output code */
 
674
  for (p = l; p;) {
 
675
    while (Getattr(p,"tmap:ignore")) {
 
676
      p = Getattr(p,"tmap:ignore:next");
 
677
    }
 
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");
 
686
    } else {
 
687
      p = nextSibling(p);
 
688
    }
 
689
  }
 
690
 
 
691
  Printf(f_java, ");\n");
 
692
  Printf(f->def,") {");
 
693
 
 
694
  // Now write code to make the function call
 
695
  if(!native_func)
 
696
        emit_action(n,f);
 
697
 
 
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);
 
705
    } else {
 
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);
 
707
    }
 
708
  }
 
709
 
 
710
  /* Output argument output code */
 
711
  Printv(f->code,outarg,0);
 
712
 
 
713
  /* Output cleanup code */
 
714
  Printv(f->code,cleanup,0);
 
715
 
 
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);
 
721
    }
 
722
  }
 
723
 
 
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);
 
729
    }
 
730
  }
 
731
 
 
732
  if(SwigType_type(t) != T_VOID)
 
733
    Printv(f->code, tab4, "return jresult;\n", 0);
 
734
  if(!jnic)
 
735
    Printf(f->code, "}");
 
736
  Printf(f->code, "}\n");
 
737
 
 
738
  /* Substitute the cleanup code */
 
739
  Replaceall(f->code,"$cleanup",cleanup);
 
740
 
 
741
  if(SwigType_type(t) != T_VOID)
 
742
    Replaceall(f->code,"$null","0");
 
743
  else
 
744
    Replaceall(f->code,"$null","");
 
745
 
 
746
  /* Dump the function out */
 
747
  if(!native_func)
 
748
        Wrapper_print(f,f_wrappers);
 
749
 
 
750
 // If registerNatives is active, store the table entry for this method
 
751
  if (useRegisterNatives) {
 
752
    Printv(registerNativesList,
 
753
           tab4, "{",
 
754
           "\"", name, "\", \"(", javaParameterSignature, ")", javaReturnSignature, "\", ", wname,
 
755
           "},\n",
 
756
           0);
 
757
 
 
758
  }
 
759
 
 
760
  Delete(jnirettype);
 
761
  Delete(javarettype);
 
762
  Delete(cleanup);
 
763
  Delete(outarg);
 
764
  Delete(body);
 
765
  Delete(javaParameterSignature);
 
766
  DelWrapper(f);
 
767
  return SWIG_OK;
 
768
}
 
769
 
 
770
// -----------------------------------------------------------------------
 
771
// JAVA::variableWrapper()
 
772
//
 
773
// Create a JAVA link to a C variable.
 
774
// -----------------------------------------------------------------------
 
775
 
 
776
int JAVA::variableWrapper(Node *n)
 
777
{
 
778
  Language::variableWrapper(n);           /* Default to functions */
 
779
  return SWIG_OK;
 
780
}
 
781
 
 
782
 
 
783
// -----------------------------------------------------------------------
 
784
// JAVA::enumDeclaration()
 
785
// ------------------------------------------------------------------------
 
786
 
 
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");
 
792
 
 
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);
 
799
  Delete(s1);
 
800
  Delete(s2);
 
801
  Delete(swigtype);
 
802
}
 
803
 
 
804
// -----------------------------------------------------------------------
 
805
// JAVA::constantWrapper()
 
806
// ------------------------------------------------------------------------
 
807
 
 
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");
 
814
 
 
815
  int OldReadOnly = ReadOnly;
 
816
  String *tm;
 
817
  char *jname;
 
818
  DOH *jout;
 
819
  String *constants_code;
 
820
  ReadOnly = 1;
 
821
  String *java_type = NewString("");
 
822
 
 
823
  if(!classdef_emitted) emit_classdef();
 
824
 
 
825
  if(shadow && wrapping_member) {
 
826
    jout = shadow_code;
 
827
    jname = shadow_variable_name;
 
828
    constants_code = shadow_constants_code;
 
829
  } else {
 
830
    jout = f_java;
 
831
    jname = name;
 
832
    constants_code = module_constants_code;
 
833
  }
 
834
 
 
835
  if ((tm = Swig_typemap_lookup_new("const",n,"",0))) {
 
836
    String *str = tm;
 
837
    Replace(str,"$value",value, DOH_REPLACE_ANY);
 
838
    Printf(jout,"  %s\n\n", str);
 
839
    Delete(tm);
 
840
  } else {
 
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);
 
848
      } else {
 
849
        Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(type,0));
 
850
      }
 
851
    } else {
 
852
      Swig_typemap_attach_parms("jtype", l, 0);
 
853
      if ((tm = Swig_typemap_lookup_new("jtype",n,"",0))) {
 
854
        Printf(java_type,"%s", tm);
 
855
      } else {
 
856
        Printf(stderr, "No jtype typemap defined for %s\n", SwigType_str(type,0));
 
857
      }
 
858
    }
 
859
    if(strcmp(jname, value) == 0 || strstr(value,"::") != NULL) {
 
860
      /* 
 
861
      We have found an enum.  The enum implementation is done using a public final static int in Java.
 
862
      */
 
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));
 
866
      }
 
867
      else {
 
868
        Printf(module_constants_code, "  public final static %s %s = %s();\n", java_type, jname, Swig_name_get(iname));
 
869
      }
 
870
      enum_flag = 1;
 
871
      variableWrapper(n);
 
872
      enum_flag = 0;
 
873
    } else {
 
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);
 
878
      else
 
879
        Printf(constants_code, "  public final static %s %s = %s;\n", java_type, jname, value);
 
880
    }
 
881
  }
 
882
  Delete(java_type);
 
883
  ReadOnly = OldReadOnly;
 
884
  return SWIG_OK;
 
885
}
 
886
/*
 
887
Valid Pragmas:
 
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
 
900
*/
 
901
 
 
902
/* 
 
903
C++ pragmas: pragmas declared within a class or c struct for the shadow class. 
 
904
These pragmas start with 'shadow'
 
905
Valid pragmas:
 
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
 
911
*/
 
912
 
 
913
void JAVA::pragma(char *lang, char *code, char *value) {
 
914
  if(strcmp(lang, "java") != 0) return;
 
915
 
 
916
  String *strvalue = NewString(value);
 
917
  Replace(strvalue,"\\\"", "\"", DOH_REPLACE_ANY);
 
918
 
 
919
  if(strcmp(code, "moduleimport") == 0) {
 
920
    Printf(module_import, "import %s;\n", strvalue);
 
921
  } 
 
922
  else if(strcmp(code, "allshadowimport") == 0) {
 
923
    if(shadow && all_shadow_import)
 
924
      Printf(all_shadow_import, "import %s;\n", strvalue);
 
925
  } 
 
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);
 
928
  }
 
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);
 
933
  } 
 
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);
 
939
    }
 
940
  } 
 
941
  else if(strcmp(code, "modulebase") == 0) {
 
942
    if(shadow && module_baseclass)
 
943
      Printf(module_baseclass, "%s", strvalue);
 
944
  } 
 
945
  else if(strcmp(code, "allshadowbase") == 0) {
 
946
    if(shadow && all_shadow_baseclass)
 
947
      Printf(all_shadow_baseclass, "%s", strvalue);
 
948
  } 
 
949
  else if(strcmp(code, "moduleinterface") == 0) {
 
950
    if(shadow && module_interfaces)
 
951
      if (!*Char(module_interfaces))
 
952
        Printf(module_interfaces, "implements %s", strvalue);
 
953
      else
 
954
        Printf(module_interfaces, ", %s", strvalue);
 
955
  } 
 
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);
 
960
      else
 
961
        Printf(all_shadow_interfaces, ", %s", strvalue);
 
962
    }
 
963
  } 
 
964
  else if(strcmp(code, "allshadowclassmodifiers") == 0) {
 
965
    if(shadow && all_shadow_class_modifiers)
 
966
      Printv(all_shadow_class_modifiers, strvalue, 0);
 
967
  } 
 
968
  else if(strcmp(code, "moduleclassmodifiers") == 0) {
 
969
    if(shadow && module_class_modifiers)
 
970
      Printv(module_class_modifiers, strvalue, 0);
 
971
  } 
 
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);
 
977
  } else if (shadow) {
 
978
    if (strcmp(code,"shadowcode") == 0) {
 
979
      if (f_shadow && shadow_code)
 
980
        Printf(shadow_code, "%s\n", strvalue);
 
981
      else
 
982
        Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
 
983
    } 
 
984
    else if (strcmp(code,"shadowimport") == 0) {
 
985
      if (this_shadow_import)
 
986
        Printf(this_shadow_import, "import %s;\n", strvalue);
 
987
      else
 
988
        Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
 
989
    } 
 
990
    else if (strcmp(code,"shadowbase") == 0) {
 
991
      if (this_shadow_baseclass)
 
992
        Printf(this_shadow_baseclass, "%s", strvalue);
 
993
      else
 
994
        Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
 
995
    } 
 
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);
 
1000
        else
 
1001
          Printf(this_shadow_interfaces, ", %s", strvalue);
 
1002
      }
 
1003
      else
 
1004
        Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
 
1005
    } 
 
1006
    else if (strcmp(code,"shadowclassmodifiers") == 0) {
 
1007
      if (this_shadow_class_modifiers)
 
1008
        Printv(this_shadow_class_modifiers, strvalue, 0);
 
1009
      else
 
1010
        Printf(stderr,"%s : Line %d. Out of scope pragma.\n", input_file, line_number);
 
1011
    }  else {
 
1012
      Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
 
1013
    }
 
1014
  } else {
 
1015
    Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file, line_number);
 
1016
  }
 
1017
  Delete(strvalue);
 
1018
}
 
1019
 
 
1020
void JAVA::emit_shadow_classdef() {
 
1021
  String* baseclass = NULL;
 
1022
 
 
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");
 
1029
 
 
1030
  // Class modifiers
 
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);
 
1035
  else
 
1036
    Printv(shadow_classdef, "public", 0);
 
1037
  if (abstract_class_flag)
 
1038
    Printv(shadow_classdef, " abstract", 0);
 
1039
  Printf(shadow_classdef, " class $class ");
 
1040
 
 
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;
 
1045
  }
 
1046
  if(all_shadow_baseclass && *Char(all_shadow_baseclass)) {
 
1047
    Printf(shadow_classdef, "extends %s ", all_shadow_baseclass);
 
1048
    baseclass = all_shadow_baseclass;
 
1049
  }
 
1050
 
 
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");
 
1055
 
 
1056
  //Display warning on attempt to use multiple inheritance
 
1057
  char* search_str = Char(shadow_classdef);
 
1058
  int count = 0;
 
1059
  while((search_str = strstr(search_str, "extends"))) {
 
1060
    search_str += strlen("extends");
 
1061
    count++;
 
1062
  }
 
1063
  if (count > 1)
 
1064
    Printf(stderr, "Warning for shadow class %s: Multiple inheritance is not supported in Java.\n", shadow_classname);
 
1065
 
 
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",
 
1071
      "  }\n",
 
1072
      "\n", 0);
 
1073
 
 
1074
    if(!have_default_constructor) {
 
1075
      Printv(shadow_classdef, 
 
1076
        "  protected $class() {\n",
 
1077
        "  }\n",
 
1078
        "\n", 0);
 
1079
    }
 
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);
 
1082
  }
 
1083
  else {
 
1084
    Printv(shadow_classdef,
 
1085
      "  protected long _cPtr;\n",
 
1086
      "  protected boolean _cMemOwn;\n",
 
1087
      "\n",
 
1088
      "  public $class(long cPointer, boolean cMemoryOwn) {\n",
 
1089
      "    _cPtr = cPointer;\n",
 
1090
      "    _cMemOwn = cMemoryOwn;\n",
 
1091
      "  }\n",
 
1092
      "\n",
 
1093
      "  public long getCPtr() {\n",
 
1094
      "    return _cPtr;\n",
 
1095
      "  }\n",
 
1096
      "\n", 0);
 
1097
 
 
1098
    if(!have_default_constructor) {
 
1099
      Printv(shadow_classdef, 
 
1100
        "  protected $class() {\n",
 
1101
        "    _cPtr = 0;\n",
 
1102
        "    _cMemOwn = false;\n",
 
1103
        "  }\n",
 
1104
        "\n", 0);
 
1105
    }
 
1106
    // No explicit super constructor call as this class does not have a SWIG base class.
 
1107
    Replace(shadow_code, "$superconstructorcall", "", DOH_REPLACE_ANY);
 
1108
  }
 
1109
 
 
1110
  Replace(shadow_classdef, "$class", shadow_classname, DOH_REPLACE_ANY);
 
1111
 
 
1112
  if (all_shadow_extra_code)
 
1113
    Printv(shadow_classdef, all_shadow_extra_code, 0);
 
1114
 
 
1115
  if (this_shadow_extra_code)
 
1116
    Printv(shadow_classdef, this_shadow_extra_code, 0);
 
1117
}
 
1118
 
 
1119
int JAVA::classHandler(Node *n) {
 
1120
 
 
1121
  if (shadow) {
 
1122
    char *classname = GetChar(n,"name");
 
1123
    char *rename = GetChar(n,"sym:name");
 
1124
    char *ctype  = GetChar(n,"kind");
 
1125
    
 
1126
    shadow_classname = Swig_copy_string(rename);
 
1127
    
 
1128
    if (strcmp(shadow_classname, module) == 0) {
 
1129
      Printf(stderr, "class name cannot be equal to module name: %s\n", shadow_classname);
 
1130
      SWIG_exit(1);
 
1131
    }
 
1132
/*    
 
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);
 
1137
    }
 
1138
   */ 
 
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);
 
1142
    }
 
1143
    
 
1144
    emit_banner(f_shadow);
 
1145
    
 
1146
    if(*package)
 
1147
      Printf(f_shadow, "package %s;\n\n", package);
 
1148
    else 
 
1149
      Printf(f_shadow, "import %s;\n", module);
 
1150
    
 
1151
    Clear(shadow_classdef);
 
1152
    Clear(shadow_code);
 
1153
    
 
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);
 
1164
  }
 
1165
  Language::classHandler(n);
 
1166
 
 
1167
  if (shadow) {
 
1168
 
 
1169
    /* Deal with inheritance */
 
1170
    List *baselist = Getattr(n,"bases");
 
1171
    if (baselist) {
 
1172
      Node *base = Firstitem(baselist);
 
1173
      String *basename = is_shadow(Getattr(base,"name"));
 
1174
      if (basename)
 
1175
        Printv(this_shadow_baseclass, basename, 0);
 
1176
      base = Nextitem(baselist);
 
1177
      if (base) {
 
1178
        Printf(stderr, "Warning: %s inherits from multiple base classes. Multiple inheritance is not supported.\n", shadow_classname);
 
1179
      }
 
1180
    }
 
1181
 
 
1182
    emit_shadow_classdef();
 
1183
 
 
1184
    Printv(f_shadow, shadow_classdef, shadow_code, 0);
 
1185
 
 
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);
 
1189
 
 
1190
    Printf(f_shadow, "}\n");
 
1191
    fclose(f_shadow);
 
1192
    f_shadow = NULL;
 
1193
    
 
1194
    free(shadow_classname);
 
1195
    shadow_classname = NULL;
 
1196
    
 
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;
 
1203
  }
 
1204
  return SWIG_OK;
 
1205
}
 
1206
 
 
1207
 
 
1208
int JAVA::memberfunctionHandler(Node *n) {
 
1209
  Language::memberfunctionHandler(n);
 
1210
 
 
1211
  if (shadow) {
 
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);
 
1216
  }
 
1217
  return SWIG_OK;
 
1218
}
 
1219
 
 
1220
int JAVA::staticmemberfunctionHandler(Node *n) {
 
1221
 
 
1222
  Language::staticmemberfunctionHandler(n);
 
1223
 
 
1224
  if (shadow) {
 
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);
 
1228
    static_flag = 1;
 
1229
    javashadowfunctionHandler(n, NOT_VIRTUAL);
 
1230
    static_flag = 0;
 
1231
  }
 
1232
  return SWIG_OK;
 
1233
}
 
1234
 
 
1235
/* 
 
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.
 
1241
*/
 
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");
 
1247
  char      arg[256];
 
1248
  String    *tm;
 
1249
  Parm      *jstypep;
 
1250
  Parm      *p;
 
1251
  int       i;
 
1252
  String    *nativecall = NewString("");
 
1253
  String    *shadowrettype = NewString("");
 
1254
  String    *user_arrays = NewString("");
 
1255
  int       num_arguments = 0;
 
1256
  int       num_required = 0;
 
1257
 
 
1258
  if(!shadow) return;
 
1259
 
 
1260
  if (l) {
 
1261
    if (SwigType_type(Getattr(l,"type")) == T_VOID) {
 
1262
      l = nextSibling(l);
 
1263
    }
 
1264
  }
 
1265
 
 
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);
 
1269
 
 
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);
 
1274
  } else {
 
1275
    Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(t,0));
 
1276
  }
 
1277
 
 
1278
  Printf(shadow_code, "  public ");
 
1279
  if (static_flag)
 
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);
 
1284
 
 
1285
  if(SwigType_type(t) == T_ARRAY && is_shadow(get_array_type(t))) {
 
1286
    Printf(nativecall, "long[] cArray = ");
 
1287
  }
 
1288
  else {
 
1289
    if(SwigType_type(t) != T_VOID) {
 
1290
      Printf(nativecall,"return ");
 
1291
    }
 
1292
    if(is_shadow(t)) {
 
1293
      Printv(nativecall, "new ", shadowrettype, "(", 0);
 
1294
    }
 
1295
  }
 
1296
 
 
1297
  Printv(nativecall, module, ".", java_function_name, "(", 0);
 
1298
  if (!static_flag)
 
1299
    Printv(nativecall, "_cPtr", 0);
 
1300
 
 
1301
  /* Get number of required and total arguments */
 
1302
  num_arguments = emit_num_arguments(l);
 
1303
  num_required  = emit_num_required(l);
 
1304
 
 
1305
  int gencomma = !static_flag;
 
1306
 
 
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);
 
1311
DelWrapper(f);
 
1312
/* Workaround end */
 
1313
 
 
1314
  /* Output each parameter */
 
1315
  for (i = 0, p=l, jstypep=l; i < num_arguments; i++) {
 
1316
    if(Getattr(p,"ignore")) continue;
 
1317
 
 
1318
    /* Ignore the 'this' argument for variable wrappers */
 
1319
    if (!(variable_wrapper_flag && i==0)) 
 
1320
    {
 
1321
      SwigType *pt = Getattr(p,"type");
 
1322
      String   *pn = Getattr(p,"name");
 
1323
      String   *javaparamtype = NewString("");
 
1324
  
 
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);
 
1331
      } else {
 
1332
        jstypep = nextSibling(jstypep);
 
1333
        Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(pt,0));
 
1334
      }
 
1335
 
 
1336
      /* Create a name for the parameter */
 
1337
      if(pn && *(Char(pn)))
 
1338
        strcpy(arg,Char(pn));
 
1339
      else {
 
1340
        sprintf(arg,"arg%d",i);
 
1341
      }
 
1342
  
 
1343
      if (gencomma)
 
1344
        Printf(nativecall, ", ");
 
1345
 
 
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);
 
1351
 
 
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 */
 
1357
      if (gencomma >= 2)
 
1358
        Printf(shadow_code, ", ");
 
1359
      gencomma = 2;
 
1360
      Printf(shadow_code, "%s %s", javaparamtype, arg);
 
1361
 
 
1362
      Delete(javaparamtype);
 
1363
    }
 
1364
    else {
 
1365
      jstypep = nextSibling(jstypep);
 
1366
    }
 
1367
 
 
1368
    p = nextSibling(p);
 
1369
  }
 
1370
 
 
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);
 
1379
 
 
1380
    // Hack the jstype typemap apart
 
1381
    Printv(array_type, shadowrettype, 0);
 
1382
    Replaceall(array_type,"[]", "");
 
1383
 
 
1384
    Replace(array_ret, "$type", array_type, DOH_REPLACE_ANY);
 
1385
    Printv(nativecall, array_ret, 0);
 
1386
    Delete(array_ret);
 
1387
    Delete(array_type);
 
1388
  }
 
1389
  else {
 
1390
    if(is_shadow(t)) {
 
1391
      switch(SwigType_type(t)) {
 
1392
      case T_USER:
 
1393
        Printf(nativecall, "), true");
 
1394
        break;
 
1395
      case T_REFERENCE:
 
1396
      case T_POINTER:
 
1397
        if (NewObject) // %new indicating Java must take responsibility for memory ownership
 
1398
          Printf(nativecall, "), true");
 
1399
        else
 
1400
          Printf(nativecall, "), false");
 
1401
        break;
 
1402
      default:
 
1403
        Printf(stderr, "Internal Error: unknown shadow type: %s\n", SwigType_str(t,0));
 
1404
        break;
 
1405
      }
 
1406
    }
 
1407
    Printf(nativecall,");\n");
 
1408
  }
 
1409
 
 
1410
  if (is_virtual == PURE_VIRTUAL) {
 
1411
    Printf(shadow_code, ");\n\n");
 
1412
    abstract_class_flag = 1;
 
1413
  }
 
1414
  else {
 
1415
    Printf(shadow_code, ") {\n");
 
1416
    Printv(shadow_code, user_arrays, 0);
 
1417
    Printf(shadow_code, "    %s", nativecall);
 
1418
    Printf(shadow_code, "  }\n\n");
 
1419
  }
 
1420
 
 
1421
  Delete(shadowrettype);
 
1422
  Delete(nativecall);
 
1423
  Delete(user_arrays);
 
1424
}
 
1425
 
 
1426
int JAVA::constructorHandler(Node *n) {
 
1427
 
 
1428
  char *iname = GetChar(n,"sym:name");
 
1429
  ParmList *l = Getattr(n,"parms");
 
1430
  String    *tm;
 
1431
  Parm      *jstypep;
 
1432
  Parm      *p;
 
1433
  int       i;
 
1434
  int       num_arguments = 0;
 
1435
  int       num_required = 0;
 
1436
 
 
1437
  Language::constructorHandler(n);
 
1438
 
 
1439
  if(shadow) {
 
1440
    String *nativecall = NewString("");
 
1441
    char arg[256];
 
1442
  
 
1443
    Printf(shadow_code, "  public %s(", shadow_classname);
 
1444
  
 
1445
    Printv(nativecall, "$superconstructorcall", 0); // Super call for filling in later.
 
1446
    if (iname != NULL)
 
1447
      Printv(nativecall, tab4, "_cPtr = ", module, ".", Swig_name_construct(iname), "(", 0);
 
1448
    else
 
1449
      Printv(nativecall, tab4, "_cPtr = ", module, ".", Swig_name_construct(shadow_classname), "(", 0);
 
1450
  
 
1451
    int pcount = ParmList_len(l);
 
1452
    if(pcount == 0)  // We must have a default constructor
 
1453
      have_default_constructor = 1;
 
1454
  
 
1455
    Swig_typemap_attach_parms("jstype", l, 0);
 
1456
 
 
1457
    /* Get number of required and total arguments */
 
1458
    num_arguments = emit_num_arguments(l);
 
1459
    num_required  = emit_num_required(l);
 
1460
  
 
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("");
 
1465
  
 
1466
      /* Create a name for the parameter */
 
1467
      if(pn && *(Char(pn)))
 
1468
        strcpy(arg,Char(pn));
 
1469
      else {
 
1470
        sprintf(arg,"arg%d",i);
 
1471
      }
 
1472
  
 
1473
      if(is_shadow(pt)) {
 
1474
        Printv(nativecall, arg, ".getCPtr()", 0);
 
1475
      } else Printv(nativecall, arg, 0);
 
1476
  
 
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);
 
1483
      } else {
 
1484
        jstypep = nextSibling(jstypep);
 
1485
        Printf(stderr, "No jstype typemap defined for %s\n", SwigType_str(pt,0));
 
1486
      }
 
1487
  
 
1488
      /* Add to java shadow function header */
 
1489
      Printf(shadow_code, "%s %s", javaparamtype, arg);
 
1490
  
 
1491
      if(i != pcount-1) {
 
1492
        Printf(nativecall, ", ");
 
1493
        Printf(shadow_code, ", ");
 
1494
      }
 
1495
      p = nextSibling(p);
 
1496
      Delete(javaparamtype);
 
1497
    }
 
1498
  
 
1499
    Printf(shadow_code, ") {\n");
 
1500
    Printv(nativecall,
 
1501
          ");\n",
 
1502
          tab4, "_cMemOwn = true;\n",
 
1503
          0);
 
1504
  
 
1505
    Printf(shadow_code, "%s", nativecall);
 
1506
    Printf(shadow_code, "  }\n\n");
 
1507
    Delete(nativecall);
 
1508
  }
 
1509
  return SWIG_OK;
 
1510
}
 
1511
 
 
1512
int JAVA::destructorHandler(Node *n) {
 
1513
  Language::destructorHandler(n);
 
1514
 
 
1515
  if(shadow) {
 
1516
    if(!nofinalize) {
 
1517
      Printf(shadow_code, "  protected void finalize() {\n");
 
1518
      Printf(shadow_code, "    _delete();\n");
 
1519
      Printf(shadow_code, "  }\n\n");
 
1520
    }
 
1521
  
 
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");
 
1528
  }
 
1529
  return SWIG_OK;
 
1530
}
 
1531
 
 
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)
 
1537
{
 
1538
    String* nametemp = Copy(name);
 
1539
    String* swigtypetemp = Copy(type);
 
1540
 
 
1541
    if (additions == pointer) {
 
1542
        SwigType_add_pointer(swigtypetemp);
 
1543
        SwigType_add_pointer(nametemp);
 
1544
    }
 
1545
    if (additions == reference) {
 
1546
        SwigType_add_reference(swigtypetemp);
 
1547
        SwigType_add_reference(nametemp);
 
1548
    }
 
1549
    if (array_flag) {
 
1550
        SwigType_add_array(swigtypetemp, (char *)"ANY");
 
1551
        SwigType_add_array(nametemp, (char *)"ANY");
 
1552
    }
 
1553
 
 
1554
    Parm *srcpat = NewParm(swigtypetemp,tmap);
 
1555
    Parm *destpat = NewParm(nametemp,0);
 
1556
    Swig_typemap_apply(srcpat,destpat);
 
1557
    Delete(nametemp);
 
1558
    Delete(swigtypetemp);
 
1559
}
 
1560
 
 
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");
 
1567
 
 
1568
  /* Add to the hash table of shadow classes */
 
1569
  if (shadow) {
 
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);
 
1575
      Delete(stype);
 
1576
    }
 
1577
  }
 
1578
 
 
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]};
 
1584
 
 
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);
 
1594
  }
 
1595
  Delete(class_tmap);
 
1596
  Delete(array_tmap);
 
1597
  Delete(swigtype);
 
1598
  return SWIG_OK;
 
1599
}
 
1600
 
 
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;
 
1608
  return SWIG_OK;
 
1609
}
 
1610
 
 
1611
int JAVA::staticmembervariableHandler(Node *n) {
 
1612
  shadow_variable_name = GetChar(n,"sym:name");
 
1613
  wrapping_member = 1;
 
1614
  static_flag = 1;
 
1615
  Language::staticmembervariableHandler(n);
 
1616
  wrapping_member = 0;
 
1617
  static_flag = 0;
 
1618
  return SWIG_OK;
 
1619
}
 
1620
 
 
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;
 
1626
  return SWIG_OK;
 
1627
}
 
1628
 
 
1629
 
 
1630
 
 
1631
 
 
1632
 
 
1633