blob: 858ac34134c9efe1f88035dab870fcfc70ad6c63 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ian Rogersdf20fe02011-07-20 20:34:16 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "jni_internal.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070018
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include <dlfcn.h>
Elliott Hughes79082e32011-08-25 12:07:32 -070020
21#include <cstdarg>
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include <utility>
23#include <vector>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080026#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080028#include "base/stringpiece.h"
Elliott Hughes40ef99e2011-08-11 17:44:34 -070029#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Jeff Hao3dd9f762013-07-08 13:09:25 -070032#include "interpreter/interpreter.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070033#include "invoke_arg_array_builder.h"
Carl Shapiroea4dca82011-08-01 13:45:38 -070034#include "jni.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class-inl.h"
36#include "mirror/class_loader.h"
37#include "mirror/field-inl.h"
38#include "mirror/abstract_method-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
41#include "mirror/throwable.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042#include "object_utils.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070043#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070044#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045#include "scoped_thread_state_change.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070046#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070047#include "thread.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "utf.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070049#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Ian Rogersdf20fe02011-07-20 20:34:16 -070051
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070052using ::art::mirror::AbstractMethod;
53using ::art::mirror::Array;
54using ::art::mirror::BooleanArray;
55using ::art::mirror::ByteArray;
56using ::art::mirror::CharArray;
57using ::art::mirror::Class;
58using ::art::mirror::ClassLoader;
59using ::art::mirror::DoubleArray;
60using ::art::mirror::Field;
61using ::art::mirror::FloatArray;
62using ::art::mirror::IntArray;
63using ::art::mirror::LongArray;
64using ::art::mirror::Object;
65using ::art::mirror::ObjectArray;
66using ::art::mirror::ShortArray;
67using ::art::mirror::String;
68using ::art::mirror::Throwable;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070070namespace art {
71
Brian Carlstrom7934ac22013-07-26 10:54:15 -070072static const size_t kMonitorsInitial = 32; // Arbitrary.
73static const size_t kMonitorsMax = 4096; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070074
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075static const size_t kLocalsInitial = 64; // Arbitrary.
76static const size_t kLocalsMax = 512; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070077
Brian Carlstrom7934ac22013-07-26 10:54:15 -070078static const size_t kPinTableInitial = 16; // Arbitrary.
79static const size_t kPinTableMax = 1024; // Arbitrary sanity check.
Elliott Hughes2ced6a52011-10-16 18:44:48 -070080
Brian Carlstrom7934ac22013-07-26 10:54:15 -070081static size_t gGlobalsInitial = 512; // Arbitrary.
82static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Elliott Hughes2ced6a52011-10-16 18:44:48 -070083
Brian Carlstrom7934ac22013-07-26 10:54:15 -070084static const size_t kWeakGlobalsInitial = 16; // Arbitrary.
85static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogersdf20fe02011-07-20 20:34:16 -070086
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087static jweak AddWeakGlobalReference(ScopedObjectAccess& soa, Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughescdf53122011-08-19 15:46:09 -070089 if (obj == NULL) {
90 return NULL;
91 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -070093 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -070094 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -070095 IndirectRef ref = weak_globals.Add(IRT_FIRST_SEGMENT, obj);
96 return reinterpret_cast<jweak>(ref);
97}
98
Jeff Hao19c5d372013-03-15 14:33:43 -070099static bool IsBadJniVersion(int version) {
100 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
101 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
102}
103
Jeff Hao5d917302013-02-27 17:57:33 -0800104static void CheckMethodArguments(AbstractMethod* m, uint32_t* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700106 MethodHelper mh(m);
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 const DexFile::TypeList* params = mh.GetParameterTypeList();
108 if (params == NULL) {
109 return; // No arguments so nothing to check.
110 }
Jeff Hao5d917302013-02-27 17:57:33 -0800111 uint32_t offset = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700112 uint32_t num_params = params->Size();
Elliott Hughesb264f082012-04-06 17:10:10 -0700113 size_t error_count = 0;
Jeff Hao5d917302013-02-27 17:57:33 -0800114 if (!m->IsStatic()) {
115 offset = 1;
116 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700117 for (uint32_t i = 0; i < num_params; i++) {
118 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
119 Class* param_type = mh.GetClassFromTypeIdx(type_idx);
120 if (param_type == NULL) {
121 Thread* self = Thread::Current();
122 CHECK(self->IsExceptionPending());
123 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
124 << mh.GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
Ian Rogers62d6c772013-02-27 08:32:07 -0800125 << self->GetException(NULL)->Dump();
Ian Rogers50b35e22012-10-04 10:09:15 -0700126 self->ClearException();
127 ++error_count;
128 } else if (!param_type->IsPrimitive()) {
129 // TODO: check primitives are in range.
Jeff Hao5d917302013-02-27 17:57:33 -0800130 Object* argument = reinterpret_cast<Object*>(args[i + offset]);
Ian Rogers50b35e22012-10-04 10:09:15 -0700131 if (argument != NULL && !argument->InstanceOf(param_type)) {
Elliott Hughesb264f082012-04-06 17:10:10 -0700132 LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
133 << PrettyTypeOf(argument) << " as argument " << (i + 1) << " to " << PrettyMethod(m);
134 ++error_count;
135 }
Jeff Hao5d917302013-02-27 17:57:33 -0800136 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
137 offset++;
Elliott Hughesb264f082012-04-06 17:10:10 -0700138 }
139 }
140 if (error_count > 0) {
141 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
142 // with an argument.
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700143 JniAbortF(NULL, "bad arguments passed to %s (see above for details)", PrettyMethod(m).c_str());
Elliott Hughesb264f082012-04-06 17:10:10 -0700144 }
145}
Elliott Hughesb264f082012-04-06 17:10:10 -0700146
Jeff Hao5d917302013-02-27 17:57:33 -0800147void InvokeWithArgArray(const ScopedObjectAccess& soa, AbstractMethod* method,
Jeff Hao6474d192013-03-26 14:08:09 -0700148 ArgArray* arg_array, JValue* result, char result_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700150 uint32_t* args = arg_array->GetArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 if (UNLIKELY(soa.Env()->check_jni)) {
Jeff Hao3dd9f762013-07-08 13:09:25 -0700152 CheckMethodArguments(method, args);
Elliott Hughes4cacde82012-04-11 18:32:27 -0700153 }
Sebastien Hertz7d658cf2013-07-09 10:56:11 +0200154 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, result_type);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700155}
156
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157static JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj,
158 jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700160 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700161 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800163 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700164 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800165 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700166 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
167 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700168}
169
Mathieu Chartier66f19252012-09-18 08:57:04 -0700170static AbstractMethod* FindVirtualMethod(Object* receiver, AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700172 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
Elliott Hughes72025e52011-08-23 17:50:30 -0700173}
174
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700175static JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
176 jobject obj, jmethodID mid, jvalue* args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700178 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700179 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700180 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800181 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800183 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700184 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
185 return result;
Elliott Hughes72025e52011-08-23 17:50:30 -0700186}
187
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188static JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
189 jobject obj, jmethodID mid, va_list args)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 Object* receiver = soa.Decode<Object*>(obj);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 AbstractMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700193 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800194 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800196 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700197 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
198 return result;
Carl Shapiroea4dca82011-08-01 13:45:38 -0700199}
200
Elliott Hughes6b436852011-08-12 10:16:44 -0700201// Section 12.3.2 of the JNI spec describes JNI class descriptors. They're
202// separated with slashes but aren't wrapped with "L;" like regular descriptors
203// (i.e. "a/b/C" rather than "La/b/C;"). Arrays of reference types are an
204// exception; there the "L;" must be present ("[La/b/C;"). Historically we've
205// supported names with dots too (such as "a.b.C").
Ian Rogers0571d352011-11-03 19:51:38 -0700206static std::string NormalizeJniClassDescriptor(const char* name) {
Elliott Hughes6b436852011-08-12 10:16:44 -0700207 std::string result;
208 // Add the missing "L;" if necessary.
209 if (name[0] == '[') {
210 result = name;
211 } else {
212 result += 'L';
213 result += name;
214 result += ';';
215 }
216 // Rewrite '.' as '/' for backwards compatibility.
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700217 if (result.find('.') != std::string::npos) {
218 LOG(WARNING) << "Call to JNI FindClass with dots in name: "
219 << "\"" << name << "\"";
220 std::replace(result.begin(), result.end(), '.', '/');
Elliott Hughes6b436852011-08-12 10:16:44 -0700221 }
222 return result;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225static void ThrowNoSuchMethodError(ScopedObjectAccess& soa, Class* c,
226 const char* name, const char* sig, const char* kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800228 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
229 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchMethodError;",
230 "no %s method \"%s.%s%s\"",
231 kind, ClassHelper(c).GetDescriptor(), name, sig);
Elliott Hughes14134a12011-09-30 16:55:51 -0700232}
233
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234static jmethodID FindMethodID(ScopedObjectAccess& soa, jclass jni_class,
235 const char* name, const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700238 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700239 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700240 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700241
Mathieu Chartier66f19252012-09-18 08:57:04 -0700242 AbstractMethod* method = NULL;
Elliott Hughescdf53122011-08-19 15:46:09 -0700243 if (is_static) {
244 method = c->FindDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700245 } else {
Elliott Hughescdf53122011-08-19 15:46:09 -0700246 method = c->FindVirtualMethod(name, sig);
247 if (method == NULL) {
248 // No virtual method matching the signature. Search declared
249 // private methods and constructors.
250 method = c->FindDeclaredDirectMethod(name, sig);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700251 }
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700252 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700253
Elliott Hughescdf53122011-08-19 15:46:09 -0700254 if (method == NULL || method->IsStatic() != is_static) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 ThrowNoSuchMethodError(soa, c, name, sig, is_static ? "static" : "non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -0700256 return NULL;
257 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700258
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 return soa.EncodeMethod(method);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700260}
261
Ian Rogersef28b142012-11-30 14:22:18 -0800262static ClassLoader* GetClassLoader(const ScopedObjectAccess& soa)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 AbstractMethod* method = soa.Self()->GetCurrentMethod(NULL);
Ian Rogersef28b142012-11-30 14:22:18 -0800265 if (method == NULL ||
266 method == soa.DecodeMethod(WellKnownClasses::java_lang_Runtime_nativeLoad)) {
267 return soa.Self()->GetClassLoaderOverride();
Brian Carlstrom00fae582011-10-28 01:16:28 -0700268 }
269 return method->GetDeclaringClass()->GetClassLoader();
270}
271
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700272static jfieldID FindFieldID(const ScopedObjectAccess& soa, jclass jni_class, const char* name,
273 const char* sig, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700275 Class* c = soa.Decode<Class*>(jni_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700276 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700277 return NULL;
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700278 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700279
280 Field* field = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Class* field_type;
282 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
283 if (sig[1] != '\0') {
Ian Rogersef28b142012-11-30 14:22:18 -0800284 ClassLoader* cl = GetClassLoader(soa);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 field_type = class_linker->FindClass(sig, cl);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700286 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 field_type = class_linker->FindPrimitiveClass(*sig);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 if (field_type == NULL) {
290 // Failed to find type from the signature of the field.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 ThrowLocation throw_location;
293 SirtRef<mirror::Throwable> cause(soa.Self(), soa.Self()->GetException(&throw_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 soa.Self()->ClearException();
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
296 "no type \"%s\" found and so no field \"%s\" could be found in class "
297 "\"%s\" or its superclasses", sig, name,
298 ClassHelper(c).GetDescriptor());
299 soa.Self()->GetException(NULL)->SetCause(cause.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 return NULL;
301 }
302 if (is_static) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800303 field = c->FindStaticField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 field = c->FindInstanceField(name, ClassHelper(field_type).GetDescriptor());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700307 if (field == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800308 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
309 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
310 "no \"%s\" field \"%s\" in class \"%s\" or its superclasses",
311 sig, name, ClassHelper(c).GetDescriptor());
Elliott Hughes8a26c5c2011-08-15 18:35:43 -0700312 return NULL;
313 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 return soa.EncodeField(field);
Carl Shapiroea4dca82011-08-01 13:45:38 -0700315}
316
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317static void PinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700320 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700321 vm->pin_table.Add(array);
322}
323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324static void UnpinPrimitiveArray(const ScopedObjectAccess& soa, const Array* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 JavaVMExt* vm = soa.Vm();
Ian Rogers50b35e22012-10-04 10:09:15 -0700327 MutexLock mu(soa.Self(), vm->pins_lock);
Elliott Hughes75770752011-08-24 17:52:38 -0700328 vm->pin_table.Remove(array);
329}
330
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331static void ThrowAIOOBE(ScopedObjectAccess& soa, Array* array, jsize start,
332 jsize length, const char* identifier)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700334 std::string type(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
336 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
337 "%s offset=%d length=%d %s.length=%d",
338 type.c_str(), start, length, identifier, array->GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -0700339}
Ian Rogers0571d352011-11-03 19:51:38 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341static void ThrowSIOOBE(ScopedObjectAccess& soa, jsize start, jsize length,
342 jsize array_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800344 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
345 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/StringIndexOutOfBoundsException;",
346 "offset=%d length=%d string.length()=%d", start, length,
347 array_length);
Elliott Hughesb465ab02011-08-24 11:21:21 -0700348}
Elliott Hughes814e4032011-08-23 12:07:56 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350int ThrowNewException(JNIEnv* env, jclass exception_class, const char* msg, jobject cause)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 // Turn the const char* into a java.lang.String.
353 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg));
354 if (msg != NULL && s.get() == NULL) {
355 return JNI_ERR;
Elliott Hughes814e4032011-08-23 12:07:56 -0700356 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Choose an appropriate constructor and set up the arguments.
359 jvalue args[2];
360 const char* signature;
361 if (msg == NULL && cause == NULL) {
362 signature = "()V";
363 } else if (msg != NULL && cause == NULL) {
364 signature = "(Ljava/lang/String;)V";
365 args[0].l = s.get();
366 } else if (msg == NULL && cause != NULL) {
367 signature = "(Ljava/lang/Throwable;)V";
368 args[0].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700369 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V";
371 args[0].l = s.get();
372 args[1].l = cause;
Elliott Hughes814e4032011-08-23 12:07:56 -0700373 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 jmethodID mid = env->GetMethodID(exception_class, "<init>", signature);
375 if (mid == NULL) {
Ian Rogersef28b142012-11-30 14:22:18 -0800376 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 LOG(ERROR) << "No <init>" << signature << " in "
378 << PrettyClass(soa.Decode<Class*>(exception_class));
379 return JNI_ERR;
380 }
Elliott Hughes814e4032011-08-23 12:07:56 -0700381
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 ScopedLocalRef<jthrowable> exception(env, reinterpret_cast<jthrowable>(env->NewObjectA(exception_class, mid, args)));
383 if (exception.get() == NULL) {
384 return JNI_ERR;
385 }
Ian Rogersef28b142012-11-30 14:22:18 -0800386 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
388 soa.Self()->SetException(throw_location, soa.Decode<Throwable*>(exception.get()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700389 return JNI_OK;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700390}
391
Elliott Hughes462c9442012-03-23 18:47:50 -0700392static jint JII_AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700393 if (vm == NULL || p_env == NULL) {
394 return JNI_ERR;
395 }
396
Elliott Hughes462c9442012-03-23 18:47:50 -0700397 // Return immediately if we're already attached.
Elliott Hughescac6cc72011-11-03 20:31:21 -0700398 Thread* self = Thread::Current();
399 if (self != NULL) {
400 *p_env = self->GetJniEnv();
401 return JNI_OK;
402 }
403
404 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->runtime;
405
406 // No threads allowed in zygote mode.
407 if (runtime->IsZygote()) {
408 LOG(ERROR) << "Attempt to attach a thread in the zygote";
409 return JNI_ERR;
410 }
411
Elliott Hughes462c9442012-03-23 18:47:50 -0700412 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
413 const char* thread_name = NULL;
Ian Rogers365c1022012-06-22 15:05:28 -0700414 jobject thread_group = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -0700415 if (args != NULL) {
Elliott Hughes83a25322013-03-14 11:18:53 -0700416 if (IsBadJniVersion(args->version)) {
417 LOG(ERROR) << "Bad JNI version passed to "
418 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
419 << args->version;
420 return JNI_EVERSION;
Brian Carlstrom86922152013-03-12 00:59:36 -0700421 }
Elliott Hughes462c9442012-03-23 18:47:50 -0700422 thread_name = args->name;
Ian Rogers365c1022012-06-22 15:05:28 -0700423 thread_group = args->group;
Elliott Hughes75770752011-08-24 17:52:38 -0700424 }
Elliott Hughes75770752011-08-24 17:52:38 -0700425
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800426 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, !runtime->IsCompiler())) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700427 *p_env = NULL;
428 return JNI_ERR;
429 } else {
430 *p_env = Thread::Current()->GetJniEnv();
431 return JNI_OK;
432 }
Elliott Hughes75770752011-08-24 17:52:38 -0700433}
434
Elliott Hughes79082e32011-08-25 12:07:32 -0700435class SharedLibrary {
436 public:
437 SharedLibrary(const std::string& path, void* handle, Object* class_loader)
438 : path_(path),
439 handle_(handle),
Shih-wei Liao31384c52011-09-06 15:27:45 -0700440 class_loader_(class_loader),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 jni_on_load_lock_("JNI_OnLoad lock"),
Ian Rogersc604d732012-10-14 16:09:54 -0700442 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 jni_on_load_thread_id_(Thread::Current()->GetThinLockId()),
Elliott Hughes79082e32011-08-25 12:07:32 -0700444 jni_on_load_result_(kPending) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700445 }
446
Elliott Hughes79082e32011-08-25 12:07:32 -0700447 Object* GetClassLoader() {
448 return class_loader_;
449 }
450
451 std::string GetPath() {
452 return path_;
453 }
454
455 /*
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700456 * Check the result of an earlier call to JNI_OnLoad on this library.
457 * If the call has not yet finished in another thread, wait for it.
Elliott Hughes79082e32011-08-25 12:07:32 -0700458 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700459 bool CheckOnLoadResult()
460 LOCKS_EXCLUDED(jni_on_load_lock_)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700462 Thread* self = Thread::Current();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
464 bool okay;
465 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700466 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700467
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 if (jni_on_load_thread_id_ == self->GetThinLockId()) {
469 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
470 // caller can continue.
471 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
472 okay = true;
473 } else {
474 while (jni_on_load_result_ == kPending) {
475 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
Ian Rogersc604d732012-10-14 16:09:54 -0700476 jni_on_load_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700478
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 okay = (jni_on_load_result_ == kOkay);
480 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
481 << (okay ? "succeeded" : "failed") << "]";
482 }
483 }
484 self->TransitionFromSuspendedToRunnable();
Elliott Hughes79082e32011-08-25 12:07:32 -0700485 return okay;
486 }
487
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 void SetResult(bool result) LOCKS_EXCLUDED(jni_on_load_lock_) {
Ian Rogersc604d732012-10-14 16:09:54 -0700489 Thread* self = Thread::Current();
490 MutexLock mu(self, jni_on_load_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -0700491
Elliott Hughes79082e32011-08-25 12:07:32 -0700492 jni_on_load_result_ = result ? kOkay : kFailed;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 jni_on_load_thread_id_ = 0;
Elliott Hughes79082e32011-08-25 12:07:32 -0700494
495 // Broadcast a wakeup to anybody sleeping on the condition variable.
Ian Rogersc604d732012-10-14 16:09:54 -0700496 jni_on_load_cond_.Broadcast(self);
Elliott Hughes79082e32011-08-25 12:07:32 -0700497 }
498
499 void* FindSymbol(const std::string& symbol_name) {
500 return dlsym(handle_, symbol_name.c_str());
501 }
502
503 private:
504 enum JNI_OnLoadState {
505 kPending,
506 kFailed,
507 kOkay,
508 };
509
510 // Path to library "/system/lib/libjni.so".
511 std::string path_;
512
513 // The void* returned by dlopen(3).
514 void* handle_;
515
516 // The ClassLoader this library is associated with.
517 Object* class_loader_;
518
519 // Guards remaining items.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes79082e32011-08-25 12:07:32 -0700521 // Wait for JNI_OnLoad in other thread.
Ian Rogersc604d732012-10-14 16:09:54 -0700522 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700523 // Recursive invocation guard.
Elliott Hughesf8349362012-06-18 15:00:06 -0700524 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700525 // Result of earlier JNI_OnLoad call.
Elliott Hughesf8349362012-06-18 15:00:06 -0700526 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700527};
528
Elliott Hughes79082e32011-08-25 12:07:32 -0700529// This exists mainly to keep implementation details out of the header file.
530class Libraries {
531 public:
532 Libraries() {
533 }
534
535 ~Libraries() {
Elliott Hughesc31664f2011-09-29 15:58:28 -0700536 STLDeleteValues(&libraries_);
Elliott Hughes79082e32011-08-25 12:07:32 -0700537 }
538
Elliott Hughesae80b492012-04-24 10:43:17 -0700539 void Dump(std::ostream& os) const {
540 bool first = true;
541 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
542 if (!first) {
543 os << ' ';
544 }
545 first = false;
546 os << it->first;
547 }
548 }
549
550 size_t size() const {
551 return libraries_.size();
552 }
553
Elliott Hughes79082e32011-08-25 12:07:32 -0700554 SharedLibrary* Get(const std::string& path) {
Ian Rogers712462a2012-04-12 16:32:29 -0700555 It it = libraries_.find(path);
556 return (it == libraries_.end()) ? NULL : it->second;
Elliott Hughes79082e32011-08-25 12:07:32 -0700557 }
558
559 void Put(const std::string& path, SharedLibrary* library) {
Elliott Hughesa0e18062012-04-13 15:59:59 -0700560 libraries_.Put(path, library);
Elliott Hughes79082e32011-08-25 12:07:32 -0700561 }
562
563 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700564 void* FindNativeMethod(const AbstractMethod* m, std::string& detail)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700566 std::string jni_short_name(JniShortName(m));
567 std::string jni_long_name(JniLongName(m));
Elliott Hughes4b093bf2011-08-25 13:34:29 -0700568 const ClassLoader* declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Elliott Hughes79082e32011-08-25 12:07:32 -0700569 for (It it = libraries_.begin(); it != libraries_.end(); ++it) {
570 SharedLibrary* library = it->second;
571 if (library->GetClassLoader() != declaring_class_loader) {
572 // We only search libraries loaded by the appropriate ClassLoader.
573 continue;
574 }
575 // Try the short name then the long name...
576 void* fn = library->FindSymbol(jni_short_name);
577 if (fn == NULL) {
578 fn = library->FindSymbol(jni_long_name);
579 }
580 if (fn != NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(jni) << "[Found native code for " << PrettyMethod(m)
582 << " in \"" << library->GetPath() << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -0700583 return fn;
584 }
585 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700586 detail += "No implementation found for ";
Elliott Hughesa2501992011-08-26 19:39:54 -0700587 detail += PrettyMethod(m);
Brian Carlstrom92827a52011-10-10 15:50:01 -0700588 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Elliott Hughes79082e32011-08-25 12:07:32 -0700589 LOG(ERROR) << detail;
Elliott Hughes79082e32011-08-25 12:07:32 -0700590 return NULL;
591 }
592
593 private:
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700594 typedef SafeMap<std::string, SharedLibrary*>::const_iterator It; // TODO: C++0x auto
Elliott Hughes79082e32011-08-25 12:07:32 -0700595
Elliott Hughesa0e18062012-04-13 15:59:59 -0700596 SafeMap<std::string, SharedLibrary*> libraries_;
Elliott Hughes79082e32011-08-25 12:07:32 -0700597};
598
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599JValue InvokeWithJValues(const ScopedObjectAccess& soa, jobject obj, jmethodID mid,
600 jvalue* args) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700601 AbstractMethod* method = soa.DecodeMethod(mid);
Jeff Hao7a549462013-03-18 19:04:24 -0700602 Object* receiver = method->IsStatic() ? NULL : soa.Decode<Object*>(obj);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700603 MethodHelper mh(method);
Jeff Hao5d917302013-02-27 17:57:33 -0800604 JValue result;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700605 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Jeff Hao5d917302013-02-27 17:57:33 -0800606 arg_array.BuildArgArray(soa, receiver, args);
Jeff Hao6474d192013-03-26 14:08:09 -0700607 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty()[0]);
608 return result;
Elliott Hughesd07986f2011-12-06 18:27:45 -0800609}
610
Elliott Hughescdf53122011-08-19 15:46:09 -0700611class JNI {
612 public:
Ian Rogers25e8b912012-09-07 11:31:36 -0700613 static jint GetVersion(JNIEnv*) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700614 return JNI_VERSION_1_6;
615 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700616
Ian Rogers25e8b912012-09-07 11:31:36 -0700617 static jclass DefineClass(JNIEnv*, const char*, jobject, const jbyte*, jsize) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700618 LOG(WARNING) << "JNI DefineClass is not supported";
Elliott Hughesf2682d52011-08-15 16:37:04 -0700619 return NULL;
620 }
621
Elliott Hughescdf53122011-08-19 15:46:09 -0700622 static jclass FindClass(JNIEnv* env, const char* name) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ScopedObjectAccess soa(env);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700624 Runtime* runtime = Runtime::Current();
625 ClassLinker* class_linker = runtime->GetClassLinker();
Elliott Hughescdf53122011-08-19 15:46:09 -0700626 std::string descriptor(NormalizeJniClassDescriptor(name));
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700627 Class* c = NULL;
628 if (runtime->IsStarted()) {
Ian Rogersef28b142012-11-30 14:22:18 -0800629 ClassLoader* cl = GetClassLoader(soa);
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800630 c = class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700631 } else {
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800632 c = class_linker->FindSystemClass(descriptor.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700633 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 return soa.AddLocalReference<jclass>(c);
Ian Rogers4dd71f12011-08-16 14:16:02 -0700635 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700636
Elliott Hughescdf53122011-08-19 15:46:09 -0700637 static jmethodID FromReflectedMethod(JNIEnv* env, jobject java_method) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700638 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700639 AbstractMethod* method = soa.Decode<AbstractMethod*>(java_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 return soa.EncodeMethod(method);
Elliott Hughesf2682d52011-08-15 16:37:04 -0700641 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700642
Elliott Hughescdf53122011-08-19 15:46:09 -0700643 static jfieldID FromReflectedField(JNIEnv* env, jobject java_field) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 ScopedObjectAccess soa(env);
645 Field* field = soa.Decode<Field*>(java_field);
646 return soa.EncodeField(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700647 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700648
Elliott Hughescdf53122011-08-19 15:46:09 -0700649 static jobject ToReflectedMethod(JNIEnv* env, jclass, jmethodID mid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 ScopedObjectAccess soa(env);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700651 AbstractMethod* method = soa.DecodeMethod(mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 return soa.AddLocalReference<jobject>(method);
Elliott Hughescdf53122011-08-19 15:46:09 -0700653 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700654
Elliott Hughescdf53122011-08-19 15:46:09 -0700655 static jobject ToReflectedField(JNIEnv* env, jclass, jfieldID fid, jboolean) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 ScopedObjectAccess soa(env);
657 Field* field = soa.DecodeField(fid);
658 return soa.AddLocalReference<jobject>(field);
Elliott Hughescdf53122011-08-19 15:46:09 -0700659 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700660
Elliott Hughes37f7a402011-08-22 18:56:01 -0700661 static jclass GetObjectClass(JNIEnv* env, jobject java_object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 ScopedObjectAccess soa(env);
663 Object* o = soa.Decode<Object*>(java_object);
664 return soa.AddLocalReference<jclass>(o->GetClass());
Elliott Hughes37f7a402011-08-22 18:56:01 -0700665 }
666
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700667 static jclass GetSuperclass(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 ScopedObjectAccess soa(env);
669 Class* c = soa.Decode<Class*>(java_class);
670 return soa.AddLocalReference<jclass>(c->GetSuperClass());
Elliott Hughescdf53122011-08-19 15:46:09 -0700671 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700672
Elliott Hughes37f7a402011-08-22 18:56:01 -0700673 static jboolean IsAssignableFrom(JNIEnv* env, jclass java_class1, jclass java_class2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700674 ScopedObjectAccess soa(env);
675 Class* c1 = soa.Decode<Class*>(java_class1);
676 Class* c2 = soa.Decode<Class*>(java_class2);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700677 return c1->IsAssignableFrom(c2) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700678 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700679
Elliott Hughese84278b2012-03-22 10:06:53 -0700680 static jboolean IsInstanceOf(JNIEnv* env, jobject jobj, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700681 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -0800682 if (java_class == NULL) {
683 JniAbortF("IsInstanceOf", "null class (second argument)");
684 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700685 if (jobj == NULL) {
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700686 // Note: JNI is different from regular Java instanceof in this respect
Elliott Hughes37f7a402011-08-22 18:56:01 -0700687 return JNI_TRUE;
688 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700689 Object* obj = soa.Decode<Object*>(jobj);
690 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughese84278b2012-03-22 10:06:53 -0700691 return obj->InstanceOf(c) ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700692 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700693 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700694
Elliott Hughes37f7a402011-08-22 18:56:01 -0700695 static jint Throw(JNIEnv* env, jthrowable java_exception) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 ScopedObjectAccess soa(env);
697 Throwable* exception = soa.Decode<Throwable*>(java_exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700698 if (exception == NULL) {
699 return JNI_ERR;
700 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800701 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
702 soa.Self()->SetException(throw_location, exception);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700703 return JNI_OK;
704 }
705
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700706 static jint ThrowNew(JNIEnv* env, jclass c, const char* msg) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700707 return ThrowNewException(env, c, msg, NULL);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700708 }
709
710 static jboolean ExceptionCheck(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700711 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? JNI_TRUE : JNI_FALSE;
Elliott Hughes37f7a402011-08-22 18:56:01 -0700712 }
713
714 static void ExceptionClear(JNIEnv* env) {
Ian Rogers120f1c72012-09-28 17:17:10 -0700715 static_cast<JNIEnvExt*>(env)->self->ClearException();
Elliott Hughes37f7a402011-08-22 18:56:01 -0700716 }
717
718 static void ExceptionDescribe(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700720
Ian Rogers62d6c772013-02-27 08:32:07 -0800721 SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
722 SirtRef<mirror::AbstractMethod> old_throw_method(soa.Self(), NULL);
723 SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
724 uint32_t old_throw_dex_pc;
725 {
726 ThrowLocation old_throw_location;
727 mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
728 old_throw_this_object.reset(old_throw_location.GetThis());
729 old_throw_method.reset(old_throw_location.GetMethod());
730 old_exception.reset(old_exception_obj);
731 old_throw_dex_pc = old_throw_location.GetDexPc();
732 soa.Self()->ClearException();
733 }
734 ScopedLocalRef<jthrowable> exception(env, soa.AddLocalReference<jthrowable>(old_exception.get()));
Elliott Hughes72025e52011-08-23 17:50:30 -0700735 ScopedLocalRef<jclass> exception_class(env, env->GetObjectClass(exception.get()));
736 jmethodID mid = env->GetMethodID(exception_class.get(), "printStackTrace", "()V");
737 if (mid == NULL) {
738 LOG(WARNING) << "JNI WARNING: no printStackTrace()V in "
Ian Rogers62d6c772013-02-27 08:32:07 -0800739 << PrettyTypeOf(old_exception.get());
Elliott Hughes72025e52011-08-23 17:50:30 -0700740 } else {
741 env->CallVoidMethod(exception.get(), mid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800742 if (soa.Self()->IsExceptionPending()) {
743 LOG(WARNING) << "JNI WARNING: " << PrettyTypeOf(soa.Self()->GetException(NULL))
Elliott Hughes72025e52011-08-23 17:50:30 -0700744 << " thrown while calling printStackTrace";
Ian Rogers62d6c772013-02-27 08:32:07 -0800745 soa.Self()->ClearException();
Elliott Hughes72025e52011-08-23 17:50:30 -0700746 }
747 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800748 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
749 old_throw_dex_pc);
Elliott Hughes72025e52011-08-23 17:50:30 -0700750
Ian Rogers62d6c772013-02-27 08:32:07 -0800751 soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
Elliott Hughescdf53122011-08-19 15:46:09 -0700752 }
Carl Shapiroea4dca82011-08-01 13:45:38 -0700753
Elliott Hughescdf53122011-08-19 15:46:09 -0700754 static jthrowable ExceptionOccurred(JNIEnv* env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700755 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800756 Object* exception = soa.Self()->GetException(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700757 return soa.AddLocalReference<jthrowable>(exception);
Elliott Hughescdf53122011-08-19 15:46:09 -0700758 }
759
Ian Rogers25e8b912012-09-07 11:31:36 -0700760 static void FatalError(JNIEnv*, const char* msg) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700761 LOG(FATAL) << "JNI FatalError called: " << msg;
762 }
763
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700764 static jint PushLocalFrame(JNIEnv* env, jint capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800765 if (EnsureLocalCapacity(env, capacity, "PushLocalFrame") != JNI_OK) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700766 return JNI_ERR;
767 }
Ian Rogersef28b142012-11-30 14:22:18 -0800768 static_cast<JNIEnvExt*>(env)->PushFrame(capacity);
Elliott Hughescdf53122011-08-19 15:46:09 -0700769 return JNI_OK;
770 }
771
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700772 static jobject PopLocalFrame(JNIEnv* env, jobject java_survivor) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700773 ScopedObjectAccess soa(env);
774 Object* survivor = soa.Decode<Object*>(java_survivor);
775 soa.Env()->PopFrame();
776 return soa.AddLocalReference<jobject>(survivor);
Elliott Hughescdf53122011-08-19 15:46:09 -0700777 }
778
Elliott Hughes2ced6a52011-10-16 18:44:48 -0700779 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity) {
Ian Rogersef28b142012-11-30 14:22:18 -0800780 return EnsureLocalCapacity(env, desired_capacity, "EnsureLocalCapacity");
Elliott Hughes72025e52011-08-23 17:50:30 -0700781 }
782
Elliott Hughescdf53122011-08-19 15:46:09 -0700783 static jobject NewGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700784 if (obj == NULL) {
785 return NULL;
786 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700787 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700789 IndirectReferenceTable& globals = vm->globals;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790 Object* decoded_obj = soa.Decode<Object*>(obj);
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 MutexLock mu(soa.Self(), vm->globals_lock);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700792 IndirectRef ref = globals.Add(IRT_FIRST_SEGMENT, decoded_obj);
Elliott Hughescdf53122011-08-19 15:46:09 -0700793 return reinterpret_cast<jobject>(ref);
794 }
795
796 static void DeleteGlobalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700797 if (obj == NULL) {
798 return;
799 }
Ian Rogersef28b142012-11-30 14:22:18 -0800800 JavaVMExt* vm = reinterpret_cast<JNIEnvExt*>(env)->vm;
Elliott Hughescdf53122011-08-19 15:46:09 -0700801 IndirectReferenceTable& globals = vm->globals;
Ian Rogersef28b142012-11-30 14:22:18 -0800802 Thread* self = reinterpret_cast<JNIEnvExt*>(env)->self;
803 MutexLock mu(self, vm->globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700804
805 if (!globals.Remove(IRT_FIRST_SEGMENT, obj)) {
806 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
807 << "failed to find entry";
808 }
809 }
810
811 static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 ScopedObjectAccess soa(env);
813 return AddWeakGlobalReference(soa, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700814 }
815
816 static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700817 if (obj == NULL) {
818 return;
819 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700820 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700821 JavaVMExt* vm = soa.Vm();
Elliott Hughescdf53122011-08-19 15:46:09 -0700822 IndirectReferenceTable& weak_globals = vm->weak_globals;
Ian Rogers50b35e22012-10-04 10:09:15 -0700823 MutexLock mu(soa.Self(), vm->weak_globals_lock);
Elliott Hughescdf53122011-08-19 15:46:09 -0700824
825 if (!weak_globals.Remove(IRT_FIRST_SEGMENT, obj)) {
826 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
827 << "failed to find entry";
828 }
829 }
830
831 static jobject NewLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700832 if (obj == NULL) {
833 return NULL;
834 }
Ian Rogers25e8b912012-09-07 11:31:36 -0700835 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 IndirectReferenceTable& locals = soa.Env()->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700837
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 uint32_t cookie = soa.Env()->local_ref_cookie;
839 IndirectRef ref = locals.Add(cookie, soa.Decode<Object*>(obj));
Elliott Hughescdf53122011-08-19 15:46:09 -0700840 return reinterpret_cast<jobject>(ref);
841 }
842
843 static void DeleteLocalRef(JNIEnv* env, jobject obj) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700844 if (obj == NULL) {
845 return;
846 }
Ian Rogersef28b142012-11-30 14:22:18 -0800847 IndirectReferenceTable& locals = reinterpret_cast<JNIEnvExt*>(env)->locals;
Elliott Hughescdf53122011-08-19 15:46:09 -0700848
Ian Rogersef28b142012-11-30 14:22:18 -0800849 uint32_t cookie = reinterpret_cast<JNIEnvExt*>(env)->local_ref_cookie;
Elliott Hughescdf53122011-08-19 15:46:09 -0700850 if (!locals.Remove(cookie, obj)) {
851 // Attempting to delete a local reference that is not in the
852 // topmost local reference frame is a no-op. DeleteLocalRef returns
853 // void and doesn't throw any exceptions, but we should probably
854 // complain about it so the user will notice that things aren't
855 // going quite the way they expect.
856 LOG(WARNING) << "JNI WARNING: DeleteLocalRef(" << obj << ") "
857 << "failed to find entry";
858 }
859 }
860
861 static jboolean IsSameObject(JNIEnv* env, jobject obj1, jobject obj2) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 ScopedObjectAccess soa(env);
Ian Rogers120f1c72012-09-28 17:17:10 -0700863 return (soa.Decode<Object*>(obj1) == soa.Decode<Object*>(obj2)) ? JNI_TRUE : JNI_FALSE;
Elliott Hughescdf53122011-08-19 15:46:09 -0700864 }
865
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700866 static jobject AllocObject(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700867 ScopedObjectAccess soa(env);
868 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700869 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700870 return NULL;
871 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700872 return soa.AddLocalReference<jobject>(c->AllocObject(soa.Self()));
Elliott Hughescdf53122011-08-19 15:46:09 -0700873 }
874
Elliott Hughese84278b2012-03-22 10:06:53 -0700875 static jobject NewObject(JNIEnv* env, jclass c, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700876 va_list args;
Elliott Hughes72025e52011-08-23 17:50:30 -0700877 va_start(args, mid);
Elliott Hughese84278b2012-03-22 10:06:53 -0700878 jobject result = NewObjectV(env, c, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -0700879 va_end(args);
880 return result;
881 }
882
Elliott Hughes72025e52011-08-23 17:50:30 -0700883 static jobject NewObjectV(JNIEnv* env, jclass java_class, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700884 ScopedObjectAccess soa(env);
885 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700886 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700887 return NULL;
888 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700889 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700890 if (result == NULL) {
891 return NULL;
892 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700893 jobject local_result = soa.AddLocalReference<jobject>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700894 CallNonvirtualVoidMethodV(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700895 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700896 return local_result;
897 } else {
898 return NULL;
899 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700900 }
901
Elliott Hughes72025e52011-08-23 17:50:30 -0700902 static jobject NewObjectA(JNIEnv* env, jclass java_class, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700903 ScopedObjectAccess soa(env);
904 Class* c = soa.Decode<Class*>(java_class);
Ian Rogers0045a292012-03-31 21:08:41 -0700905 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes885c3bd2011-08-22 16:59:20 -0700906 return NULL;
907 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700908 Object* result = c->AllocObject(soa.Self());
Elliott Hughes30646832011-10-13 16:59:46 -0700909 if (result == NULL) {
910 return NULL;
911 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700912 jobject local_result = soa.AddLocalReference<jobjectArray>(result);
Elliott Hughes72025e52011-08-23 17:50:30 -0700913 CallNonvirtualVoidMethodA(env, local_result, java_class, mid, args);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700914 if (!soa.Self()->IsExceptionPending()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700915 return local_result;
916 } else {
917 return NULL;
918 }
Elliott Hughescdf53122011-08-19 15:46:09 -0700919 }
920
Elliott Hughescdf53122011-08-19 15:46:09 -0700921 static jmethodID GetMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700922 ScopedObjectAccess soa(env);
923 return FindMethodID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -0700924 }
925
926 static jmethodID GetStaticMethodID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700927 ScopedObjectAccess soa(env);
928 return FindMethodID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -0700929 }
930
Elliott Hughes72025e52011-08-23 17:50:30 -0700931 static jobject CallObjectMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700932 va_list ap;
933 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700934 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700935 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700936 va_end(ap);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700937 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700938 }
939
Elliott Hughes72025e52011-08-23 17:50:30 -0700940 static jobject CallObjectMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700941 ScopedObjectAccess soa(env);
942 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args));
943 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700944 }
945
Elliott Hughes72025e52011-08-23 17:50:30 -0700946 static jobject CallObjectMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700947 ScopedObjectAccess soa(env);
948 JValue result(InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args));
949 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -0700950 }
951
Elliott Hughes72025e52011-08-23 17:50:30 -0700952 static jboolean CallBooleanMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700953 va_list ap;
954 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700955 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700956 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700957 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700958 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700959 }
960
Elliott Hughes72025e52011-08-23 17:50:30 -0700961 static jboolean CallBooleanMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700962 ScopedObjectAccess soa(env);
963 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700964 }
965
Elliott Hughes72025e52011-08-23 17:50:30 -0700966 static jboolean CallBooleanMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700967 ScopedObjectAccess soa(env);
968 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 }
970
Elliott Hughes72025e52011-08-23 17:50:30 -0700971 static jbyte CallByteMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700972 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -0700973 va_list ap;
974 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700975 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700976 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700977 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700978 }
979
Elliott Hughes72025e52011-08-23 17:50:30 -0700980 static jbyte CallByteMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700981 ScopedObjectAccess soa(env);
982 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700983 }
984
Elliott Hughes72025e52011-08-23 17:50:30 -0700985 static jbyte CallByteMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 ScopedObjectAccess soa(env);
987 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 }
989
Elliott Hughes72025e52011-08-23 17:50:30 -0700990 static jchar CallCharMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -0700991 va_list ap;
992 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -0700993 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -0700995 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700996 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -0700997 }
998
Elliott Hughes72025e52011-08-23 17:50:30 -0700999 static jchar CallCharMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001000 ScopedObjectAccess soa(env);
1001 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 }
1003
Elliott Hughes72025e52011-08-23 17:50:30 -07001004 static jchar CallCharMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 ScopedObjectAccess soa(env);
1006 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001007 }
1008
Elliott Hughes72025e52011-08-23 17:50:30 -07001009 static jdouble CallDoubleMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001010 va_list ap;
1011 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001012 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001014 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001015 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001016 }
1017
Elliott Hughes72025e52011-08-23 17:50:30 -07001018 static jdouble CallDoubleMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001019 ScopedObjectAccess soa(env);
1020 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001021 }
1022
Elliott Hughes72025e52011-08-23 17:50:30 -07001023 static jdouble CallDoubleMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 ScopedObjectAccess soa(env);
1025 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001026 }
1027
Elliott Hughes72025e52011-08-23 17:50:30 -07001028 static jfloat CallFloatMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001029 ScopedObjectAccess soa(env);
Elliott Hughes72025e52011-08-23 17:50:30 -07001030 va_list ap;
1031 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001032 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001033 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001034 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001035 }
1036
Elliott Hughes72025e52011-08-23 17:50:30 -07001037 static jfloat CallFloatMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038 ScopedObjectAccess soa(env);
1039 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001040 }
1041
Elliott Hughes72025e52011-08-23 17:50:30 -07001042 static jfloat CallFloatMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001043 ScopedObjectAccess soa(env);
1044 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001045 }
1046
Elliott Hughes72025e52011-08-23 17:50:30 -07001047 static jint CallIntMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001048 va_list ap;
1049 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001050 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001052 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001053 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001054 }
1055
Elliott Hughes72025e52011-08-23 17:50:30 -07001056 static jint CallIntMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001057 ScopedObjectAccess soa(env);
1058 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001059 }
1060
Elliott Hughes72025e52011-08-23 17:50:30 -07001061 static jint CallIntMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 ScopedObjectAccess soa(env);
1063 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001064 }
1065
Elliott Hughes72025e52011-08-23 17:50:30 -07001066 static jlong CallLongMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001067 va_list ap;
1068 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001069 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001071 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001072 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001073 }
1074
Elliott Hughes72025e52011-08-23 17:50:30 -07001075 static jlong CallLongMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076 ScopedObjectAccess soa(env);
1077 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001078 }
1079
Elliott Hughes72025e52011-08-23 17:50:30 -07001080 static jlong CallLongMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001081 ScopedObjectAccess soa(env);
1082 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001083 }
1084
Elliott Hughes72025e52011-08-23 17:50:30 -07001085 static jshort CallShortMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001086 va_list ap;
1087 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001088 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001089 JValue result(InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap));
Elliott Hughes72025e52011-08-23 17:50:30 -07001090 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001091 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 }
1093
Elliott Hughes72025e52011-08-23 17:50:30 -07001094 static jshort CallShortMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001095 ScopedObjectAccess soa(env);
1096 return InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001097 }
1098
Elliott Hughes72025e52011-08-23 17:50:30 -07001099 static jshort CallShortMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100 ScopedObjectAccess soa(env);
1101 return InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001102 }
1103
Elliott Hughes72025e52011-08-23 17:50:30 -07001104 static void CallVoidMethod(JNIEnv* env, jobject obj, jmethodID mid, ...) {
Elliott Hughes72025e52011-08-23 17:50:30 -07001105 va_list ap;
1106 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001107 ScopedObjectAccess soa(env);
Ian Rogers1b09b092012-08-20 15:35:52 -07001108 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, ap);
Elliott Hughes72025e52011-08-23 17:50:30 -07001109 va_end(ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001110 }
1111
Elliott Hughes72025e52011-08-23 17:50:30 -07001112 static void CallVoidMethodV(JNIEnv* env, jobject obj, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001113 ScopedObjectAccess soa(env);
1114 InvokeVirtualOrInterfaceWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 }
1116
Elliott Hughes72025e52011-08-23 17:50:30 -07001117 static void CallVoidMethodA(JNIEnv* env, jobject obj, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118 ScopedObjectAccess soa(env);
1119 InvokeVirtualOrInterfaceWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001120 }
1121
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001122 static jobject CallNonvirtualObjectMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001123 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001124 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001125 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
1127 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001128 va_end(ap);
1129 return local_result;
1130 }
1131
1132 static jobject CallNonvirtualObjectMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001133 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 ScopedObjectAccess soa(env);
1135 JValue result(InvokeWithVarArgs(soa, obj, mid, args));
1136 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001137 }
1138
1139 static jobject CallNonvirtualObjectMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001140 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141 ScopedObjectAccess soa(env);
1142 JValue result(InvokeWithJValues(soa, obj, mid, args));
1143 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001144 }
1145
1146 static jboolean CallNonvirtualBooleanMethod(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001147 jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001148 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001150 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001152 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001153 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001154 }
1155
1156 static jboolean CallNonvirtualBooleanMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001157 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001158 ScopedObjectAccess soa(env);
1159 return InvokeWithVarArgs(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001160 }
1161
1162 static jboolean CallNonvirtualBooleanMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001163 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001164 ScopedObjectAccess soa(env);
1165 return InvokeWithJValues(soa, obj, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001166 }
1167
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001168 static jbyte CallNonvirtualByteMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001169 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001170 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001171 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001172 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001173 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001174 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001175 }
1176
1177 static jbyte CallNonvirtualByteMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001178 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 ScopedObjectAccess soa(env);
1180 return InvokeWithVarArgs(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001181 }
1182
1183 static jbyte CallNonvirtualByteMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001184 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 ScopedObjectAccess soa(env);
1186 return InvokeWithJValues(soa, obj, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 }
1188
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001189 static jchar CallNonvirtualCharMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001190 ScopedObjectAccess soa(env);
Elliott Hughescdf53122011-08-19 15:46:09 -07001191 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001192 va_start(ap, mid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001193 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001194 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001195 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001196 }
1197
1198 static jchar CallNonvirtualCharMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001199 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001200 ScopedObjectAccess soa(env);
1201 return InvokeWithVarArgs(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001202 }
1203
1204 static jchar CallNonvirtualCharMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001205 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001206 ScopedObjectAccess soa(env);
1207 return InvokeWithJValues(soa, obj, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 }
1209
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001210 static jshort CallNonvirtualShortMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001212 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001213 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001214 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001215 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001216 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001217 }
1218
1219 static jshort CallNonvirtualShortMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001220 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 ScopedObjectAccess soa(env);
1222 return InvokeWithVarArgs(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 }
1224
1225 static jshort CallNonvirtualShortMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001226 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001227 ScopedObjectAccess soa(env);
1228 return InvokeWithJValues(soa, obj, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001229 }
1230
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001231 static jint CallNonvirtualIntMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001232 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001233 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001234 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001235 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001236 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001237 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001238 }
1239
1240 static jint CallNonvirtualIntMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001241 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001242 ScopedObjectAccess soa(env);
1243 return InvokeWithVarArgs(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001244 }
1245
1246 static jint CallNonvirtualIntMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001247 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001248 ScopedObjectAccess soa(env);
1249 return InvokeWithJValues(soa, obj, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 }
1251
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001252 static jlong CallNonvirtualLongMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001253 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001254 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001255 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001256 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001257 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001258 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001259 }
1260
1261 static jlong CallNonvirtualLongMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001262 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001263 ScopedObjectAccess soa(env);
1264 return InvokeWithVarArgs(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001265 }
1266
1267 static jlong CallNonvirtualLongMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001268 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001269 ScopedObjectAccess soa(env);
1270 return InvokeWithJValues(soa, obj, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001271 }
1272
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001273 static jfloat CallNonvirtualFloatMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001274 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001275 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001276 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001277 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001278 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001279 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001280 }
1281
1282 static jfloat CallNonvirtualFloatMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001283 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001284 ScopedObjectAccess soa(env);
1285 return InvokeWithVarArgs(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001286 }
1287
1288 static jfloat CallNonvirtualFloatMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001289 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001290 ScopedObjectAccess soa(env);
1291 return InvokeWithJValues(soa, obj, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001292 }
1293
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001294 static jdouble CallNonvirtualDoubleMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001295 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001296 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001297 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001298 JValue result(InvokeWithVarArgs(soa, obj, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001299 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001300 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001301 }
1302
1303 static jdouble CallNonvirtualDoubleMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001304 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001305 ScopedObjectAccess soa(env);
1306 return InvokeWithVarArgs(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001307 }
1308
1309 static jdouble CallNonvirtualDoubleMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001310 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001311 ScopedObjectAccess soa(env);
1312 return InvokeWithJValues(soa, obj, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001313 }
1314
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001315 static void CallNonvirtualVoidMethod(JNIEnv* env, jobject obj, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001316 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001317 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001318 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001319 InvokeWithVarArgs(soa, obj, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001320 va_end(ap);
1321 }
1322
1323 static void CallNonvirtualVoidMethodV(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001324 jobject obj, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001325 ScopedObjectAccess soa(env);
1326 InvokeWithVarArgs(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001327 }
1328
1329 static void CallNonvirtualVoidMethodA(JNIEnv* env,
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001330 jobject obj, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 ScopedObjectAccess soa(env);
1332 InvokeWithJValues(soa, obj, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001333 }
1334
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001335 static jfieldID GetFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001336 ScopedObjectAccess soa(env);
1337 return FindFieldID(soa, c, name, sig, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07001338 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001339
1340
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001341 static jfieldID GetStaticFieldID(JNIEnv* env, jclass c, const char* name, const char* sig) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001342 ScopedObjectAccess soa(env);
1343 return FindFieldID(soa, c, name, sig, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07001344 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001345
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001346 static jobject GetObjectField(JNIEnv* env, jobject obj, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001347 ScopedObjectAccess soa(env);
1348 Object* o = soa.Decode<Object*>(obj);
1349 Field* f = soa.DecodeField(fid);
1350 return soa.AddLocalReference<jobject>(f->GetObject(o));
Elliott Hughescdf53122011-08-19 15:46:09 -07001351 }
Carl Shapiroea4dca82011-08-01 13:45:38 -07001352
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001353 static jobject GetStaticObjectField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001354 ScopedObjectAccess soa(env);
1355 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001356 return soa.AddLocalReference<jobject>(f->GetObject(f->GetDeclaringClass()));
Elliott Hughescdf53122011-08-19 15:46:09 -07001357 }
1358
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001359 static void SetObjectField(JNIEnv* env, jobject java_object, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001360 ScopedObjectAccess soa(env);
1361 Object* o = soa.Decode<Object*>(java_object);
1362 Object* v = soa.Decode<Object*>(java_value);
1363 Field* f = soa.DecodeField(fid);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001364 f->SetObject(o, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001365 }
1366
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001367 static void SetStaticObjectField(JNIEnv* env, jclass, jfieldID fid, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001368 ScopedObjectAccess soa(env);
1369 Object* v = soa.Decode<Object*>(java_value);
1370 Field* f = soa.DecodeField(fid);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001371 f->SetObject(f->GetDeclaringClass(), v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001372 }
1373
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001374#define GET_PRIMITIVE_FIELD(fn, instance) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001375 ScopedObjectAccess soa(env); \
1376 Object* o = soa.Decode<Object*>(instance); \
1377 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001378 return f->fn(o)
1379
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001380#define GET_STATIC_PRIMITIVE_FIELD(fn) \
1381 ScopedObjectAccess soa(env); \
1382 Field* f = soa.DecodeField(fid); \
1383 return f->fn(f->GetDeclaringClass())
1384
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001385#define SET_PRIMITIVE_FIELD(fn, instance, value) \
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001386 ScopedObjectAccess soa(env); \
1387 Object* o = soa.Decode<Object*>(instance); \
1388 Field* f = soa.DecodeField(fid); \
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001389 f->fn(o, value)
1390
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001391#define SET_STATIC_PRIMITIVE_FIELD(fn, value) \
1392 ScopedObjectAccess soa(env); \
1393 Field* f = soa.DecodeField(fid); \
1394 f->fn(f->GetDeclaringClass(), value)
1395
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001396 static jboolean GetBooleanField(JNIEnv* env, jobject obj, jfieldID fid) {
1397 GET_PRIMITIVE_FIELD(GetBoolean, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001398 }
1399
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001400 static jbyte GetByteField(JNIEnv* env, jobject obj, jfieldID fid) {
1401 GET_PRIMITIVE_FIELD(GetByte, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001402 }
1403
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001404 static jchar GetCharField(JNIEnv* env, jobject obj, jfieldID fid) {
1405 GET_PRIMITIVE_FIELD(GetChar, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001406 }
1407
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001408 static jshort GetShortField(JNIEnv* env, jobject obj, jfieldID fid) {
1409 GET_PRIMITIVE_FIELD(GetShort, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001410 }
1411
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001412 static jint GetIntField(JNIEnv* env, jobject obj, jfieldID fid) {
1413 GET_PRIMITIVE_FIELD(GetInt, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001414 }
1415
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001416 static jlong GetLongField(JNIEnv* env, jobject obj, jfieldID fid) {
1417 GET_PRIMITIVE_FIELD(GetLong, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001418 }
1419
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001420 static jfloat GetFloatField(JNIEnv* env, jobject obj, jfieldID fid) {
1421 GET_PRIMITIVE_FIELD(GetFloat, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001422 }
1423
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001424 static jdouble GetDoubleField(JNIEnv* env, jobject obj, jfieldID fid) {
1425 GET_PRIMITIVE_FIELD(GetDouble, obj);
Elliott Hughescdf53122011-08-19 15:46:09 -07001426 }
1427
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001428 static jboolean GetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001429 GET_STATIC_PRIMITIVE_FIELD(GetBoolean);
Elliott Hughescdf53122011-08-19 15:46:09 -07001430 }
1431
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001432 static jbyte GetStaticByteField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001433 GET_STATIC_PRIMITIVE_FIELD(GetByte);
Elliott Hughescdf53122011-08-19 15:46:09 -07001434 }
1435
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001436 static jchar GetStaticCharField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001437 GET_STATIC_PRIMITIVE_FIELD(GetChar);
Elliott Hughescdf53122011-08-19 15:46:09 -07001438 }
1439
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001440 static jshort GetStaticShortField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 GET_STATIC_PRIMITIVE_FIELD(GetShort);
Elliott Hughescdf53122011-08-19 15:46:09 -07001442 }
1443
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001444 static jint GetStaticIntField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001445 GET_STATIC_PRIMITIVE_FIELD(GetInt);
Elliott Hughescdf53122011-08-19 15:46:09 -07001446 }
1447
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001448 static jlong GetStaticLongField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001449 GET_STATIC_PRIMITIVE_FIELD(GetLong);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001450 }
1451
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001452 static jfloat GetStaticFloatField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001453 GET_STATIC_PRIMITIVE_FIELD(GetFloat);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001454 }
1455
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001456 static jdouble GetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001457 GET_STATIC_PRIMITIVE_FIELD(GetDouble);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001458 }
1459
1460 static void SetBooleanField(JNIEnv* env, jobject obj, jfieldID fid, jboolean v) {
1461 SET_PRIMITIVE_FIELD(SetBoolean, obj, v);
1462 }
1463
1464 static void SetByteField(JNIEnv* env, jobject obj, jfieldID fid, jbyte v) {
1465 SET_PRIMITIVE_FIELD(SetByte, obj, v);
1466 }
1467
1468 static void SetCharField(JNIEnv* env, jobject obj, jfieldID fid, jchar v) {
1469 SET_PRIMITIVE_FIELD(SetChar, obj, v);
1470 }
1471
1472 static void SetFloatField(JNIEnv* env, jobject obj, jfieldID fid, jfloat v) {
1473 SET_PRIMITIVE_FIELD(SetFloat, obj, v);
1474 }
1475
1476 static void SetDoubleField(JNIEnv* env, jobject obj, jfieldID fid, jdouble v) {
1477 SET_PRIMITIVE_FIELD(SetDouble, obj, v);
1478 }
1479
1480 static void SetIntField(JNIEnv* env, jobject obj, jfieldID fid, jint v) {
1481 SET_PRIMITIVE_FIELD(SetInt, obj, v);
1482 }
1483
1484 static void SetLongField(JNIEnv* env, jobject obj, jfieldID fid, jlong v) {
1485 SET_PRIMITIVE_FIELD(SetLong, obj, v);
1486 }
1487
1488 static void SetShortField(JNIEnv* env, jobject obj, jfieldID fid, jshort v) {
1489 SET_PRIMITIVE_FIELD(SetShort, obj, v);
1490 }
1491
1492 static void SetStaticBooleanField(JNIEnv* env, jclass, jfieldID fid, jboolean v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001493 SET_STATIC_PRIMITIVE_FIELD(SetBoolean, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001494 }
1495
1496 static void SetStaticByteField(JNIEnv* env, jclass, jfieldID fid, jbyte v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001497 SET_STATIC_PRIMITIVE_FIELD(SetByte, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001498 }
1499
1500 static void SetStaticCharField(JNIEnv* env, jclass, jfieldID fid, jchar v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001501 SET_STATIC_PRIMITIVE_FIELD(SetChar, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001502 }
1503
1504 static void SetStaticFloatField(JNIEnv* env, jclass, jfieldID fid, jfloat v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001505 SET_STATIC_PRIMITIVE_FIELD(SetFloat, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001506 }
1507
1508 static void SetStaticDoubleField(JNIEnv* env, jclass, jfieldID fid, jdouble v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001509 SET_STATIC_PRIMITIVE_FIELD(SetDouble, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001510 }
1511
1512 static void SetStaticIntField(JNIEnv* env, jclass, jfieldID fid, jint v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001513 SET_STATIC_PRIMITIVE_FIELD(SetInt, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001514 }
1515
1516 static void SetStaticLongField(JNIEnv* env, jclass, jfieldID fid, jlong v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001517 SET_STATIC_PRIMITIVE_FIELD(SetLong, v);
Elliott Hughes885c3bd2011-08-22 16:59:20 -07001518 }
1519
1520 static void SetStaticShortField(JNIEnv* env, jclass, jfieldID fid, jshort v) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001521 SET_STATIC_PRIMITIVE_FIELD(SetShort, v);
Elliott Hughescdf53122011-08-19 15:46:09 -07001522 }
1523
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001524 static jobject CallStaticObjectMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001525 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001526 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001527 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001528 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
1529 jobject local_result = soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001530 va_end(ap);
1531 return local_result;
1532 }
1533
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001534 static jobject CallStaticObjectMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001535 ScopedObjectAccess soa(env);
1536 JValue result(InvokeWithVarArgs(soa, NULL, mid, args));
1537 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001538 }
1539
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001540 static jobject CallStaticObjectMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001541 ScopedObjectAccess soa(env);
1542 JValue result(InvokeWithJValues(soa, NULL, mid, args));
1543 return soa.AddLocalReference<jobject>(result.GetL());
Elliott Hughescdf53122011-08-19 15:46:09 -07001544 }
1545
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001546 static jboolean CallStaticBooleanMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001547 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001548 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001549 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001550 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001551 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001552 return result.GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001553 }
1554
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001555 static jboolean CallStaticBooleanMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001556 ScopedObjectAccess soa(env);
1557 return InvokeWithVarArgs(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001558 }
1559
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001560 static jboolean CallStaticBooleanMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001561 ScopedObjectAccess soa(env);
1562 return InvokeWithJValues(soa, NULL, mid, args).GetZ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001563 }
1564
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001565 static jbyte CallStaticByteMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001566 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001567 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001568 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001569 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001570 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001571 return result.GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001572 }
1573
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001574 static jbyte CallStaticByteMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001575 ScopedObjectAccess soa(env);
1576 return InvokeWithVarArgs(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001577 }
1578
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001579 static jbyte CallStaticByteMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001580 ScopedObjectAccess soa(env);
1581 return InvokeWithJValues(soa, NULL, mid, args).GetB();
Elliott Hughescdf53122011-08-19 15:46:09 -07001582 }
1583
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001584 static jchar CallStaticCharMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001585 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001586 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001587 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001588 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001589 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001590 return result.GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001591 }
1592
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001593 static jchar CallStaticCharMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001594 ScopedObjectAccess soa(env);
1595 return InvokeWithVarArgs(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001596 }
1597
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001598 static jchar CallStaticCharMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001599 ScopedObjectAccess soa(env);
1600 return InvokeWithJValues(soa, NULL, mid, args).GetC();
Elliott Hughescdf53122011-08-19 15:46:09 -07001601 }
1602
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001603 static jshort CallStaticShortMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001604 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001606 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001607 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001608 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001609 return result.GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001610 }
1611
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001612 static jshort CallStaticShortMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001613 ScopedObjectAccess soa(env);
1614 return InvokeWithVarArgs(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001615 }
1616
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001617 static jshort CallStaticShortMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001618 ScopedObjectAccess soa(env);
1619 return InvokeWithJValues(soa, NULL, mid, args).GetS();
Elliott Hughescdf53122011-08-19 15:46:09 -07001620 }
1621
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001622 static jint CallStaticIntMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001623 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001624 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001625 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001626 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001627 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001628 return result.GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001629 }
1630
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001631 static jint CallStaticIntMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001632 ScopedObjectAccess soa(env);
1633 return InvokeWithVarArgs(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001634 }
1635
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001636 static jint CallStaticIntMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001637 ScopedObjectAccess soa(env);
1638 return InvokeWithJValues(soa, NULL, mid, args).GetI();
Elliott Hughescdf53122011-08-19 15:46:09 -07001639 }
1640
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001641 static jlong CallStaticLongMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001642 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001643 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001644 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001645 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001646 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001647 return result.GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001648 }
1649
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001650 static jlong CallStaticLongMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001651 ScopedObjectAccess soa(env);
1652 return InvokeWithVarArgs(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001653 }
1654
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001655 static jlong CallStaticLongMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001656 ScopedObjectAccess soa(env);
1657 return InvokeWithJValues(soa, NULL, mid, args).GetJ();
Elliott Hughescdf53122011-08-19 15:46:09 -07001658 }
1659
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001660 static jfloat CallStaticFloatMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001661 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001662 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001663 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001665 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001666 return result.GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001667 }
1668
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001669 static jfloat CallStaticFloatMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 ScopedObjectAccess soa(env);
1671 return InvokeWithVarArgs(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001672 }
1673
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001674 static jfloat CallStaticFloatMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001675 ScopedObjectAccess soa(env);
1676 return InvokeWithJValues(soa, NULL, mid, args).GetF();
Elliott Hughescdf53122011-08-19 15:46:09 -07001677 }
1678
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001679 static jdouble CallStaticDoubleMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001680 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001681 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001682 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001683 JValue result(InvokeWithVarArgs(soa, NULL, mid, ap));
Elliott Hughescdf53122011-08-19 15:46:09 -07001684 va_end(ap);
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001685 return result.GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001686 }
1687
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001688 static jdouble CallStaticDoubleMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001689 ScopedObjectAccess soa(env);
1690 return InvokeWithVarArgs(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001691 }
1692
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001693 static jdouble CallStaticDoubleMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001694 ScopedObjectAccess soa(env);
1695 return InvokeWithJValues(soa, NULL, mid, args).GetD();
Elliott Hughescdf53122011-08-19 15:46:09 -07001696 }
1697
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001698 static void CallStaticVoidMethod(JNIEnv* env, jclass, jmethodID mid, ...) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001699 va_list ap;
Elliott Hughes72025e52011-08-23 17:50:30 -07001700 va_start(ap, mid);
Ian Rogers25e8b912012-09-07 11:31:36 -07001701 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001702 InvokeWithVarArgs(soa, NULL, mid, ap);
Elliott Hughescdf53122011-08-19 15:46:09 -07001703 va_end(ap);
1704 }
1705
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001706 static void CallStaticVoidMethodV(JNIEnv* env, jclass, jmethodID mid, va_list args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001707 ScopedObjectAccess soa(env);
1708 InvokeWithVarArgs(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001709 }
1710
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001711 static void CallStaticVoidMethodA(JNIEnv* env, jclass, jmethodID mid, jvalue* args) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001712 ScopedObjectAccess soa(env);
1713 InvokeWithJValues(soa, NULL, mid, args);
Elliott Hughescdf53122011-08-19 15:46:09 -07001714 }
1715
Elliott Hughes814e4032011-08-23 12:07:56 -07001716 static jstring NewString(JNIEnv* env, const jchar* chars, jsize char_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001717 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001718 String* result = String::AllocFromUtf16(soa.Self(), char_count, chars);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001719 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001720 }
1721
1722 static jstring NewStringUTF(JNIEnv* env, const char* utf) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001723 if (utf == NULL) {
1724 return NULL;
1725 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001726 ScopedObjectAccess soa(env);
Ian Rogers50b35e22012-10-04 10:09:15 -07001727 String* result = String::AllocFromModifiedUtf8(soa.Self(), utf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001728 return soa.AddLocalReference<jstring>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001729 }
1730
Elliott Hughes814e4032011-08-23 12:07:56 -07001731 static jsize GetStringLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001732 ScopedObjectAccess soa(env);
1733 return soa.Decode<String*>(java_string)->GetLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001734 }
1735
1736 static jsize GetStringUTFLength(JNIEnv* env, jstring java_string) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001737 ScopedObjectAccess soa(env);
1738 return soa.Decode<String*>(java_string)->GetUtfLength();
Elliott Hughes814e4032011-08-23 12:07:56 -07001739 }
1740
Elliott Hughesb465ab02011-08-24 11:21:21 -07001741 static void GetStringRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001742 ScopedObjectAccess soa(env);
1743 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001744 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001746 } else {
1747 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1748 memcpy(buf, chars + start, length * sizeof(jchar));
1749 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001750 }
1751
Elliott Hughesb465ab02011-08-24 11:21:21 -07001752 static void GetStringUTFRegion(JNIEnv* env, jstring java_string, jsize start, jsize length, char* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001753 ScopedObjectAccess soa(env);
1754 String* s = soa.Decode<String*>(java_string);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001755 if (start < 0 || length < 0 || start + length > s->GetLength()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001756 ThrowSIOOBE(soa, start, length, s->GetLength());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001757 } else {
1758 const jchar* chars = s->GetCharArray()->GetData() + s->GetOffset();
1759 ConvertUtf16ToModifiedUtf8(buf, chars + start, length);
1760 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001761 }
1762
Elliott Hughes75770752011-08-24 17:52:38 -07001763 static const jchar* GetStringChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001764 ScopedObjectAccess soa(env);
1765 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001766 const CharArray* chars = s->GetCharArray();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001767 PinPrimitiveArray(soa, chars);
Elliott Hughes75770752011-08-24 17:52:38 -07001768 if (is_copy != NULL) {
1769 *is_copy = JNI_FALSE;
1770 }
1771 return chars->GetData() + s->GetOffset();
Elliott Hughes814e4032011-08-23 12:07:56 -07001772 }
1773
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001774 static void ReleaseStringChars(JNIEnv* env, jstring java_string, const jchar*) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001775 ScopedObjectAccess soa(env);
1776 UnpinPrimitiveArray(soa, soa.Decode<String*>(java_string)->GetCharArray());
Elliott Hughescdf53122011-08-19 15:46:09 -07001777 }
1778
Elliott Hughes75770752011-08-24 17:52:38 -07001779 static const jchar* GetStringCritical(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001780 return GetStringChars(env, java_string, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001781 }
1782
Elliott Hughes75770752011-08-24 17:52:38 -07001783 static void ReleaseStringCritical(JNIEnv* env, jstring java_string, const jchar* chars) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001784 ScopedObjectAccess soa(env);
Elliott Hughes75770752011-08-24 17:52:38 -07001785 return ReleaseStringChars(env, java_string, chars);
Elliott Hughescdf53122011-08-19 15:46:09 -07001786 }
1787
Elliott Hughes75770752011-08-24 17:52:38 -07001788 static const char* GetStringUTFChars(JNIEnv* env, jstring java_string, jboolean* is_copy) {
Elliott Hughes75770752011-08-24 17:52:38 -07001789 if (java_string == NULL) {
1790 return NULL;
1791 }
1792 if (is_copy != NULL) {
1793 *is_copy = JNI_TRUE;
1794 }
Ian Rogersef28b142012-11-30 14:22:18 -08001795 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001796 String* s = soa.Decode<String*>(java_string);
Elliott Hughes75770752011-08-24 17:52:38 -07001797 size_t byte_count = s->GetUtfLength();
1798 char* bytes = new char[byte_count + 1];
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001799 CHECK(bytes != NULL); // bionic aborts anyway.
Elliott Hughes75770752011-08-24 17:52:38 -07001800 const uint16_t* chars = s->GetCharArray()->GetData() + s->GetOffset();
1801 ConvertUtf16ToModifiedUtf8(bytes, chars, s->GetLength());
1802 bytes[byte_count] = '\0';
1803 return bytes;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001804 }
1805
Elliott Hughes75770752011-08-24 17:52:38 -07001806 static void ReleaseStringUTFChars(JNIEnv* env, jstring, const char* chars) {
Elliott Hughes75770752011-08-24 17:52:38 -07001807 delete[] chars;
Elliott Hughesb465ab02011-08-24 11:21:21 -07001808 }
1809
Elliott Hughesbd935992011-08-22 11:59:34 -07001810 static jsize GetArrayLength(JNIEnv* env, jarray java_array) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001811 ScopedObjectAccess soa(env);
1812 Object* obj = soa.Decode<Object*>(java_array);
Elliott Hughes96a98872012-12-19 14:21:15 -08001813 if (!obj->IsArrayInstance()) {
1814 JniAbortF("GetArrayLength", "not an array: %s", PrettyTypeOf(obj).c_str());
1815 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001816 Array* array = obj->AsArray();
1817 return array->GetLength();
Elliott Hughescdf53122011-08-19 15:46:09 -07001818 }
1819
Elliott Hughes814e4032011-08-23 12:07:56 -07001820 static jobject GetObjectArrayElement(JNIEnv* env, jobjectArray java_array, jsize index) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 ScopedObjectAccess soa(env);
1822 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1823 return soa.AddLocalReference<jobject>(array->Get(index));
Elliott Hughescdf53122011-08-19 15:46:09 -07001824 }
1825
1826 static void SetObjectArrayElement(JNIEnv* env,
1827 jobjectArray java_array, jsize index, jobject java_value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001828 ScopedObjectAccess soa(env);
1829 ObjectArray<Object>* array = soa.Decode<ObjectArray<Object>*>(java_array);
1830 Object* value = soa.Decode<Object*>(java_value);
Elliott Hughescdf53122011-08-19 15:46:09 -07001831 array->Set(index, value);
1832 }
1833
1834 static jbooleanArray NewBooleanArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 ScopedObjectAccess soa(env);
1836 return NewPrimitiveArray<jbooleanArray, BooleanArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001837 }
1838
1839 static jbyteArray NewByteArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001840 ScopedObjectAccess soa(env);
1841 return NewPrimitiveArray<jbyteArray, ByteArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001842 }
1843
1844 static jcharArray NewCharArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001845 ScopedObjectAccess soa(env);
1846 return NewPrimitiveArray<jcharArray, CharArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001847 }
1848
1849 static jdoubleArray NewDoubleArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 ScopedObjectAccess soa(env);
1851 return NewPrimitiveArray<jdoubleArray, DoubleArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001852 }
1853
1854 static jfloatArray NewFloatArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001855 ScopedObjectAccess soa(env);
1856 return NewPrimitiveArray<jfloatArray, FloatArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001857 }
1858
1859 static jintArray NewIntArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001860 ScopedObjectAccess soa(env);
1861 return NewPrimitiveArray<jintArray, IntArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001862 }
1863
1864 static jlongArray NewLongArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 ScopedObjectAccess soa(env);
1866 return NewPrimitiveArray<jlongArray, LongArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001867 }
1868
1869 static jobjectArray NewObjectArray(JNIEnv* env, jsize length, jclass element_jclass, jobject initial_element) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001870 ScopedObjectAccess soa(env);
Elliott Hughes96a98872012-12-19 14:21:15 -08001871 if (length < 0) {
1872 JniAbortF("NewObjectArray", "negative array length: %d", length);
1873 }
Elliott Hughescdf53122011-08-19 15:46:09 -07001874
1875 // Compute the array class corresponding to the given element class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001876 Class* element_class = soa.Decode<Class*>(element_jclass);
Elliott Hughescdf53122011-08-19 15:46:09 -07001877 std::string descriptor;
1878 descriptor += "[";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001879 descriptor += ClassHelper(element_class).GetDescriptor();
Elliott Hughescdf53122011-08-19 15:46:09 -07001880
1881 // Find the class.
Elliott Hughes75770752011-08-24 17:52:38 -07001882 ScopedLocalRef<jclass> java_array_class(env, FindClass(env, descriptor.c_str()));
1883 if (java_array_class.get() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001884 return NULL;
1885 }
1886
Elliott Hughes75770752011-08-24 17:52:38 -07001887 // Allocate and initialize if necessary.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 Class* array_class = soa.Decode<Class*>(java_array_class.get());
Ian Rogers50b35e22012-10-04 10:09:15 -07001889 ObjectArray<Object>* result = ObjectArray<Object>::Alloc(soa.Self(), array_class, length);
Elliott Hughes75770752011-08-24 17:52:38 -07001890 if (initial_element != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 Object* initial_object = soa.Decode<Object*>(initial_element);
Elliott Hughes75770752011-08-24 17:52:38 -07001892 for (jsize i = 0; i < length; ++i) {
1893 result->Set(i, initial_object);
1894 }
1895 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001896 return soa.AddLocalReference<jobjectArray>(result);
Elliott Hughescdf53122011-08-19 15:46:09 -07001897 }
1898
1899 static jshortArray NewShortArray(JNIEnv* env, jsize length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 ScopedObjectAccess soa(env);
1901 return NewPrimitiveArray<jshortArray, ShortArray>(soa, length);
Elliott Hughescdf53122011-08-19 15:46:09 -07001902 }
1903
Ian Rogersa15e67d2012-02-28 13:51:55 -08001904 static void* GetPrimitiveArrayCritical(JNIEnv* env, jarray java_array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001905 ScopedObjectAccess soa(env);
1906 Array* array = soa.Decode<Array*>(java_array);
1907 PinPrimitiveArray(soa, array);
Ian Rogersa15e67d2012-02-28 13:51:55 -08001908 if (is_copy != NULL) {
1909 *is_copy = JNI_FALSE;
1910 }
1911 return array->GetRawData(array->GetClass()->GetComponentSize());
Elliott Hughesb465ab02011-08-24 11:21:21 -07001912 }
1913
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001914 static void ReleasePrimitiveArrayCritical(JNIEnv* env, jarray array, void*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001915 ReleasePrimitiveArray(env, array, mode);
Elliott Hughesb465ab02011-08-24 11:21:21 -07001916 }
1917
Elliott Hughes75770752011-08-24 17:52:38 -07001918 static jboolean* GetBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001919 ScopedObjectAccess soa(env);
1920 return GetPrimitiveArray<jbooleanArray, jboolean*, BooleanArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001921 }
1922
Elliott Hughes75770752011-08-24 17:52:38 -07001923 static jbyte* GetByteArrayElements(JNIEnv* env, jbyteArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001924 ScopedObjectAccess soa(env);
1925 return GetPrimitiveArray<jbyteArray, jbyte*, ByteArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001926 }
1927
Elliott Hughes75770752011-08-24 17:52:38 -07001928 static jchar* GetCharArrayElements(JNIEnv* env, jcharArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001929 ScopedObjectAccess soa(env);
1930 return GetPrimitiveArray<jcharArray, jchar*, CharArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001931 }
1932
Elliott Hughes75770752011-08-24 17:52:38 -07001933 static jdouble* GetDoubleArrayElements(JNIEnv* env, jdoubleArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 ScopedObjectAccess soa(env);
1935 return GetPrimitiveArray<jdoubleArray, jdouble*, DoubleArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001936 }
1937
Elliott Hughes75770752011-08-24 17:52:38 -07001938 static jfloat* GetFloatArrayElements(JNIEnv* env, jfloatArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001939 ScopedObjectAccess soa(env);
1940 return GetPrimitiveArray<jfloatArray, jfloat*, FloatArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001941 }
1942
Elliott Hughes75770752011-08-24 17:52:38 -07001943 static jint* GetIntArrayElements(JNIEnv* env, jintArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001944 ScopedObjectAccess soa(env);
1945 return GetPrimitiveArray<jintArray, jint*, IntArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001946 }
1947
Elliott Hughes75770752011-08-24 17:52:38 -07001948 static jlong* GetLongArrayElements(JNIEnv* env, jlongArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001949 ScopedObjectAccess soa(env);
1950 return GetPrimitiveArray<jlongArray, jlong*, LongArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001951 }
1952
Elliott Hughes75770752011-08-24 17:52:38 -07001953 static jshort* GetShortArrayElements(JNIEnv* env, jshortArray array, jboolean* is_copy) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001954 ScopedObjectAccess soa(env);
1955 return GetPrimitiveArray<jshortArray, jshort*, ShortArray>(soa, array, is_copy);
Elliott Hughescdf53122011-08-19 15:46:09 -07001956 }
1957
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001958 static void ReleaseBooleanArrayElements(JNIEnv* env, jbooleanArray array, jboolean*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001959 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001960 }
1961
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001962 static void ReleaseByteArrayElements(JNIEnv* env, jbyteArray array, jbyte*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001963 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001964 }
1965
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001966 static void ReleaseCharArrayElements(JNIEnv* env, jcharArray array, jchar*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001967 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001968 }
1969
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001970 static void ReleaseDoubleArrayElements(JNIEnv* env, jdoubleArray array, jdouble*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001971 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001972 }
1973
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001974 static void ReleaseFloatArrayElements(JNIEnv* env, jfloatArray array, jfloat*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001975 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001976 }
1977
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001978 static void ReleaseIntArrayElements(JNIEnv* env, jintArray array, jint*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001979 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001980 }
1981
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001982 static void ReleaseLongArrayElements(JNIEnv* env, jlongArray array, jlong*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001983 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001984 }
1985
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001986 static void ReleaseShortArrayElements(JNIEnv* env, jshortArray array, jshort*, jint mode) {
Ian Rogersef28b142012-11-30 14:22:18 -08001987 ReleasePrimitiveArray(env, array, mode);
Elliott Hughescdf53122011-08-19 15:46:09 -07001988 }
1989
Elliott Hughes814e4032011-08-23 12:07:56 -07001990 static void GetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001991 ScopedObjectAccess soa(env);
1992 GetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001993 }
1994
Elliott Hughes814e4032011-08-23 12:07:56 -07001995 static void GetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001996 ScopedObjectAccess soa(env);
1997 GetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07001998 }
1999
Elliott Hughes814e4032011-08-23 12:07:56 -07002000 static void GetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002001 ScopedObjectAccess soa(env);
2002 GetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002003 }
2004
Elliott Hughes814e4032011-08-23 12:07:56 -07002005 static void GetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002006 ScopedObjectAccess soa(env);
2007 GetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002008 }
2009
Elliott Hughes814e4032011-08-23 12:07:56 -07002010 static void GetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002011 ScopedObjectAccess soa(env);
2012 GetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002013 }
2014
Elliott Hughes814e4032011-08-23 12:07:56 -07002015 static void GetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002016 ScopedObjectAccess soa(env);
2017 GetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002018 }
2019
Elliott Hughes814e4032011-08-23 12:07:56 -07002020 static void GetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002021 ScopedObjectAccess soa(env);
2022 GetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002023 }
2024
Elliott Hughes814e4032011-08-23 12:07:56 -07002025 static void GetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002026 ScopedObjectAccess soa(env);
2027 GetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002028 }
2029
Elliott Hughes814e4032011-08-23 12:07:56 -07002030 static void SetBooleanArrayRegion(JNIEnv* env, jbooleanArray array, jsize start, jsize length, const jboolean* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002031 ScopedObjectAccess soa(env);
2032 SetPrimitiveArrayRegion<jbooleanArray, jboolean, BooleanArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002033 }
2034
Elliott Hughes814e4032011-08-23 12:07:56 -07002035 static void SetByteArrayRegion(JNIEnv* env, jbyteArray array, jsize start, jsize length, const jbyte* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002036 ScopedObjectAccess soa(env);
2037 SetPrimitiveArrayRegion<jbyteArray, jbyte, ByteArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002038 }
2039
Elliott Hughes814e4032011-08-23 12:07:56 -07002040 static void SetCharArrayRegion(JNIEnv* env, jcharArray array, jsize start, jsize length, const jchar* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002041 ScopedObjectAccess soa(env);
2042 SetPrimitiveArrayRegion<jcharArray, jchar, CharArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002043 }
2044
Elliott Hughes814e4032011-08-23 12:07:56 -07002045 static void SetDoubleArrayRegion(JNIEnv* env, jdoubleArray array, jsize start, jsize length, const jdouble* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002046 ScopedObjectAccess soa(env);
2047 SetPrimitiveArrayRegion<jdoubleArray, jdouble, DoubleArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002048 }
2049
Elliott Hughes814e4032011-08-23 12:07:56 -07002050 static void SetFloatArrayRegion(JNIEnv* env, jfloatArray array, jsize start, jsize length, const jfloat* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002051 ScopedObjectAccess soa(env);
2052 SetPrimitiveArrayRegion<jfloatArray, jfloat, FloatArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002053 }
2054
Elliott Hughes814e4032011-08-23 12:07:56 -07002055 static void SetIntArrayRegion(JNIEnv* env, jintArray array, jsize start, jsize length, const jint* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002056 ScopedObjectAccess soa(env);
2057 SetPrimitiveArrayRegion<jintArray, jint, IntArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002058 }
2059
Elliott Hughes814e4032011-08-23 12:07:56 -07002060 static void SetLongArrayRegion(JNIEnv* env, jlongArray array, jsize start, jsize length, const jlong* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002061 ScopedObjectAccess soa(env);
2062 SetPrimitiveArrayRegion<jlongArray, jlong, LongArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002063 }
2064
Elliott Hughes814e4032011-08-23 12:07:56 -07002065 static void SetShortArrayRegion(JNIEnv* env, jshortArray array, jsize start, jsize length, const jshort* buf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002066 ScopedObjectAccess soa(env);
2067 SetPrimitiveArrayRegion<jshortArray, jshort, ShortArray>(soa, array, start, length, buf);
Elliott Hughescdf53122011-08-19 15:46:09 -07002068 }
2069
Elliott Hughes5174fe62011-08-23 15:12:35 -07002070 static jint RegisterNatives(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, jint method_count) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002071 return RegisterNativeMethods(env, java_class, methods, method_count, true);
2072 }
2073
2074 static jint RegisterNativeMethods(JNIEnv* env, jclass java_class, const JNINativeMethod* methods, size_t method_count, bool return_errors) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002075 ScopedObjectAccess soa(env);
2076 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002077
Elliott Hughesc8fece32013-01-02 11:27:23 -08002078 for (size_t i = 0; i < method_count; ++i) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002079 const char* name = methods[i].name;
2080 const char* sig = methods[i].signature;
2081
2082 if (*sig == '!') {
2083 // TODO: fast jni. it's too noisy to log all these.
2084 ++sig;
2085 }
2086
Mathieu Chartier66f19252012-09-18 08:57:04 -07002087 AbstractMethod* m = c->FindDirectMethod(name, sig);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002088 if (m == NULL) {
2089 m = c->FindVirtualMethod(name, sig);
Elliott Hughescdf53122011-08-19 15:46:09 -07002090 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002091 if (m == NULL) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002092 LOG(return_errors ? ERROR : FATAL) << "Failed to register native method "
2093 << PrettyDescriptor(c) << "." << name << sig;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002094 ThrowNoSuchMethodError(soa, c, name, sig, "static or non-static");
Elliott Hughescdf53122011-08-19 15:46:09 -07002095 return JNI_ERR;
Elliott Hughes5174fe62011-08-23 15:12:35 -07002096 } else if (!m->IsNative()) {
Elliott Hughesc8fece32013-01-02 11:27:23 -08002097 LOG(return_errors ? ERROR : FATAL) << "Failed to register non-native method "
2098 << PrettyDescriptor(c) << "." << name << sig
2099 << " as native";
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002100 ThrowNoSuchMethodError(soa, c, name, sig, "native");
Elliott Hughescdf53122011-08-19 15:46:09 -07002101 return JNI_ERR;
2102 }
Elliott Hughes5174fe62011-08-23 15:12:35 -07002103
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002104 VLOG(jni) << "[Registering JNI native method " << PrettyMethod(m) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002105
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 m->RegisterNative(soa.Self(), methods[i].fnPtr);
Elliott Hughescdf53122011-08-19 15:46:09 -07002107 }
2108 return JNI_OK;
2109 }
2110
Elliott Hughes5174fe62011-08-23 15:12:35 -07002111 static jint UnregisterNatives(JNIEnv* env, jclass java_class) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002112 ScopedObjectAccess soa(env);
2113 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002114
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002115 VLOG(jni) << "[Unregistering JNI native methods for " << PrettyClass(c) << "]";
Elliott Hughes5174fe62011-08-23 15:12:35 -07002116
2117 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002118 AbstractMethod* m = c->GetDirectMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002119 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002120 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002121 }
2122 }
2123 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002124 AbstractMethod* m = c->GetVirtualMethod(i);
Elliott Hughes5174fe62011-08-23 15:12:35 -07002125 if (m->IsNative()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002126 m->UnregisterNative(soa.Self());
Elliott Hughes5174fe62011-08-23 15:12:35 -07002127 }
2128 }
2129
2130 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002131 }
2132
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002133 static jint MonitorEnter(JNIEnv* env, jobject java_object)
2134 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_) {
2135 ScopedObjectAccess soa(env);
2136 Object* o = soa.Decode<Object*>(java_object);
2137 o->MonitorEnter(soa.Self());
2138 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002139 return JNI_ERR;
2140 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002141 soa.Env()->monitors.Add(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002142 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002143 }
2144
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002145 static jint MonitorExit(JNIEnv* env, jobject java_object)
2146 UNLOCK_FUNCTION(monitor_lock_) {
2147 ScopedObjectAccess soa(env);
2148 Object* o = soa.Decode<Object*>(java_object);
2149 o->MonitorExit(soa.Self());
2150 if (soa.Self()->IsExceptionPending()) {
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002151 return JNI_ERR;
2152 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002153 soa.Env()->monitors.Remove(o);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07002154 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002155 }
2156
2157 static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002158 Runtime* runtime = Runtime::Current();
2159 if (runtime != NULL) {
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002160 *vm = runtime->GetJavaVM();
Elliott Hughescdf53122011-08-19 15:46:09 -07002161 } else {
2162 *vm = NULL;
2163 }
2164 return (*vm != NULL) ? JNI_OK : JNI_ERR;
2165 }
2166
Elliott Hughescdf53122011-08-19 15:46:09 -07002167 static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002168 if (capacity < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002169 JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002170 }
Elliott Hughes11a796e2012-12-19 14:42:57 -08002171 if (address == NULL && capacity != 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002172 JniAbortF("NewDirectByteBuffer", "non-zero capacity for NULL pointer: %lld", capacity);
Elliott Hughes96a98872012-12-19 14:21:15 -08002173 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002174
Elliott Hughes96a98872012-12-19 14:21:15 -08002175 // At the moment, the Java side is limited to 32 bits.
Elliott Hughesb465ab02011-08-24 11:21:21 -07002176 CHECK_LE(reinterpret_cast<uintptr_t>(address), 0xffffffff);
2177 CHECK_LE(capacity, 0xffffffff);
Elliott Hughesb5681212013-03-29 17:29:22 -07002178 jlong address_arg = reinterpret_cast<jlong>(address);
Elliott Hughesb465ab02011-08-24 11:21:21 -07002179 jint capacity_arg = static_cast<jint>(capacity);
2180
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002181 jobject result = env->NewObject(WellKnownClasses::java_nio_DirectByteBuffer,
2182 WellKnownClasses::java_nio_DirectByteBuffer_init,
Elliott Hugheseac76672012-05-24 21:56:51 -07002183 address_arg, capacity_arg);
Ian Rogersef28b142012-11-30 14:22:18 -08002184 return static_cast<JNIEnvExt*>(env)->self->IsExceptionPending() ? NULL : result;
Elliott Hughescdf53122011-08-19 15:46:09 -07002185 }
2186
Elliott Hughesb465ab02011-08-24 11:21:21 -07002187 static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
Jeff Hao534f2b62013-07-10 15:29:36 -07002188 return reinterpret_cast<void*>(env->GetLongField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress));
Elliott Hughescdf53122011-08-19 15:46:09 -07002189 }
2190
Elliott Hughesb465ab02011-08-24 11:21:21 -07002191 static jlong GetDirectBufferCapacity(JNIEnv* env, jobject java_buffer) {
Elliott Hughesaecb5f32013-03-28 08:27:38 -07002192 return static_cast<jlong>(env->GetIntField(java_buffer, WellKnownClasses::java_nio_DirectByteBuffer_capacity));
Elliott Hughescdf53122011-08-19 15:46:09 -07002193 }
2194
Elliott Hughesb465ab02011-08-24 11:21:21 -07002195 static jobjectRefType GetObjectRefType(JNIEnv* env, jobject java_object) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002196 if (java_object == NULL) {
2197 JniAbortF("GetObjectRefType", "null object");
2198 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002199
2200 // Do we definitely know what kind of reference this is?
2201 IndirectRef ref = reinterpret_cast<IndirectRef>(java_object);
2202 IndirectRefKind kind = GetIndirectRefKind(ref);
2203 switch (kind) {
2204 case kLocal:
Ian Rogersef28b142012-11-30 14:22:18 -08002205 if (static_cast<JNIEnvExt*>(env)->locals.Get(ref) != kInvalidIndirectRefObject) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002206 return JNILocalRefType;
2207 }
2208 return JNIInvalidRefType;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002209 case kGlobal:
2210 return JNIGlobalRefType;
2211 case kWeakGlobal:
2212 return JNIWeakGlobalRefType;
2213 case kSirtOrInvalid:
2214 // Is it in a stack IRT?
Ian Rogersef28b142012-11-30 14:22:18 -08002215 if (static_cast<JNIEnvExt*>(env)->self->SirtContains(java_object)) {
Elliott Hughesb465ab02011-08-24 11:21:21 -07002216 return JNILocalRefType;
2217 }
2218
Ian Rogersef28b142012-11-30 14:22:18 -08002219 if (!static_cast<JNIEnvExt*>(env)->vm->work_around_app_jni_bugs) {
Elliott Hughesc5bfa8f2011-08-30 14:32:49 -07002220 return JNIInvalidRefType;
2221 }
2222
Elliott Hughesb465ab02011-08-24 11:21:21 -07002223 // If we're handing out direct pointers, check whether it's a direct pointer
2224 // to a local reference.
Ian Rogersef28b142012-11-30 14:22:18 -08002225 {
2226 ScopedObjectAccess soa(env);
2227 if (soa.Decode<Object*>(java_object) == reinterpret_cast<Object*>(java_object)) {
2228 if (soa.Env()->locals.ContainsDirectPointer(reinterpret_cast<Object*>(java_object))) {
2229 return JNILocalRefType;
2230 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002231 }
2232 }
Elliott Hughesb465ab02011-08-24 11:21:21 -07002233 return JNIInvalidRefType;
2234 }
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002235 LOG(FATAL) << "IndirectRefKind[" << kind << "]";
2236 return JNIInvalidRefType;
Elliott Hughescdf53122011-08-19 15:46:09 -07002237 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002238
2239 private:
Ian Rogersef28b142012-11-30 14:22:18 -08002240 static jint EnsureLocalCapacity(JNIEnv* env, jint desired_capacity,
2241 const char* caller) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002242 // TODO: we should try to expand the table if necessary.
2243 if (desired_capacity < 1 || desired_capacity > static_cast<jint>(kLocalsMax)) {
2244 LOG(ERROR) << "Invalid capacity given to " << caller << ": " << desired_capacity;
2245 return JNI_ERR;
2246 }
2247 // TODO: this isn't quite right, since "capacity" includes holes.
Ian Rogersef28b142012-11-30 14:22:18 -08002248 size_t capacity = static_cast<JNIEnvExt*>(env)->locals.Capacity();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002249 bool okay = (static_cast<jint>(kLocalsMax - capacity) >= desired_capacity);
2250 if (!okay) {
Ian Rogersef28b142012-11-30 14:22:18 -08002251 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002252 soa.Self()->ThrowOutOfMemoryError(caller);
2253 }
2254 return okay ? JNI_OK : JNI_ERR;
2255 }
2256
2257 template<typename JniT, typename ArtT>
2258 static JniT NewPrimitiveArray(const ScopedObjectAccess& soa, jsize length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes96a98872012-12-19 14:21:15 -08002260 if (length < 0) {
2261 JniAbortF("NewPrimitiveArray", "negative array length: %d", length);
2262 }
Ian Rogers50b35e22012-10-04 10:09:15 -07002263 ArtT* result = ArtT::Alloc(soa.Self(), length);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002264 return soa.AddLocalReference<JniT>(result);
2265 }
2266
2267 template <typename ArrayT, typename CArrayT, typename ArtArrayT>
2268 static CArrayT GetPrimitiveArray(ScopedObjectAccess& soa, ArrayT java_array,
2269 jboolean* is_copy)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002271 ArtArrayT* array = soa.Decode<ArtArrayT*>(java_array);
2272 PinPrimitiveArray(soa, array);
2273 if (is_copy != NULL) {
2274 *is_copy = JNI_FALSE;
2275 }
2276 return array->GetData();
2277 }
2278
2279 template <typename ArrayT>
Ian Rogersef28b142012-11-30 14:22:18 -08002280 static void ReleasePrimitiveArray(JNIEnv* env, ArrayT java_array, jint mode) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002281 if (mode != JNI_COMMIT) {
Ian Rogersef28b142012-11-30 14:22:18 -08002282 ScopedObjectAccess soa(env);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002283 Array* array = soa.Decode<Array*>(java_array);
2284 UnpinPrimitiveArray(soa, array);
2285 }
2286 }
2287
2288 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2289 static void GetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2290 jsize start, jsize length, JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002292 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2293 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2294 ThrowAIOOBE(soa, array, start, length, "src");
2295 } else {
2296 JavaT* data = array->GetData();
2297 memcpy(buf, data + start, length * sizeof(JavaT));
2298 }
2299 }
2300
2301 template <typename JavaArrayT, typename JavaT, typename ArrayT>
2302 static void SetPrimitiveArrayRegion(ScopedObjectAccess& soa, JavaArrayT java_array,
2303 jsize start, jsize length, const JavaT* buf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002304 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002305 ArrayT* array = soa.Decode<ArrayT*>(java_array);
2306 if (start < 0 || length < 0 || start + length > array->GetLength()) {
2307 ThrowAIOOBE(soa, array, start, length, "dst");
2308 } else {
2309 JavaT* data = array->GetData();
2310 memcpy(data + start, buf, length * sizeof(JavaT));
2311 }
2312 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002313};
Carl Shapiroea4dca82011-08-01 13:45:38 -07002314
Elliott Hughes88c5c352012-03-15 18:49:48 -07002315const JNINativeInterface gJniNativeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002316 NULL, // reserved0.
2317 NULL, // reserved1.
2318 NULL, // reserved2.
2319 NULL, // reserved3.
Elliott Hughescdf53122011-08-19 15:46:09 -07002320 JNI::GetVersion,
2321 JNI::DefineClass,
2322 JNI::FindClass,
2323 JNI::FromReflectedMethod,
2324 JNI::FromReflectedField,
2325 JNI::ToReflectedMethod,
2326 JNI::GetSuperclass,
2327 JNI::IsAssignableFrom,
2328 JNI::ToReflectedField,
2329 JNI::Throw,
2330 JNI::ThrowNew,
2331 JNI::ExceptionOccurred,
2332 JNI::ExceptionDescribe,
2333 JNI::ExceptionClear,
2334 JNI::FatalError,
2335 JNI::PushLocalFrame,
2336 JNI::PopLocalFrame,
2337 JNI::NewGlobalRef,
2338 JNI::DeleteGlobalRef,
2339 JNI::DeleteLocalRef,
2340 JNI::IsSameObject,
2341 JNI::NewLocalRef,
2342 JNI::EnsureLocalCapacity,
2343 JNI::AllocObject,
2344 JNI::NewObject,
2345 JNI::NewObjectV,
2346 JNI::NewObjectA,
2347 JNI::GetObjectClass,
2348 JNI::IsInstanceOf,
2349 JNI::GetMethodID,
2350 JNI::CallObjectMethod,
2351 JNI::CallObjectMethodV,
2352 JNI::CallObjectMethodA,
2353 JNI::CallBooleanMethod,
2354 JNI::CallBooleanMethodV,
2355 JNI::CallBooleanMethodA,
2356 JNI::CallByteMethod,
2357 JNI::CallByteMethodV,
2358 JNI::CallByteMethodA,
2359 JNI::CallCharMethod,
2360 JNI::CallCharMethodV,
2361 JNI::CallCharMethodA,
2362 JNI::CallShortMethod,
2363 JNI::CallShortMethodV,
2364 JNI::CallShortMethodA,
2365 JNI::CallIntMethod,
2366 JNI::CallIntMethodV,
2367 JNI::CallIntMethodA,
2368 JNI::CallLongMethod,
2369 JNI::CallLongMethodV,
2370 JNI::CallLongMethodA,
2371 JNI::CallFloatMethod,
2372 JNI::CallFloatMethodV,
2373 JNI::CallFloatMethodA,
2374 JNI::CallDoubleMethod,
2375 JNI::CallDoubleMethodV,
2376 JNI::CallDoubleMethodA,
2377 JNI::CallVoidMethod,
2378 JNI::CallVoidMethodV,
2379 JNI::CallVoidMethodA,
2380 JNI::CallNonvirtualObjectMethod,
2381 JNI::CallNonvirtualObjectMethodV,
2382 JNI::CallNonvirtualObjectMethodA,
2383 JNI::CallNonvirtualBooleanMethod,
2384 JNI::CallNonvirtualBooleanMethodV,
2385 JNI::CallNonvirtualBooleanMethodA,
2386 JNI::CallNonvirtualByteMethod,
2387 JNI::CallNonvirtualByteMethodV,
2388 JNI::CallNonvirtualByteMethodA,
2389 JNI::CallNonvirtualCharMethod,
2390 JNI::CallNonvirtualCharMethodV,
2391 JNI::CallNonvirtualCharMethodA,
2392 JNI::CallNonvirtualShortMethod,
2393 JNI::CallNonvirtualShortMethodV,
2394 JNI::CallNonvirtualShortMethodA,
2395 JNI::CallNonvirtualIntMethod,
2396 JNI::CallNonvirtualIntMethodV,
2397 JNI::CallNonvirtualIntMethodA,
2398 JNI::CallNonvirtualLongMethod,
2399 JNI::CallNonvirtualLongMethodV,
2400 JNI::CallNonvirtualLongMethodA,
2401 JNI::CallNonvirtualFloatMethod,
2402 JNI::CallNonvirtualFloatMethodV,
2403 JNI::CallNonvirtualFloatMethodA,
2404 JNI::CallNonvirtualDoubleMethod,
2405 JNI::CallNonvirtualDoubleMethodV,
2406 JNI::CallNonvirtualDoubleMethodA,
2407 JNI::CallNonvirtualVoidMethod,
2408 JNI::CallNonvirtualVoidMethodV,
2409 JNI::CallNonvirtualVoidMethodA,
2410 JNI::GetFieldID,
2411 JNI::GetObjectField,
2412 JNI::GetBooleanField,
2413 JNI::GetByteField,
2414 JNI::GetCharField,
2415 JNI::GetShortField,
2416 JNI::GetIntField,
2417 JNI::GetLongField,
2418 JNI::GetFloatField,
2419 JNI::GetDoubleField,
2420 JNI::SetObjectField,
2421 JNI::SetBooleanField,
2422 JNI::SetByteField,
2423 JNI::SetCharField,
2424 JNI::SetShortField,
2425 JNI::SetIntField,
2426 JNI::SetLongField,
2427 JNI::SetFloatField,
2428 JNI::SetDoubleField,
2429 JNI::GetStaticMethodID,
2430 JNI::CallStaticObjectMethod,
2431 JNI::CallStaticObjectMethodV,
2432 JNI::CallStaticObjectMethodA,
2433 JNI::CallStaticBooleanMethod,
2434 JNI::CallStaticBooleanMethodV,
2435 JNI::CallStaticBooleanMethodA,
2436 JNI::CallStaticByteMethod,
2437 JNI::CallStaticByteMethodV,
2438 JNI::CallStaticByteMethodA,
2439 JNI::CallStaticCharMethod,
2440 JNI::CallStaticCharMethodV,
2441 JNI::CallStaticCharMethodA,
2442 JNI::CallStaticShortMethod,
2443 JNI::CallStaticShortMethodV,
2444 JNI::CallStaticShortMethodA,
2445 JNI::CallStaticIntMethod,
2446 JNI::CallStaticIntMethodV,
2447 JNI::CallStaticIntMethodA,
2448 JNI::CallStaticLongMethod,
2449 JNI::CallStaticLongMethodV,
2450 JNI::CallStaticLongMethodA,
2451 JNI::CallStaticFloatMethod,
2452 JNI::CallStaticFloatMethodV,
2453 JNI::CallStaticFloatMethodA,
2454 JNI::CallStaticDoubleMethod,
2455 JNI::CallStaticDoubleMethodV,
2456 JNI::CallStaticDoubleMethodA,
2457 JNI::CallStaticVoidMethod,
2458 JNI::CallStaticVoidMethodV,
2459 JNI::CallStaticVoidMethodA,
2460 JNI::GetStaticFieldID,
2461 JNI::GetStaticObjectField,
2462 JNI::GetStaticBooleanField,
2463 JNI::GetStaticByteField,
2464 JNI::GetStaticCharField,
2465 JNI::GetStaticShortField,
2466 JNI::GetStaticIntField,
2467 JNI::GetStaticLongField,
2468 JNI::GetStaticFloatField,
2469 JNI::GetStaticDoubleField,
2470 JNI::SetStaticObjectField,
2471 JNI::SetStaticBooleanField,
2472 JNI::SetStaticByteField,
2473 JNI::SetStaticCharField,
2474 JNI::SetStaticShortField,
2475 JNI::SetStaticIntField,
2476 JNI::SetStaticLongField,
2477 JNI::SetStaticFloatField,
2478 JNI::SetStaticDoubleField,
2479 JNI::NewString,
2480 JNI::GetStringLength,
2481 JNI::GetStringChars,
2482 JNI::ReleaseStringChars,
2483 JNI::NewStringUTF,
2484 JNI::GetStringUTFLength,
2485 JNI::GetStringUTFChars,
2486 JNI::ReleaseStringUTFChars,
2487 JNI::GetArrayLength,
2488 JNI::NewObjectArray,
2489 JNI::GetObjectArrayElement,
2490 JNI::SetObjectArrayElement,
2491 JNI::NewBooleanArray,
2492 JNI::NewByteArray,
2493 JNI::NewCharArray,
2494 JNI::NewShortArray,
2495 JNI::NewIntArray,
2496 JNI::NewLongArray,
2497 JNI::NewFloatArray,
2498 JNI::NewDoubleArray,
2499 JNI::GetBooleanArrayElements,
2500 JNI::GetByteArrayElements,
2501 JNI::GetCharArrayElements,
2502 JNI::GetShortArrayElements,
2503 JNI::GetIntArrayElements,
2504 JNI::GetLongArrayElements,
2505 JNI::GetFloatArrayElements,
2506 JNI::GetDoubleArrayElements,
2507 JNI::ReleaseBooleanArrayElements,
2508 JNI::ReleaseByteArrayElements,
2509 JNI::ReleaseCharArrayElements,
2510 JNI::ReleaseShortArrayElements,
2511 JNI::ReleaseIntArrayElements,
2512 JNI::ReleaseLongArrayElements,
2513 JNI::ReleaseFloatArrayElements,
2514 JNI::ReleaseDoubleArrayElements,
2515 JNI::GetBooleanArrayRegion,
2516 JNI::GetByteArrayRegion,
2517 JNI::GetCharArrayRegion,
2518 JNI::GetShortArrayRegion,
2519 JNI::GetIntArrayRegion,
2520 JNI::GetLongArrayRegion,
2521 JNI::GetFloatArrayRegion,
2522 JNI::GetDoubleArrayRegion,
2523 JNI::SetBooleanArrayRegion,
2524 JNI::SetByteArrayRegion,
2525 JNI::SetCharArrayRegion,
2526 JNI::SetShortArrayRegion,
2527 JNI::SetIntArrayRegion,
2528 JNI::SetLongArrayRegion,
2529 JNI::SetFloatArrayRegion,
2530 JNI::SetDoubleArrayRegion,
2531 JNI::RegisterNatives,
2532 JNI::UnregisterNatives,
2533 JNI::MonitorEnter,
2534 JNI::MonitorExit,
2535 JNI::GetJavaVM,
2536 JNI::GetStringRegion,
2537 JNI::GetStringUTFRegion,
2538 JNI::GetPrimitiveArrayCritical,
2539 JNI::ReleasePrimitiveArrayCritical,
2540 JNI::GetStringCritical,
2541 JNI::ReleaseStringCritical,
2542 JNI::NewWeakGlobalRef,
2543 JNI::DeleteWeakGlobalRef,
2544 JNI::ExceptionCheck,
2545 JNI::NewDirectByteBuffer,
2546 JNI::GetDirectBufferAddress,
2547 JNI::GetDirectBufferCapacity,
2548 JNI::GetObjectRefType,
Carl Shapiroea4dca82011-08-01 13:45:38 -07002549};
2550
Elliott Hughes75770752011-08-24 17:52:38 -07002551JNIEnvExt::JNIEnvExt(Thread* self, JavaVMExt* vm)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002552 : self(self),
Elliott Hughes75770752011-08-24 17:52:38 -07002553 vm(vm),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002554 local_ref_cookie(IRT_FIRST_SEGMENT),
2555 locals(kLocalsInitial, kLocalsMax, kLocal),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002556 check_jni(false),
Elliott Hughesbbd76712011-08-17 10:25:24 -07002557 critical(false),
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002558 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002559 functions = unchecked_functions = &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002560 if (vm->check_jni) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002561 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002562 }
Ian Rogers5a7a74a2011-09-26 16:32:29 -07002563 // The JniEnv local reference values must be at a consistent offset or else cross-compilation
2564 // errors will ensue.
2565 CHECK_EQ(JNIEnvExt::LocalRefCookieOffset().Int32Value(), 12);
2566 CHECK_EQ(JNIEnvExt::SegmentStateOffset().Int32Value(), 16);
Elliott Hughes40ef99e2011-08-11 17:44:34 -07002567}
2568
Elliott Hughesc1674ed2011-08-25 18:09:09 -07002569JNIEnvExt::~JNIEnvExt() {
2570}
2571
Elliott Hughes88c5c352012-03-15 18:49:48 -07002572void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
2573 check_jni = enabled;
2574 functions = enabled ? GetCheckJniNativeInterface() : &gJniNativeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002575}
2576
Elliott Hughes73e66f72012-05-09 09:34:45 -07002577void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
2578 locals.Dump(os);
2579 monitors.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002580}
2581
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002582void JNIEnvExt::PushFrame(int /*capacity*/) {
2583 // TODO: take 'capacity' into account.
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002584 stacked_local_ref_cookies.push_back(local_ref_cookie);
2585 local_ref_cookie = locals.GetSegmentState();
2586}
2587
2588void JNIEnvExt::PopFrame() {
2589 locals.SetSegmentState(local_ref_cookie);
2590 local_ref_cookie = stacked_local_ref_cookies.back();
2591 stacked_local_ref_cookies.pop_back();
2592}
2593
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002594Offset JNIEnvExt::SegmentStateOffset() {
2595 return Offset(OFFSETOF_MEMBER(JNIEnvExt, locals) +
2596 IndirectReferenceTable::SegmentStateOffset().Int32Value());
2597}
2598
Carl Shapiroea4dca82011-08-01 13:45:38 -07002599// JNI Invocation interface.
2600
Brian Carlstrombddf9762013-05-14 11:35:37 -07002601extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002602 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Elliott Hughes83a25322013-03-14 11:18:53 -07002603 if (IsBadJniVersion(args->version)) {
2604 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002605 return JNI_EVERSION;
2606 }
2607 Runtime::Options options;
2608 for (int i = 0; i < args->nOptions; ++i) {
2609 JavaVMOption* option = &args->options[i];
Elliott Hughesf1a5adc2012-02-10 18:09:35 -08002610 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002611 }
2612 bool ignore_unrecognized = args->ignoreUnrecognized;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002613 if (!Runtime::Create(options, ignore_unrecognized)) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002614 return JNI_ERR;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002615 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002616 Runtime* runtime = Runtime::Current();
Brian Carlstrombd86bcc2013-03-10 20:26:16 -07002617 bool started = runtime->Start();
2618 if (!started) {
2619 delete Thread::Current()->GetJniEnv();
2620 delete runtime->GetJavaVM();
2621 LOG(WARNING) << "CreateJavaVM failed";
2622 return JNI_ERR;
2623 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002624 *p_env = Thread::Current()->GetJniEnv();
2625 *p_vm = runtime->GetJavaVM();
2626 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002627}
2628
Elliott Hughesf2682d52011-08-15 16:37:04 -07002629extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize, jsize* vm_count) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002630 Runtime* runtime = Runtime::Current();
2631 if (runtime == NULL) {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002632 *vm_count = 0;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002633 } else {
Elliott Hughesf2682d52011-08-15 16:37:04 -07002634 *vm_count = 1;
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002635 vms[0] = runtime->GetJavaVM();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002636 }
2637 return JNI_OK;
2638}
2639
2640// Historically unsupported.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002641extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002642 return JNI_ERR;
2643}
2644
Elliott Hughescdf53122011-08-19 15:46:09 -07002645class JII {
2646 public:
2647 static jint DestroyJavaVM(JavaVM* vm) {
2648 if (vm == NULL) {
2649 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002650 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002651 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2652 delete raw_vm->runtime;
2653 return JNI_OK;
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002654 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002655
Elliott Hughescdf53122011-08-19 15:46:09 -07002656 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002657 return JII_AttachCurrentThread(vm, p_env, thr_args, false);
Elliott Hughescdf53122011-08-19 15:46:09 -07002658 }
2659
2660 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
Elliott Hughes75770752011-08-24 17:52:38 -07002661 return JII_AttachCurrentThread(vm, p_env, thr_args, true);
Elliott Hughescdf53122011-08-19 15:46:09 -07002662 }
2663
2664 static jint DetachCurrentThread(JavaVM* vm) {
Brian Carlstrom4d571432012-05-16 00:21:41 -07002665 if (vm == NULL || Thread::Current() == NULL) {
Elliott Hughescdf53122011-08-19 15:46:09 -07002666 return JNI_ERR;
Elliott Hughescdf53122011-08-19 15:46:09 -07002667 }
Elliott Hughes6a144332012-04-03 13:07:11 -07002668 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
2669 Runtime* runtime = raw_vm->runtime;
2670 runtime->DetachCurrentThread();
2671 return JNI_OK;
Elliott Hughescdf53122011-08-19 15:46:09 -07002672 }
2673
2674 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Elliott Hughes83a25322013-03-14 11:18:53 -07002675 if (IsBadJniVersion(version)) {
2676 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
Elliott Hughescdf53122011-08-19 15:46:09 -07002677 return JNI_EVERSION;
2678 }
2679 if (vm == NULL || env == NULL) {
2680 return JNI_ERR;
2681 }
2682 Thread* thread = Thread::Current();
2683 if (thread == NULL) {
2684 *env = NULL;
2685 return JNI_EDETACHED;
2686 }
2687 *env = thread->GetJniEnv();
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002688 return JNI_OK;
2689 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002690};
Carl Shapiro2ed144c2011-07-26 16:52:08 -07002691
Elliott Hughes88c5c352012-03-15 18:49:48 -07002692const JNIInvokeInterface gJniInvokeInterface = {
Carl Shapiroea4dca82011-08-01 13:45:38 -07002693 NULL, // reserved0
2694 NULL, // reserved1
2695 NULL, // reserved2
Elliott Hughescdf53122011-08-19 15:46:09 -07002696 JII::DestroyJavaVM,
2697 JII::AttachCurrentThread,
2698 JII::DetachCurrentThread,
2699 JII::GetEnv,
2700 JII::AttachCurrentThreadAsDaemon
Carl Shapiroea4dca82011-08-01 13:45:38 -07002701};
2702
Elliott Hughesa0957642011-09-02 14:27:33 -07002703JavaVMExt::JavaVMExt(Runtime* runtime, Runtime::ParsedOptions* options)
Elliott Hughes69f5bc62011-08-24 09:26:14 -07002704 : runtime(runtime),
Elliott Hughesa2501992011-08-26 19:39:54 -07002705 check_jni_abort_hook(NULL),
Elliott Hughesb264f082012-04-06 17:10:10 -07002706 check_jni_abort_hook_data(NULL),
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002707 check_jni(false),
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002708 force_copy(false), // TODO: add a way to enable this
Elliott Hughesa0957642011-09-02 14:27:33 -07002709 trace(options->jni_trace_),
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08002710 work_around_app_jni_bugs(false),
Ian Rogers62d6c772013-02-27 08:32:07 -08002711 pins_lock("JNI pin table lock", kPinTableLock),
Elliott Hughes2ced6a52011-10-16 18:44:48 -07002712 pin_table("pin table", kPinTableInitial, kPinTableMax),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002713 globals_lock("JNI global reference table lock"),
Elliott Hughesbb1e8f02011-10-18 14:14:25 -07002714 globals(gGlobalsInitial, gGlobalsMax, kGlobal),
Elliott Hughes8daa0922011-09-11 13:46:25 -07002715 weak_globals_lock("JNI weak global reference table lock"),
Elliott Hughes79082e32011-08-25 12:07:32 -07002716 weak_globals(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal),
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002717 libraries_lock("JNI shared libraries map lock", kLoadLibraryLock),
Elliott Hughes79082e32011-08-25 12:07:32 -07002718 libraries(new Libraries) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002719 functions = unchecked_functions = &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002720 if (options->check_jni_) {
Elliott Hughes88c5c352012-03-15 18:49:48 -07002721 SetCheckJniEnabled(true);
Elliott Hughesa2501992011-08-26 19:39:54 -07002722 }
Elliott Hughesf2682d52011-08-15 16:37:04 -07002723}
2724
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002725JavaVMExt::~JavaVMExt() {
Elliott Hughes79082e32011-08-25 12:07:32 -07002726 delete libraries;
Elliott Hughesde69d7f2011-08-18 16:49:37 -07002727}
2728
Elliott Hughes88c5c352012-03-15 18:49:48 -07002729void JavaVMExt::SetCheckJniEnabled(bool enabled) {
2730 check_jni = enabled;
2731 functions = enabled ? GetCheckJniInvokeInterface() : &gJniInvokeInterface;
Elliott Hughes4ffd3132011-10-24 12:06:42 -07002732}
2733
Elliott Hughesae80b492012-04-24 10:43:17 -07002734void JavaVMExt::DumpForSigQuit(std::ostream& os) {
2735 os << "JNI: CheckJNI is " << (check_jni ? "on" : "off");
2736 if (force_copy) {
2737 os << " (with forcecopy)";
2738 }
2739 os << "; workarounds are " << (work_around_app_jni_bugs ? "on" : "off");
Ian Rogers50b35e22012-10-04 10:09:15 -07002740 Thread* self = Thread::Current();
Elliott Hughesae80b492012-04-24 10:43:17 -07002741 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002742 MutexLock mu(self, pins_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002743 os << "; pins=" << pin_table.Size();
2744 }
2745 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002746 MutexLock mu(self, globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002747 os << "; globals=" << globals.Capacity();
2748 }
2749 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002750 MutexLock mu(self, weak_globals_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002751 if (weak_globals.Capacity() > 0) {
2752 os << " (plus " << weak_globals.Capacity() << " weak)";
2753 }
2754 }
2755 os << '\n';
2756
2757 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002758 MutexLock mu(self, libraries_lock);
Elliott Hughesae80b492012-04-24 10:43:17 -07002759 os << "Libraries: " << Dumpable<Libraries>(*libraries) << " (" << libraries->size() << ")\n";
2760 }
2761}
2762
Elliott Hughes73e66f72012-05-09 09:34:45 -07002763void JavaVMExt::DumpReferenceTables(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002764 Thread* self = Thread::Current();
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002765 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002766 MutexLock mu(self, globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002767 globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002768 }
2769 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002770 MutexLock mu(self, weak_globals_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002771 weak_globals.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002772 }
2773 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002774 MutexLock mu(self, pins_lock);
Elliott Hughes73e66f72012-05-09 09:34:45 -07002775 pin_table.Dump(os);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07002776 }
2777}
2778
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002779bool JavaVMExt::LoadNativeLibrary(const std::string& path, ClassLoader* class_loader,
2780 std::string& detail) {
Elliott Hughes75770752011-08-24 17:52:38 -07002781 detail.clear();
Elliott Hughescdf53122011-08-19 15:46:09 -07002782
2783 // See if we've already loaded this library. If we have, and the class loader
2784 // matches, return successfully without doing anything.
Elliott Hughes75770752011-08-24 17:52:38 -07002785 // TODO: for better results we should canonicalize the pathname (or even compare
2786 // inodes). This implementation is fine if everybody is using System.loadLibrary.
Elliott Hughes79082e32011-08-25 12:07:32 -07002787 SharedLibrary* library;
Ian Rogers50b35e22012-10-04 10:09:15 -07002788 Thread* self = Thread::Current();
Elliott Hughes79082e32011-08-25 12:07:32 -07002789 {
2790 // TODO: move the locking (and more of this logic) into Libraries.
Ian Rogers50b35e22012-10-04 10:09:15 -07002791 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002792 library = libraries->Get(path);
2793 }
Elliott Hughescdf53122011-08-19 15:46:09 -07002794 if (library != NULL) {
2795 if (library->GetClassLoader() != class_loader) {
Elliott Hughes75770752011-08-24 17:52:38 -07002796 // The library will be associated with class_loader. The JNI
2797 // spec says we can't load the same library into more than one
2798 // class loader.
2799 StringAppendF(&detail, "Shared library \"%s\" already opened by "
2800 "ClassLoader %p; can't open in ClassLoader %p",
2801 path.c_str(), library->GetClassLoader(), class_loader);
2802 LOG(WARNING) << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002803 return false;
2804 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002805 VLOG(jni) << "[Shared library \"" << path << "\" already loaded in "
2806 << "ClassLoader " << class_loader << "]";
Elliott Hughes1bac54f2012-03-16 12:48:31 -07002807 if (!library->CheckOnLoadResult()) {
Elliott Hughes75770752011-08-24 17:52:38 -07002808 StringAppendF(&detail, "JNI_OnLoad failed on a previous attempt "
2809 "to load \"%s\"", path.c_str());
Elliott Hughescdf53122011-08-19 15:46:09 -07002810 return false;
2811 }
2812 return true;
2813 }
2814
2815 // Open the shared library. Because we're using a full path, the system
2816 // doesn't have to search through LD_LIBRARY_PATH. (It may do so to
2817 // resolve this library's dependencies though.)
2818
2819 // Failures here are expected when java.library.path has several entries
2820 // and we have to hunt for the lib.
2821
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002822 // Below we dlopen but there is no paired dlclose, this would be necessary if we supported
2823 // class unloading. Libraries will only be unloaded when the reference count (incremented by
2824 // dlopen) becomes zero from dlclose.
2825
Elliott Hughescdf53122011-08-19 15:46:09 -07002826 // This can execute slowly for a large library on a busy system, so we
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002827 // want to switch from kRunnable while it executes. This allows the GC to ignore us.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002828 self->TransitionFromRunnableToSuspended(kWaitingForJniOnLoad);
2829 void* handle = dlopen(path.empty() ? NULL : path.c_str(), RTLD_LAZY);
2830 self->TransitionFromSuspendedToRunnable();
Elliott Hughescdf53122011-08-19 15:46:09 -07002831
Elliott Hughes84b2f142012-09-27 09:16:28 -07002832 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_LAZY) returned " << handle << "]";
Elliott Hughescdf53122011-08-19 15:46:09 -07002833
2834 if (handle == NULL) {
Elliott Hughes75770752011-08-24 17:52:38 -07002835 detail = dlerror();
Elliott Hughes84b2f142012-09-27 09:16:28 -07002836 LOG(ERROR) << "dlopen(\"" << path << "\", RTLD_LAZY) failed: " << detail;
Elliott Hughescdf53122011-08-19 15:46:09 -07002837 return false;
2838 }
2839
2840 // Create a new entry.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002841 // TODO: move the locking (and more of this logic) into Libraries.
2842 bool created_library = false;
Elliott Hughescdf53122011-08-19 15:46:09 -07002843 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002844 MutexLock mu(self, libraries_lock);
Elliott Hughes79082e32011-08-25 12:07:32 -07002845 library = libraries->Get(path);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002846 if (library == NULL) { // We won race to get libraries_lock
2847 library = new SharedLibrary(path, handle, class_loader);
2848 libraries->Put(path, library);
2849 created_library = true;
Elliott Hughescdf53122011-08-19 15:46:09 -07002850 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002851 }
2852 if (!created_library) {
2853 LOG(INFO) << "WOW: we lost a race to add shared library: "
2854 << "\"" << path << "\" ClassLoader=" << class_loader;
2855 return library->CheckOnLoadResult();
Elliott Hughescdf53122011-08-19 15:46:09 -07002856 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002857
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002858 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002859
2860 bool result = true;
2861 void* sym = dlsym(handle, "JNI_OnLoad");
2862 if (sym == NULL) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002863 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002864 } else {
2865 // Call JNI_OnLoad. We have to override the current class
2866 // loader, which will always be "null" since the stuff at the
2867 // top of the stack is around Runtime.loadLibrary(). (See
2868 // the comments in the JNI FindClass function.)
2869 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
2870 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
Ian Rogers365c1022012-06-22 15:05:28 -07002871 ClassLoader* old_class_loader = self->GetClassLoaderOverride();
Elliott Hughes79082e32011-08-25 12:07:32 -07002872 self->SetClassLoaderOverride(class_loader);
2873
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002874 int version = 0;
2875 {
Elliott Hughes34e06962012-04-09 13:55:55 -07002876 ScopedThreadStateChange tsc(self, kNative);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002877 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
Elliott Hughesad7c2a32011-08-31 11:58:10 -07002878 version = (*jni_on_load)(this, NULL);
Elliott Hughes79082e32011-08-25 12:07:32 -07002879 }
Elliott Hughes79082e32011-08-25 12:07:32 -07002880
Brian Carlstromaded5f72011-10-07 17:15:04 -07002881 self->SetClassLoaderOverride(old_class_loader);
Elliott Hughes79082e32011-08-25 12:07:32 -07002882
Elliott Hughes83a25322013-03-14 11:18:53 -07002883 if (IsBadJniVersion(version)) {
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002884 StringAppendF(&detail, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
2885 path.c_str(), version);
Elliott Hughes79082e32011-08-25 12:07:32 -07002886 // It's unwise to call dlclose() here, but we can mark it
2887 // as bad and ensure that future load attempts will fail.
2888 // We don't know how far JNI_OnLoad got, so there could
2889 // be some partially-initialized stuff accessible through
2890 // newly-registered native method calls. We could try to
2891 // unregister them, but that doesn't seem worthwhile.
2892 result = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07002893 }
Brian Carlstrom75fe90c2013-06-26 22:26:16 -07002894 VLOG(jni) << "[Returned " << (result ? "successfully" : "failure")
2895 << " from JNI_OnLoad in \"" << path << "\"]";
Elliott Hughes79082e32011-08-25 12:07:32 -07002896 }
2897
2898 library->SetResult(result);
2899 return result;
2900}
2901
Mathieu Chartier66f19252012-09-18 08:57:04 -07002902void* JavaVMExt::FindCodeForNativeMethod(AbstractMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002903 CHECK(m->IsNative());
2904
2905 Class* c = m->GetDeclaringClass();
2906
2907 // If this is a static method, it could be called before the class
2908 // has been initialized.
2909 if (m->IsStatic()) {
Ian Rogers0045a292012-03-31 21:08:41 -07002910 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes79082e32011-08-25 12:07:32 -07002911 return NULL;
2912 }
2913 } else {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -07002914 CHECK(c->GetStatus() >= Class::kStatusInitializing) << c->GetStatus() << " " << PrettyMethod(m);
Elliott Hughes79082e32011-08-25 12:07:32 -07002915 }
2916
Brian Carlstrom16192862011-09-12 17:50:06 -07002917 std::string detail;
2918 void* native_method;
Ian Rogers50b35e22012-10-04 10:09:15 -07002919 Thread* self = Thread::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -07002920 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002921 MutexLock mu(self, libraries_lock);
Brian Carlstrom16192862011-09-12 17:50:06 -07002922 native_method = libraries->FindNativeMethod(m, detail);
2923 }
Ian Rogers62d6c772013-02-27 08:32:07 -08002924 // Throwing can cause libraries_lock to be reacquired.
Brian Carlstrom16192862011-09-12 17:50:06 -07002925 if (native_method == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -08002926 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
2927 self->ThrowNewException(throw_location, "Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Brian Carlstrom16192862011-09-12 17:50:06 -07002928 }
2929 return native_method;
Elliott Hughescdf53122011-08-19 15:46:09 -07002930}
2931
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002932void JavaVMExt::VisitRoots(RootVisitor* visitor, void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -07002933 Thread* self = Thread::Current();
Elliott Hughes410c0c82011-09-01 17:58:25 -07002934 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002935 MutexLock mu(self, globals_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002936 globals.VisitRoots(visitor, arg);
2937 }
2938 {
Ian Rogers50b35e22012-10-04 10:09:15 -07002939 MutexLock mu(self, pins_lock);
Elliott Hughes410c0c82011-09-01 17:58:25 -07002940 pin_table.VisitRoots(visitor, arg);
2941 }
2942 // The weak_globals table is visited by the GC itself (because it mutates the table).
2943}
2944
Elliott Hughesc8fece32013-01-02 11:27:23 -08002945void RegisterNativeMethods(JNIEnv* env, const char* jni_class_name, const JNINativeMethod* methods,
2946 size_t method_count) {
2947 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
2948 if (c.get() == NULL) {
2949 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
2950 }
2951 JNI::RegisterNativeMethods(env, c.get(), methods, method_count, false);
2952}
2953
Ian Rogersdf20fe02011-07-20 20:34:16 -07002954} // namespace art
Elliott Hughesb465ab02011-08-24 11:21:21 -07002955
2956std::ostream& operator<<(std::ostream& os, const jobjectRefType& rhs) {
2957 switch (rhs) {
2958 case JNIInvalidRefType:
2959 os << "JNIInvalidRefType";
2960 return os;
2961 case JNILocalRefType:
2962 os << "JNILocalRefType";
2963 return os;
2964 case JNIGlobalRefType:
2965 os << "JNIGlobalRefType";
2966 return os;
2967 case JNIWeakGlobalRefType:
2968 os << "JNIWeakGlobalRefType";
2969 return os;
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002970 default:
Shih-wei Liao24782c62012-01-08 12:46:11 -08002971 LOG(FATAL) << "jobjectRefType[" << static_cast<int>(rhs) << "]";
Brian Carlstrom2e3d1b22012-01-09 18:01:56 -08002972 return os;
Elliott Hughesb465ab02011-08-24 11:21:21 -07002973 }
2974}