blob: 68fc97e922970db0ca2176a082ef8a5398dc8730 [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
2 * Copyright (C) 2008 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 */
16
Elliott Hughesd369bb72011-09-12 14:41:14 -070017#include "class_linker.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070018#include "class_loader.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070020#include "nth_caller_visitor.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070021#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Elliott Hughes80609252011-09-23 17:24:51 -070024#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070025#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070026#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070027
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030static Class* DecodeClass(const ScopedObjectAccess& soa, jobject java_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070031 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 Class* c = soa.Decode<Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070033 DCHECK(c != NULL);
34 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070035 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
36 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
37 // every time probably doesn't make much difference to reflection performance anyway.
38 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070039}
40
Brian Carlstromf91c8c32011-09-21 17:30:34 -070041// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070042static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 ScopedObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044 ScopedUtfChars name(env, javaName);
45 if (name.c_str() == NULL) {
46 return NULL;
47 }
48
49 // We need to validate and convert the name (from x.y.z to x/y/z). This
50 // is especially handy for array types, since we want to avoid
51 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070052 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070053 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070054 "Invalid name: %s", name.c_str());
55 return NULL;
56 }
57
58 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 ClassLoader* class_loader = soa.Decode<ClassLoader*>(javaLoader);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070062 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080063 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070064 env->ExceptionClear();
Elliott Hugheseac76672012-05-24 21:56:51 -070065 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
66 WellKnownClasses::java_lang_ClassNotFoundException_init,
67 javaName, cause.get()));
Elliott Hughes844f9a02012-01-24 20:19:58 -080068 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070069 return NULL;
70 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071 if (initialize) {
Ian Rogers0045a292012-03-31 21:08:41 -070072 class_linker->EnsureInitialized(c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070073 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 return soa.AddLocalReference<jclass>(c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075}
76
Elliott Hughes0512f022012-03-15 22:10:52 -070077static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 ScopedObjectAccess soa(env);
79 Class* c = DecodeClass(soa, javaClass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
81 return 0; // primitive, array and proxy classes don't have class definitions
82 }
83 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
84 if (class_def == NULL) {
85 return 0; // not found
86 } else {
87 return class_def->annotations_off_;
88 }
89}
90
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091// TODO: Remove this redundant struct when GCC annotalysis works correctly on top-level functions.
92struct WorkAroundGccAnnotalysisBug {
Elliott Hughes80609252011-09-23 17:24:51 -070093template<typename T>
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094static jobjectArray ToArray(const ScopedObjectAccessUnchecked& soa, const char* array_class_name,
95 const std::vector<T*>& objects)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 ScopedLocalRef<jclass> array_class(soa.Env(), soa.Env()->FindClass(array_class_name));
98 jobjectArray result = soa.Env()->NewObjectArray(objects.size(), array_class.get(), NULL);
Elliott Hughes80609252011-09-23 17:24:51 -070099 for (size_t i = 0; i < objects.size(); ++i) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 ScopedLocalRef<jobject> object(soa.Env(), soa.AddLocalReference<jobject>(objects[i]));
101 soa.Env()->SetObjectArrayElement(result, i, object.get());
Elliott Hughes80609252011-09-23 17:24:51 -0700102 }
103 return result;
104}
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105};
106#define ToArray(a, b, c) WorkAroundGccAnnotalysisBug::ToArray(a, b, c)
Elliott Hughes80609252011-09-23 17:24:51 -0700107
Ian Rogersd418eda2012-01-30 12:14:28 -0800108static bool IsVisibleConstructor(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700109 if (public_only && !m->IsPublic()) {
110 return false;
111 }
Ian Rogers9074b992011-10-26 17:41:55 -0700112 if (m->IsStatic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700113 return false;
114 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800115 return m->IsConstructor();
Elliott Hughes80609252011-09-23 17:24:51 -0700116}
117
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 ScopedObjectAccess soa(env);
120 Class* c = DecodeClass(soa, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700121 std::vector<Method*> constructors;
122 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
123 Method* m = c->GetDirectMethod(i);
124 if (IsVisibleConstructor(m, publicOnly)) {
125 constructors.push_back(m);
126 }
127 }
128
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 return ToArray(soa, "java/lang/reflect/Constructor", constructors);
Elliott Hughes80609252011-09-23 17:24:51 -0700130}
131
Ian Rogersd418eda2012-01-30 12:14:28 -0800132static bool IsVisibleField(Field* f, bool public_only) {
Elliott Hughesc0dd3122011-09-26 10:15:43 -0700133 if (public_only && !f->IsPublic()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700134 return false;
135 }
Elliott Hughes80609252011-09-23 17:24:51 -0700136 return true;
137}
138
Elliott Hughes0512f022012-03-15 22:10:52 -0700139static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700140 ScopedObjectAccess soa(env);
141 Class* c = DecodeClass(soa, javaClass);
Elliott Hughes80609252011-09-23 17:24:51 -0700142 std::vector<Field*> fields;
jeffhao441d9122012-03-21 17:29:10 -0700143 FieldHelper fh;
Elliott Hughes80609252011-09-23 17:24:51 -0700144 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
145 Field* f = c->GetInstanceField(i);
jeffhao441d9122012-03-21 17:29:10 -0700146 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700147 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700148 if (fh.GetType() == NULL) {
149 DCHECK(env->ExceptionOccurred());
150 return NULL;
151 }
Elliott Hughes80609252011-09-23 17:24:51 -0700152 fields.push_back(f);
153 }
Jesse Wilson53494312011-11-29 16:43:09 -0500154 if (env->ExceptionOccurred()) {
155 return NULL;
156 }
Elliott Hughes80609252011-09-23 17:24:51 -0700157 }
158 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
159 Field* f = c->GetStaticField(i);
jeffhao441d9122012-03-21 17:29:10 -0700160 fh.ChangeField(f);
Elliott Hughes80609252011-09-23 17:24:51 -0700161 if (IsVisibleField(f, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700162 if (fh.GetType() == NULL) {
163 DCHECK(env->ExceptionOccurred());
164 return NULL;
165 }
Elliott Hughes80609252011-09-23 17:24:51 -0700166 fields.push_back(f);
167 }
Jesse Wilson53494312011-11-29 16:43:09 -0500168 if (env->ExceptionOccurred()) {
169 return NULL;
170 }
Elliott Hughes80609252011-09-23 17:24:51 -0700171 }
172
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 return ToArray(soa, "java/lang/reflect/Field", fields);
Elliott Hughes80609252011-09-23 17:24:51 -0700174}
175
Ian Rogersd418eda2012-01-30 12:14:28 -0800176static bool IsVisibleMethod(Method* m, bool public_only) {
Elliott Hughes80609252011-09-23 17:24:51 -0700177 if (public_only && !m->IsPublic()) {
178 return false;
179 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 if (m->IsConstructor()) {
Elliott Hughes80609252011-09-23 17:24:51 -0700181 return false;
182 }
Ian Rogers56437422012-02-06 21:46:00 -0800183 if (m->IsMiranda()) {
184 return false;
185 }
Elliott Hughes80609252011-09-23 17:24:51 -0700186 return true;
187}
188
Elliott Hughes0512f022012-03-15 22:10:52 -0700189static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(env);
191 Class* c = DecodeClass(soa, javaClass);
Elliott Hughes15216932012-03-21 21:53:06 -0700192 if (c == NULL) {
193 return NULL;
194 }
195
Elliott Hughes80609252011-09-23 17:24:51 -0700196 std::vector<Method*> methods;
jeffhao441d9122012-03-21 17:29:10 -0700197 MethodHelper mh;
Elliott Hughes80609252011-09-23 17:24:51 -0700198 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
199 Method* m = c->GetVirtualMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700200 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700201 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700202 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
203 DCHECK(env->ExceptionOccurred());
204 return NULL;
205 }
Elliott Hughes80609252011-09-23 17:24:51 -0700206 methods.push_back(m);
207 }
Jesse Wilson53494312011-11-29 16:43:09 -0500208 if (env->ExceptionOccurred()) {
209 return NULL;
210 }
Elliott Hughes80609252011-09-23 17:24:51 -0700211 }
212 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
213 Method* m = c->GetDirectMethod(i);
jeffhao441d9122012-03-21 17:29:10 -0700214 mh.ChangeMethod(m);
Elliott Hughes80609252011-09-23 17:24:51 -0700215 if (IsVisibleMethod(m, publicOnly)) {
jeffhao441d9122012-03-21 17:29:10 -0700216 if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
217 DCHECK(env->ExceptionOccurred());
218 return NULL;
219 }
Elliott Hughes80609252011-09-23 17:24:51 -0700220 methods.push_back(m);
221 }
Jesse Wilson53494312011-11-29 16:43:09 -0500222 if (env->ExceptionOccurred()) {
223 return NULL;
224 }
Elliott Hughes80609252011-09-23 17:24:51 -0700225 }
226
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 return ToArray(soa, "java/lang/reflect/Method", methods);
Elliott Hughes80609252011-09-23 17:24:51 -0700228}
229
Elliott Hughes0512f022012-03-15 22:10:52 -0700230static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 ScopedObjectAccess soa(env);
232 Class* c = DecodeClass(soa, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -0400233
234 DexCache* dex_cache = c->GetDexCache();
235 if (dex_cache == NULL) {
236 return NULL;
237 }
238
239 return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
240}
241
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700243 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 if (name != mh->GetName()) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700245 return false;
246 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 const DexFile::TypeList* m_type_list = mh->GetParameterTypeList();
248 uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size();
249 uint32_t sig_length = arg_array->GetLength();
250
251 if (m_type_list_size != sig_length) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252 return false;
253 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800254
255 for (uint32_t i = 0; i < sig_length; i++) {
256 if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) {
257 return false;
258 }
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500259 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700260 return true;
261}
262
Ian Rogersd418eda2012-01-30 12:14:28 -0800263static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 ObjectArray<Class>* arg_array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500266 if (methods == NULL) {
267 return NULL;
268 }
269 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 MethodHelper mh;
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500271 for (int32_t i = 0; i < methods->GetLength(); ++i) {
272 Method* method = methods->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 mh.ChangeMethod(method);
274 if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500275 continue;
276 }
277
278 result = method;
279
280 // Covariant return types permit the class to define multiple
281 // methods with the same name and parameter types. Prefer to return
282 // a non-synthetic method in such situations. We may still return
283 // a synthetic method to handle situations like escalated visibility.
284 if (!method->IsSynthetic()) {
285 break;
286 }
287 }
288 return result;
289}
290
Elliott Hughes0512f022012-03-15 22:10:52 -0700291static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName,
292 jobjectArray javaArgs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700293 ScopedObjectAccess soa(env);
294 Class* c = DecodeClass(soa, javaClass);
295 std::string name(soa.Decode<String*>(javaName)->ToModifiedUtf8());
296 ObjectArray<Class>* arg_array = soa.Decode<ObjectArray<Class>*>(javaArgs);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700297
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500299 if (m == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array);
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700301 }
302
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500303 if (m != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 return soa.AddLocalReference<jobject>(m);
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500305 } else {
306 return NULL;
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700307 }
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -0700308}
309
Elliott Hughes15216932012-03-21 21:53:06 -0700310static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 ScopedObjectAccess soa(env);
312 Class* c = DecodeClass(soa, java_class);
313 String* name = soa.Decode<String*>(jname);
Elliott Hughesdbb40792011-11-18 17:05:22 -0800314 DCHECK(name->GetClass()->IsStringClass());
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700315
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 FieldHelper fh;
Elliott Hughes15216932012-03-21 21:53:06 -0700317 for (size_t i = 0; i < c->NumInstanceFields(); ++i) {
318 Field* f = c->GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 fh.ChangeField(f);
320 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700321 if (fh.GetType() == NULL) {
322 DCHECK(env->ExceptionOccurred());
323 return NULL;
324 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700326 }
327 }
Elliott Hughes15216932012-03-21 21:53:06 -0700328 for (size_t i = 0; i < c->NumStaticFields(); ++i) {
329 Field* f = c->GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800330 fh.ChangeField(f);
331 if (name->Equals(fh.GetName())) {
jeffhao441d9122012-03-21 17:29:10 -0700332 if (fh.GetType() == NULL) {
333 DCHECK(env->ExceptionOccurred());
334 return NULL;
335 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 return soa.AddLocalReference<jclass>(f);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700337 }
338 }
339 return NULL;
340}
341
Elliott Hughes0512f022012-03-15 22:10:52 -0700342static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 ScopedObjectAccess soa(env);
344 Class* c = DecodeClass(soa, javaThis);
345 return soa.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700346}
347
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700348static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 ScopedObjectAccess soa(env);
350 SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
351 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700352}
353
Elliott Hughes0512f022012-03-15 22:10:52 -0700354static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 ScopedObjectAccess soa(env);
356 Class* lhs = DecodeClass(soa, javaLhs);
357 Class* rhs = soa.Decode<Class*>(javaRhs); // Can be null.
Elliott Hughesdd8df692011-09-23 14:42:41 -0700358 if (rhs == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null");
Elliott Hughesdd8df692011-09-23 14:42:41 -0700360 return JNI_FALSE;
361 }
362 return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE;
363}
364
Elliott Hughes0512f022012-03-15 22:10:52 -0700365static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700366 ScopedObjectAccess soa(env);
367 Class* c = DecodeClass(soa, javaThis);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700368 if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800370 "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700371 return NULL;
372 }
373
Ian Rogers0045a292012-03-31 21:08:41 -0700374 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700375 return NULL;
376 }
377
Brian Carlstrom01a96582012-03-12 15:17:29 -0700378 Method* init = c->FindDeclaredDirectMethod("<init>", "()V");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700379 if (init == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700382 return NULL;
383 }
384
385 // Verify access from the call site.
386 //
387 // First, make sure the method invoking Class.newInstance() has permission
388 // to access the class.
389 //
390 // Second, make sure it has permission to invoke the constructor. The
391 // constructor must be public or, if the caller is in the same package,
392 // have package scope.
Elliott Hughes6a144332012-04-03 13:07:11 -0700393
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 NthCallerVisitor visitor(soa.Self()->GetManagedStack(), soa.Self()->GetTraceStack(), 2);
Ian Rogers0399dde2012-06-06 17:09:28 -0700395 visitor.WalkStack();
TDYa1273f9137d2012-04-08 15:59:19 -0700396 Class* caller_class = visitor.caller->GetDeclaringClass();
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700397
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 ClassHelper caller_ch(caller_class);
Brian Carlstrombc2f3e32011-09-22 17:16:54 -0700399 if (!caller_class->CanAccess(c)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700401 "Class %s is not accessible from class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800402 PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(),
403 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700404 return NULL;
405 }
Elliott Hughes26c5e152012-07-11 11:47:22 -0700406 if (!caller_class->CanAccessMember(init->GetDeclaringClass(), init->GetAccessFlags())) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 soa.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;",
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700408 "%s is not accessible from class %s",
409 PrettyMethod(init).c_str(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 PrettyDescriptor(caller_ch.GetDescriptor()).c_str());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700411 return NULL;
412 }
413
414 Object* new_obj = c->AllocObject();
415 if (new_obj == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 DCHECK(soa.Self()->IsExceptionPending());
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700417 return NULL;
418 }
419
420 // invoke constructor; unlike reflection calls, we don't wrap exceptions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421 jclass java_class = soa.AddLocalReference<jclass>(c);
422 jmethodID mid = soa.EncodeMethod(init);
Elliott Hughes15216932012-03-21 21:53:06 -0700423 return env->NewObject(java_class, mid);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700424}
425
Elliott Hughesd369bb72011-09-12 14:41:14 -0700426static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700427 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"),
430 NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"),
431 NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"),
432 NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"),
433 NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400434 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700435 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700436 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesdd8df692011-09-23 14:42:41 -0700437 NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700438 NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700439};
440
Elliott Hughesd369bb72011-09-12 14:41:14 -0700441void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700442 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700443}
444
445} // namespace art