1 /* src/vm/initialize.c - static class initializer functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "threads/lock-common.h"
34 #include "vm/global.h"
35 #include "vm/initialize.h"
36 #include "vm/builtin.h"
37 #include "vm/exceptions.h"
38 #include "vm/stringlocal.h"
41 #include "vm/jit/asmpart.h"
43 #include "vmcore/class.h"
44 #include "vmcore/loader.h"
45 #include "vmcore/options.h"
47 #if defined(ENABLE_STATISTICS)
48 # include "vmcore/statistics.h"
52 /* private functions **********************************************************/
54 static bool initialize_class_intern(classinfo *c);
57 /* initialize_init *************************************************************
59 Initialize important system classes.
61 *******************************************************************************/
63 void initialize_init(void)
65 TRACESUBSYSTEMINITIALIZATION("initialize_init");
67 #if defined(ENABLE_JAVASE)
68 # if defined(WITH_CLASSPATH_GNU)
72 # elif defined(WITH_CLASSPATH_SUN)
74 if (!initialize_class(class_java_lang_String))
75 vm_abort("initialize_init: Initialization failed: java.lang.String");
77 if (!initialize_class(class_java_lang_System))
78 vm_abort("initialize_init: Initialization failed: java.lang.System");
80 if (!initialize_class(class_java_lang_ThreadGroup))
81 vm_abort("initialize_init: Initialization failed: java.lang.ThreadGroup");
83 if (!initialize_class(class_java_lang_Thread))
84 vm_abort("initialize_init: Initialization failed: java.lang.Thread");
87 # error unknown classpath configuration
90 #elif defined(ENABLE_JAVAME_CLDC1_1)
95 # error unknown Java configuration
99 /* initialize_class ************************************************************
101 In Java, every class can have a static initialization
102 function. This function has to be called BEFORE calling other
103 methods or accessing static variables.
105 *******************************************************************************/
107 bool initialize_class(classinfo *c)
111 if (!makeinitializations)
114 LOCK_MONITOR_ENTER(c);
116 /* maybe the class is already initalized or the current thread, which can
117 pass the monitor, is currently initalizing this class */
119 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
120 LOCK_MONITOR_EXIT(c);
125 /* if <clinit> throw an Error before, the class was marked with an
126 error and we have to throw a NoClassDefFoundError */
128 if (c->state & CLASS_ERROR) {
129 exceptions_throw_noclassdeffounderror(c->name);
131 LOCK_MONITOR_EXIT(c);
133 /* ...but return true, this is ok (mauve test) */
138 /* this initalizing run begins NOW */
140 c->state |= CLASS_INITIALIZING;
142 /* call the internal function */
144 r = initialize_class_intern(c);
146 /* if return value is not NULL everything was ok and the class is
150 c->state |= CLASS_INITIALIZED;
152 /* this initalizing run is done */
154 c->state &= ~CLASS_INITIALIZING;
156 LOCK_MONITOR_EXIT(c);
162 /* initialize_class_intern *****************************************************
164 This function MUST NOT be called directly, because of thread
165 <clinit> race conditions.
167 *******************************************************************************/
169 static bool initialize_class_intern(classinfo *c)
172 java_handle_t *cause;
175 /* maybe the class is not already linked */
177 if (!(c->state & CLASS_LINKED))
181 #if defined(ENABLE_STATISTICS)
186 /* Initialize super class. */
188 if (c->super != NULL) {
189 if (!(c->super->state & CLASS_INITIALIZED)) {
192 log_message_class_message_class("Initialize super class ",
198 if (!initialize_class(c->super))
203 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
205 m = class_findmethod(c, utf_clinit, utf_void__void);
210 log_message_class("Class has no static class initializer: ", c);
216 /* Sun's and IBM's JVM don't care about the static flag */
217 /* if (!(m->flags & ACC_STATIC)) { */
218 /* log_text("Class initializer is not static!"); */
222 log_message_class("Starting static class initializer for class: ", c);
225 /* now call the initializer */
227 (void) vm_call_method(m, NULL);
229 /* we have an exception or error */
231 cause = exceptions_get_exception();
234 /* class is NOT initialized and is marked with error */
236 c->state |= CLASS_ERROR;
238 /* Load java/lang/Exception for the instanceof check. */
240 class = load_class_bootstrap(utf_java_lang_Exception);
245 /* Is this an exception? Yes, than wrap it. */
247 if (builtin_instanceof(cause, class)) {
248 /* clear exception, because we are calling jit code again */
250 exceptions_clear_exception();
252 /* wrap the exception */
254 exceptions_throw_exceptionininitializererror(cause);
262 log_message_class("Finished static class initializer for class: ", c);
270 * These are local overrides for various environment variables in Emacs.
271 * Please do not remove this and leave it at the end of the file, where
272 * Emacs will automagically detect them.
273 * ---------------------------------------------------------------------
276 * indent-tabs-mode: t