Merge from mainline
[official-gcc.git] / libjava / gnu / java / lang / natMainThread.cc
blob95626eb3a71a40e422ba108d8dae8c7a241e5feb
1 // natMainThread.cc - Implementation of MainThread native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2006 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
13 #include <stdio.h>
14 #include <stdlib.h>
16 #include <gcj/cni.h>
17 #include <jvm.h>
18 #include <java-threads.h>
20 #include <gnu/java/lang/MainThread.h>
21 #include <java/lang/Runtime.h>
22 #include <java/lang/ThreadGroup.h>
24 typedef void main_func (jobject);
26 void
27 gnu::java::lang::MainThread::call_main (void)
29 Utf8Const* main_signature = _Jv_makeUtf8Const ("([Ljava.lang.String;)V", 22);
30 Utf8Const* main_name = _Jv_makeUtf8Const ("main", 4);
32 _Jv_Method *meth = _Jv_LookupDeclaredMethod (klass, main_name,
33 main_signature);
35 // Some checks from Java Spec section 12.1.4.
36 const char *msg = NULL;
37 if (meth == NULL)
38 msg = "no suitable method `main' in class";
39 else if (! ::java::lang::reflect::Modifier::isStatic(meth->accflags))
40 msg = "`main' must be static";
41 else if (! ::java::lang::reflect::Modifier::isPublic(meth->accflags))
42 msg = "`main' must be public";
43 if (msg != NULL)
45 fprintf (stderr, "%s\n", msg);
46 ::exit(1);
49 main_func *real_main = (main_func *) meth->ncode;
50 (*real_main) (args);
52 // Note that we do thread cleanup here. We have to do this here and
53 // not in _Jv_RunMain; if we do if after the main thread has exited,
54 // our ThreadGroup will be null, and if Runtime.exit tries to create
55 // a new Thread (which it does when running shutdown hooks), it will
56 // eventually NPE due to this.
57 _Jv_ThreadWait ();
59 int status = (int) ::java::lang::ThreadGroup::had_uncaught_exception;
60 ::java::lang::Runtime *runtime = ::java::lang::Runtime::getRuntime ();
61 runtime->exit (status);