Removed String class from Java module

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@547 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-07-09 21:48:35 +00:00
commit f4c4610522
2 changed files with 255 additions and 270 deletions

View file

@ -38,6 +38,31 @@ Java Options\n\
-finalize - generate finalize methods\n\ -finalize - generate finalize methods\n\
-rn - generate register natives code\n\n"; -rn - generate register natives code\n\n";
static char *module = 0; // Name of the module
static char *java_path = (char*)"java";
static char *package = 0; // Name of the package
static char *c_pkgstr; // Name of the package
static char *jni_pkgstr; // Name of the package
static char *shadow_classname;
static char *jimport = 0;
static char *method_modifiers = (char*)"public final static";
static FILE *f_java = 0;
static FILE *f_shadow = 0;
static int shadow = 0;
static DOHHash *shadow_classes;
static DOHString *shadow_classdef;
static char *shadow_name = 0;
static char *shadow_baseclass = 0;
static int classdef_emitted = 0;
static int shadow_classdef_emitted = 0;
static int have_default_constructor = 0;
static int native_func = 0; // Set to 1 when wrapping a native function
static int member_func = 0; // Set to 1 when wrapping a member function
static int jnic = -1; // 1: use c syntax jni; 0: use c++ syntax jni
static int finalize = 0; // generate finalize methods
static int useRegisterNatives = 0; // Set to 1 when doing stuff with register natives
static DOHString *registerNativesList = 0;
char *JAVA::SwigTcToJniType(DataType *t, int ret) { char *JAVA::SwigTcToJniType(DataType *t, int ret) {
if(t->is_pointer == 1) { if(t->is_pointer == 1) {
switch(t->type) { switch(t->type) {
@ -225,62 +250,57 @@ char *JAVA::makeValidJniName(char *name) {
} }
// !! this approach fails for functions without arguments // !! this approach fails for functions without arguments
char *JAVA::JNICALL(String& func) { char *JAVA::JNICALL(DOHString_or_char *func) {
if(jnic) if(jnic)
sprintf(bigbuf, "(*jenv)->%s(jenv, ", (char *) func.get()); sprintf(bigbuf, "(*jenv)->%s(jenv, ", Char(func));
else else
sprintf(bigbuf, "jenv->%s(", (char *) func.get()); sprintf(bigbuf, "jenv->%s(", Char(func));
return strdup(bigbuf);
}
// !! this approach fails for functions without arguments
char *JAVA::JNICALL(char *func) {
if(jnic)
sprintf(bigbuf, "(*jenv)->%s(jenv, ", func);
else
sprintf(bigbuf, "jenv->%s(", func);
return strdup(bigbuf); return strdup(bigbuf);
} }
void JAVA::writeRegisterNatives() void JAVA::writeRegisterNatives()
{ {
if(strlen(registerNativesList.get()) == 0) if(Len(registerNativesList) == 0)
return; return;
fprintf(f_wrappers,"\n"); Printf(f_wrappers,"\n");
fprintf(f_wrappers,"JNINativeMethod nativeMethods[] = {\n"); Printf(f_wrappers,"JNINativeMethod nativeMethods[] = {\n");
fprintf(f_wrappers, registerNativesList.get()); Printv(f_wrappers, registerNativesList, 0);
fprintf(f_wrappers, "};\n"); Printf(f_wrappers, "};\n");
fprintf(f_wrappers,"\nint numberOfNativeMethods=sizeof(nativeMethods)/sizeof(JNINativeMethod);\n\n"); Printf(f_wrappers,"\nint numberOfNativeMethods=sizeof(nativeMethods)/sizeof(JNINativeMethod);\n\n");
// The registerNatives function // The registerNatives function
String registerFunction;
registerFunction << "jint registerNatives(JNIEnv *jenv) {" << "\n" Printv(f_wrappers,
<< tab4 << "jclass nativeClass = " << JNICALL((char*)"FindClass") "jint registerNatives(JNIEnv *jenv) {", "\n",
<< "\"" << jni_pkgstr << module << "\");" <<"\n"; tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
registerFunction << tab4 << "if (nativeClass == 0)" << "\n" << tab8 << "return -1;" << "\n"; "\"", jni_pkgstr, module, "\");","\n",
0);
registerFunction << tab4 << "return " << JNICALL((char*)"RegisterNatives") << "nativeClass, nativeMethods, " << "numberOfNativeMethods);" << "\n"; Printv(f_wrappers,
registerFunction << "}" << "\n" << "\n"; tab4, "if (nativeClass == 0)", "\n", tab8, "return -1;", "\n",
tab4, "return ", JNICALL((char*)"RegisterNatives"), "nativeClass, nativeMethods, ", "numberOfNativeMethods);", "\n",
"}", "\n", "\n",
0);
// The unregisterNatives function // The unregisterNatives function
registerFunction << "jint unregisterNatives(JNIEnv *jenv) {" << "\n" Printv(f_wrappers,
<< tab4 << "jclass nativeClass = " << JNICALL((char*)"FindClass") "jint unregisterNatives(JNIEnv *jenv) {", "\n",
<< "\"" << jni_pkgstr << module << "\");" <<"\n"; tab4, "jclass nativeClass = ", JNICALL((char*)"FindClass"),
registerFunction << tab4 << "if (nativeClass == 0)" << "\n" << tab8 << "return -1;" << "\n"; "\"", jni_pkgstr, module, "\");","\n",
registerFunction << tab4 << "// Sun documentation suggests that this method should not be invoked in " 0);
<< "\"normal native code\"." << "\n";
registerFunction << tab4 << "// return " << JNICALL((char*)"UnregisterNatives") << "nativeClass);" << "\n";
registerFunction << tab4 << "return 0;" << "\n";
registerFunction << "}" << "\n";
// Now write the C function Printv(f_wrappers,
fprintf(f_wrappers,registerFunction.get()); tab4, "if (nativeClass == 0)", "\n", tab8, "return -1;", "\n",
tab4, "// Sun documentation suggests that this method should not be invoked in ",
"\"normal native code\".", "\n",
tab4, "// return ", JNICALL((char*)"UnregisterNatives"), "nativeClass);", "\n",
tab4, "return 0;", "\n",
"}", "\n",
0);
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -362,16 +382,11 @@ void JAVA::parse() {
fprintf(stderr,"Generating wrappers for Java\n"); fprintf(stderr,"Generating wrappers for Java\n");
shadow_classes = NewHash();
shadow_classdef = NewString("");
registerNativesList = NewString("");
headers(); // Emit header files and other supporting code headers(); // Emit header files and other supporting code
/*
// Tell the parser to first include a typemap definition file
if (include_file("typemaps.i") == -1) {
fprintf(stderr,"Unable to find typemaps.i!\n");
SWIG_exit(1);
}
*/
yyparse(); // Run the SWIG parser yyparse(); // Run the SWIG parser
} }
@ -425,15 +440,17 @@ void JAVA::initialize()
} }
if(package) { if(package) {
String s = package; DOHString *s = NewString(package);
s.replace(".", "_"); Replace(s,".","_", DOH_REPLACE_ANY);
s << "_"; Append(s,"_");
c_pkgstr = copy_string(s.get()); c_pkgstr = copy_string(Char(s));
Delete(s);
String s2 = package; DOHString *s2 = NewString(package);
s2.replace(".", "/"); Replace(s2,".","/", DOH_REPLACE_ANY);
s2 << "/"; Append(s2,"/");
jni_pkgstr = copy_string(s2.get()); jni_pkgstr = copy_string(Char(s2));
Delete(s2);
} else { } else {
package = c_pkgstr = jni_pkgstr = (char*)""; package = c_pkgstr = jni_pkgstr = (char*)"";
} }
@ -509,12 +526,16 @@ void JAVA::add_native(char *name, char *iname, DataType *t, ParmList *l) {
void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l) void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
{ {
String source, target; char source[256], target[256];
char *tm; char *tm;
String cleanup, outarg; DOHString *cleanup, *outarg, *body;
String body;
char *javaReturnSignature; char *javaReturnSignature;
String javaParameterSignature; DOHString *javaParameterSignature;
cleanup = NewString("");
outarg = NewString("");
body = NewString("");
javaParameterSignature = NewString("");
// A new wrapper function object // A new wrapper function object
WrapperFunction f; WrapperFunction f;
@ -545,18 +566,19 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
fprintf(f_java, " %s ", method_modifiers); fprintf(f_java, " %s ", method_modifiers);
fprintf(f_java, "native %s %s(", javarettype, iname); fprintf(f_java, "native %s %s(", javarettype, iname);
if(shadow && member_func) { if(shadow && member_func) {
String member_name = ""; DOHString *member_name = NewString("");
if(strcmp(iname, Swig_name_set(Swig_name_member(shadow_classname, shadow_name))) == 0) if(strcmp(iname, Swig_name_set(Swig_name_member(shadow_classname, shadow_name))) == 0)
member_name << "set"; Printf(member_name,"set");
else member_name << "get"; else Printf(member_name,"get");
member_name << (char) toupper((int) *shadow_name); Putc(toupper((int) *shadow_name), member_name);
member_name << shadow_name + 1; Printf(member_name, "%s", shadow_name+1);
fprintf(f_shadow, " public %s %s(", javarettype, (char *) member_name.get()); Printf(f_shadow, " public %s %s(", javarettype, member_name);
body << tab4 << "return " << module << "." << iname << "(_self"; Printv(body, tab4, "return ", module, ".", iname, "(_self", 0);
Delete(member_name);
} }
if(!jnic) f.def << "extern \"C\"\n"; if(!jnic) Printf(f._def,"extern \"C\"\n");
f.def << "JNIEXPORT " << jnirettype << " JNICALL " << wname << "(JNIEnv *jenv, jclass jcls"; Printv(f._def, "JNIEXPORT ", jnirettype, " JNICALL ", wname, "(JNIEnv *jenv, jclass jcls", 0);
// Emit all of the local variables for holding arguments. // Emit all of the local variables for holding arguments.
int pcount = emit_args(t,l,f); int pcount = emit_args(t,l,f);
@ -569,19 +591,17 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
char *target_copy = NULL; char *target_copy = NULL;
char *target_length = NULL; char *target_length = NULL;
char *local_i = NULL; char *local_i = NULL;
source = "";
target = "";
// Produce string representation of source and target arguments // Produce string representation of source and target arguments
source << "jarg" << i; sprintf(source,"jarg%d",i);
target << "_arg" << i; sprintf(target,"_arg%d",i);
char *jnitype = JavaTypeFromTypemap((char*)"jni", typemap_lang, p->t, p->name); char *jnitype = JavaTypeFromTypemap((char*)"jni", typemap_lang, p->t, p->name);
if(!jnitype) jnitype = SwigTcToJniType(p->t, 0); if(!jnitype) jnitype = SwigTcToJniType(p->t, 0);
char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name); char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name);
if(!jtype) jtype = SwigTcToJavaType(p->t, 0, 0); if(!jtype) jtype = SwigTcToJavaType(p->t, 0, 0);
if (useRegisterNatives) { if (useRegisterNatives) {
javaParameterSignature << JavaMethodSignature(p->t, 0, 0); Printv(javaParameterSignature, JavaMethodSignature(p->t, 0, 0), 0);
} }
if(p->ignore) continue; if(p->ignore) continue;
@ -590,35 +610,35 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
if(shadow && member_func) { if(shadow && member_func) {
if(i > 0) { if(i > 0) {
if(i>1) fprintf(f_shadow, ", "); if(i>1) fprintf(f_shadow, ", ");
fprintf(f_shadow, "%s %s", jtype, (char *) source.get()); fprintf(f_shadow, "%s %s", jtype, source);
body << ", " << source; Printv(body,", ", source, 0);
} }
} }
if(gencomma) fprintf(f_java, ", "); if(gencomma) fprintf(f_java, ", ");
fprintf(f_java, "%s %s", jtype, (char *) source.get()); fprintf(f_java, "%s %s", jtype, source);
gencomma = 1; gencomma = 1;
// Add to Jni function header // Add to Jni function header
f.def << ", " << jnitype << " " << source; Printv(f._def, ", ", jnitype, " ", source, 0);
// Get typemap for this argument // Get typemap for this argument
tm = typemap_lookup((char*)"in",typemap_lang,p->t,p->name,source.get(),target.get(),&f); tm = typemap_lookup((char*)"in",typemap_lang,p->t,p->name,source,target,&f);
if (tm) { if (tm) {
f.code << tm << "\n"; Printf(f._code,"%s\n", tm);
f.code.replace("$arg",source.get()); // Perform a variable replacement Replace(f._code,"$arg",source, DOH_REPLACE_ANY);
} else { } else {
if(!p->t->is_pointer) if(!p->t->is_pointer)
f.code << tab4 << target << " = " << p->t->print_cast() << source << ";\n"; Printv(f._code, tab4, target, " = ", p->t->print_cast(), source, ";\n", 0);
else if((p->t->type == T_VOID && p->t->is_pointer == 1) || else if((p->t->type == T_VOID && p->t->is_pointer == 1) ||
(p->t->type == T_USER && p->t->is_pointer == 1)) { (p->t->type == T_USER && p->t->is_pointer == 1)) {
p->t->is_pointer++; p->t->is_pointer++;
f.code << tab4 << target << " = *" << p->t->print_cast() << "&" << source << ";\n"; Printv(f._code, tab4, target, " = *", p->t->print_cast(), "&", source, ";\n", 0);
p->t->is_pointer--; p->t->is_pointer--;
} else { } else {
if(p->t->type == T_CHAR && p->t->is_pointer == 1) { if(p->t->type == T_CHAR && p->t->is_pointer == 1) {
f.code << tab4 << target << " = (" << source << ") ? (char *)" << JNICALL((char*)"GetStringUTFChars") << source << ", 0) : NULL;\n"; Printv(f._code, tab4, target, " = (", source, ") ? (char *)", JNICALL((char*)"GetStringUTFChars"), source, ", 0) : NULL;\n", 0);
} else { } else {
char *scalarType = SwigTcToJniScalarType(p->t); char *scalarType = SwigTcToJniScalarType(p->t);
char *cptrtype = p->t->print_type(); char *cptrtype = p->t->print_type();
@ -631,55 +651,55 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
}; };
p->t->is_pointer++; p->t->is_pointer++;
String basic_jniptrtype = basic_jnitype; DOHString *basic_jniptrtype = NewStringf("%s*",basic_jnitype);
basic_jniptrtype << "*"; DOHString *source_length = NewStringf("%s%s)", JNICALL((char*)"GetArrayLength"), source);
String source_length = JNICALL((char*)"GetArrayLength");
source_length << source << ")";
target_copy = copy_string(f.new_local((char *) basic_jniptrtype.get(), target.get(), NULL)); target_copy = copy_string(f.new_local(Char(basic_jniptrtype), target, NULL));
target_length = copy_string(f.new_local((char*)"jsize", target.get(), source_length.get())); target_length = copy_string(f.new_local((char*)"jsize", target, Char(source_length)));
if(local_i == NULL) local_i = copy_string(f.new_local((char*)"int", (char*)"i", NULL)); if(local_i == NULL) local_i = copy_string(f.new_local((char*)"int", (char*)"i", NULL));
String scalarFunc = "Get"; DOHString *scalarFunc = NewStringf("Get%sArrayElements",scalarType);
scalarFunc << scalarType << "ArrayElements";
f.code << tab4 << target_copy << " = " << JNICALL(scalarFunc) << source << ", 0);\n"; Printv(f._code, tab4, target_copy, " = ", JNICALL(scalarFunc), source, ", 0);\n", 0);
f.code << tab4 << target << " = " << p->t->print_cast() << " malloc(" << target_length << " * sizeof(" << ctype << "));\n"; Printv(f._code, tab4, target, " = ", p->t->print_cast(), " malloc(", target_length, " * sizeof(", ctype, "));\n", 0);
f.code << tab4 << "for(i=0; i<" << target_length << "; i++)\n"; Printv(f._code, tab4, "for(i=0; i<", target_length, "; i++)\n", 0);
// if(p->t->is_pointer && ((p->t->type == T_VOID) || (p->t->type == T_USER))) {
if(p->t->is_pointer > 1) { if(p->t->is_pointer > 1) {
f.code << tab8 << target << "[i] = *" << p->t->print_cast() << "&" << target_copy << "[i];\n"; Printv(f._code, tab8, target, "[i] = *", p->t->print_cast(), "&", target_copy, "[i];\n", 0);
} else { } else {
p->t->is_pointer--; p->t->is_pointer--;
f.code << tab8 << target << "[i] = " << p->t->print_cast() << target_copy << "[i];\n"; Printv(f._code, tab8, target, "[i] = ", p->t->print_cast(), target_copy, "[i];\n", 0);
p->t->is_pointer++; p->t->is_pointer++;
} }
Delete(scalarFunc);
Delete(source_length);
Delete(basic_jniptrtype);
} }
} }
} }
// Check to see if there was any sort of a constaint typemap // Check to see if there was any sort of a constaint typemap
if ((tm = typemap_lookup((char*)"check",typemap_lang,p->t,p->name,source.get(),target.get()))) { if ((tm = typemap_lookup((char*)"check",typemap_lang,p->t,p->name,source,target))) {
// Yep. Use it instead of the default // Yep. Use it instead of the default
f.code << tm << "\n"; Printf(f._code,"%s\n", tm);
f.code.replace("$arg",source.get()); Replace(f._code,"$arg",source, DOH_REPLACE_ANY);
} }
// Check if there was any cleanup code (save it for later) // Check if there was any cleanup code (save it for later)
if ((tm = typemap_lookup((char*)"freearg",typemap_lang,p->t,p->name,source.get(),target.get()))) { if ((tm = typemap_lookup((char*)"freearg",typemap_lang,p->t,p->name,source,target))) {
// Yep. Use it instead of the default // Yep. Use it instead of the default
cleanup << tm << "\n"; Printf(cleanup,"%s\n", tm);
cleanup.replace("$arg",source.get()); Replace(cleanup,"$arg",source, DOH_REPLACE_ANY);
} }
if ((tm = typemap_lookup((char*)"argout",typemap_lang,p->t,p->name,source.get(),target.get()))) { if ((tm = typemap_lookup((char*)"argout",typemap_lang,p->t,p->name,source,target))) {
// Yep. Use it instead of the default // Yep. Use it instead of the default
outarg << tm << "\n"; Printf(outarg,"%s\n", tm);
outarg.replace("$arg",source.get()); Replace(outarg,"$arg",source, DOH_REPLACE_ANY);
} else { } else {
// if(p->t->is_pointer && p->t->type != T_USER && p->t->type != T_VOID) { // if(p->t->is_pointer && p->t->type != T_USER && p->t->type != T_VOID) {
if(p->t->is_pointer) { if(p->t->is_pointer) {
if(p->t->type == T_CHAR && p->t->is_pointer == 1) { if(p->t->type == T_CHAR && p->t->is_pointer == 1) {
outarg << tab4 << "if(" << target <<") " << JNICALL((char*)"ReleaseStringUTFChars") << source << ", " << target << ");\n"; Printv(outarg, tab4, "if(", target,") ", JNICALL((char*)"ReleaseStringUTFChars"), source, ", ", target, ");\n", 0);
} else if((p->t->type == T_VOID && p->t->is_pointer == 1) || } else if((p->t->type == T_VOID && p->t->is_pointer == 1) ||
(p->t->type == T_USER && p->t->is_pointer == 1)) { (p->t->type == T_USER && p->t->is_pointer == 1)) {
// nothing to do // nothing to do
@ -694,18 +714,16 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
SWIG_exit(1); SWIG_exit(1);
}; };
p->t->is_pointer++; p->t->is_pointer++;
Printf(outarg, " for(i=0; i< %d; i++)\n", target_length);
outarg << tab4 << "for(i=0; i<" << target_length << "; i++)\n";
if(p->t->is_pointer > 1) { if(p->t->is_pointer > 1) {
outarg << tab8 << "*" << p->t->print_cast() << "&" << target_copy << "[i] = " << target << "[i];\n"; Printv(outarg, tab8, "*", p->t->print_cast(), "&", target_copy, "[i] = ", target, "[i];\n", 0);
} else { } else {
outarg << tab8 << target_copy << "[i] = (" << basic_jnitype << ") " << target << "[i];\n"; Printv(outarg, tab8, target_copy, "[i] = (", basic_jnitype, ") ", target, "[i];\n", 0);
} }
String scalarFunc = "Release"; DOHString *scalarFunc = NewStringf("Release%sArrayElements",scalarType);
scalarFunc << scalarType << "ArrayElements"; Printv(outarg, tab4, JNICALL(scalarFunc), source, ", ", target_copy, ", 0);\n", 0);
outarg << tab4 << JNICALL(scalarFunc) << source << ", " << target_copy << ", 0);\n"; Printv(outarg, tab4, "free(", target, ");\n", 0);
outarg << tab4 << "free(" << target << ");\n"; Delete(scalarFunc);
free(target_copy); free(target_copy);
free(target_length); free(target_length);
free(local_i); free(local_i);
@ -717,10 +735,10 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
fprintf(f_java, ");\n"); fprintf(f_java, ");\n");
if(shadow && member_func) { if(shadow && member_func) {
fprintf(f_shadow, ") {\n"); fprintf(f_shadow, ") {\n");
body << ")"; Printf(body,")");
fprintf(f_shadow, "%s;\n }\n\n", (char *) body.get()); Printf(f_shadow, "%s;\n }\n\n",body);
} }
f.def << ") {"; Printf(f._def,") {");
// Now write code to make the function call // Now write code to make the function call
@ -731,27 +749,27 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
if((t->type != T_VOID || t->is_pointer) && !native_func) { if((t->type != T_VOID || t->is_pointer) && !native_func) {
if ((tm = typemap_lookup((char*)"out",typemap_lang,t,iname,(char*)"_result",(char*)"_jresult"))) { if ((tm = typemap_lookup((char*)"out",typemap_lang,t,iname,(char*)"_result",(char*)"_jresult"))) {
f.code << tm << "\n"; Printf(f._code,"%s\n", tm);
} else { } else {
if(t->is_pointer == 0 && t->type == T_USER) { /* return by value */ if(t->is_pointer == 0 && t->type == T_USER) { /* return by value */
t->is_pointer=2; t->is_pointer=2;
f.code << tab4 << "*" << t->print_cast() << "&_jresult = _result;\n"; Printv(f._code, tab4, "*", t->print_cast(), "&_jresult = _result;\n", 0);
t->is_pointer=0; t->is_pointer=0;
} else if(t->is_pointer == 0 && t->type != T_USER) { } else if(t->is_pointer == 0 && t->type != T_USER) {
f.code << tab4 << "_jresult = (" << jnirettype << ") _result;\n"; Printv(f._code, tab4, "_jresult = (", jnirettype, ") _result;\n", 0);
} else if((t->type == T_VOID && t->is_pointer == 1) || } else if((t->type == T_VOID && t->is_pointer == 1) ||
(t->type == T_USER && t->is_pointer == 1)) { (t->type == T_USER && t->is_pointer == 1)) {
t->is_pointer++; t->is_pointer++;
f.code << tab4 << "*" << t->print_cast() << "&_jresult = _result;\n"; Printv(f._code, tab4, "*", t->print_cast(), "&_jresult = _result;\n", 0);
t->is_pointer--; t->is_pointer--;
} else { } else {
if(t->type == T_CHAR && t->is_pointer == 1) { if(t->type == T_CHAR && t->is_pointer == 1) {
f.code << tab4 << "if(_result != NULL)\n"; Printv(f._code, tab4, "if(_result != NULL)\n", 0);
f.code << tab8 << "_jresult = (jstring)" << JNICALL((char*)"NewStringUTF") << "_result);\n"; Printv(f._code, tab8, "_jresult = (jstring)", JNICALL((char*)"NewStringUTF"), "_result);\n", 0);
} else { } else {
fprintf(stderr,"%s : Line %d. Warning: no return typemap for datatype %s\n", input_file,line_number,t->print_type()); fprintf(stderr,"%s : Line %d. Warning: no return typemap for datatype %s\n", input_file,line_number,t->print_type());
t->is_pointer++; t->is_pointer++;
f.code << tab4 << "*" << t->print_cast() << "&_jresult = _result;\n"; Printv(f._code, tab4, "*", t->print_cast(), "&_jresult = _result;\n", 0);
t->is_pointer--; t->is_pointer--;
} }
} }
@ -759,32 +777,32 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
} }
// Dump argument output code; // Dump argument output code;
f.code << outarg; Printv(f._code, outarg, 0);
// Dump the argument cleanup code // Dump the argument cleanup code
f.code << cleanup; Printv(f._code,cleanup, 0);
// Look for any remaining cleanup // Look for any remaining cleanup
if (NewObject) { if (NewObject) {
if ((tm = typemap_lookup((char*)"newfree",typemap_lang,t,iname,(char*)"_result",(char*)""))) { if ((tm = typemap_lookup((char*)"newfree",typemap_lang,t,iname,(char*)"_result",(char*)""))) {
f.code << tm << "\n"; Printf(f._code,"%s\n", tm);
} }
} }
if((t->type != T_VOID || t->is_pointer) && !native_func) { if((t->type != T_VOID || t->is_pointer) && !native_func) {
if ((tm = typemap_lookup((char*)"ret",typemap_lang,t,iname,(char*)"_result",(char*)"_jresult", NULL))) { if ((tm = typemap_lookup((char*)"ret",typemap_lang,t,iname,(char*)"_result",(char*)"_jresult", NULL))) {
f.code << tm << "\n"; Printf(f._code,"%s\n", tm);
} }
} }
// Wrap things up (in a manner of speaking) // Wrap things up (in a manner of speaking)
if(t->type != T_VOID || t->is_pointer) if(t->type != T_VOID || t->is_pointer)
f.code << tab4 << "return _jresult;\n"; Printv(f._code, tab4, "return _jresult;\n", 0);
f.code << "}\n"; Printf(f._code, "}\n");
// Substitute the cleanup code (some exception handlers like to have this) // Substitute the cleanup code (some exception handlers like to have this)
f.code.replace("$cleanup",cleanup.get()); Replace(f._code,"$cleanup",cleanup, DOH_REPLACE_ANY);
// Emit the function // Emit the function
@ -794,9 +812,12 @@ void JAVA::create_function(char *name, char *iname, DataType *t, ParmList *l)
// If registerNatives is active, store the table entry for this method // If registerNatives is active, store the table entry for this method
if (useRegisterNatives) { if (useRegisterNatives) {
String methodEntry; Printv(registerNativesList,
methodEntry << "\"" << name << "\", \"(" << javaParameterSignature << ")" << javaReturnSignature << "\", " << wname; tab4, "{",
registerNativesList << tab4 << "{" << methodEntry << "}," << "\n"; "\"", name, "\", \"(", javaParameterSignature, ")", javaReturnSignature, "\", ", wname,
"},\n",
0);
} }
} }
@ -831,9 +852,10 @@ void JAVA::declare_const(char *name, char *iname, DataType *type, char *value) {
} }
if ((tm = typemap_lookup((char*)"const",typemap_lang,type,name,name,iname))) { if ((tm = typemap_lookup((char*)"const",typemap_lang,type,name,name,iname))) {
String str = tm; DOHString *str = NewString(tm);
str.replace("$value",value); Replace(str,"$value",value, DOH_REPLACE_ANY);
fprintf(jfile," %s\n\n", (char *) str.get()); Printf(jfile," %s\n\n", str);
Delete(str);
} else { } else {
if((type->is_pointer == 0)) { if((type->is_pointer == 0)) {
char *jtype = typemap_lookup((char*)"jtype", typemap_lang, type, name, name, iname); char *jtype = typemap_lookup((char*)"jtype", typemap_lang, type, name, name, iname);
@ -875,27 +897,28 @@ void emit_shadow_banner(FILE *f) {
void JAVA::pragma(char *lang, char *code, char *value) { void JAVA::pragma(char *lang, char *code, char *value) {
if(strcmp(lang, "java") != 0) return; if(strcmp(lang, "java") != 0) return;
String s = value; DOHString *s = NewString(value);
s.replace("\\\"", "\""); Replace(s,"\\\"", "\"", DOH_REPLACE_ANY);
if(strcmp(code, "import") == 0) { if(strcmp(code, "import") == 0) {
jimport = copy_string((char *) s.get()); jimport = copy_string(Char(s));
fprintf(f_java, "// pragma\nimport %s;\n\n", jimport); Printf(f_java, "// pragma\nimport %s;\n\n", jimport);
} else if(strcmp(code, "module") == 0) { } else if(strcmp(code, "module") == 0) {
if(!classdef_emitted) emit_classdef(); if(!classdef_emitted) emit_classdef();
fprintf(f_java, "// pragma\n"); Printf(f_java, "// pragma\n");
fprintf(f_java, (char *) s.get()); Printf(f_java, "%s", s);
fprintf(f_java, "\n\n"); Printf(f_java, "\n\n");
} else if(strcmp(code, "shadow") == 0) { } else if(strcmp(code, "shadow") == 0) {
if(shadow && f_shadow) { if(shadow && f_shadow) {
fprintf(f_shadow, "// pragma\n"); Printf(f_shadow, "// pragma\n");
fprintf(f_shadow, (char *) s.get()); Printf(f_shadow, "%s", s);
fprintf(f_shadow, "\n\n"); Printf(f_shadow, "\n\n");
} }
} else if(strcmp(code, "modifiers") == 0) { } else if(strcmp(code, "modifiers") == 0) {
method_modifiers = copy_string(value); method_modifiers = copy_string(value);
} else { } else {
fprintf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file,line_number); fprintf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file,line_number);
} }
Delete(s);
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -958,8 +981,8 @@ void JAVA::cpp_open_class(char *classname, char *rename, char *ctype, int strip)
if(jimport != NULL) if(jimport != NULL)
fprintf(f_shadow, "import %s;\n\n", jimport); fprintf(f_shadow, "import %s;\n\n", jimport);
shadow_classdef = ""; Clear(shadow_classdef);
shadow_classdef << "public class " << shadow_classname << " %BASECLASS% " << "{\n"; Printv(shadow_classdef, "public class ", shadow_classname, " %BASECLASS% ", "{\n", 0);
shadow_baseclass = (char*) ""; shadow_baseclass = (char*) "";
shadow_classdef_emitted = 0; shadow_classdef_emitted = 0;
@ -969,24 +992,24 @@ void JAVA::cpp_open_class(char *classname, char *rename, char *ctype, int strip)
void JAVA::emit_shadow_classdef() { void JAVA::emit_shadow_classdef() {
if(*shadow_baseclass) { if(*shadow_baseclass) {
sprintf(bigbuf, "extends %s", shadow_baseclass); sprintf(bigbuf, "extends %s", shadow_baseclass);
shadow_classdef.replace("%BASECLASS%", bigbuf); Replace(shadow_classdef,"%BASECLASS%", bigbuf, DOH_REPLACE_ANY);
shadow_classdef << " public " << shadow_classname << "(java.lang.Long obj) {\n" << tab4 << "_self = obj.longValue();\n }\n\n"; Printv(shadow_classdef, " public ", shadow_classname, "(java.lang.Long obj) {\n", tab4, "_self = obj.longValue();\n }\n\n", 0);
} else { } else {
shadow_classdef.replace("%BASECLASS%", ""); Replace(shadow_classdef,"%BASECLASS%", "",DOH_REPLACE_ANY);
shadow_classdef << " public long _self = 0;\n"; Printv(shadow_classdef,
shadow_classdef << " public boolean _selfown = false;\n\n"; " public long _self = 0;\n",
" public boolean _selfown = false;\n\n",
" public static Object newInstance(long p) {\n",
" return new ", shadow_classname, "(new Long(p));\n",
" };\n\n",
shadow_classdef << " public static Object newInstance(long p) {\n"; " public ", shadow_classname, "(java.lang.Long obj) {\n", tab4, "_self = obj.longValue();\n }\n\n",
shadow_classdef << " return new " << shadow_classname << "(new Long(p));\n"; 0);
shadow_classdef << " };\n\n";
shadow_classdef << " public " << shadow_classname << "(java.lang.Long obj) {\n" << tab4 << "_self = obj.longValue();\n }\n\n";
} }
Printv(shadow_classdef, " public Class _selfClass() {\n", tab4, "return ", shadow_classname, ".class;\n", " };\n\n", 0);
shadow_classdef << " public Class _selfClass() {\n" << tab4 << "return " << shadow_classname << ".class;\n" << " };\n\n"; Printv(f_shadow, shadow_classdef,0);
fprintf(f_shadow, (char *) shadow_classdef.get());
shadow_classdef_emitted = 1; shadow_classdef_emitted = 1;
} }
@ -1009,8 +1032,10 @@ void JAVA::cpp_close_class() {
} }
void JAVA::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) { void JAVA::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) {
String arg; char arg[256];
String nativecall; DOHString *nativecall;
nativecall = NewString("");
this->Language::cpp_member_func(name,iname,t,l); this->Language::cpp_member_func(name,iname,t,l);
@ -1027,12 +1052,12 @@ void JAVA::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) {
fprintf(f_shadow, " public %s %s(", (shadowrettype) ? shadowrettype : javarettype, iname); fprintf(f_shadow, " public %s %s(", (shadowrettype) ? shadowrettype : javarettype, iname);
if((t->type != T_VOID || t->is_pointer)) { if((t->type != T_VOID || t->is_pointer)) {
nativecall << "return "; Printf(nativecall,"return ");
if(shadowrettype) { if(shadowrettype) {
nativecall << "new " << shadowrettype << "(new Long("; Printv(nativecall, "new ", shadowrettype, "(new Long(", 0);
} }
} }
nativecall << module << "." << Swig_name_member(shadow_classname,iname) << "(_self"; Printv(nativecall, module, ".", Swig_name_member(shadow_classname,iname), "(_self", 0);
int pcount = l->nparms; int pcount = l->nparms;
@ -1040,15 +1065,14 @@ void JAVA::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) {
Parm *p = l->get(i); // Get the ith argument Parm *p = l->get(i); // Get the ith argument
// Produce string representation of source and target arguments // Produce string representation of source and target arguments
if(p->name && *(p->name)) if(p->name && *(p->name))
arg = p->name; strcpy(arg,p->name);
else { else {
arg = "arg"; sprintf(arg,"arg%d",i);
arg << i;
} }
if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) { if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) {
nativecall << ", " << arg << "._self"; Printv(nativecall, ", ", arg, "._self", 0);
} else nativecall << ", " << arg; } else Printv(nativecall, ", ", arg, 0);
char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name); char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name);
if(!jtype) jtype = SwigTcToJavaType(p->t, 0, 0); if(!jtype) jtype = SwigTcToJavaType(p->t, 0, 0);
@ -1059,30 +1083,32 @@ void JAVA::cpp_member_func(char *name, char *iname, DataType *t, ParmList *l) {
} }
// Add to java function header // Add to java function header
fprintf(f_shadow, "%s %s", (jstype) ? jstype : jtype, (char *) arg.get()); fprintf(f_shadow, "%s %s", (jstype) ? jstype : jtype, arg);
if(i != pcount-1) { if(i != pcount-1) {
fprintf(f_shadow, ", "); fprintf(f_shadow, ", ");
} }
} }
if((t->type != T_VOID) && shadowrettype) if((t->type != T_VOID) && shadowrettype)
nativecall << "))"; Printf(nativecall, "))");
nativecall << ");\n"; Printf(nativecall,");\n");
Printf(f_shadow, ") {\n");
Printf(f_shadow, "\t%s\n", nativecall);
Printf(f_shadow, " }\n\n");
Delete(nativecall);
fprintf(f_shadow, ") {\n");
fprintf(f_shadow, "\t%s\n", (char *) nativecall.get());
fprintf(f_shadow, " }\n\n");
} }
void JAVA::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) { void JAVA::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) {
String arg; char arg[256];
String nativecall = ""; DOHString *nativecall;
this->Language::cpp_static_func(name,iname,t,l); this->Language::cpp_static_func(name,iname,t,l);
if(!shadow) return; if(!shadow) return;
nativecall = NewString("");
if(!shadow_classdef_emitted) emit_shadow_classdef(); if(!shadow_classdef_emitted) emit_shadow_classdef();
char *javarettype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, t, iname); char *javarettype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, t, iname);
@ -1095,12 +1121,12 @@ void JAVA::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) {
fprintf(f_shadow, " public static %s %s(", (shadowrettype) ? shadowrettype : javarettype, iname); fprintf(f_shadow, " public static %s %s(", (shadowrettype) ? shadowrettype : javarettype, iname);
if((t->type != T_VOID || t->is_pointer)) { if((t->type != T_VOID || t->is_pointer)) {
nativecall << "return "; Printf(nativecall, "return ");
if(shadowrettype) { if(shadowrettype) {
nativecall << "new " << shadowrettype << "(new Long("; Printv(nativecall, "new ", shadowrettype, "(new Long(", 0);
} }
} }
nativecall << module << "." << Swig_name_member(shadow_classname,iname) << "("; Printv(nativecall, module, ".", Swig_name_member(shadow_classname,iname), "(", 0);
int pcount = l->nparms; int pcount = l->nparms;
int gencomma = 0; int gencomma = 0;
@ -1109,17 +1135,16 @@ void JAVA::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) {
Parm *p = l->get(i); // Get the ith argument Parm *p = l->get(i); // Get the ith argument
// Produce string representation of source and target arguments // Produce string representation of source and target arguments
if(p->name && *(p->name)) if(p->name && *(p->name))
arg = p->name; strcpy(arg,p->name);
else { else {
arg = "arg"; sprintf(arg,"arg%d",i);
arg << i;
} }
if(gencomma) nativecall << ", "; if(gencomma) Printf(nativecall,", ");
if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) { if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) {
nativecall << arg << "._self"; Printv(nativecall, arg, "._self", 0);
} else nativecall << arg; } else Printv(nativecall,arg,0);
gencomma = 1; gencomma = 1;
@ -1132,21 +1157,22 @@ void JAVA::cpp_static_func(char *name, char *iname, DataType *t, ParmList *l) {
} }
// Add to java function header // Add to java function header
fprintf(f_shadow, "%s %s", (jstype) ? jstype : jtype, (char *) arg.get()); Printf(f_shadow, "%s %s", (jstype) ? jstype : jtype, arg);
if(i != pcount-1) { if(i != pcount-1) {
fprintf(f_shadow, ", "); Printf(f_shadow, ", ");
} }
} }
if((t->type != T_VOID || t->is_pointer) && shadowrettype) if((t->type != T_VOID || t->is_pointer) && shadowrettype)
nativecall << "))"; Printf(nativecall,"))");
nativecall << ");\n"; Printf(nativecall,");\n");
fprintf(f_shadow, ") {\n"); Printf(f_shadow, ") {\n");
fprintf(f_shadow, "\t%s\n", (char *) nativecall.get()); Printf(f_shadow, "\t%s\n", nativecall);
fprintf(f_shadow, " }\n\n"); Printf(f_shadow, " }\n\n");
Delete(nativecall);
} }
void JAVA::cpp_constructor(char *name, char *iname, ParmList *l) { void JAVA::cpp_constructor(char *name, char *iname, ParmList *l) {
@ -1155,16 +1181,16 @@ void JAVA::cpp_constructor(char *name, char *iname, ParmList *l) {
if(!shadow) return; if(!shadow) return;
if(!shadow_classdef_emitted) emit_shadow_classdef(); if(!shadow_classdef_emitted) emit_shadow_classdef();
String nativecall; DOHString *nativecall = NewString("");
String arg; char arg[256];
fprintf(f_shadow, " public %s(", shadow_classname); fprintf(f_shadow, " public %s(", shadow_classname);
nativecall << " if(_self == 0 && " << shadow_classname << ".class == _selfClass()) {\n"; Printv(nativecall, " if(_self == 0 && ", shadow_classname, ".class == _selfClass()) {\n", 0);
if (iname != NULL) if (iname != NULL)
nativecall << tab8 << " _self = " << module << "." << Swig_name_construct(iname) << "("; Printv(nativecall, tab8, " _self = ", module, ".", Swig_name_construct(iname), "(", 0);
else else
nativecall << tab8 << " _self = " << module << "." << Swig_name_construct(shadow_classname) << "("; Printv(nativecall, tab8, " _self = ", module, ".", Swig_name_construct(shadow_classname), "(", 0);
int pcount = l->nparms; int pcount = l->nparms;
if(pcount == 0) // We must have a default constructor if(pcount == 0) // We must have a default constructor
@ -1174,10 +1200,9 @@ void JAVA::cpp_constructor(char *name, char *iname, ParmList *l) {
Parm *p = l->get(i); // Get the ith argument Parm *p = l->get(i); // Get the ith argument
// Produce string representation of source and target arguments // Produce string representation of source and target arguments
if(p->name && *(p->name)) if(p->name && *(p->name))
arg = p->name; strcpy(arg,p->name);
else { else {
arg = "arg"; sprintf(arg,"arg%d",i);
arg << i;
} }
char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name); char *jtype = JavaTypeFromTypemap((char*)"jtype", typemap_lang, p->t, p->name);
@ -1187,26 +1212,29 @@ void JAVA::cpp_constructor(char *name, char *iname, ParmList *l) {
if(strcmp(jtype, jstype) == 0) jstype = NULL; if(strcmp(jtype, jstype) == 0) jstype = NULL;
// Add to java function header // Add to java function header
fprintf(f_shadow, "%s %s", (jstype) ? jstype : jtype, (char *) arg.get()); Printf(f_shadow, "%s %s", (jstype) ? jstype : jtype, arg);
if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) { if(p->t->type == T_USER && p->t->is_pointer <= 1 && Getattr(shadow_classes,p->t->name)) {
nativecall << arg << "._self"; Printv(nativecall,arg, "._self", 0);
} else nativecall << arg; } else Printv(nativecall, arg, 0);
if(i != pcount-1) { if(i != pcount-1) {
nativecall << ", "; Printf(nativecall, ", ");
fprintf(f_shadow, ", "); Printf(f_shadow, ", ");
} }
} }
fprintf(f_shadow, ") {\n"); Printf(f_shadow, ") {\n");
nativecall << ");\n"; Printv(nativecall,
nativecall << tab8 << " _selfown = true;\n"; ");\n",
nativecall << " }\n"; tab8, " _selfown = true;\n",
" }\n",
0);
fprintf(f_shadow, "%s", (char *) nativecall.get()); Printf(f_shadow, "%s", nativecall);
fprintf(f_shadow, " }\n\n"); Printf(f_shadow, " }\n\n");
Delete(nativecall);
} }
void JAVA::cpp_destructor(char *name, char *newname) { void JAVA::cpp_destructor(char *name, char *newname) {

View file

@ -5,49 +5,7 @@
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
class JAVA : public Language { class JAVA : public Language {
private:
char *module; // Name of the module
char *java_path;
char *package; // Name of the package
char *c_pkgstr; // Name of the package
char *jni_pkgstr; // Name of the package
char *shadow_classname;
char *jimport;
char *method_modifiers;
FILE *f_java;
FILE *f_shadow;
int shadow;
DOH *shadow_classes;
String shadow_classdef;
char *shadow_name;
char *shadow_baseclass;
int classdef_emitted;
int shadow_classdef_emitted;
int have_default_constructor;
int native_func; // Set to 1 when wrapping a native function
int member_func; // Set to 1 when wrapping a member function
int jnic; // 1: use c syntax jni; 0: use c++ syntax jni
int finalize; // generate finalize methods
int useRegisterNatives; // Set to 1 when doing stuff with register natives
String registerNativesList;
public : public :
JAVA() {
module = NULL;
package = NULL;
java_path = (char*)"java";
f_java = f_shadow = NULL;
native_func = shadow = member_func = 0;
jimport = shadow_name = shadow_baseclass = NULL;
method_modifiers = (char*)"public final static";
jnic = -1;
useRegisterNatives=0;
finalize=0;
shadow_classes = NewHash();
classdef_emitted = 0;
shadow_classdef_emitted = 0;
};
// Virtual functions required by the SWIG parser // Virtual functions required by the SWIG parser
@ -80,8 +38,7 @@ public :
/* Java Module methods */ /* Java Module methods */
void emit_classdef(); void emit_classdef();
void emit_shadow_classdef(); void emit_shadow_classdef();
char *JNICALL(char *func); char *JNICALL(DOHString_or_char *func);
char *JNICALL(String& func);
char *SwigTcToJniType(DataType *t, int ret); char *SwigTcToJniType(DataType *t, int ret);
char *SwigTcToJavaType(DataType *t, int ret, int inShadow); char *SwigTcToJavaType(DataType *t, int ret, int inShadow);
char *SwigTcToJniScalarType(DataType *t); char *SwigTcToJniScalarType(DataType *t);