diff options
author | Tom Tromey <tromey@redhat.com> | 2003-01-31 17:54:14 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2003-01-31 17:54:14 +0000 |
commit | 5aac1dac6279f13c13e4068c4c734c48f0084205 (patch) | |
tree | 3d061835fbf308ad2ee29fe5eed56c06842c6b38 /libjava/jni/classpath | |
parent | 371ac725580efe2c70111c2403a98b0fef3b11d6 (diff) | |
download | gcc-5aac1dac6279f13c13e4068c4c734c48f0084205.zip gcc-5aac1dac6279f13c13e4068c4c734c48f0084205.tar.gz gcc-5aac1dac6279f13c13e4068c4c734c48f0084205.tar.bz2 |
Window.java (Window(Window,GraphicsConfiguration)): Don't try to find graphics configuration.
* java/awt/Window.java (Window(Window,GraphicsConfiguration)):
Don't try to find graphics configuration.
* java/awt/Toolkit.java (default_toolkit_name): Use new
Configuration entry.
* gnu/classpath/Configuration.java.in (default_awt_peer_toolkit):
New global.
* configure: Rebuilt.
* configure.in (TOOLKIT): New subst.
(--enable-java-awt) [xlib, gtk]: Set TOOLKIT if required.
Do AWT tests much earlier. Run Gtk tests. Make jniinclude
directory. Make output directories for .c files.
* Makefile.in: Rebuilt.
* Makefile.am (lib_gnu_java_awt_peer_gtk_la_SOURCES): New macro.
(toolexeclib_LTLIBRARIES): Added cond_gtk_ltlibrary.
(all_java_source_files): Added new sources.
($(lib_gnu_java_awt_peer_gtk_la_OBJECTS)): New target.
(gtk_c_files): New macro.
(gtk_c_source_files): New macro.
(cond_gtk_ltlibrary): New macro.
($(gtk_c_files)): New target.
(lib_gnu_java_awt_peer_gtk_la_LIBADD): New macro.
(gtk_awt_peer_sources): New macro.
(gtk_c_headers): New macro.
($(gtk_c_headers)): New target.
(ACLOCAL_AMFLAGS): New macro.
* gtk.m4, glib.m4, libart.m4: New files.
* gnu/java/awt/peer/gtk/GdkFontMetrics.java,
gnu/java/awt/peer/gtk/GdkGraphics.java,
gnu/java/awt/peer/gtk/GtkArg.java,
gnu/java/awt/peer/gtk/GtkArgList.java,
gnu/java/awt/peer/gtk/GtkButtonPeer.java,
gnu/java/awt/peer/gtk/GtkCanvasPeer.java,
gnu/java/awt/peer/gtk/GtkCheckboxGroupPeer.java,
gnu/java/awt/peer/gtk/GtkCheckboxMenuItemPeer.java,
gnu/java/awt/peer/gtk/GtkCheckboxPeer.java,
gnu/java/awt/peer/gtk/GtkChoicePeer.java,
gnu/java/awt/peer/gtk/GtkClipboard.java,
gnu/java/awt/peer/gtk/GtkComponentPeer.java,
gnu/java/awt/peer/gtk/GtkContainerPeer.java,
gnu/java/awt/peer/gtk/GtkDialogPeer.java,
gnu/java/awt/peer/gtk/GtkFileDialogPeer.java,
gnu/java/awt/peer/gtk/GtkFontPeer.java,
gnu/java/awt/peer/gtk/GtkFramePeer.java,
gnu/java/awt/peer/gtk/GtkGenericPeer.java,
gnu/java/awt/peer/gtk/GtkImage.java,
gnu/java/awt/peer/gtk/GtkImagePainter.java,
gnu/java/awt/peer/gtk/GtkLabelPeer.java,
gnu/java/awt/peer/gtk/GtkListPeer.java,
gnu/java/awt/peer/gtk/GtkMainThread.java,
gnu/java/awt/peer/gtk/GtkMenuBarPeer.java,
gnu/java/awt/peer/gtk/GtkMenuComponentPeer.java,
gnu/java/awt/peer/gtk/GtkMenuItemPeer.java,
gnu/java/awt/peer/gtk/GtkMenuPeer.java,
gnu/java/awt/peer/gtk/GtkOffScreenImage.java,
gnu/java/awt/peer/gtk/GtkPanelPeer.java,
gnu/java/awt/peer/gtk/GtkPopupMenuPeer.java,
gnu/java/awt/peer/gtk/GtkScrollbarPeer.java,
gnu/java/awt/peer/gtk/GtkScrollPanePeer.java,
gnu/java/awt/peer/gtk/GtkTextAreaPeer.java,
gnu/java/awt/peer/gtk/GtkTextComponentPeer.java,
gnu/java/awt/peer/gtk/GtkTextFieldPeer.java,
gnu/java/awt/peer/gtk/GtkToolkit.java,
gnu/java/awt/peer/gtk/GtkWindowPeer.java,
gnu/java/awt/peer/gtk/TestAWT.java,
gnu/java/awt/peer/gtk/Test.java: New files from Classpath.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GdkGraphics.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkButtonPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkCanvasPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkCheckboxMenuItemPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkCheckboxPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkChoicePeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkClipboard.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkComponentPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkEvents.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkFileDialogPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkImagePainter.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkLabelPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkListPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMainThread.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMenuBarPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMenuItemPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkMenuPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkPanelPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkPopupMenuPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkScrollBarPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkScrollPanePeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextAreaPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextComponentPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkTextFieldPeer.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkToolkit.c,
jni/gtk-peer/gnu_java_awt_peer_gtk_GtkWindowPeer.c,
jni/gtk-peer/gthread-jni.c, jni/gtk-peer/gthread-jni.h,
jni/gtk-peer/gtkpeer.h, jni/classpath/jcl.c, jni/classpath/jcl.h,
jni/classpath/jnilink.c, jni/classpath/jnilink.h,
jni/classpath/native_state.c, jni/classpath/native_state.h,
jni/classpath/primlib.c, jni/classpath/primlib.h: Likewise.
From-SVN: r62187
Diffstat (limited to 'libjava/jni/classpath')
-rw-r--r-- | libjava/jni/classpath/jcl.c | 134 | ||||
-rw-r--r-- | libjava/jni/classpath/jcl.h | 64 | ||||
-rw-r--r-- | libjava/jni/classpath/jnilink.c | 117 | ||||
-rw-r--r-- | libjava/jni/classpath/jnilink.h | 86 | ||||
-rw-r--r-- | libjava/jni/classpath/native_state.c | 247 | ||||
-rw-r--r-- | libjava/jni/classpath/native_state.h | 71 | ||||
-rw-r--r-- | libjava/jni/classpath/primlib.c | 463 | ||||
-rw-r--r-- | libjava/jni/classpath/primlib.h | 102 |
8 files changed, 1284 insertions, 0 deletions
diff --git a/libjava/jni/classpath/jcl.c b/libjava/jni/classpath/jcl.c new file mode 100644 index 0000000..66c756e0 --- /dev/null +++ b/libjava/jni/classpath/jcl.c @@ -0,0 +1,134 @@ +/* jcl.c + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#include <stdio.h> +#include <jcl.h> +#include <malloc.h> + +static char errstr[4098]; // this way the memory is pre-allocated, so that we do not have to worry if we are out of memory. + +JNIEXPORT void JNICALL JCL_ThrowException(JNIEnv * env, char * className, char * errMsg) { + jclass excClass; + if((*env)->ExceptionOccurred(env)) { + (*env)->ExceptionClear(env); + } + excClass = (*env)->FindClass(env, className); + if(excClass == NULL) { + jclass errExcClass; + errExcClass = (*env)->FindClass(env, "java/lang/ClassNotFoundException"); + if(errExcClass == NULL) { + errExcClass = (*env)->FindClass(env, "java/lang/InternalError"); + if(errExcClass == NULL) { + sprintf(errstr,"JCL: Utterly failed to throw exeption %s with message %s.",className,errMsg); + fprintf(stderr, errstr); + return; + } + } + sprintf(errstr,"JCL: Failed to throw exception %s with message %s: could not find exception class.", className, errMsg); + (*env)->ThrowNew(env, errExcClass, errstr); + } + (*env)->ThrowNew(env, excClass, errMsg); +} + +JNIEXPORT void * JNICALL JCL_malloc(JNIEnv * env, size_t size) { + void * mem = malloc(size); + if(mem == NULL) { + JCL_ThrowException(env, "java/lang/OutOfMemoryError", "malloc() failed."); + return NULL; + } + return mem; +} + +JNIEXPORT void * JNICALL JCL_realloc(JNIEnv *env, void *ptr, size_t size) +{ + ptr = realloc(ptr, size); + if (ptr == 0) + { + JCL_ThrowException(env, "java/lang/OutOfMemoryError", + "malloc() failed."); + return NULL; + } + return(ptr); +} + +JNIEXPORT void JNICALL JCL_free(JNIEnv * env, void * p) { + if(p != NULL) { + free(p); + } +} + +JNIEXPORT char * JNICALL JCL_jstring_to_cstring(JNIEnv * env, jstring s) { + char* cstr; + if(s == NULL) { + JCL_ThrowException(env, "java/lang/NullPointerException","Null string"); + return NULL; + } + cstr = (char*)(*env)->GetStringUTFChars(env, s, NULL); + if(cstr == NULL) { + JCL_ThrowException(env, "java/lang/InternalError", "GetStringUTFChars() failed."); + return NULL; + } + return cstr; +} + +JNIEXPORT void JNICALL JCL_free_cstring(JNIEnv * env, jstring s, char * cstr) { + (*env)->ReleaseStringUTFChars(env, s, cstr); +} + +JNIEXPORT jint JNICALL JCL_MonitorEnter(JNIEnv * env, jobject o) { + jint retval = (*env)->MonitorEnter(env,o); + if(retval != 0) { + JCL_ThrowException(env, "java/lang/InternalError", "MonitorEnter() failed."); + } + return retval; +} + +JNIEXPORT jint JNICALL JCL_MonitorExit(JNIEnv * env, jobject o) { + jint retval = (*env)->MonitorExit(env,o); + if(retval != 0) { + JCL_ThrowException(env, "java/lang/InternalError", "MonitorExit() failed."); + } + return retval; +} + +JNIEXPORT jclass JNICALL JCL_FindClass(JNIEnv * env, char * className) { + jclass retval = (*env)->FindClass(env,className); + if(retval == NULL) { + JCL_ThrowException(env, "java/lang/ClassNotFoundException", className); + } + return retval; +} diff --git a/libjava/jni/classpath/jcl.h b/libjava/jni/classpath/jcl.h new file mode 100644 index 0000000..a7d00b4 --- /dev/null +++ b/libjava/jni/classpath/jcl.h @@ -0,0 +1,64 @@ +/* jcl.h + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#ifndef __JCL_H__ +#define __JCL_H__ + +#include <stddef.h> +#include <jni.h> +#include <config.h> + +JNIEXPORT jclass JNICALL JCL_FindClass(JNIEnv * env, char * className); +JNIEXPORT void JNICALL JCL_ThrowException(JNIEnv * env, char * className, char * errMsg); +JNIEXPORT void * JNICALL JCL_malloc(JNIEnv *env, size_t size); +JNIEXPORT void * JNICALL JCL_realloc(JNIEnv *env, void *ptr, size_t size); +JNIEXPORT void JNICALL JCL_free(JNIEnv *env, void * p); +JNIEXPORT char * JNICALL JCL_jstring_to_cstring(JNIEnv *env, jstring s); +JNIEXPORT void JNICALL JCL_free_cstring(JNIEnv *env, jstring s, char * cstr); +JNIEXPORT jint JNICALL JCL_MonitorEnter(JNIEnv *env, jobject o); +JNIEXPORT jint JNICALL JCL_MonitorExit(JNIEnv *env, jobject o); + +#define JCL_RETHROW_EXCEPTION(env) if((*(env))->ExceptionOccurred((env)) != NULL) return NULL; + +/* Simple debug macro */ +#ifdef DEBUG +#define DBG(x) fprintf(stderr, (x)); +#else +#define DBG(x) +#endif + +#endif diff --git a/libjava/jni/classpath/jnilink.c b/libjava/jni/classpath/jnilink.c new file mode 100644 index 0000000..0c244f2 --- /dev/null +++ b/libjava/jni/classpath/jnilink.c @@ -0,0 +1,117 @@ +/* JNILINK 1.1: JNI version. + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#include "jnilink.h" +#include <string.h> +#include <jcl.h> + +#include <malloc.h> + +#define GETCLASS(c) *(jclass*)(c) + +JNIEXPORT jclass JNICALL +LINK_RelinkClass (JNIEnv * env, linkedClass * c, char * name) { + jclass found; + LINK_UnlinkClass(env,*c); + + found = (*env)->FindClass(env,name); + if(found == NULL) + return NULL; + + *c = JCL_malloc(env,sizeof(jclass)); + if(*c == NULL) + return NULL; + + GETCLASS(*c) = (*env)->NewGlobalRef(env,found); + return GETCLASS(*c); +} + +JNIEXPORT jclass JNICALL +LINK_RelinkKnownClass(JNIEnv * env, linkedClass * c, jclass newClass) { + LINK_UnlinkClass(env,*c); + + *c = JCL_malloc(env,sizeof(jclass)); + if(*c == NULL) + return NULL; + + GETCLASS(*c) = (*env)->NewGlobalRef(env,newClass); + return newClass; +} + +JNIEXPORT jmethodID JNICALL +LINK_RelinkMethod (JNIEnv * env, jmethodID * m, linkedClass c, + char * name, char * sig) { + *m = (*env)->GetMethodID(env,GETCLASS(c),name,sig); + return *m; +} + +JNIEXPORT jmethodID JNICALL +LINK_RelinkStaticMethod(JNIEnv * env, jmethodID * m, linkedClass c, + char * name, char * sig) { + *m = (*env)->GetStaticMethodID(env,GETCLASS(c),name,sig); + return *m; +} + +JNIEXPORT jfieldID JNICALL +LINK_RelinkField (JNIEnv * env, jfieldID * f, linkedClass c, + char * name, char * sig) { + *f = (*env)->GetFieldID(env,GETCLASS(c),name,sig); + return *f; +} + +JNIEXPORT jfieldID JNICALL +LINK_RelinkStaticField (JNIEnv * env, jfieldID * f, linkedClass c, + char * name, char * sig) { + *f = (*env)->GetStaticFieldID(env,GETCLASS(c),name,sig); + return *f; +} + + +/* These are for when the class referencing the symbols is unloaded; it +destroys any object references + * the linker might have kept around. + */ +JNIEXPORT void JNICALL LINK_UnlinkClass (JNIEnv * env, linkedClass * c) { + if(*c != NULL) { + if(GETCLASS(*c) != NULL) + (*env)->DeleteGlobalRef(env,GETCLASS(*c)); + JCL_free(env,*c); + *c = NULL; + } +} + diff --git a/libjava/jni/classpath/jnilink.h b/libjava/jni/classpath/jnilink.h new file mode 100644 index 0000000..448e2b5 --- /dev/null +++ b/libjava/jni/classpath/jnilink.h @@ -0,0 +1,86 @@ +/* JNILINK 1.1: JNI version. + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +#ifndef __JNILINK_H__ +#define __JNILINK_H__ + +#include <jni.h> + +typedef void* linkedClass; + +#define LINK_LinkClass(env,c,name) ((c)==NULL ? LINK_ReallyLinkClass((env),&(c),(name)) : (c)) +#define LINK_LinkKnownClass(env,c,newClass) ((c)==NULL ? LINK_ReallyLinkKnownClass((env),&(c),(newClass)) : (c)) +#define LINK_LinkMethod(env,m,c,name,sig) ((m)==NULL ? LINK_RelinkMethod((env),&(m),(c),(name),(sig)) : (m)) +#define LINK_LinkStaticMethod(env,m,c,name,sig) ((m)==NULL ? LINK_RelinkStaticMethod((env),&(m),(c),(name),(sig)) : (m)) +#define LINK_LinkField(env,f,c,name,sig) ((m)==NULL ? LINK_RelinkField((env),&(f),(c),(name),(sig)) : (f)) +#define LINK_LinkStaticField(env,f,c,name,sig) ((m)==NULL ? LINK_RelinkStaticField((env),&(f),(c),(name),(sig)) : (f)) + +#define LINK_LinkConstructor(env,m,c,sig) ((m)==NULL ? LINK_RelinkMethod((env),&(m),(c),"<init>",(sig)) : (m)) + +JNIEXPORT jclass JNICALL +LINK_ReallyLinkClass (JNIEnv * env, linkedClass * c, + char * name); +JNIEXPORT jclass JNICALL +LINK_ReallyLinkKnownClass(JNIEnv * env, linkedClass * c, + jclass newClass); +JNIEXPORT jclass JNICALL +LINK_RelinkClass (JNIEnv * env, linkedClass * c, + char * name); +JNIEXPORT jclass JNICALL +LINK_RelinkKnownClass (JNIEnv * env, linkedClass * c, + jclass newClass); +JNIEXPORT jmethodID JNICALL +LINK_RelinkMethod (JNIEnv * env, jmethodID * m, linkedClass c, + char * name, char * sig); +JNIEXPORT jmethodID JNICALL +LINK_RelinkStaticMethod(JNIEnv * env, jmethodID * m, linkedClass c, + char * name, char * sig); +JNIEXPORT jfieldID JNICALL +LINK_RelinkField (JNIEnv * env, jfieldID * f, linkedClass c, + char * name, char * sig); +JNIEXPORT jfieldID JNICALL +LINK_RelinkStaticField (JNIEnv * env, jfieldID * f, linkedClass c, + char * name, char * sig); + +/* These are for when the class referencing the symbols is unloaded; it +destroys any object references + * the linker might have kept around. + */ +JNIEXPORT void JNICALL LINK_UnlinkClass (JNIEnv * env, linkedClass * c); + +#endif diff --git a/libjava/jni/classpath/native_state.c b/libjava/jni/classpath/native_state.c new file mode 100644 index 0000000..746686e --- /dev/null +++ b/libjava/jni/classpath/native_state.c @@ -0,0 +1,247 @@ +/* Magical NSA API -- Associate a C ptr with an instance of an object + Copyright (C) 1998, 2002 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#include <stdlib.h> +#include <jni.h> +#include "native_state.h" + +#define DEFAULT_TABLE_SIZE 97 + +struct state_table * +init_state_table_with_size (JNIEnv *env, jclass clazz, jint size) +{ + struct state_table *table; + jfieldID hash; + jclass clazz_g; + + hash = (*env)->GetFieldID (env, clazz, "native_state", "I"); + if (hash == NULL) + return NULL; + + clazz_g = (*env)->NewGlobalRef (env, clazz); + if (clazz_g == NULL) + return NULL; + + table = (struct state_table *) malloc (sizeof (struct state_table)); + table->size = size; + table->head = (struct state_node **) calloc (sizeof (struct state_node *), + table->size); + table->hash = hash; + table->clazz = clazz_g; + + return table; +} + +struct state_table * +init_state_table (JNIEnv *env, jclass clazz) +{ + return init_state_table_with_size (env, clazz, DEFAULT_TABLE_SIZE); +} + +static void * +remove_node (struct state_node **head, jint obj_id) +{ + struct state_node *back_ptr = NULL; + struct state_node *node = *head; + + while (node != NULL) + { + if (node->key == obj_id) + { + void *return_value; + if (back_ptr == NULL) + *head = node->next; + else + back_ptr->next = node->next; + return_value = node->c_state; + free (node); + return return_value; + } + back_ptr = node; + node = node->next; + } + + return NULL; +} + +static void * +get_node (struct state_node **head, jint obj_id) +{ + struct state_node *back_ptr = NULL; + struct state_node *node = *head; + + while (node != NULL) + { + if (node->key == obj_id) + { + /* Move the node we found to the front of the list. */ + if (back_ptr != NULL) + { + back_ptr->next = node->next; + node->next = *head; + *head = node; + } + + /* Return the match. */ + return node->c_state; + } + + back_ptr = node; + node = node->next; + } + + return NULL; +} + +static void +add_node (struct state_node **head, jint obj_id, void *state) +{ + struct state_node *node = *head; + struct state_node *back_ptr = NULL; + + struct state_node *new_node; + + if (node != NULL) + { + while (node->next != NULL && obj_id != node->key) + { + back_ptr = node; + node = node->next; + } + + if (node->key == obj_id) + { + /* If we're updating a node, move it to the front of the + list. */ + if (back_ptr != NULL) + { + back_ptr->next = node->next; + node->next = *head; + } + node->c_state = state; + return; + } + } + + new_node = (struct state_node *) malloc (sizeof (struct state_node)); + new_node->key = obj_id; + new_node->c_state = state; + new_node->next = *head; + *head = new_node; +} + +void +set_state_oid (JNIEnv *env, jobject lock, struct state_table *table, + jint obj_id, void *state) +{ + jint hash; + + hash = obj_id % table->size; + + (*env)->MonitorEnter (env, lock); + add_node (&table->head[hash], obj_id, state); + (*env)->MonitorExit (env, lock); +} + +void * +get_state_oid (JNIEnv *env, jobject lock, struct state_table *table, + jint obj_id) +{ + jint hash; + void *return_value; + + hash = obj_id % table->size; + + (*env)->MonitorEnter (env, lock); + return_value = get_node (&table->head[hash], obj_id); + (*env)->MonitorExit (env, lock); + + return return_value; +} + +void * +remove_state_oid (JNIEnv *env, jobject lock, struct state_table *table, + jint obj_id) +{ + jint hash; + void *return_value; + + hash = obj_id % table->size; + + (*env)->MonitorEnter (env, lock); + return_value = remove_node (&table->head[hash], obj_id); + (*env)->MonitorExit (env, lock); + + return return_value; +} + +int +set_state (JNIEnv *env, jobject obj, struct state_table *table, void *state) +{ + jint obj_id; + obj_id = (*env)->GetIntField (env, obj, table->hash); + + if ((*env)->ExceptionOccurred (env) != NULL) + return -1; + + set_state_oid (env, table->clazz, table, obj_id, state); + return 0; +} + +void * +get_state (JNIEnv *env, jobject obj, struct state_table *table) +{ + jint obj_id; + obj_id = (*env)->GetIntField (env, obj, table->hash); + + if ((*env)->ExceptionOccurred (env) != NULL) + return NULL; + + return get_state_oid (env, table->clazz, table, obj_id); +} + +void * +remove_state_slot (JNIEnv *env, jobject obj, struct state_table *table) +{ + jint obj_id; + obj_id = (*env)->GetIntField (env, obj, table->hash); + + if ((*env)->ExceptionOccurred (env) != NULL) + return NULL; + + return remove_state_oid (env, table->clazz, table, obj_id); +} diff --git a/libjava/jni/classpath/native_state.h b/libjava/jni/classpath/native_state.h new file mode 100644 index 0000000..25ef6b5 --- /dev/null +++ b/libjava/jni/classpath/native_state.h @@ -0,0 +1,71 @@ +/* Magical NSA API -- Associate a C ptr with an instance of an object + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#ifndef JCL_NATIVE_STATE +#define JCL_NATIVE_STATE + +#include <jni.h> + +struct state_table +{ + jint size; /* number of slots, should be prime */ + jfieldID hash; /* field containing System.identityHashCode(this) */ + jclass clazz; /* lock aquired for reading/writing nodes */ + struct state_node **head; +}; + +struct state_node +{ + jint key; + void *c_state; + struct state_node *next; +}; + +struct state_table * init_state_table_with_size (JNIEnv *, jclass, jint); +struct state_table * init_state_table (JNIEnv *, jclass); + +/* lowlevel api */ +void set_state_oid (JNIEnv *, jobject, struct state_table *, jint, void *); +void * get_state_oid (JNIEnv *, jobject, struct state_table *, jint); +void * remove_state_oid (JNIEnv *, jobject, struct state_table *, jint); + +/* highlevel api */ +int set_state (JNIEnv *, jobject, struct state_table *, void *); +void * get_state (JNIEnv *, jobject, struct state_table *); +void * remove_state_slot (JNIEnv *, jobject, struct state_table *); + +#endif diff --git a/libjava/jni/classpath/primlib.c b/libjava/jni/classpath/primlib.c new file mode 100644 index 0000000..c7396e2 --- /dev/null +++ b/libjava/jni/classpath/primlib.c @@ -0,0 +1,463 @@ +/* primlib.c + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#include <jnilink.h> +#include <primlib.h> +#include <jcl.h> + +static jclass nativeWrapClass[PRIMLIB_NUMTYPES] = {NULL,NULL,NULL, NULL,NULL,NULL, + NULL,NULL,NULL, NULL,NULL,NULL}; + +static jclass nativeTypeClass[PRIMLIB_NUMTYPES] = {NULL,NULL,NULL, NULL,NULL,NULL, + NULL,NULL,NULL, NULL,NULL,NULL}; + +static jmethodID nativeWrapClassConstructor[PRIMLIB_NUMTYPES] = {NULL,NULL,NULL, NULL,NULL,NULL, + NULL,NULL,NULL, NULL,NULL,NULL}; + +static jmethodID nativeWrapClassAccessor[PRIMLIB_NUMTYPES] = {NULL,NULL,NULL, NULL,NULL,NULL, + NULL,NULL,NULL, NULL,NULL,NULL}; + +static char * nativeWrapClassName[PRIMLIB_NUMTYPES] = { + NULL, + NULL, + "java/lang/Boolean", + "java/lang/Byte", + "java/lang/Character", + "java/lang/Short", + "java/lang/Integer", + "java/lang/Long", + "java/lang/Float", + "java/lang/Double", + "java/lang/Void", + NULL + }; + +static char * nativeWrapClassConstructorSig[PRIMLIB_NUMTYPES] = { + NULL, + NULL, + "(Z)V", + "(B)V", + "(C)V", + "(S)V", + "(I)V", + "(J)V", + "(F)V", + "(D)V", + "()V", + NULL + }; + +static char * nativeWrapClassAccessorName[PRIMLIB_NUMTYPES] = { + NULL, + NULL, + "booleanValue", + "byteValue", + "charValue", + "shortValue", + "intValue", + "longValue", + "floatValue", + "doubleValue", + NULL, + NULL +}; + +static char * nativeWrapClassAccessorSig[PRIMLIB_NUMTYPES] = { + NULL, + NULL, + "()Z", + "()B", + "()C", + "()S", + "()I", + "()J", + "()F", + "()D", + NULL, + NULL +}; + + +JNIEXPORT jclass JNICALL PRIMLIB_GetNativeWrapClass(JNIEnv * env, int reflectType) { + return LINK_LinkClass(env,nativeWrapClass[reflectType],nativeWrapClassName[reflectType]); +} + +static jclass ActuallyGetNativeTypeClass(JNIEnv * env, int reflectType) { + jclass wrapClass; + jfieldID typeField; + + wrapClass = PRIMLIB_GetNativeWrapClass(env, reflectType); + if(wrapClass == NULL) + return NULL; + typeField = (*env)->GetStaticFieldID(env, wrapClass, "TYPE", "Ljava/lang/Class"); + if(typeField == NULL) + return NULL; + return (*env)->GetStaticObjectField(env, wrapClass, typeField); +} + +JNIEXPORT jclass JNICALL PRIMLIB_GetNativeTypeClass(JNIEnv * env, int reflectType) { + return LINK_LinkKnownClass(env, nativeTypeClass[reflectType], ActuallyGetNativeTypeClass(env,reflectType)); +} + +JNIEXPORT jmethodID JNICALL PRIMLIB_GetNativeWrapClassConstructor(JNIEnv * env, int reflectType) { + PRIMLIB_GetNativeWrapClass(env,reflectType); + return LINK_LinkConstructor(env, nativeWrapClassConstructor[reflectType], nativeWrapClass[reflectType], nativeWrapClassConstructorSig[reflectType]); +} + +JNIEXPORT jmethodID JNICALL PRIMLIB_GetNativeWrapClassAccessor(JNIEnv * env, int reflectType) { + PRIMLIB_GetNativeWrapClass(env,reflectType); + return LINK_LinkMethod(env, nativeWrapClassAccessor[reflectType], nativeWrapClass[reflectType], nativeWrapClassAccessorName[reflectType], nativeWrapClassAccessorSig[reflectType]); +} + + + +JNIEXPORT jobject JNICALL PRIMLIB_WrapBoolean(JNIEnv * env, jboolean b) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_BOOLEAN); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BOOLEAN), construct, b); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapByte (JNIEnv * env, jbyte b) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_BYTE); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE), construct, b); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapChar (JNIEnv * env, jchar c) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_CHAR); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR), construct, c); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapShort (JNIEnv * env, jshort s) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_SHORT); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT), construct, s); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapInt (JNIEnv * env, jint i) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_INT); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT), construct, i); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapLong (JNIEnv * env, jlong l) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_LONG); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_LONG), construct, l); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapFloat (JNIEnv * env, jfloat f) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_FLOAT); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_FLOAT), construct, f); +} + +JNIEXPORT jobject JNICALL PRIMLIB_WrapDouble (JNIEnv * env, jdouble d) { + jmethodID construct = PRIMLIB_GetNativeWrapClassConstructor(env, PRIMLIB_DOUBLE); + JCL_RETHROW_EXCEPTION(env); + return (*env)->NewObject(env, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_DOUBLE), construct, d); +} + + +JNIEXPORT jboolean JNICALL PRIMLIB_UnwrapBoolean(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BOOLEAN))) { + return PRIMLIB_GetBooleanObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return JNI_FALSE; + } +} + +JNIEXPORT jbyte JNICALL PRIMLIB_UnwrapByte(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jshort JNICALL PRIMLIB_UnwrapShort(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT))) { + return PRIMLIB_GetShortObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return (jshort)PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jchar JNICALL PRIMLIB_UnwrapChar(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR))) { + return PRIMLIB_GetCharObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jint JNICALL PRIMLIB_UnwrapInt(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT))) { + return PRIMLIB_GetIntObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT))) { + return (jint)PRIMLIB_GetShortObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR))) { + return (jint)PRIMLIB_GetCharObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return (jint)PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jlong JNICALL PRIMLIB_UnwrapLong(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_LONG))) { + return PRIMLIB_GetLongObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT))) { + return (jlong)PRIMLIB_GetIntObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT))) { + return (jlong)PRIMLIB_GetShortObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR))) { + return (jlong)PRIMLIB_GetCharObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return (jlong)PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jfloat JNICALL PRIMLIB_UnwrapFloat(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_FLOAT))) { + return PRIMLIB_GetFloatObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_LONG))) { + return (jfloat)PRIMLIB_GetLongObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT))) { + return (jfloat)PRIMLIB_GetIntObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT))) { + return (jfloat)PRIMLIB_GetShortObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR))) { + return (jfloat)PRIMLIB_GetCharObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return (jfloat)PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jdouble JNICALL PRIMLIB_UnwrapDouble(JNIEnv * env, jobject obj) { + if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_DOUBLE))) { + return PRIMLIB_GetDoubleObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_FLOAT))) { + return (jdouble)PRIMLIB_GetFloatObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_LONG))) { + return (jdouble)PRIMLIB_GetLongObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT))) { + return (jdouble)PRIMLIB_GetIntObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT))) { + return (jdouble)PRIMLIB_GetShortObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR))) { + return (jdouble)PRIMLIB_GetCharObjectValue(env, obj); + } else if((*env)->IsInstanceOf(env, obj, PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE))) { + return (jdouble)PRIMLIB_GetByteObjectValue(env, obj); + } else { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct type."); + return 0; + } +} + +JNIEXPORT jint JNICALL PRIMLIB_GetReflectiveWrapperType(JNIEnv * env, jobject obj) { + jclass typeClass; + if(obj == NULL) { + return PRIMLIB_NULL; + } + + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_DOUBLE); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_DOUBLE; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_FLOAT); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_FLOAT; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_LONG); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_LONG; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_INT); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_INT; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_CHAR); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_CHAR; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_SHORT); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_SHORT; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BYTE); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_BYTE; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_BOOLEAN); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_BOOLEAN; + } + typeClass = PRIMLIB_GetNativeWrapClass(env, PRIMLIB_VOID); + if((*env)->IsInstanceOf(env, obj, typeClass)) { + return PRIMLIB_VOID; + } + return PRIMLIB_OBJECT; +} + +JNIEXPORT jint JNICALL PRIMLIB_GetReflectiveType(JNIEnv * env, jclass returnType) { + jclass typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_DOUBLE); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_DOUBLE; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_FLOAT); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_FLOAT; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_LONG); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_LONG; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_INT); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_INT; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_CHAR); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_CHAR; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_SHORT); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_SHORT; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_BYTE); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_BYTE; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_BOOLEAN); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_BOOLEAN; + } + typeClass = PRIMLIB_GetNativeTypeClass(env, PRIMLIB_VOID); + if((*env)->IsAssignableFrom(env, returnType, typeClass)) { + return PRIMLIB_VOID; + } + return PRIMLIB_OBJECT; +} + + +JNIEXPORT jboolean JNICALL PRIMLIB_GetBooleanObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_BOOLEAN); + return (*env)->CallBooleanMethod(env, obj, acc); +} + +JNIEXPORT jbyte JNICALL PRIMLIB_GetByteObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_BYTE); + return (*env)->CallByteMethod(env, obj, acc); +} + +JNIEXPORT jshort JNICALL PRIMLIB_GetShortObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_SHORT); + return (*env)->CallShortMethod(env, obj, acc); +} + +JNIEXPORT jchar JNICALL PRIMLIB_GetCharObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_CHAR); + return (*env)->CallCharMethod(env, obj, acc); +} + +JNIEXPORT jint JNICALL PRIMLIB_GetIntObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_INT); + return (*env)->CallIntMethod(env, obj, acc); +} + +JNIEXPORT jlong JNICALL PRIMLIB_GetLongObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_LONG); + return (*env)->CallLongMethod(env, obj, acc); +} + +JNIEXPORT jfloat JNICALL PRIMLIB_GetFloatObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_FLOAT); + return (*env)->CallFloatMethod(env, obj, acc); +} + +JNIEXPORT jdouble JNICALL PRIMLIB_GetDoubleObjectValue(JNIEnv * env, jobject obj) { + jmethodID acc = PRIMLIB_GetNativeWrapClassAccessor(env, PRIMLIB_DOUBLE); + return (*env)->CallDoubleMethod(env, obj, acc); +} + + + +JNIEXPORT jvalue JNICALL PRIMLIB_UnwrapJValue(JNIEnv* env, jobject obj, jclass classType) { + jvalue retval; + jint objType = PRIMLIB_GetReflectiveType(env, classType); + if(objType == PRIMLIB_BOOLEAN) { + retval.z = PRIMLIB_UnwrapBoolean(env,obj); + } else if(objType == PRIMLIB_BYTE) { + retval.b = PRIMLIB_UnwrapByte(env,obj); + } else if(objType == PRIMLIB_CHAR) { + retval.c = PRIMLIB_UnwrapChar(env,obj); + } else if(objType == PRIMLIB_SHORT) { + retval.s = PRIMLIB_UnwrapShort(env,obj); + } else if(objType == PRIMLIB_INT) { + retval.i = PRIMLIB_UnwrapInt(env,obj); + } else if(objType == PRIMLIB_LONG) { + retval.j = PRIMLIB_UnwrapLong(env,obj); + } else if(objType == PRIMLIB_FLOAT) { + retval.f = PRIMLIB_UnwrapFloat(env,obj); + } else if(objType == PRIMLIB_DOUBLE) { + retval.d = PRIMLIB_UnwrapDouble(env,obj); + } else { + if(obj != NULL && !(*env)->IsInstanceOf(env, obj, classType)) { + JCL_ThrowException(env, "java/lang/IllegalArgumentException", "Argument not of correct object type."); + return retval; + } + retval.l = obj; + } + return retval; +} + diff --git a/libjava/jni/classpath/primlib.h b/libjava/jni/classpath/primlib.h new file mode 100644 index 0000000..12f3bae --- /dev/null +++ b/libjava/jni/classpath/primlib.h @@ -0,0 +1,102 @@ +/* primlib.h + Copyright (C) 1998 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath 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. + +GNU Classpath 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 GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +#ifndef __PRIMLIB_H__ +#define __PRIMLIB_H__ + +#include <jni.h> + +#define PRIMLIB_UNKNOWN 0 +#define PRIMLIB_OBJECT 1 +#define PRIMLIB_BOOLEAN 2 +#define PRIMLIB_BYTE 3 +#define PRIMLIB_CHAR 4 +#define PRIMLIB_SHORT 5 +#define PRIMLIB_INT 6 +#define PRIMLIB_LONG 7 +#define PRIMLIB_FLOAT 8 +#define PRIMLIB_DOUBLE 9 +#define PRIMLIB_VOID 10 +#define PRIMLIB_NULL 11 +#define PRIMLIB_NUMTYPES 12 + +/* Low-level primitive class accessor functions. */ +JNIEXPORT jclass JNICALL PRIMLIB_GetNativeWrapClass(JNIEnv * env, int reflectType); +JNIEXPORT jclass JNICALL PRIMLIB_GetNativeTypeClass(JNIEnv * env, int reflectType); +JNIEXPORT jmethodID JNICALL PRIMLIB_GetNativeWrapClassConstructor(JNIEnv * env, int reflectType); +JNIEXPORT jmethodID JNICALL PRIMLIB_GetNativeWrapClassAccessor(JNIEnv * env, int reflectType); + +/* Type discovery functions: WrapperType finds out j.l.Boolean/Byte/etc., and + Type finds out j.l.Boolean.TYPE, etc. +*/ +JNIEXPORT jint JNICALL PRIMLIB_GetReflectiveWrapperType(JNIEnv * env, jobject obj); +JNIEXPORT jint JNICALL PRIMLIB_GetReflectiveType(JNIEnv * env, jclass returnType); + +/* Constructor functions. */ +JNIEXPORT jobject JNICALL PRIMLIB_WrapBoolean(JNIEnv * env, jboolean b); +JNIEXPORT jobject JNICALL PRIMLIB_WrapByte (JNIEnv * env, jbyte b); +JNIEXPORT jobject JNICALL PRIMLIB_WrapChar (JNIEnv * env, jchar c); +JNIEXPORT jobject JNICALL PRIMLIB_WrapShort (JNIEnv * env, jshort s); +JNIEXPORT jobject JNICALL PRIMLIB_WrapInt (JNIEnv * env, jint i); +JNIEXPORT jobject JNICALL PRIMLIB_WrapLong (JNIEnv * env, jlong l); +JNIEXPORT jobject JNICALL PRIMLIB_WrapFloat (JNIEnv * env, jfloat f); +JNIEXPORT jobject JNICALL PRIMLIB_WrapDouble (JNIEnv * env, jdouble d); + +/* Widening conversion unwrapping functions. */ +JNIEXPORT jboolean JNICALL PRIMLIB_UnwrapBoolean(JNIEnv * env, jobject obj); +JNIEXPORT jbyte JNICALL PRIMLIB_UnwrapByte (JNIEnv * env, jobject obj); +JNIEXPORT jshort JNICALL PRIMLIB_UnwrapShort (JNIEnv * env, jobject obj); +JNIEXPORT jchar JNICALL PRIMLIB_UnwrapChar (JNIEnv * env, jobject obj); +JNIEXPORT jint JNICALL PRIMLIB_UnwrapInt (JNIEnv * env, jobject obj); +JNIEXPORT jlong JNICALL PRIMLIB_UnwrapLong (JNIEnv * env, jobject obj); +JNIEXPORT jfloat JNICALL PRIMLIB_UnwrapFloat (JNIEnv * env, jobject obj); +JNIEXPORT jdouble JNICALL PRIMLIB_UnwrapDouble (JNIEnv * env, jobject obj); + +/* Simple unwrapping functions. Objects *must* be of correct type. */ +JNIEXPORT jboolean JNICALL PRIMLIB_GetBooleanObjectValue(JNIEnv * env, jobject obj); +JNIEXPORT jbyte JNICALL PRIMLIB_GetByteObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jshort JNICALL PRIMLIB_GetShortObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jchar JNICALL PRIMLIB_GetCharObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jint JNICALL PRIMLIB_GetIntObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jlong JNICALL PRIMLIB_GetLongObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jfloat JNICALL PRIMLIB_GetFloatObjectValue (JNIEnv * env, jobject obj); +JNIEXPORT jdouble JNICALL PRIMLIB_GetDoubleObjectValue (JNIEnv * env, jobject obj); + +/* jvalue conversion: Unwrap obj to the type of classType, with widening conversion. */ +JNIEXPORT jvalue JNICALL PRIMLIB_UnwrapJValue(JNIEnv* env, jobject obj, jclass classType); + +#endif |