1 /* src/vm/initialize.c - static class initializer functions
3 Copyright (C) 1996-2005, 2006 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
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
33 $Id: initialize.c 5123 2006-07-12 21:45:34Z twisti $
44 #if defined(ENABLE_THREADS)
45 # include "threads/native/lock.h"
47 # include "threads/none/lock.h"
50 #include "vm/global.h"
51 #include "vm/initialize.h"
52 #include "vm/builtin.h"
54 #include "vm/loader.h"
55 #include "vm/exceptions.h"
56 #include "vm/options.h"
57 #include "vm/statistics.h"
58 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
63 /* private functions **********************************************************/
65 static bool initialize_class_intern(classinfo *c);
68 /* initialize_class ************************************************************
70 In Java, every class can have a static initialization
71 function. This function has to be called BEFORE calling other
72 methods or accessing static variables.
74 *******************************************************************************/
76 bool initialize_class(classinfo *c)
80 if (!makeinitializations)
83 LOCK_MONITOR_ENTER(c);
85 /* maybe the class is already initalized or the current thread, which can
86 pass the monitor, is currently initalizing this class */
88 if (CLASS_IS_OR_ALMOST_INITIALIZED(c)) {
94 /* if <clinit> throw an Error before, the class was marked with an
95 error and we have to throw a NoClassDefFoundError */
97 if (c->state & CLASS_ERROR) {
98 *exceptionptr = new_noclassdeffounderror(c->name);
100 LOCK_MONITOR_EXIT(c);
102 /* ...but return true, this is ok (mauve test) */
107 /* this initalizing run begins NOW */
109 c->state |= CLASS_INITIALIZING;
111 /* call the internal function */
113 r = initialize_class_intern(c);
115 /* if return value is not NULL everything was ok and the class is
119 c->state |= CLASS_INITIALIZED;
121 /* this initalizing run is done */
123 c->state &= ~CLASS_INITIALIZING;
125 LOCK_MONITOR_EXIT(c);
131 /* initialize_class_intern *****************************************************
133 This function MUST NOT be called directly, because of thread
134 <clinit> race conditions.
136 *******************************************************************************/
138 static bool initialize_class_intern(classinfo *c)
141 java_objectheader *xptr;
143 /* maybe the class is not already linked */
145 if (!(c->state & CLASS_LINKED))
149 #if defined(ENABLE_STATISTICS)
154 /* initialize super class */
157 if (!(c->super.cls->state & CLASS_INITIALIZED)) {
160 log_message_class_message_class("Initialize super class ",
166 if (!initialize_class(c->super.cls))
171 /* interfaces implemented need not to be initialized (VM Spec 2.17.4) */
173 m = class_findmethod(c, utf_clinit, utf_void__void);
178 log_message_class("Class has no static class initializer: ", c);
184 /* Sun's and IBM's JVM don't care about the static flag */
185 /* if (!(m->flags & ACC_STATIC)) { */
186 /* log_text("Class initializer is not static!"); */
190 log_message_class("Starting static class initializer for class: ", c);
193 /* now call the initializer */
195 (void) vm_call_method(m, NULL);
197 /* we have an exception or error */
199 xptr = *exceptionptr;
202 /* class is NOT initialized and is marked with error */
204 c->state |= CLASS_ERROR;
206 /* is this an exception, than wrap it */
208 if (builtin_instanceof(xptr, class_java_lang_Exception)) {
209 /* clear exception, because we are calling jit code again */
211 *exceptionptr = NULL;
213 /* wrap the exception */
216 new_exception_throwable(string_java_lang_ExceptionInInitializerError,
217 (java_lang_Throwable *) xptr);
225 log_message_class("Finished static class initializer for class: ", c);
233 * These are local overrides for various environment variables in Emacs.
234 * Please do not remove this and leave it at the end of the file, where
235 * Emacs will automagically detect them.
236 * ---------------------------------------------------------------------
239 * indent-tabs-mode: t