fixed loging crash caused by printf
[cacao.git] / src / native / native.c
index 5378f1d7e10c4ecde1f3fa6a25c91251860db75c..b4b01001fb8752fa7df5bf648440bf4fa7322178 100644 (file)
@@ -1,38 +1,63 @@
-/****************************** native.c ***************************************
+/* native.c - table of native functions
 
-       Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
+   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
+   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
+   P. Tomsich, J. Wenninger
 
-       See file COPYRIGHT for information on usage and disclaimer of warranties
+   This file is part of CACAO.
 
-    Contains the tables for native methods.
-       The .hh files created with the header file generator are all included here
-       as are the C functions implementing these methods.
-       
-       Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at 
-                Roman Obermaisser   EMAIL: cacao@complang.tuwien.ac.at 
-                Andreas Krall       EMAIL: cacao@complang.tuwien.ac.at 
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
 
-       Last Change: 2003/02/26
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
 
-*******************************************************************************/
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.
+
+   Contact: cacao@complang.tuwien.ac.at
+
+   Authors: Reinhard Grafl
+            Roman Obermaisser
+            Andreas Krall
+
+   The .hh files created with the header file generator are all
+   included here as are the C functions implementing these methods.
+
+   $Id: native.c 730 2003-12-11 21:23:31Z edwin $
+
+*/
 
+
+#include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
+#include <math.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/time.h>
+#include <utime.h>
+
+#include "config.h"
 #include "global.h"
+#include "jni.h"
 #include "native.h"
 #include "nativetypes.hh"
 #include "builtin.h"
 #include "asmpart.h"
 #include "tables.h"
 #include "loader.h"
-#include <math.h>
-#include <string.h>
-#include <assert.h>
-#include <sys/time.h>
-#include <utime.h>
-#include <sys/utsname.h>
-
-#include "threads/thread.h"                       /* schani */
+#include "toolbox/loging.h"
+#include "toolbox/memory.h"
+#include "threads/thread.h"
+#include "threads/threadio.h"
 #include "threads/locks.h"
 
 /* Include files for IO functions */
 #endif
 #include <sys/stat.h>
 
-#include "../threads/threadio.h"                    
+#include "threads/threadio.h"
 
 /* searchpath for classfiles */
-static char *classpath;
+char *classpath;
 
 /* for java-string to char conversion */
 #define MAXSTRINGSIZE 1000                          
 
 /******************** systemclasses required for native methods ***************/
 
-static classinfo *class_java_lang_Class;
-static classinfo *class_java_lang_Cloneable;
-static classinfo *class_java_lang_CloneNotSupportedException;
-static classinfo *class_java_lang_System;
-static classinfo *class_java_lang_ClassLoader;
-static classinfo *class_java_lang_ClassNotFoundException;
-static classinfo *class_java_lang_InstantiationException;
-static classinfo *class_java_lang_NoSuchMethodError;   
-static classinfo *class_java_lang_NoSuchFieldError;
-static classinfo *class_java_lang_ClassFormatError;
-static classinfo *class_java_lang_IllegalArgumentException;
-static classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
-static classinfo *class_java_lang_NoSuchFieldException;
-static classinfo *class_java_io_SyncFailedException;
-static classinfo *class_java_io_IOException;
-static classinfo *class_java_io_FileNotFoundException;
-static classinfo *class_java_io_UnixFileSystem;
-static classinfo *class_java_security_PrivilegedActionException;
-static classinfo *class_java_net_UnknownHostException;
-static classinfo *class_java_net_SocketException;
-static classinfo *class_java_lang_NoSuchMethodException;
-static classinfo *class_java_lang_Double;
-static classinfo *class_java_lang_Float;
-static classinfo *class_java_lang_Long;
-static classinfo *class_java_lang_Byte;
-static classinfo *class_java_lang_Short;
-static classinfo *class_java_lang_Boolean;
-static classinfo *class_java_lang_Void;
-static classinfo *class_java_lang_Character;
-static classinfo *class_java_lang_Integer;
+classinfo *class_java_lang_Class;
+classinfo *class_java_lang_VMClass;
+methodinfo *method_vmclass_init;
+/* static classinfo *class_java_lang_Cloneable=0; */ /* now in global.h */
+classinfo *class_java_lang_CloneNotSupportedException;
+classinfo *class_java_lang_System;
+classinfo *class_java_lang_ClassLoader;
+classinfo *class_java_lang_ClassNotFoundException;
+classinfo *class_java_lang_LinkageError;
+classinfo *class_java_lang_InstantiationException;
+classinfo *class_java_lang_NoSuchMethodError;   
+classinfo *class_java_lang_NoSuchFieldError;
+classinfo *class_java_lang_ClassFormatError;
+classinfo *class_java_lang_IllegalArgumentException;
+classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
+classinfo *class_java_lang_NoSuchFieldException;
+classinfo *class_java_io_SyncFailedException;
+classinfo *class_java_io_IOException;
+classinfo *class_java_io_FileNotFoundException;
+classinfo *class_java_io_UnixFileSystem;
+classinfo *class_java_security_PrivilegedActionException;
+classinfo *class_java_net_UnknownHostException;
+classinfo *class_java_net_SocketException;
+classinfo *class_java_lang_NoSuchMethodException;
+classinfo *class_java_lang_Double;
+classinfo *class_java_lang_Float;
+classinfo *class_java_lang_Long;
+classinfo *class_java_lang_Byte;
+classinfo *class_java_lang_Short;
+classinfo *class_java_lang_Boolean;
+classinfo *class_java_lang_Void;
+classinfo *class_java_lang_Character;
+classinfo *class_java_lang_Integer;
 
 /* the system classloader object */
 struct java_lang_ClassLoader *SystemClassLoader = NULL;
@@ -94,74 +122,71 @@ java_objectheader* exceptionptr = NULL;
 
 /************* use classinfo structure as java.lang.Class object **************/
 
-void use_class_as_object (classinfo *c) 
+void use_class_as_object(classinfo *c) 
 {
-       vftbl *vt = class_java_lang_Class -> vftbl;
+       vftbl *vt;
        vftbl *newtbl;
+
+       if (!class_java_lang_Class)
+               class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
+
+       vt = class_java_lang_Class->vftbl;
+
+
        if (!c->classvftbl) {
                c->classvftbl = true;
-               copy_vftbl(&newtbl, vt);
-               newtbl->class = c->header.vftbl->class;
-               newtbl->baseval = c->header.vftbl->baseval;
-               newtbl->diffval = c->header.vftbl->diffval;
-               c->header.vftbl = newtbl;
+
+               /*                copy_vftbl(&newtbl, vt);
+                                                 newtbl->class = c->header.vftbl->class;
+                                                 newtbl->baseval = c->header.vftbl->baseval;
+                                                 newtbl->diffval = c->header.vftbl->diffval;
+                                                 c->header.vftbl = newtbl;*/
+               
+               c->header.vftbl = class_java_lang_Class->vftbl;
+        
+               if (!class_java_lang_VMClass) {
+                       loader_load_sysclass(&class_java_lang_VMClass,
+                                                                utf_new_char("java/lang/VMClass"));
+
+                       method_vmclass_init =
+                               class_findmethod(class_java_lang_VMClass,
+                                                                utf_new_char("<init>"),
+                                                                utf_new_char("(Lgnu/classpath/RawData;)V"));
+
+                       if (method_vmclass_init == 0) {
+                               class_showmethods(class_java_lang_VMClass);
+                               panic("Needed class initializer for VMClass could not be found");
+                       }
+               }
+               {     
+                       java_objectheader *vmo = builtin_new(class_java_lang_VMClass);
+
+                       if (!vmo) panic("Error while creating instance of java/lang/VMClass");
+                       asm_calljavamethod(method_vmclass_init, vmo, c, NULL, NULL);
+                       c->vmClass = (java_lang_VMClass *) vmo;
+                       /*log_text("VMCLASS has been attached");*/
+               }
        }
 }
 
-/*********************** include Java Native Interface ************************/ 
-
-#include "jni.c"
 
 /*************************** include native methods ***************************/ 
 
-#include "nat/Object.c"
-#include "nat/String.c"
-#include "nat/ClassLoader.c"
-#include "nat/Class.c"
-#include "nat/Compiler.c"
-#include "nat/Double.c"
-#include "nat/Float.c"
-#include "nat/Math.c"
-#include "nat/Package.c"
-#include "nat/Runtime.c"
-#include "nat/SecurityManager.c"
-#include "nat/System.c"
-#include "nat/Thread.c"
-#include "nat/Throwable.c"
-#include "nat/Finalizer.c"
-#include "nat/Array.c"
-#include "nat/Constructor.c"
-#include "nat/Field.c"
-#include "nat/Method.c"
-#include "nat/FileDescriptor.c"
-#include "nat/FileInputStream.c"
-#include "nat/FileOutputStream.c"
-#include "nat/FileSystem.c"
-#include "nat/ObjectInputStream.c"
-#include "nat/ObjectStreamClass.c"
-#include "nat/RandomAccessFile.c"
-#include "nat/ResourceBundle.c"
-#include "nat/JarFile.c"
-#include "nat/Adler32.c"
-#include "nat/CRC32.c"
-#include "nat/Deflater.c"
-#include "nat/Inflater.c"
-#include "nat/ZipEntry.c"
-#include "nat/ZipFile.c"
-#include "nat/BigInteger.c"
-#include "nat/InetAddress.c"
-#include "nat/InetAddressImpl.c"
-#include "nat/DatagramPacket.c"
-#include "nat/PlainDatagramSocketImpl.c"
-#include "nat/PlainSocketImpl.c"
-#include "nat/SocketInputStream.c"
-#include "nat/SocketOutputStream.c"
-#include "nat/AccessController.c"
-#include "nat/ClassLoader_NativeLibrary.c"
-#include "nat/UnixFileSystem.c"
+#ifdef USE_GTK 
+#include "nat/GdkGraphics.c"
+#include "nat/GtkComponentPeer.c"
+#include "nat/GdkPixbufDecoder.c"
+#include "nat/GtkScrollPanePeer.c"
+#include "nat/GtkFileDialogPeer.c"
+#include "nat/GtkLabelPeer.c"
+#endif
+
 
 /************************** tables for methods ********************************/
 
+#undef JOWENN_DEBUG
+#undef JOWENN_DEBUG1
+
 /* table for locating native methods */
 static struct nativeref {
        char *classname;
@@ -185,7 +210,7 @@ static struct nativecompref {
        utf *descriptor;
        bool isstatic;
        functionptr func;
-       } nativecomptable [NATIVETABLESIZE];
+} nativecomptable [NATIVETABLESIZE];
 
 /* string comparsion table initialized */
 static bool nativecompdone = false;
@@ -213,77 +238,89 @@ static bool nativecompdone = false;
 
 void native_loadclasses()
 {
+       static int classesLoaded=0; /*temporary hack JoWenn*/
+       if (classesLoaded) return;
+       classesLoaded=1;
+/*     log_text("loadclasses entered");*/
+
+
+       /*class_java_lang_System =*/
+               (void)class_new ( utf_new_char ("java/lang/VMClass") );/*JoWenn*/
+               (void)class_new ( utf_new_char ("java/lang/Class") );/*JoWenn*/
        /* class_new adds the class to the list of classes to be loaded */
-       class_java_lang_Cloneable = 
-               class_new ( utf_new_char ("java/lang/Cloneable") );
+       if (!class_java_lang_Cloneable)
+               class_java_lang_Cloneable = 
+                       class_new ( utf_new_char ("java/lang/Cloneable") );
+/*     log_text("loadclasses: class_java_lang_Cloneable has been initialized");*/
        class_java_lang_CloneNotSupportedException = 
                class_new ( utf_new_char ("java/lang/CloneNotSupportedException") );
-       class_java_lang_Class =
-               class_new ( utf_new_char ("java/lang/Class") );
+       if (!class_java_lang_Class)
+               class_java_lang_Class =
+                       class_new ( utf_new_char ("java/lang/Class") );
        class_java_io_IOException = 
-               class_new ( utf_new_char ("java/io/IOException") );
+               class_new(utf_new_char("java/io/IOException"));
        class_java_io_FileNotFoundException = 
-               class_new ( utf_new_char ("java/io/FileNotFoundException") );
+               class_new(utf_new_char("java/io/FileNotFoundException"));
        class_java_lang_ClassNotFoundException =
-               class_new ( utf_new_char ("java/lang/ClassNotFoundException") );
+               class_new(utf_new_char("java/lang/ClassNotFoundException"));
+       class_java_lang_LinkageError =
+               class_new(utf_new_char("java/lang/LinkageError"));
        class_java_lang_InstantiationException =
-               class_new ( utf_new_char ("java/lang/InstantiationException") );
+               class_new(utf_new_char("java/lang/InstantiationException"));
        class_java_lang_NoSuchMethodError =
-               class_new ( utf_new_char ("java/lang/NoSuchMethodError") );
+               class_new(utf_new_char("java/lang/NoSuchMethodError"));
        class_java_lang_NoSuchFieldError =
-               class_new ( utf_new_char ("java/lang/NoSuchFieldError") );      
+               class_new(utf_new_char("java/lang/NoSuchFieldError"));  
        class_java_lang_ClassFormatError =
-               class_new ( utf_new_char ("java/lang/ClassFormatError") );      
+               class_new(utf_new_char("java/lang/ClassFormatError"));  
        class_java_io_SyncFailedException =
                class_new ( utf_new_char ("java/io/SyncFailedException") );
-       class_java_io_UnixFileSystem =
-               class_new ( utf_new_char ("java/io/UnixFileSystem") );
-       class_java_lang_System =
-               class_new ( utf_new_char ("java/lang/System") );
+               
+/*     log_text("native_loadclasses: class_new(\"java/lang/ClassLoader\")");           */
        class_java_lang_ClassLoader =
                class_new ( utf_new_char ("java/lang/ClassLoader") );   
+/*     log_text("native_loadclasses: class_new(\"java/security/PrivilegedActionException\")");         */
        class_java_security_PrivilegedActionException =
-               class_new( utf_new_char("java/security/PrivilegedActionException"));
-       class_java_net_UnknownHostException = 
-               loader_load( utf_new_char ("java/net/UnknownHostException") );
-       class_java_net_SocketException = 
-               loader_load( utf_new_char ("java/net/SocketException") );
+               class_new(utf_new_char("java/security/PrivilegedActionException"));
+
+       loader_load_sysclass(&class_java_net_UnknownHostException,
+                                                utf_new_char("java/net/UnknownHostException"));
+       loader_load_sysclass(&class_java_net_SocketException,
+                                                utf_new_char("java/net/SocketException"));
+
        class_java_lang_IllegalArgumentException =
-               class_new( utf_new_char("java/lang/IllegalArgumentException"));
+               class_new(utf_new_char("java/lang/IllegalArgumentException"));
        class_java_lang_ArrayIndexOutOfBoundsException =
-               class_new( utf_new_char ("java/lang/ArrayIndexOutOfBoundsException") );
+               class_new(utf_new_char("java/lang/ArrayIndexOutOfBoundsException"));
        class_java_lang_NoSuchFieldException =
-               class_new( utf_new_char ("java/lang/NoSuchFieldException") );       
+               class_new(utf_new_char("java/lang/NoSuchFieldException"));
        class_java_lang_NoSuchMethodException = 
-               class_new( utf_new_char ("java/lang/NoSuchMethodException") );      
+               class_new(utf_new_char("java/lang/NoSuchMethodException"));
 
        /* load classes for wrapping primitive types */
        class_java_lang_Double =
                class_new( utf_new_char ("java/lang/Double") );
+       class_init(class_java_lang_Double);
+
        class_java_lang_Float =
-               class_new( utf_new_char ("java/lang/Float") );
+               class_new(utf_new_char("java/lang/Float"));
        class_java_lang_Character =
-               class_new( utf_new_char ("java/lang/Character") );
+               class_new(utf_new_char("java/lang/Character"));
        class_java_lang_Integer =
-               class_new( utf_new_char ("java/lang/Integer") );
+               class_new(utf_new_char("java/lang/Integer"));
        class_java_lang_Long =
-               class_new( utf_new_char ("java/lang/Long") );
+               class_new(utf_new_char("java/lang/Long"));
        class_java_lang_Byte =
-               class_new( utf_new_char ("java/lang/Byte") );
+               class_new(utf_new_char("java/lang/Byte"));
        class_java_lang_Short =
-               class_new( utf_new_char ("java/lang/Short") );
+               class_new(utf_new_char("java/lang/Short"));
        class_java_lang_Boolean =
-               class_new( utf_new_char ("java/lang/Boolean") );
+               class_new(utf_new_char("java/lang/Boolean"));
        class_java_lang_Void =
-               class_new( utf_new_char ("java/lang/Void") );
+               class_new(utf_new_char("java/lang/Void"));
 
-       /* load to avoid dynamic classloading */
-       class_new(utf_new_char("sun/net/www/protocol/file/Handler"));
-       class_new(utf_new_char("sun/net/www/protocol/jar/Handler"));    
-       class_new(utf_new_char("sun/io/CharToByteISO8859_1"));
-       
-       /* start classloader */
-       loader_load(utf_new_char("sun/io/ByteToCharISO8859_1")); 
+       classesLoaded=1;
+       log_text("native_loadclasses finished");
 }
 
 
@@ -291,51 +328,34 @@ void native_loadclasses()
 
 void systemclassloader_addclass(classinfo *c)
 {
-        methodinfo *m;
+       methodinfo *m;
 
        /* find method addClass of java.lang.ClassLoader */
-       m = class_resolvemethod (
-               class_java_lang_ClassLoader, 
-               utf_new_char("addClass"),
-               utf_new_char("(Ljava/lang/Class;)")
-           );
-
+       m = class_resolvemethod(
+                                                       class_java_lang_ClassLoader, 
+                                                       utf_new_char("addClass"),
+                                                       utf_new_char("(Ljava/lang/Class;)")
+                                                       );
+       
        if (!m) panic("warning: cannot initialize classloader");
 
        /* prepare class to be passed as argument */
-       use_class_as_object (c);
+       use_class_as_object (c);
 
        /* call 'addClass' */
        asm_calljavamethod(m,
-                           (java_objectheader*) SystemClassLoader, 
-                          (java_objectheader*) c,
-                          NULL,  
-                          NULL
-                         );       
+                                          (java_objectheader*) SystemClassLoader, 
+                                          (java_objectheader*) c,
+                                          NULL,  
+                                          NULL
+                                          );
 }
 
 /*************** adds a library to the vector of loaded libraries *************/
 
 void systemclassloader_addlibrary(java_objectheader *o)
 {
-        methodinfo *m;
-
-       /* find method addElement of java.util.Vector */
-       m = class_resolvemethod (
-               loader_load ( utf_new_char ("java/util/Vector") ),
-               utf_new_char("addElement"),
-               utf_new_char("(Ljava/lang/Object;)V")
-           );
-
-       if (!m) panic("cannot initialize classloader");
-
-       /* call 'addElement' */
-       asm_calljavamethod(m,
-                          SystemClassLoader->nativeLibraries,
-                          o,
-                          NULL,  
-                          NULL
-                         );       
+       log_text("systemclassloader_addlibrary");
 }
 
 /*****************************************************************************
@@ -347,7 +367,8 @@ void systemclassloader_addlibrary(java_objectheader *o)
 void init_systemclassloader() 
 {
   if (!SystemClassLoader) {
-
+       native_loadclasses();
+       log_text("Initializing new system class loader");
        /* create object and call initializer */
        SystemClassLoader = (java_lang_ClassLoader*) native_new_and_init(class_java_lang_ClassLoader);  
        heap_addreference((void**) &SystemClassLoader);
@@ -356,6 +377,7 @@ void init_systemclassloader()
        SystemClassLoader->parent      = NULL;
        SystemClassLoader->initialized = true;
   }
+  log_text("leaving system class loader");
 }
 
 
@@ -363,26 +385,25 @@ void init_systemclassloader()
 
 void systemclassloader_addlibname(java_objectheader *o)
 {
-        methodinfo *m;
+       methodinfo *m;
        jfieldID id;
 
-       m = class_resolvemethod (
-               loader_load ( utf_new_char ("java/util/Vector") ),
-               utf_new_char("addElement"),
-               utf_new_char("(Ljava/lang/Object;)V")
-           );
+       m = class_resolvemethod(loader_load_sysclass(NULL,utf_new_char ("java/util/Vector")),
+                                                       utf_new_char("addElement"),
+                                                       utf_new_char("(Ljava/lang/Object;)V")
+                                                       );
 
        if (!m) panic("cannot initialize classloader");
 
-       id = env.GetStaticFieldID(&env,class_java_lang_ClassLoader,"loadedLibraryNames","Ljava/util/Vector;");
+       id = envTable.GetStaticFieldID(&env,class_java_lang_ClassLoader,"loadedLibraryNames","Ljava/util/Vector;");
        if (!id) panic("can not access ClassLoader");
 
        asm_calljavamethod(m,
-                          GetStaticObjectField(&env,class_java_lang_ClassLoader,id),
-                          o,
-                          NULL,  
-                          NULL
-                         );       
+                                          GetStaticObjectField(&env,class_java_lang_ClassLoader,id),
+                                          o,
+                                          NULL,  
+                                          NULL
+                                          );       
 }
 
 
@@ -394,16 +415,50 @@ void native_setclasspath (char *path)
        classpath = path;
 }
 
+
 /***************** function: throw_classnotfoundexception *********************/
 
 void throw_classnotfoundexception()
 {
+       dolog("throw_classnotfoundexception");
     if (!class_java_lang_ClassNotFoundException) {
         panic("java.lang.ClassNotFoundException not found. Maybe wrong classpath?");
     }
 
        /* throws a ClassNotFoundException */
-       exceptionptr = native_new_and_init (class_java_lang_ClassNotFoundException);
+       exceptionptr = native_new_and_init(class_java_lang_ClassNotFoundException);
+       dolog("set exceptionptr");
+}
+
+
+void throw_classnotfoundexception2(utf* classname)
+{
+       dolog("throw_classnotfoundexception2");
+       log_plain("Class: "); log_plain_utf(classname); log_nl();
+       
+       if (!class_java_lang_ClassNotFoundException) {
+               panic("java.lang.ClassNotFoundException not found. Maybe wrong classpath?");
+       }
+
+       /* throws a ClassNotFoundException with message */
+       exceptionptr = native_new_and_init_string(class_java_lang_ClassNotFoundException,
+                                                                                         javastring_new(classname));
+       dolog("set exceptionptr");
+}
+
+void throw_linkageerror2(utf* classname)
+{
+       dolog("throw_linkageerror2");
+       log_plain("Class: "); log_plain_utf(classname); log_nl();
+       
+       if (!class_java_lang_LinkageError) {
+               panic("java.lang.LinkageError not found. Maybe wrong classpath?");
+       }
+
+       /* throws a ClassNotFoundException with message */
+       exceptionptr = native_new_and_init_string(class_java_lang_LinkageError,
+                                                                                         javastring_new(classname));
+       dolog("set exceptionptr");
 }
 
 
@@ -418,60 +473,107 @@ void throw_classnotfoundexception()
 
 *******************************************************************************/
 
-functionptr native_findfunction (utf *cname, utf *mname, 
-                                 utf *desc, bool isstatic)
+functionptr native_findfunction(utf *cname, utf *mname, 
+                                                               utf *desc, bool isstatic)
 {
        int i;
        /* entry of table for fast string comparison */
        struct nativecompref *n;
-        /* for warning message if no function is found */
+       /* for warning message if no function is found */
        char *buffer;                   
        int buffer_len;
 
        isstatic = isstatic ? true : false;
-
+       
        if (!nativecompdone) {
                for (i = 0; i < NATIVETABLESIZE; i++) {
                        nativecomptable[i].classname  = 
-                                       utf_new_char(nativetable[i].classname);
+                               utf_new_char(nativetable[i].classname);
                        nativecomptable[i].methodname = 
-                                       utf_new_char(nativetable[i].methodname);
+                               utf_new_char(nativetable[i].methodname);
                        nativecomptable[i].descriptor = 
-                                       utf_new_char(nativetable[i].descriptor);
+                               utf_new_char(nativetable[i].descriptor);
                        nativecomptable[i].isstatic   = 
-                                       nativetable[i].isstatic;
+                               nativetable[i].isstatic;
                        nativecomptable[i].func       = 
-                                       nativetable[i].func;
-                       }
-               nativecompdone = true;
+                               nativetable[i].func;
                }
+               nativecompdone = true;
+       }
+
+#ifdef JOWENN_DEBUG
+       buffer_len = 
+         utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
+       
+       buffer = MNEW(char, buffer_len);
+
+       strcpy(buffer, "searching matching function in native table:");
+        utf_sprint(buffer+strlen(buffer), mname);
+       strcpy(buffer+strlen(buffer), ": ");
+        utf_sprint(buffer+strlen(buffer), desc);
+       strcpy(buffer+strlen(buffer), " for class ");
+        utf_sprint(buffer+strlen(buffer), cname);
 
+       log_text(buffer);       
+
+       MFREE(buffer, char, buffer_len);
+#endif
+               
        for (i = 0; i < NATIVETABLESIZE; i++) {
                n = &(nativecomptable[i]);
 
                if (cname == n->classname && mname == n->methodname &&
                    desc == n->descriptor && isstatic == n->isstatic)
                        return n->func;
-               }
+#ifdef JOWENN_DEBUG
+                       else {
+                               if (cname == n->classname && mname == n->methodname )  log_text("static and descriptor mismatch");
+                       
+                               else {
+                                       buffer_len = 
+                                         utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
+       
+                                       buffer = MNEW(char, buffer_len);
+
+                                       strcpy(buffer, "comparing with:");
+                                       utf_sprint(buffer+strlen(buffer), n->methodname);
+                                       strcpy (buffer+strlen(buffer), ": ");
+                                       utf_sprint(buffer+strlen(buffer), n->descriptor);
+                                       strcpy(buffer+strlen(buffer), " for class ");
+                                       utf_sprint(buffer+strlen(buffer), n->classname);
+
+                                       log_text(buffer);       
 
+                                       MFREE(buffer, char, buffer_len);
+                       
+                               }
+                       } 
+#endif
+       }
+
+               
        /* no function was found, display warning */
 
        buffer_len = 
-         utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
+               utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
 
        buffer = MNEW(char, buffer_len);
 
        strcpy(buffer, "warning: native function ");
-        utf_sprint(buffer+strlen(buffer), mname);
-       strcpy(buffer+strlen(buffer), ": ");
-        utf_sprint(buffer+strlen(buffer), desc);
-       strcpy(buffer+strlen(buffer), " not found in class ");
-        utf_sprint(buffer+strlen(buffer), cname);
+       utf_sprint(buffer + strlen(buffer), mname);
+       strcpy(buffer + strlen(buffer), ": ");
+       utf_sprint(buffer + strlen(buffer), desc);
+       strcpy(buffer + strlen(buffer), " not found in class ");
+       utf_sprint(buffer + strlen(buffer), cname);
 
        log_text(buffer);       
 
        MFREE(buffer, char, buffer_len);
 
+
+       exit(1);
+
+       
        return NULL;
 }
 
@@ -493,6 +595,8 @@ java_objectheader *javastring_new (utf *u)
        java_chararray *a;
        s4 i;
        
+/*     log_text("javastring_new");*/
+       
        s = (java_lang_String*) builtin_new (class_java_lang_String);
        a = builtin_newarray_char (utflength);
 
@@ -505,9 +609,9 @@ java_objectheader *javastring_new (utf *u)
                a->data[i] = utf_nextu2(&utf_ptr);
        
        /* set fields of the javastring-object */
-       s -> value  = a;
-       s -> offset = 0;
-       s -> count  = utflength;
+       s->value  = a;
+       s->offset = 0;
+       s->count  = utflength;
 
        return (java_objectheader*) s;
 }
@@ -528,6 +632,7 @@ java_objectheader *javastring_new_char (char *text)
        java_lang_String *s;   /* result-string */
        java_chararray *a;
        
+       /*log_text("javastring_new_char");*/
        s = (java_lang_String*) builtin_new (class_java_lang_String);
        a = builtin_newarray_char (len);
 
@@ -539,9 +644,9 @@ java_objectheader *javastring_new_char (char *text)
                a->data[i] = text[i];
        
        /* set fields of the javastring-object */
-       s -> value = a;
-       s -> offset = 0;
-       s -> count = len;
+       s->value = a;
+       s->offset = 0;
+       s->count = len;
 
        return (java_objectheader*) s;
 }
@@ -565,6 +670,8 @@ char *javastring_tochar (java_objectheader *so)
        java_chararray *a;
        s4 i;
        
+       log_text("javastring_tochar");
+       
        if (!s)
                return "";
        a = s->value;
@@ -586,20 +693,35 @@ char *javastring_tochar (java_objectheader *so)
 
 *******************************************************************************/
  
-fieldinfo *class_findfield_approx (classinfo *c, utf *name)
+fieldinfo *class_findfield_approx(classinfo *c, utf *name)
 {
        s4 i;
        for (i = 0; i < c->fieldscount; i++) {
                /* compare field names */
                if ((c->fields[i].name == name))
                        return &(c->fields[i]);
-               }
+       }
 
        /* field was not found, raise exception */      
        exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
+
        return NULL;
 }
 
+s4 class_findfield_index_approx (classinfo *c, utf *name)
+{
+       s4 i;
+       for (i = 0; i < c->fieldscount; i++) {
+               /* compare field names */
+               if ((c->fields[i].name == name))
+                       return i;
+               }
+
+       /* field was not found, raise exception */      
+       exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
+       return -1;
+}
+
 
 /********************** function: native_new_and_init *************************
 
@@ -608,32 +730,81 @@ fieldinfo *class_findfield_approx (classinfo *c, utf *name)
                        
 *******************************************************************************/
 
-java_objectheader *native_new_and_init (classinfo *c)
+java_objectheader *native_new_and_init(classinfo *c)
 {
        methodinfo *m;
-       java_objectheader *o = builtin_new (c);         /*          create object */
+       java_objectheader *o;
 
-       if (!o) return NULL;
+       /* if c==NULL it is probebly because loader_load failed */
+       if (!c) return exceptionptr;
+
+       o = builtin_new(c);          /* create object          */
        
+        /*
+       printf("native_new_and_init ");
+       utf_display(c->name);
+       printf("\n");
+        */
+       if (!o) return NULL;
+       /* printf("o!=NULL\n"); */
        /* find initializer */
 
        m = class_findmethod(c, utf_new_char("<init>"), utf_new_char("()V"));
                                                      
        if (!m) {                                       /* initializer not found  */
                if (verbose) {
+                       char logtext[MAXLOGTEXT];
                        sprintf(logtext, "Warning: class has no instance-initializer: ");
                        utf_sprint(logtext + strlen(logtext), c->name);
-                       dolog();
-                       }
+                       log_text(logtext);
+               }
                return o;
+       }
+
+       /* call initializer */
+
+       asm_calljavamethod(m, o, NULL, NULL, NULL);
+
+       return o;
+}
+
+
+java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
+{
+       methodinfo *m;
+       java_objectheader *o;
+
+       /* if c==NULL it is probebly because loader_load failed */
+       if (!c) return exceptionptr;
+
+       o = builtin_new(c);          /* create object          */
+       
+       if (!o) return NULL;
+
+       /* find initializer */
+
+       m = class_findmethod(c,
+                                                utf_new_char("<init>"),
+                                                utf_new_char("(Ljava/lang/String;)V"));
+                                                     
+       if (!m) {                                       /* initializer not found  */
+               if (verbose) {
+                       char logtext[MAXLOGTEXT];
+                       sprintf(logtext, "Warning: class has no instance-initializer: ");
+                       utf_sprint(logtext + strlen(logtext), c->name);
+                       log_text(logtext);
                }
+               return o;
+       }
 
        /* call initializer */
 
-       asm_calljavamethod (m, o, NULL, NULL, NULL);
+       asm_calljavamethod(m, o, s, NULL, NULL);
+
        return o;
 }
 
+
 /******************** function: stringtable_update ****************************
 
        traverses the javastring hashtable and sets the vftbl-entries of
@@ -666,7 +837,7 @@ void stringtable_update ()
 
                                if (!a->header.objheader.vftbl) 
                                        /* vftbl of character-array is NULL */ 
-                                       a->header.objheader.vftbl = class_array -> vftbl;
+                                       a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
 
                                /* follow link in external hash chain */
                                s = s->hashlink;
@@ -722,12 +893,16 @@ utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
        utf *result;  /* resulting utf-string */
        int i;          
 
-       /* determine utf length in bytes and allocate memory */         
+       /* determine utf length in bytes and allocate memory */
+       /* printf("utf_new_u2: unicode_length=%d\n",unicode_length);            */
        buflength = u2_utflength(unicode_pos, unicode_length); 
        buffer    = MNEW(char,buflength);
  
        /* memory allocation failed */
-       if (!buffer) return NULL;
+       if (!buffer) {
+               printf("length: %d\n",buflength);
+               log_text("utf_new_u2:buffer==NULL");
+       }
 
        left = buflength;
        pos  = buffer;
@@ -768,6 +943,7 @@ utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
        
        /* insert utf-string into symbol-table */
        result = utf_new(buffer,buflength);
+
        MFREE(buffer, char, buflength);
        return result;
 }
@@ -781,7 +957,9 @@ utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
 utf *javastring_toutf(java_lang_String *string, bool isclassname)
 {
         java_lang_String *str = (java_lang_String *) string;
-       return utf_new_u2(str->value->data,str->count, isclassname);
+/*     printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count);
+       fflush(stdout);*/
+       return utf_new_u2(str->value->data+str->offset,str->count, isclassname);
 }
 
 /********************* function: literalstring_u2 *****************************
@@ -803,6 +981,11 @@ java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode
     u4 slot;  
     u2 i;
 
+#if JOWENN_DEBUG1
+    printf("literalstring_u2: length: %d\n",length);    
+    log_text("literalstring_u2");
+#endif
+    
     /* find location in hashtable */
     key  = unicode_hashkey (a->data, length);
     slot = key & (string_hash.size-1);
@@ -821,6 +1004,10 @@ java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode
        if (!copymode)
          lit_mem_free(a, sizeof(java_chararray) + sizeof(u2)*(length-1)+10);
 
+#ifdef JOWENN_DEBUG1
+       log_text("literalstring_u2: foundentry");
+       utf_display(javastring_toutf(js,0));
+#endif
        return (java_objectheader *) js;
       }
 
@@ -839,10 +1026,8 @@ java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode
       stringdata = a;
 
     /* location in hashtable found, complete arrayheader */
-    if (class_array==NULL) panic("class_array not initialized");
-    stringdata -> header.objheader.vftbl = class_array -> vftbl;
+    stringdata -> header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
     stringdata -> header.size = length;        
-    stringdata -> header.arraytype = ARRAYTYPE_CHAR;   
 
     /* create new javastring */
     js = LNEW (java_lang_String);
@@ -894,6 +1079,10 @@ java_objectheader *literalstring_u2 (java_chararray *a, u4 length, bool copymode
       MFREE (string_hash.ptr, void*, string_hash.size);
       string_hash = newhash;
     }
+#ifdef JOWENN_DEBUG1
+       log_text("literalstring_u2: newly created");
+/*     utf_display(javastring_toutf(js,0));*/
+#endif
                        
     return (java_objectheader *) js;
 }
@@ -911,7 +1100,10 @@ java_objectheader *literalstring_new (utf *u)
     u4 utflength  = utf_strlen(u);   /* length of utf-string if uncompressed */
     java_chararray *a;               /* u2-array constructed from utf string */
     u4 i;
-    
+/*    log_text("literalstring_new"); */
+/*    utf_display(u);*/
+    /*if (utflength==0) while (1) sleep(60);*/
+/*    log_text("------------------");    */
     /* allocate memory */ 
     a = lit_mem_alloc (sizeof(java_chararray) + sizeof(u2)*(utflength-1)+10 ); 
     /* convert utf-string to u2-array */
@@ -945,11 +1137,196 @@ void literalstring_free (java_objectheader* sobj)
 
 void copy_vftbl(vftbl **dest, vftbl *src)
 {
+    *dest = src;
+#if 0
+    /* XXX this kind of copying does not work (in the general
+     * case). The interface tables would have to be copied, too. I
+     * don't see why we should make a copy anyway. -Edwin
+     */
        *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
        memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
        memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
+#endif
 }
 
+
+/******************************************************************************************                                                                                                            
+
+       creates method signature (excluding return type) from array of 
+       class-objects representing the parameters of the method 
+
+*******************************************************************************************/
+
+
+utf *create_methodsig(java_objectarray* types, char *retType)
+{
+    char *buffer;       /* buffer for building the desciptor */
+    char *pos;          /* current position in buffer */
+    utf *result;        /* the method signature */
+    u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
+    u4 i, j;
+    if (!types) return NULL;
+
+    /* determine required buffer-size */    
+    for (i=0;i<types->header.size;i++) {
+      classinfo *c = (classinfo *) types->data[i];
+      buffer_size  = buffer_size + c->name->blength+2;
+    }
+
+    if (retType) buffer_size+=strlen(retType);
+
+    /* allocate buffer */
+    buffer = MNEW(u1, buffer_size);
+    pos    = buffer;
+    
+    /* method-desciptor starts with parenthesis */
+    *pos++ = '(';
+
+    for (i=0;i<types->header.size;i++) {
+
+            char ch;      
+            /* current argument */
+           classinfo *c = (classinfo *) types->data[i];
+           /* current position in utf-text */
+           char *utf_ptr = c->name->text; 
+           
+           /* determine type of argument */
+           if ( (ch = utf_nextu2(&utf_ptr)) == '[' ) {
+       
+               /* arrayclass */
+               for ( utf_ptr--; utf_ptr<utf_end(c->name); utf_ptr++)
+                  *pos++ = *utf_ptr; /* copy text */
+
+           } else
+           {           
+               /* check for primitive types */
+               for (j=0; j<PRIMITIVETYPE_COUNT; j++) {
+
+                       char *utf_pos   = utf_ptr-1;
+                       char *primitive = primitivetype_table[j].wrapname;
+
+                       /* compare text */
+                       while (utf_pos<utf_end(c->name))
+                               if (*utf_pos++ != *primitive++) goto nomatch;
+
+                       /* primitive type found */
+                       *pos++ = primitivetype_table[j].typesig;
+                       goto next_type;
+
+               nomatch:
+               }
+
+               /* no primitive type and no arrayclass, so must be object */
+               *pos++ = 'L';
+               /* copy text */
+               for ( utf_ptr--; utf_ptr<utf_end(c->name); utf_ptr++)
+                       *pos++ = *utf_ptr;
+               *pos++ = ';';
+
+               next_type:
+           }  
+    }      
+
+    *pos++ = ')';
+
+    if (retType) {
+       for (i=0;i<strlen(retType);i++) {
+               *pos++=retType[i];
+       }
+    }
+    /* create utf-string */
+    result = utf_new(buffer,(pos-buffer));
+    MFREE(buffer, u1, buffer_size);
+
+    return result;
+}
+
+
+/******************************************************************************************
+
+       retrieve the next argument or returntype from a descriptor
+       and return the corresponding class 
+
+*******************************************************************************************/
+
+classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
+{
+    classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
+                                         (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
+    if (!c)
+       /* unknown type */
+       panic("illegal descriptor");
+
+    if (skip) return NULL;
+
+    use_class_as_object(c);
+    return c;
+}
+
+
+/******************************************************************************************
+
+       use the descriptor of a method to generate a java/lang/Class array
+       which contains the classes of the parametertypes of the method
+
+*******************************************************************************************/
+
+java_objectarray* get_parametertypes(methodinfo *m) 
+{
+    utf  *descr    =  m->descriptor;    /* method-descriptor */ 
+    char *utf_ptr  =  descr->text;      /* current position in utf-text */
+    char *desc_end =  utf_end(descr);   /* points behind utf string     */
+    java_objectarray* result;
+    int parametercount = 0;
+    int i;
+
+    /* skip '(' */
+    utf_nextu2(&utf_ptr);
+  
+    /* determine number of parameters */
+    while ( *utf_ptr != ')' ) {
+       get_type(&utf_ptr,desc_end,true);
+       parametercount++;
+    }
+
+    /* create class-array */
+    result = builtin_anewarray(parametercount, class_java_lang_Class);
+
+    utf_ptr  =  descr->text;
+    utf_nextu2(&utf_ptr);
+
+    /* get returntype classes */
+    for (i = 0; i < parametercount; i++)
+           result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
+
+    return result;
+}
+
+
+/******************************************************************************************
+
+       get the returntype class of a method
+
+*******************************************************************************************/
+
+classinfo *get_returntype(methodinfo *m) 
+{
+       char *utf_ptr;   /* current position in utf-text */
+       char *desc_end;  /* points behind utf string     */
+        utf *desc = m->descriptor; /* method-descriptor  */
+
+       utf_ptr  = desc->text;
+       desc_end = utf_end(desc);
+
+       /* ignore parametertypes */
+        while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
+               /* skip */ ;
+
+       return get_type(&utf_ptr,desc_end, false);
+}
+
+
 /*****************************************************************************/
 /*****************************************************************************/
 
@@ -960,22 +1337,22 @@ void printNativeCall(nativeCall nc) {
 
   printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
   for (i=0; i<nc.methCnt; i++) {  
-    printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
+      printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
 
     for (j=0; j<nc.callCnt[i]; j++) {  
-      printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
+        printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
        nc.methods[i].methodCalls[j].classname, 
        nc.methods[i].methodCalls[j].methodname, 
        nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
       }
     }
-printf("-+++++--------------------\n");fflush(stdout);
+  printf("-+++++--------------------\n");fflush(stdout);
 }
 
 /*--------------------------------------------------------*/
 void printCompNativeCall(nativeCompCall nc) {
   int i,j;
-printf("printCompNativeCall BEGIN\n");fflush(stdout);
+  printf("printCompNativeCall BEGIN\n");fflush(stdout); 
   printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
   utf_display(nc.classname); fflush(stdout);
   
@@ -1070,3 +1447,17 @@ for (i=0;i<NATIVECALLSSIZE; i++) {
 return true;
 }
 /*--------------------------------------------------------*/
+
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */