blob: 2056b0b2d1404465d94e3b63a6b39373d6d7bbd0 [file] [log] [blame]
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003#include <cstring>
4#include <cstdio>
5#include <signal.h>
6#include <string>
Carl Shapiro7b216702011-06-17 15:09:26 -07007
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include "jni.h"
9#include "logging.h"
10#include "scoped_ptr.h"
Elliott Hughes0a96fe02011-08-19 10:22:04 -070011#include "toStringArray.h"
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070012#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070013
Carl Shapiro2ed144c2011-07-26 16:52:08 -070014// Determine whether or not the specified method is public.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070015static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070016 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz,
17 method_id, JNI_FALSE));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018 if (reflected.get() == NULL) {
19 fprintf(stderr, "Unable to get reflected method\n");
20 return false;
21 }
22 // We now have a Method instance. We need to call its
23 // getModifiers() method.
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070024 ScopedLocalRef<jclass> method(env,
25 env->FindClass("java/lang/reflect/Method"));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070026 if (method.get() == NULL) {
27 fprintf(stderr, "Unable to find class Method\n");
28 return false;
29 }
30 jmethodID get_modifiers = env->GetMethodID(method.get(),
31 "getModifiers",
32 "()I");
33 if (get_modifiers == NULL) {
34 fprintf(stderr, "Unable to find reflect.Method.getModifiers\n");
35 return false;
36 }
37 static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070038#if 0 // CallIntMethod not yet implemented
Elliott Hughesc1674ed2011-08-25 18:09:09 -070039 int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070040#else
41 int modifiers = PUBLIC;
42 UNIMPLEMENTED(WARNING) << "assuming main is public...";
43#endif
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044 if ((modifiers & PUBLIC) == 0) {
45 return false;
46 }
47 return true;
48}
49
Elliott Hughesc1674ed2011-08-25 18:09:09 -070050static void InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051 // We want to call main() with a String array with our arguments in
52 // it. Create an array and populate it. Note argv[0] is not
53 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070054 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070055 if (args.get() == NULL) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070056 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070057 }
58
59 // Find [class].main(String[]).
60
61 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070062 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070063 std::replace(class_name.begin(), class_name.end(), '.', '/');
64
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070065 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 if (klass.get() == NULL) {
67 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070068 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070069 }
70
71 jmethodID method = env->GetStaticMethodID(klass.get(),
72 "main",
73 "([Ljava/lang/String;)V");
74 if (method == NULL) {
75 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
76 class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070077 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070078 }
79
80 // Make sure the method is public. JNI doesn't prevent us from
81 // calling a private method, so we have to check it explicitly.
82 if (!IsMethodPublic(env, klass.get(), method)) {
83 fprintf(stderr, "Sorry, main() is not public\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -070084 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070085 }
86
87 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070088 env->CallStaticVoidMethod(klass.get(), method, args.get());
Carl Shapiro2ed144c2011-07-26 16:52:08 -070089}
90
91// Parse arguments. Most of it just gets passed through to the VM.
92// The JNI spec defines a handful of standard arguments.
Carl Shapiro7b216702011-06-17 15:09:26 -070093int main(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070094 setvbuf(stdout, NULL, _IONBF, 0);
95
96 // Skip over argv[0].
97 argv++;
98 argc--;
99
100 // If we're adding any additional stuff, e.g. function hook specifiers,
101 // add them to the count here.
102 //
103 // We're over-allocating, because this includes the options to the VM
104 // plus the options to the program.
105 int option_count = argc;
106 scoped_array<JavaVMOption> options(new JavaVMOption[option_count]());
107
108 // Copy options over. Everything up to the name of the class starts
109 // with a '-' (the function hook stuff is strictly internal).
110 //
111 // [Do we need to catch & handle "-jar" here?]
112 bool need_extra = false;
113 int curr_opt, arg_idx;
114 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
115 if (argv[arg_idx][0] != '-' && !need_extra) {
116 break;
117 }
118 options[curr_opt++].optionString = argv[arg_idx];
119
120 // Some options require an additional argument.
121 need_extra = false;
122 if (strcmp(argv[arg_idx], "-classpath") == 0 ||
123 strcmp(argv[arg_idx], "-cp") == 0) {
124 // others?
125 need_extra = true;
126 }
127 }
128
129 if (need_extra) {
130 fprintf(stderr, "VM requires value after last option flag\n");
131 return EXIT_FAILURE;
132 }
133
134 // Make sure they provided a class name. We do this after VM init
135 // so that things like "-Xrunjdwp:help" have the opportunity to emit
136 // a usage statement.
137 if (arg_idx == argc) {
138 fprintf(stderr, "Class name required\n");
139 return EXIT_FAILURE;
140 }
141
142 // insert additional internal options here
143
144 DCHECK_LE(curr_opt, option_count);
145
146 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700147 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700148 init_args.options = options.get();
149 init_args.nOptions = curr_opt;
150 init_args.ignoreUnrecognized = JNI_FALSE;
151
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700152 // Start VM. The current thread becomes the main thread of the VM.
153 JavaVM* vm = NULL;
154 JNIEnv* env = NULL;
155 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
156 fprintf(stderr, "VM init failed (check log file)\n");
157 return EXIT_FAILURE;
158 }
159
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700160 InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
161 int rc = env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
162 if (rc == EXIT_FAILURE) {
163 env->ExceptionDescribe();
164 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700165
Elliott Hughesf2682d52011-08-15 16:37:04 -0700166 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700167 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700168 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700169 }
170
Elliott Hughesf2682d52011-08-15 16:37:04 -0700171 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700172 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700173 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700174 }
175
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700176 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700177}