d7c5175bcb573a03b4c720082fa39c28f797af3d
[cacao.git] / src / native / vm / gnu / gnu_classpath_VMStackWalker.c
1 /* src/native/vm/gnu/gnu_classpath_VMStackWalker.c
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: gnu_classpath_VMStackWalker.c 8318 2007-08-16 10:05:34Z michi $
26
27 */
28
29
30 #include "config.h"
31
32 #include "native/jni.h"
33 #include "native/llni.h"
34 #include "native/native.h"
35
36 #include "native/include/java_lang_Class.h"
37 #include "native/include/java_lang_ClassLoader.h"
38
39 #include "native/include/gnu_classpath_VMStackWalker.h"
40
41 #include "vm/builtin.h"
42 #include "vm/global.h"
43
44 #include "vm/jit/stacktrace.h"
45
46 #include "vmcore/class.h"
47
48
49 /* native methods implemented by this file ************************************/
50
51 static JNINativeMethod methods[] = {
52         { "getClassContext",         "()[Ljava/lang/Class;",      (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getClassContext         },
53         { "getCallingClass",         "()Ljava/lang/Class;",       (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getCallingClass         },
54         { "getCallingClassLoader",   "()Ljava/lang/ClassLoader;", (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getCallingClassLoader   },
55         { "firstNonNullClassLoader", "()Ljava/lang/ClassLoader;", (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader },
56 };
57
58
59 /* _Jv_gnu_classpath_VMStackWalker_init ****************************************
60
61    Register native functions.
62
63 *******************************************************************************/
64
65 void _Jv_gnu_classpath_VMStackWalker_init(void)
66 {
67         utf *u;
68
69         u = utf_new_char("gnu/classpath/VMStackWalker");
70
71         native_method_register(u, methods, NATIVE_METHODS_COUNT);
72 }
73
74
75 /*
76  * Class:     gnu/classpath/VMStackWalker
77  * Method:    getClassContext
78  * Signature: ()[Ljava/lang/Class;
79  */
80 JNIEXPORT java_handle_objectarray_t* JNICALL Java_gnu_classpath_VMStackWalker_getClassContext(JNIEnv *env, jclass clazz)
81 {
82         java_handle_objectarray_t *oa;
83
84         oa = stacktrace_getClassContext();
85
86         return oa;
87 }
88
89
90 /*
91  * Class:     gnu/classpath/VMStackWalker
92  * Method:    getCallingClass
93  * Signature: ()Ljava/lang/Class;
94  */
95 JNIEXPORT java_lang_Class* JNICALL Java_gnu_classpath_VMStackWalker_getCallingClass(JNIEnv *env, jclass clazz)
96 {
97         java_handle_objectarray_t *oa;
98         java_handle_t             *o;
99
100         oa = stacktrace_getClassContext();
101
102         if (oa == NULL)
103                 return NULL;
104
105         if (LLNI_array_size(oa) < 2)
106                 return NULL;
107
108         LLNI_objectarray_element_get(oa, 1, o);
109
110         return (java_lang_Class *) o;
111 }
112
113
114 /*
115  * Class:     gnu/classpath/VMStackWalker
116  * Method:    getCallingClassLoader
117  * Signature: ()Ljava/lang/ClassLoader;
118  */
119 JNIEXPORT java_lang_ClassLoader* JNICALL Java_gnu_classpath_VMStackWalker_getCallingClassLoader(JNIEnv *env, jclass clazz)
120 {
121         java_handle_objectarray_t *oa;
122         classinfo                 *c;
123         classloader               *cl;
124
125         oa = stacktrace_getClassContext();
126
127         if (oa == NULL)
128                 return NULL;
129
130         if (LLNI_array_size(oa) < 2)
131                 return NULL;
132          
133         c  = (classinfo *) LLNI_array_direct(oa, 1);
134         cl = c->classloader;
135
136         return (java_lang_ClassLoader *) cl;
137 }
138
139
140 /*
141  * Class:     gnu/classpath/VMStackWalker
142  * Method:    firstNonNullClassLoader
143  * Signature: ()Ljava/lang/ClassLoader;
144  */
145 JNIEXPORT java_lang_ClassLoader* JNICALL Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader(JNIEnv *env, jclass clazz)
146 {
147         java_handle_objectarray_t *oa;
148         classinfo                 *c;
149         classloader               *cl;
150         s4                         i;
151
152         oa = stacktrace_getClassContext();
153
154         if (oa == NULL)
155                 return NULL;
156
157         for (i = 0; i < LLNI_array_size(oa); i++) {
158                 c  = (classinfo *) LLNI_array_direct(oa, i);
159                 cl = c->classloader;
160
161                 if (cl != NULL)
162                         return (java_lang_ClassLoader *) cl;
163         }
164
165         return NULL;
166 }
167
168
169 /*
170  * These are local overrides for various environment variables in Emacs.
171  * Please do not remove this and leave it at the end of the file, where
172  * Emacs will automagically detect them.
173  * ---------------------------------------------------------------------
174  * Local variables:
175  * mode: c
176  * indent-tabs-mode: t
177  * c-basic-offset: 4
178  * tab-width: 4
179  * End:
180  * vim:noexpandtab:sw=4:ts=4:
181  */