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