1 /* src/vm/initialize.c - static class initializer functions
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
8 This file is part of CACAO.
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.
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.
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
34 #include "threads/lock-common.h"
36 #include "vm/global.h"
37 #include "vm/initialize.h"
38 #include "vm/builtin.h"
39 #include "vm/exceptions.h"
40 #include "vm/stringlocal.h"
43 #include "vm/jit/asmpart.h"
45 #include "vmcore/class.h"
46 #include "vmcore/loader.h"
47 #include "vmcore/options.h"
49 #if defined(ENABLE_STATISTICS)
50 # include "vmcore/statistics.h"
54 /* private functions **********************************************************/
56 static bool initialize_class_intern(classinfo *c);
59 /* initialize_class ************************************************************
61 In Java, every class can have a static initialization
62 function. This function has to be called BEFORE calling other
63 methods or accessing static variables.
65 *******************************************************************************/
67 bool initialize_class(classinfo *c)
71 if (!makeinitializations)
74 LOCK_MONITOR_ENTER(c);
76 /* maybe the class is already initalized or the current thread, which can
77 pass the monitor, is currently initalizing this class */
79 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
85 /* if <clinit> throw an Error before, the class was marked with an
86 error and we have to throw a NoClassDefFoundError */
88 if (c->state & CLASS_ERROR) {
89 exceptions_throw_noclassdeffounderror(c->name);
93 /* ...but return true, this is ok (mauve test) */
98 /* this initalizing run begins NOW */
100 c->state |= CLASS_INITIALIZING;
102 /* call the internal function */
104 r = initialize_class_intern(c);
106 /* if return value is not NULL everything was ok and the class is
110 c->state |= CLASS_INITIALIZED;
112 /* this initalizing run is done */
114 c->state &= ~CLASS_INITIALIZING;
116 LOCK_MONITOR_EXIT(c);
122 /* initialize_class_intern *****************************************************
124 This function MUST NOT be called directly, because of thread
125 <clinit> race conditions.
127 *******************************************************************************/
129 static bool initialize_class_intern(classinfo *c)
132 java_handle_t *cause;
135 /* maybe the class is not already linked */
137 if (!(c->state & CLASS_LINKED))
141 #if defined(ENABLE_STATISTICS)
146 /* Initialize super class. */
148 if (c->super != NULL) {
149 if (!(c->super->state & CLASS_INITIALIZED)) {
152 log_message_class_message_class("Initialize super class ",
158 if (!initialize_class(c->super))
163 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
165 m = class_findmethod(c, utf_clinit, utf_void__void);
170 log_message_class("Class has no static class initializer: ", c);
176 /* Sun's and IBM's JVM don't care about the static flag */
177 /* if (!(m->flags & ACC_STATIC)) { */
178 /* log_text("Class initializer is not static!"); */
182 log_message_class("Starting static class initializer for class: ", c);
185 /* now call the initializer */
187 (void) vm_call_method(m, NULL);
189 /* we have an exception or error */
191 cause = exceptions_get_exception();
194 /* class is NOT initialized and is marked with error */
196 c->state |= CLASS_ERROR;
198 /* Load java/lang/Exception for the instanceof check. */
200 class = load_class_bootstrap(utf_java_lang_Exception);
205 /* Is this an exception? Yes, than wrap it. */
207 if (builtin_instanceof(cause, class)) {
208 /* clear exception, because we are calling jit code again */
210 exceptions_clear_exception();
212 /* wrap the exception */
214 exceptions_throw_exceptionininitializererror(cause);
222 log_message_class("Finished static class initializer for class: ", c);
230 * These are local overrides for various environment variables in Emacs.
231 * Please do not remove this and leave it at the end of the file, where
232 * Emacs will automagically detect them.
233 * ---------------------------------------------------------------------
236 * indent-tabs-mode: t