* Merged with default branch at rev 16f3633aaa5a.
[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 */
26
27
28 #include "config.h"
29
30 #include "native/jni.h"
31 #include "native/llni.h"
32 #include "native/native.h"
33
34 #include "native/include/java_lang_Class.h"
35 #include "native/include/java_lang_ClassLoader.h"
36
37 #include "native/include/gnu_classpath_VMStackWalker.h"
38
39 #include "vm/builtin.h"
40 #include "vm/global.h"
41
42 #include "vm/jit/stacktrace.h"
43
44 #include "vmcore/class.h"
45
46
47 /* native methods implemented by this file ************************************/
48
49 static JNINativeMethod methods[] = {
50         { "getClassContext",         "()[Ljava/lang/Class;",      (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getClassContext         },
51         { "getCallingClass",         "()Ljava/lang/Class;",       (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getCallingClass         },
52         { "getCallingClassLoader",   "()Ljava/lang/ClassLoader;", (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_getCallingClassLoader   },
53         { "firstNonNullClassLoader", "()Ljava/lang/ClassLoader;", (void *) (ptrint) &Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader },
54 };
55
56
57 /* _Jv_gnu_classpath_VMStackWalker_init ****************************************
58
59    Register native functions.
60
61 *******************************************************************************/
62
63 void _Jv_gnu_classpath_VMStackWalker_init(void)
64 {
65         utf *u;
66
67         u = utf_new_char("gnu/classpath/VMStackWalker");
68
69         native_method_register(u, methods, NATIVE_METHODS_COUNT);
70 }
71
72
73 /*
74  * Class:     gnu/classpath/VMStackWalker
75  * Method:    getClassContext
76  * Signature: ()[Ljava/lang/Class;
77  */
78 JNIEXPORT java_handle_objectarray_t* JNICALL Java_gnu_classpath_VMStackWalker_getClassContext(JNIEnv *env, jclass clazz)
79 {
80         java_handle_objectarray_t *oa;
81
82         oa = stacktrace_getClassContext();
83
84         return oa;
85 }
86
87
88 /*
89  * Class:     gnu/classpath/VMStackWalker
90  * Method:    getCallingClass
91  * Signature: ()Ljava/lang/Class;
92  */
93 JNIEXPORT java_lang_Class* JNICALL Java_gnu_classpath_VMStackWalker_getCallingClass(JNIEnv *env, jclass clazz)
94 {
95         java_handle_objectarray_t *oa;
96         java_handle_t             *o;
97
98         oa = stacktrace_getClassContext();
99
100         if (oa == NULL)
101                 return NULL;
102
103         if (LLNI_array_size(oa) < 2)
104                 return NULL;
105
106         LLNI_objectarray_element_get(oa, 1, o);
107
108         return (java_lang_Class *) o;
109 }
110
111
112 /*
113  * Class:     gnu/classpath/VMStackWalker
114  * Method:    getCallingClassLoader
115  * Signature: ()Ljava/lang/ClassLoader;
116  */
117 JNIEXPORT java_lang_ClassLoader* JNICALL Java_gnu_classpath_VMStackWalker_getCallingClassLoader(JNIEnv *env, jclass clazz)
118 {
119         java_handle_objectarray_t *oa;
120         classinfo                 *c;
121         classloader               *cl;
122
123         oa = stacktrace_getClassContext();
124
125         if (oa == NULL)
126                 return NULL;
127
128         if (LLNI_array_size(oa) < 2)
129                 return NULL;
130          
131         c  = (classinfo *) LLNI_array_direct(oa, 1);
132         cl = c->classloader;
133
134         if (cl == NULL)
135                 return NULL;
136
137         return (java_lang_ClassLoader *) cl->object;
138 }
139
140
141 /*
142  * Class:     gnu/classpath/VMStackWalker
143  * Method:    firstNonNullClassLoader
144  * Signature: ()Ljava/lang/ClassLoader;
145  */
146 JNIEXPORT java_lang_ClassLoader* JNICALL Java_gnu_classpath_VMStackWalker_firstNonNullClassLoader(JNIEnv *env, jclass clazz)
147 {
148         java_handle_objectarray_t *oa;
149         classinfo                 *c;
150         classloader               *cl;
151         s4                         i;
152
153         oa = stacktrace_getClassContext();
154
155         if (oa == NULL)
156                 return NULL;
157
158         for (i = 0; i < LLNI_array_size(oa); i++) {
159                 c  = (classinfo *) LLNI_array_direct(oa, i);
160                 cl = c->classloader;
161
162                 if (cl != NULL)
163                         return (java_lang_ClassLoader *) cl->object;
164         }
165
166         return NULL;
167 }
168
169
170 /*
171  * These are local overrides for various environment variables in Emacs.
172  * Please do not remove this and leave it at the end of the file, where
173  * Emacs will automagically detect them.
174  * ---------------------------------------------------------------------
175  * Local variables:
176  * mode: c
177  * indent-tabs-mode: t
178  * c-basic-offset: 4
179  * tab-width: 4
180  * End:
181  * vim:noexpandtab:sw=4:ts=4:
182  */